Skip to content

Commit 8c478cd

Browse files
committed
feat(ui): display server icons in the registry web client
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 looked identical apart from its text. Closes modelcontextprotocol#784. Selection, in order: - HTTPS only. The publish API already rejects other schemes, 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 at render time rather than trusted. - `theme: "dark"` icons are used only when nothing else is offered, since this UI has a light background. - Scalable icons (`sizes: ["any"]`) win outright. Otherwise the smallest icon at least as wide as the rendered size is chosen, so it stays crisp without fetching a needlessly large file. Rendering notes: - `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"` keeps a grid of cards from fetching every icon at once, and `referrerpolicy="no-referrer"` avoids leaking registry URLs to icon hosts. - A broken or blocked image removes itself instead of leaving a placeholder. Verified against the first 100 servers in the production registry, which exercises every shape currently published: no `sizes` field, a single 512x512, `["any"]`, and a 256/512 pair. Also checked that http, `javascript:`, `data:` and null sources render nothing, and that a quote injected into `src` is escaped. Unit tests pass, including internal/api/handlers/v0, which is the package that embeds this file. `make validate` and `make lint` both fail identically on a clean checkout of upstream main with these changes absent, so those failures are pre-existing rather than introduced here; the lint ones are gofmt objecting to CRLF from a Windows checkout. Tests ran without `-race`, which needs cgo and a C toolchain this machine does not have.
1 parent 6036804 commit 8c478cd

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
@@ -288,6 +288,7 @@ <h3 class="text-lg font-semibold mb-4">API Base URL</h3>
288288
header.onclick = () => toggleDetails(card, item);
289289
header.innerHTML = `
290290
<div class="flex items-start justify-between gap-3 mb-2">
291+
${renderIcon(server)}
291292
<h3 class="font-semibold text-gray-900 text-base flex-1 min-w-0 break-all">${escapeHtml(server.name)}</h3>
292293
<span class="text-xs text-gray-500 font-medium whitespace-nowrap">v${escapeHtml(server.version)}</span>
293294
</div>
@@ -353,6 +354,7 @@ <h3 class="font-semibold text-gray-900 text-base flex-1 min-w-0 break-all">${esc
353354
header.onclick = () => toggleDetails(card, item);
354355
header.innerHTML = `
355356
<div class="flex items-start justify-between gap-3 mb-2">
357+
${renderIcon(server)}
356358
<h3 class="font-semibold text-gray-900 text-base flex-1 min-w-0">${escapeHtml(server.name)}</h3>
357359
<span class="text-xs text-gray-500 font-medium whitespace-nowrap">v${escapeHtml(server.version)}</span>
358360
</div>
@@ -511,6 +513,58 @@ <h4 class="font-medium text-gray-900 mb-2">Remotes</h4>
511513
.replace(/'/g, '&#39;');
512514
}
513515

516+
// Icons are optional and come from `server.icons` in server.json.
517+
// The publish API already rejects non-HTTPS icon sources, but this UI can
518+
// be pointed at a staging, local or custom API base URL, so the scheme is
519+
// re-checked here rather than trusted.
520+
function isSafeIconSrc(src) {
521+
if (typeof src !== 'string') return false;
522+
try {
523+
return new URL(src, window.location.href).protocol === 'https:';
524+
} catch {
525+
return false;
526+
}
527+
}
528+
529+
// Smallest icon at least as large as the rendered size, so it stays crisp
530+
// without pulling a needlessly large file. Scalable icons win outright.
531+
// `theme: 'dark'` means "for a dark background"; this UI is light, so those
532+
// are used only when nothing else is available.
533+
const ICON_RENDER_PX = 40;
534+
535+
function iconScore(icon) {
536+
const sizes = Array.isArray(icon.sizes) ? icon.sizes : [];
537+
if (sizes.includes('any')) return 0;
538+
const widths = sizes
539+
.map((s) => parseInt(String(s).split('x')[0], 10))
540+
.filter((n) => Number.isFinite(n) && n > 0);
541+
if (!widths.length) return ICON_RENDER_PX * 100;
542+
const atLeast = widths.filter((w) => w >= ICON_RENDER_PX);
543+
return atLeast.length
544+
? Math.min(...atLeast)
545+
: ICON_RENDER_PX * 100 + (ICON_RENDER_PX - Math.max(...widths));
546+
}
547+
548+
function pickIcon(server) {
549+
const icons = Array.isArray(server?.icons) ? server.icons : [];
550+
const usable = icons.filter((i) => i && isSafeIconSrc(i.src));
551+
if (!usable.length) return null;
552+
const preferred = usable.filter((i) => i.theme !== 'dark');
553+
const pool = preferred.length ? preferred : usable;
554+
return pool.reduce((best, i) => (iconScore(i) < iconScore(best) ? i : best));
555+
}
556+
557+
// alt is intentionally empty: the icon sits beside the server name, so
558+
// announcing it again is noise for screen readers. A broken or blocked
559+
// image removes itself rather than leaving a placeholder in the layout.
560+
function renderIcon(server) {
561+
const icon = pickIcon(server);
562+
if (!icon) return '';
563+
return `<img src="${escapeAttr(icon.src)}" alt=""
564+
class="w-10 h-10 rounded flex-shrink-0 object-contain bg-white border border-gray-200"
565+
loading="lazy" referrerpolicy="no-referrer" onerror="this.remove()">`;
566+
}
567+
514568
function formatDate(dateStr) {
515569
if (!dateStr) return '-';
516570
const date = new Date(dateStr);

0 commit comments

Comments
 (0)