|
| 1 | +/** |
| 2 | + * Icons with light and dark themes (SEP-973, #3131), as each era's client |
| 3 | + * sees them. |
| 4 | + * |
| 5 | + * The server attaches one light/dark icon pair per primitive kind |
| 6 | + * (server/icons.ts). These tests read what crosses the wire, in both |
| 7 | + * protocol eras: `serverInfo` from `initialize` (2025-era) or |
| 8 | + * `server/discover` (2026-07-28), and every entry of `tools/list`, |
| 9 | + * `prompts/list`, `resources/list` and `resources/templates/list`. Each |
| 10 | + * must carry a "light" and a "dark" icon, and each icon must be a scalable |
| 11 | + * SVG `data:` URI that decodes to an SVG document. |
| 12 | + */ |
| 13 | +import { afterEach, describe, expect, it } from "vitest"; |
| 14 | +import type { Icon } from "@modelcontextprotocol/client"; |
| 15 | +import { |
| 16 | + ALL_CAPABILITIES, |
| 17 | + connect, |
| 18 | + connectModern, |
| 19 | + connectRaw, |
| 20 | + MODERN_CAPABILITIES, |
| 21 | + type RawSession, |
| 22 | + type Session, |
| 23 | +} from "./harness.js"; |
| 24 | +import { |
| 25 | + PROMPT_ICONS, |
| 26 | + RESOURCE_ICONS, |
| 27 | + RESOURCE_TEMPLATE_ICONS, |
| 28 | + SERVER_ICONS, |
| 29 | + TOOL_ICONS, |
| 30 | +} from "../server/icons.js"; |
| 31 | + |
| 32 | +let session: Session | undefined; |
| 33 | +let raw: RawSession | undefined; |
| 34 | + |
| 35 | +afterEach(async () => { |
| 36 | + await session?.close(); |
| 37 | + await raw?.close(); |
| 38 | + session = undefined; |
| 39 | + raw = undefined; |
| 40 | +}); |
| 41 | + |
| 42 | +const SVG_DATA_URI = "data:image/svg+xml;base64,"; |
| 43 | + |
| 44 | +/** Assert that `icons` is a themed pair of decodable SVG icons. */ |
| 45 | +function expectThemedPair(icons: Icon[] | undefined): void { |
| 46 | + expect(icons).toBeDefined(); |
| 47 | + const themes = (icons ?? []).map((icon) => icon.theme).sort(); |
| 48 | + expect(themes).toEqual(["dark", "light"]); |
| 49 | + for (const icon of icons ?? []) { |
| 50 | + expect(icon.mimeType).toBe("image/svg+xml"); |
| 51 | + expect(icon.sizes).toEqual(["any"]); |
| 52 | + expect(icon.src.startsWith(SVG_DATA_URI)).toBe(true); |
| 53 | + const svg = Buffer.from( |
| 54 | + icon.src.slice(SVG_DATA_URI.length), |
| 55 | + "base64", |
| 56 | + ).toString("utf8"); |
| 57 | + expect(svg).toMatch(/^<svg xmlns="http:\/\/www\.w3\.org\/2000\/svg"/); |
| 58 | + expect(svg).toMatch(/<\/svg>$/); |
| 59 | + } |
| 60 | + // The two variants are drawn differently, not one image labelled twice. |
| 61 | + expect(icons?.[0]?.src).not.toBe(icons?.[1]?.src); |
| 62 | +} |
| 63 | + |
| 64 | +describe("the icon sets", () => { |
| 65 | + it("gives every primitive kind its own light/dark pair", () => { |
| 66 | + const sets = [ |
| 67 | + SERVER_ICONS, |
| 68 | + TOOL_ICONS, |
| 69 | + PROMPT_ICONS, |
| 70 | + RESOURCE_ICONS, |
| 71 | + RESOURCE_TEMPLATE_ICONS, |
| 72 | + ]; |
| 73 | + for (const set of sets) expectThemedPair(set); |
| 74 | + const lightSources = new Set( |
| 75 | + sets.map((set) => set.find((icon) => icon.theme === "light")?.src), |
| 76 | + ); |
| 77 | + expect(lightSources.size).toBe(sets.length); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe.each([ |
| 82 | + { |
| 83 | + era: "2025-era", |
| 84 | + open: () => connect({ capabilities: ALL_CAPABILITIES }), |
| 85 | + }, |
| 86 | + { |
| 87 | + era: "2026-07-28", |
| 88 | + open: () => connectModern({ capabilities: MODERN_CAPABILITIES }), |
| 89 | + }, |
| 90 | +])("icons on the $era surface", ({ open }) => { |
| 91 | + it("puts the server's pair on serverInfo", async () => { |
| 92 | + session = await open(); |
| 93 | + const info = session.client.getServerVersion(); |
| 94 | + expect(info?.icons).toEqual(SERVER_ICONS); |
| 95 | + expectThemedPair(info?.icons); |
| 96 | + }); |
| 97 | + |
| 98 | + it("puts the tool pair on every tool", async () => { |
| 99 | + session = await open(); |
| 100 | + const { tools } = await session.client.listTools(); |
| 101 | + expect(tools.length).toBeGreaterThan(0); |
| 102 | + for (const tool of tools) expect(tool.icons).toEqual(TOOL_ICONS); |
| 103 | + }); |
| 104 | + |
| 105 | + it("puts the prompt pair on every prompt", async () => { |
| 106 | + session = await open(); |
| 107 | + const { prompts } = await session.client.listPrompts(); |
| 108 | + expect(prompts.length).toBeGreaterThan(0); |
| 109 | + for (const prompt of prompts) expect(prompt.icons).toEqual(PROMPT_ICONS); |
| 110 | + }); |
| 111 | + |
| 112 | + it("puts the resource pair on every listed resource", async () => { |
| 113 | + session = await open(); |
| 114 | + const { resources } = await session.client.listResources(); |
| 115 | + expect(resources.length).toBeGreaterThan(0); |
| 116 | + for (const resource of resources) { |
| 117 | + expect(resource.icons).toEqual(RESOURCE_ICONS); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it("puts the template pair on every resource template", async () => { |
| 122 | + session = await open(); |
| 123 | + const { resourceTemplates } = await session.client.listResourceTemplates(); |
| 124 | + expect(resourceTemplates.length).toBeGreaterThan(0); |
| 125 | + for (const template of resourceTemplates) { |
| 126 | + expect(template.icons).toEqual(RESOURCE_TEMPLATE_ICONS); |
| 127 | + } |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe("icons on the raw 2026-07-28 wire", () => { |
| 132 | + it("carries the server's pair in server/discover's serverInfo", async () => { |
| 133 | + raw = await connectRaw({}); |
| 134 | + const discover = await raw.result("server/discover"); |
| 135 | + expect(discover).toMatchObject({ |
| 136 | + _meta: { |
| 137 | + "io.modelcontextprotocol/serverInfo": { icons: SERVER_ICONS }, |
| 138 | + }, |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments