|
| 1 | +/** |
| 2 | + * Every MCP tool must advertise its `inputSchema` / `outputSchema` as JSON |
| 3 | + * Schema 2020-12. |
| 4 | + * |
| 5 | + * The SDK converts our Zod v4 schemas with no target, which lands on |
| 6 | + * draft-07. Clients that validate structured output against the declared |
| 7 | + * dialect then reject the tool before it ever runs, taking down the whole |
| 8 | + * tool surface rather than degrading one tool. An Ajv 2020 instance refuses |
| 9 | + * every draft-07-declaring schema with `no schema with key or ref |
| 10 | + * "http://json-schema.org/draft-07/schema#"`. |
| 11 | + * |
| 12 | + * Black-box: assertions read the `tools/list` result off a real client |
| 13 | + * connected over an in-memory transport, so they observe exactly the wire |
| 14 | + * bytes a Claude Code or Claude Desktop client would parse. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 18 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 19 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 20 | +import { describe, expect, test, vi } from 'vitest'; |
| 21 | +import { z } from 'zod'; |
| 22 | +import { installJsonSchemaDialect, JSON_SCHEMA_DIALECT_2020_12 } from './json-schema-dialect.ts'; |
| 23 | + |
| 24 | +const DRAFT_07 = 'http://json-schema.org/draft-07/schema#'; |
| 25 | + |
| 26 | +function buildServer(): McpServer { |
| 27 | + const server = new McpServer({ name: 'json-schema-dialect-test', version: '0.0.0' }); |
| 28 | + server.registerTool( |
| 29 | + 'exec_like', |
| 30 | + { |
| 31 | + description: 'Mirrors the shape every OK tool registers: both schemas present.', |
| 32 | + inputSchema: { command: z.string(), cwd: z.string().optional() }, |
| 33 | + outputSchema: { text: z.string(), exitCode: z.number() }, |
| 34 | + }, |
| 35 | + async () => ({ |
| 36 | + content: [{ type: 'text' as const, text: 'ok' }], |
| 37 | + structuredContent: { text: 'ok', exitCode: 0 }, |
| 38 | + }), |
| 39 | + ); |
| 40 | + return server; |
| 41 | +} |
| 42 | + |
| 43 | +async function listToolsOverWire(server: McpServer) { |
| 44 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 45 | + const client = new Client({ name: 'test-client', version: '0.0.0' }); |
| 46 | + await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]); |
| 47 | + const result = await client.listTools(); |
| 48 | + await client.close(); |
| 49 | + return result; |
| 50 | +} |
| 51 | + |
| 52 | +describe('installJsonSchemaDialect', () => { |
| 53 | + test('the SDK default is draft-07, which is the bug this guards', async () => { |
| 54 | + const { tools } = await listToolsOverWire(buildServer()); |
| 55 | + expect(tools).toHaveLength(1); |
| 56 | + expect(tools[0]?.inputSchema?.$schema).toBe(DRAFT_07); |
| 57 | + expect(tools[0]?.outputSchema?.$schema).toBe(DRAFT_07); |
| 58 | + }); |
| 59 | + |
| 60 | + test('re-declares both schema channels as 2020-12', async () => { |
| 61 | + const server = buildServer(); |
| 62 | + installJsonSchemaDialect(server); |
| 63 | + |
| 64 | + const { tools } = await listToolsOverWire(server); |
| 65 | + expect(tools[0]?.inputSchema?.$schema).toBe(JSON_SCHEMA_DIALECT_2020_12); |
| 66 | + expect(tools[0]?.outputSchema?.$schema).toBe(JSON_SCHEMA_DIALECT_2020_12); |
| 67 | + }); |
| 68 | + |
| 69 | + test('leaves the rest of the schema untouched', async () => { |
| 70 | + const before = (await listToolsOverWire(buildServer())).tools[0]; |
| 71 | + const server = buildServer(); |
| 72 | + installJsonSchemaDialect(server); |
| 73 | + const after = (await listToolsOverWire(server)).tools[0]; |
| 74 | + |
| 75 | + const strip = (s: unknown) => { |
| 76 | + const { $schema: _drop, ...rest } = (s ?? {}) as Record<string, unknown>; |
| 77 | + return rest; |
| 78 | + }; |
| 79 | + expect(strip(after?.inputSchema)).toEqual(strip(before?.inputSchema)); |
| 80 | + expect(strip(after?.outputSchema)).toEqual(strip(before?.outputSchema)); |
| 81 | + }); |
| 82 | + |
| 83 | + test('is idempotent', async () => { |
| 84 | + const server = buildServer(); |
| 85 | + installJsonSchemaDialect(server); |
| 86 | + installJsonSchemaDialect(server); |
| 87 | + |
| 88 | + const { tools } = await listToolsOverWire(server); |
| 89 | + expect(tools[0]?.outputSchema?.$schema).toBe(JSON_SCHEMA_DIALECT_2020_12); |
| 90 | + }); |
| 91 | + |
| 92 | + test('warns instead of throwing when installed before any tool is registered', async () => { |
| 93 | + // The ordering contract: the SDK installs its `tools/list` handler lazily |
| 94 | + // on the first `registerTool`, so an install hoisted above tool |
| 95 | + // registration silently does nothing. Fail loud rather than ship draft-07. |
| 96 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 97 | + const server = new McpServer({ name: 'json-schema-dialect-test', version: '0.0.0' }); |
| 98 | + |
| 99 | + expect(() => installJsonSchemaDialect(server)).not.toThrow(); |
| 100 | + expect(warn).toHaveBeenCalledWith(expect.stringContaining('[json-schema-dialect]')); |
| 101 | + warn.mockRestore(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('a tool registered after install still gets re-declared', async () => { |
| 105 | + // The wrapper defers to the SDK handler, which reads the live tool |
| 106 | + // registry per request, so late registrations flow through it. |
| 107 | + const server = buildServer(); |
| 108 | + installJsonSchemaDialect(server); |
| 109 | + server.registerTool( |
| 110 | + 'late_tool', |
| 111 | + { description: 'registered after install', outputSchema: { text: z.string() } }, |
| 112 | + async () => ({ |
| 113 | + content: [{ type: 'text' as const, text: 'ok' }], |
| 114 | + structuredContent: { text: 'ok' }, |
| 115 | + }), |
| 116 | + ); |
| 117 | + |
| 118 | + const { tools } = await listToolsOverWire(server); |
| 119 | + const late = tools.find((t) => t.name === 'late_tool'); |
| 120 | + expect(late?.outputSchema?.$schema).toBe(JSON_SCHEMA_DIALECT_2020_12); |
| 121 | + }); |
| 122 | +}); |
0 commit comments