Skip to content

Commit 6b42270

Browse files
authored
feat(ui): display server icons in the registry web client (modelcontextprotocol#1586)
Closes modelcontextprotocol#784. Servers can declare `icons` in `server.json` and the schema has supported it for some time, but the web client never rendered them, so every card looks identical apart from its text. This renders them in both the "Recently Updated" cards and the main search results. ## Selection - **HTTPS only at render time.** `validateIcon` already rejects other schemes at publish, but this UI can be pointed at a staging, local or custom API base URL from its own settings, so the scheme is re-checked rather than trusted. - **Scalable icons win outright** (`sizes: ["any"]`). Otherwise the smallest icon at least as wide as the rendered size, so it stays crisp without fetching a needlessly large file. - **`theme: "dark"` is a fallback only**, used when nothing else is offered, since this UI has a light background. ## Rendering - `src` goes through the existing `escapeAttr`, so a quote in a URL cannot break out of the attribute. - `alt` is empty on purpose: the icon sits directly beside the server name, so announcing it again is noise for screen readers. - `loading="lazy"` stops a full grid of cards fetching every icon at once. - `referrerpolicy="no-referrer"` avoids leaking registry URLs to third-party icon hosts. - A broken or blocked image removes itself rather than leaving a placeholder in the layout. SVG is rendered via `<img src>`, where scripts do not execute, which is the safe path for the `image/svg+xml` case the schema warns about. ## Testing Ran the selection logic against the first 100 servers in the production registry. That covers every icon shape currently published: | Published shape | Picked | |---|---| | no `sizes` field | the only icon | | single `512x512` | `512x512` | | `["any"]` (SVG) | the SVG | | `256x256` + `512x512` | `256x256` | | single `96x96` | `96x96` | Also verified that `http://`, `javascript:`, `data:` and null sources render nothing, that a dark-only icon set still renders, that light is preferred when both are offered, and that a quote injected into `src` is escaped rather than breaking the attribute. Unit tests pass, including `internal/api/handlers/v0`, the package that embeds this file. **Two pre-existing failures, noted for transparency:** `make validate` and `make lint` both fail identically on a clean checkout of `main` with these changes absent. The lint ones are `gofmt` objecting to CRLF from a Windows checkout. Neither is introduced here and neither is touched by this change. Tests ran without `-race`, which needs cgo and a C toolchain unavailable on my machine.
1 parent 909717d commit 6b42270

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

‎internal/api/handlers/v0/ui_index.html‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ <h3 class="text-lg font-semibold mb-4">API Base URL</h3>
318318
header.onclick = () => toggleDetails(card, item);
319319
header.innerHTML = `
320320
<div class="flex items-start justify-between gap-3 mb-2">
321+
${renderIcon(server)}
321322
<h3 class="font-semibold text-gray-900 text-base flex-1 min-w-0 break-all">${escapeHtml(server.name)}</h3>
322323
<span class="text-xs text-gray-500 font-medium whitespace-nowrap">v${escapeHtml(server.version)}</span>
323324
</div>
@@ -386,6 +387,7 @@ <h3 class="font-semibold text-gray-900 text-base flex-1 min-w-0 break-all">${esc
386387
header.onclick = () => toggleDetails(card, item);
387388
header.innerHTML = `
388389
<div class="flex items-start justify-between gap-3 mb-2">
390+
${renderIcon(server)}
389391
<h3 class="font-semibold text-gray-900 text-base flex-1 min-w-0">${escapeHtml(server.name)}</h3>
390392
<span class="text-xs text-gray-500 font-medium whitespace-nowrap">v${escapeHtml(server.version)}</span>
391393
</div>
@@ -544,6 +546,58 @@ <h4 class="font-medium text-gray-900 mb-2">Remotes</h4>
544546
.replace(/'/g, '&#39;');
545547
}
546548

549+
// Icons are optional and come from `server.icons` in server.json.
550+
// The publish API already rejects non-HTTPS icon sources, but this UI can
551+
// be pointed at a staging, local or custom API base URL, so the scheme is
552+
// re-checked here rather than trusted.
553+
function isSafeIconSrc(src) {
554+
if (typeof src !== 'string') return false;
555+
try {
556+
return new URL(src, window.location.href).protocol === 'https:';
557+
} catch {
558+
return false;
559+
}
560+
}
561+
562+
// Smallest icon at least as large as the rendered size, so it stays crisp
563+
// without pulling a needlessly large file. Scalable icons win outright.
564+
// `theme: 'dark'` means "for a dark background"; this UI is light, so those
565+
// are used only when nothing else is available.
566+
const ICON_RENDER_PX = 40;
567+
568+
function iconScore(icon) {
569+
const sizes = Array.isArray(icon.sizes) ? icon.sizes : [];
570+
if (sizes.includes('any')) return 0;
571+
const widths = sizes
572+
.map((s) => parseInt(String(s).split('x')[0], 10))
573+
.filter((n) => Number.isFinite(n) && n > 0);
574+
if (!widths.length) return ICON_RENDER_PX * 100;
575+
const atLeast = widths.filter((w) => w >= ICON_RENDER_PX);
576+
return atLeast.length
577+
? Math.min(...atLeast)
578+
: ICON_RENDER_PX * 100 + (ICON_RENDER_PX - Math.max(...widths));
579+
}
580+
581+
function pickIcon(server) {
582+
const icons = Array.isArray(server?.icons) ? server.icons : [];
583+
const usable = icons.filter((i) => i && isSafeIconSrc(i.src));
584+
if (!usable.length) return null;
585+
const preferred = usable.filter((i) => i.theme !== 'dark');
586+
const pool = preferred.length ? preferred : usable;
587+
return pool.reduce((best, i) => (iconScore(i) < iconScore(best) ? i : best));
588+
}
589+
590+
// alt is intentionally empty: the icon sits beside the server name, so
591+
// announcing it again is noise for screen readers. A broken or blocked
592+
// image removes itself rather than leaving a placeholder in the layout.
593+
function renderIcon(server) {
594+
const icon = pickIcon(server);
595+
if (!icon) return '';
596+
return `<img src="${escapeAttr(icon.src)}" alt=""
597+
class="w-10 h-10 rounded flex-shrink-0 object-contain bg-white border border-gray-200"
598+
loading="lazy" referrerpolicy="no-referrer" onerror="this.remove()">`;
599+
}
600+
547601
function formatDate(dateStr) {
548602
if (!dateStr) return '-';
549603
const date = new Date(dateStr);

0 commit comments

Comments
 (0)