Skip to content

Commit b288675

Browse files
authored
fix: relabel tool schema dialect from draft-07 to 2020-12 (#83) (#84)
Fixes #83. The MCP SDK we use converts every tool's Zod schema to JSON Schema via zod-to-json-schema without setting a target, so it stamps "$schema": "http://json-schema.org/draft-07/schema#" on every inputSchema/outputSchema regardless of SDK version. This is a known, still-open upstream issue (modelcontextprotocol/typescript-sdk#745) with no shipped SDK fix yet (a v2 rewrite is planned but unreleased). This PR overwrites the schema to https://json-schema.org/draft/2020-12/schema.
1 parent 59519e7 commit b288675

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎src/server.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2-
import { McpError, ErrorCode, SetLevelRequestSchema, LoggingLevelSchema, type LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
2+
import { McpError, ErrorCode, SetLevelRequestSchema, ListToolsRequestSchema, LoggingLevelSchema, type LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
33
import { z } from 'zod';
44
import { randomBytes, randomInt } from 'node:crypto';
55
import { UptimeKumaClient } from './uptime-kuma-client.js';
@@ -146,6 +146,49 @@ export async function createServer(config: UptimeKumaConfig): Promise<{ server:
146146
return schema;
147147
}
148148

149+
// Issue #83: the SDK converts every tool's input/output Zod schema to JSON Schema via
150+
// zod-to-json-schema without setting a `target`, so each one is stamped
151+
// "$schema": "http://json-schema.org/draft-07/schema#" — verified against both the SDK
152+
// version this project pins (^1.22.0) and the current latest (1.30.0); the behavior is
153+
// unchanged between them. Clients whose validator only accepts the MCP spec's default
154+
// dialect (2020-12, per SEP-1613) reject every tool outright before ever calling it
155+
// (see modelcontextprotocol/typescript-sdk#745, still open upstream).
156+
//
157+
// None of the schemas below use anything that differs between the two drafts (no
158+
// `dependencies`, no positional tuple items), so relabeling the declared dialect is safe —
159+
// only the version stamp was wrong, not the shape. This intercepts the SDK's own
160+
// `setRequestHandler(ListToolsRequestSchema, ...)` call (made lazily on first
161+
// `registerTool`, below) rather than reimplementing tools/list, so it keeps working across
162+
// SDK internals changing between versions.
163+
const DRAFT_07_DIALECT = 'http://json-schema.org/draft-07/schema#';
164+
const DIALECT_2020_12 = 'https://json-schema.org/draft/2020-12/schema';
165+
const relabelDialect = (value: unknown): unknown => {
166+
if (Array.isArray(value)) return value.map(relabelDialect);
167+
if (value && typeof value === 'object') {
168+
const out: Record<string, unknown> = {};
169+
for (const [key, val] of Object.entries(value as Record<string, unknown>)) {
170+
out[key] = key === '$schema' && val === DRAFT_07_DIALECT ? DIALECT_2020_12 : relabelDialect(val);
171+
}
172+
return out;
173+
}
174+
return value;
175+
};
176+
177+
const setRequestHandlerUnpatched = server.server.setRequestHandler.bind(server.server) as (
178+
requestSchema: unknown,
179+
handler: (...args: unknown[]) => unknown
180+
) => unknown;
181+
(server.server as unknown as { setRequestHandler: typeof server.server.setRequestHandler }).setRequestHandler = ((
182+
requestSchema: unknown,
183+
handler: (...args: unknown[]) => unknown
184+
) => {
185+
if (requestSchema === ListToolsRequestSchema) {
186+
const wrapped = async (...args: unknown[]) => relabelDialect(await handler(...args));
187+
return setRequestHandlerUnpatched(requestSchema, wrapped);
188+
}
189+
return setRequestHandlerUnpatched(requestSchema, handler);
190+
}) as typeof server.server.setRequestHandler;
191+
149192
// Handle logging level changes via the underlying server
150193
server.server.setRequestHandler(SetLevelRequestSchema, async (request) => {
151194
const level = request.params?.level as LoggingLevel | undefined;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { createServer } from '../../src/server.js';
3+
4+
/**
5+
* Issue #83: the SDK stamps every tool's input/output schema with
6+
* "$schema": "http://json-schema.org/draft-07/schema#" (zod-to-json-schema's default target),
7+
* which clients validating strictly against the MCP spec's default dialect (2020-12, per
8+
* SEP-1613) reject outright. server.ts relabels the dialect on the way out; this asserts none
9+
* of the advertised schemas still declare draft-07.
10+
*/
11+
describe('tools/list schema dialect', () => {
12+
it('advertises the 2020-12 dialect on every tool schema, never draft-07', async () => {
13+
const { server } = await createServer({
14+
url: 'http://localhost:3001',
15+
username: undefined,
16+
password: undefined,
17+
token: undefined,
18+
jwtToken: undefined,
19+
});
20+
21+
const handler = (server.server as unknown as {
22+
_requestHandlers: Map<string, (request: unknown, extra: unknown) => Promise<{ tools: unknown[] }>>;
23+
})._requestHandlers.get('tools/list');
24+
expect(handler).toBeDefined();
25+
26+
const { tools } = await handler!({ method: 'tools/list', params: {} }, {});
27+
expect(tools.length).toBeGreaterThan(0);
28+
29+
const draft07 = 'http://json-schema.org/draft-07/schema#';
30+
const found = new Set<string>();
31+
const walk = (value: unknown): void => {
32+
if (Array.isArray(value)) {
33+
value.forEach(walk);
34+
return;
35+
}
36+
if (value && typeof value === 'object') {
37+
for (const [key, val] of Object.entries(value as Record<string, unknown>)) {
38+
if (key === '$schema' && typeof val === 'string') found.add(val);
39+
walk(val);
40+
}
41+
}
42+
};
43+
walk(tools);
44+
45+
expect(found.has(draft07)).toBe(false);
46+
expect(found.has('https://json-schema.org/draft/2020-12/schema')).toBe(true);
47+
});
48+
});

0 commit comments

Comments
 (0)