fix: advertise tool schemas as JSON Schema 2020-12 (#335)

* refactor(tools): pass Zod schemas to registerTool instead of raw shapes

registerTool accepts either a ZodObject or a raw shape, and passing the
schema itself keeps each tool definition closer to how its schema is
declared. The advertised JSON Schema is unchanged either way.

* fix(server): advertise tool schemas as JSON Schema 2020-12

The SDK converts our Zod schemas on every tools/list with a hardcoded
draft-07 target: mcp.js calls toJsonSchemaCompat() without a `target`, and
mapMiniTarget(undefined) falls back to 'draft-7'. No registerTool option
overrides it, so every schema we advertised declared draft-07.

Clients that validate against 2020-12 reject those definitions outright,
which takes down the whole tool list rather than a single tool. Separately,
brave_place_search's coordinates tuple emitted draft-07's array-form
`items`, which is invalid under 2020-12 where positional validation moved
to `prefixItems`.

SEP-1613 §5 requires servers to generate 2020-12 by default, so re-emit the
schemas on the tools/list response. Each correction is conditional, so this
decays to a no-op once the SDK emits 2020-12 itself.

Refs: https://github.com/modelcontextprotocol/typescript-sdk/issues/2084

* test(server): assert advertised schemas are valid JSON Schema 2020-12

Compile every tool's inputSchema and outputSchema from a real tools/list
response with a 2020-12 validator. This runs against the wire format rather
than our Zod definitions because the SDK performs the conversion itself: the
Zod schemas can be correct while the advertised JSON Schema is not.

Compiling resolves whichever dialect a schema declares, so a single check
covers both failure modes - a draft-07 declaration, and keywords that are
invalid under 2020-12. The SDK's own default client validator is Ajv
draft-07 with schema validation disabled, so it accepts output that 2020-12
clients reject; add ajv as a devDependency to check the dialect explicitly.
This commit is contained in:
Sampson authored and GitHub committed 2026-08-20 10:41:28 -05:00
1 parent 937e85a61f
commit e8b62148ad
13 files changed
+128 -14

No files matched your search

+4 -3
View File
@@ -21,6 +21,7 @@
"devDependencies": {
"@types/express": "5.0.6",
"@types/node": "24.10.13",
"ajv": "8.20.0",
"prettier": "3.8.1",
"semver": "7.7.3",
"shx": "0.4.0",
@@ -656,9 +657,9 @@
}
},
"node_modules/ajv": {
"version": "8.18.0",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.18.0.tgz",
"integrity": "sha512-PlXPeEWMXMZ7sPYOHqmDyCJzcfNrUr3fGNKtezX14ykXOEIvyK81d+qydx89KY5O71FKMPaQ2vBfBFI5NHR63A==",
"version": "8.20.0",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.20.0.tgz",
"integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==",
"license": "MIT",
"dependencies": {
"fast-deep-equal": "^3.1.3",
+1
View File
@@ -45,6 +45,7 @@
"devDependencies": {
"@types/express": "5.0.6",
"@types/node": "24.10.13",
"ajv": "8.20.0",
"prettier": "3.8.1",
"semver": "7.7.3",
"shx": "0.4.0",
+82
View File
@@ -0,0 +1,82 @@
import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import {
ListToolsRequestSchema,
type ListToolsResult,
type Tool,
} from '@modelcontextprotocol/sdk/types.js';
import { z, type ZodRawShape } from 'zod';
import tools from './tools/index.js';
/**
* The dialect servers MUST emit by default, per SEP-1613 §5.
* https://modelcontextprotocol.io/seps/1613-establish-json-schema-2020-12-as-default-dialect-f
*/
export const JSON_SCHEMA_2020_12 = 'https://json-schema.org/draft/2020-12/schema';
type ToolSchemaShapes = {
inputSchema?: ZodRawShape;
outputSchema?: ZodRawShape;
};
const schemaShapesByTool = new Map<string, ToolSchemaShapes>(
Object.values(tools).map((tool) => [tool.name, tool as ToolSchemaShapes])
);
const toJsonSchema2020 = (shape: ZodRawShape, io: 'input' | 'output'): Tool['inputSchema'] =>
z.toJSONSchema(z.object(shape), {
target: 'draft-2020-12',
io,
}) as unknown as Tool['inputSchema'];
type ListToolsHandler = (request: unknown, extra: unknown) => Promise<ListToolsResult>;
// An absent `$schema` defaults to 2020-12, but says nothing about which dialect's
// keywords were emitted, so only an explicit declaration counts as correct.
const needsRetargeting = (schema?: Tool['inputSchema'] | Tool['outputSchema']): boolean =>
(schema as { $schema?: string } | undefined)?.$schema !== JSON_SCHEMA_2020_12;
/**
* Re-emits advertised tool schemas as 2020-12, skipping any the SDK already got
* right - so this decays to a string comparison once the SDK is fixed, and can
* then be deleted.
*
* The SDK hardcodes draft-07 (`mcp.js` calls `toJsonSchemaCompat()` with no
* `target`), which 2020-12-only clients reject outright, and whose array-form
* `items` is invalid 2020-12 (`brave_place_search`'s coordinates tuple).
* Wrapping its handler preserves the other fields it emits.
*
* https://github.com/modelcontextprotocol/typescript-sdk/issues/2084 (PR #2085)
*
* Must be called after all tools are registered.
*/
export default function enforce2020Dialect(mcpServer: McpServer): void {
const requestHandlers = (
mcpServer.server as unknown as { _requestHandlers?: Map<string, ListToolsHandler> }
)._requestHandlers;
const listTools = requestHandlers?.get('tools/list');
// Only reachable through SDK internals; if a future version moves it, leave its
// output alone rather than failing to start - server.test.ts catches regressions.
if (!listTools) return;
mcpServer.server.setRequestHandler(ListToolsRequestSchema, async (request, extra) => {
const result = await listTools(request, extra);
for (const tool of result.tools) {
const shapes = schemaShapesByTool.get(tool.name);
if (!shapes) continue;
if (shapes.inputSchema && needsRetargeting(tool.inputSchema)) {
tool.inputSchema = toJsonSchema2020(shapes.inputSchema, 'input');
}
if (shapes.outputSchema && tool.outputSchema && needsRetargeting(tool.outputSchema)) {
tool.outputSchema = toJsonSchema2020(shapes.outputSchema, 'output');
}
}
return result;
});
}
+26
View File
@@ -1,6 +1,7 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { Ajv2020 } from 'ajv/dist/2020.js';
import { connectTestClient } from './testUtils/mcpTestHarness.js';
import tools from './tools/index.js';
@@ -26,4 +27,29 @@ describe('MCP server <-> SDK Client wiring (in-memory)', () => {
assert.ok(tool.inputSchema, `${tool.name} is missing an inputSchema`);
}
});
it('advertises valid JSON Schema 2020-12 schemas', async () => {
const { tools: listedTools } = await client.listTools();
// Compiling resolves the declared dialect, so a draft-07 tag fails here as
// readily as invalid 2020-12 keywords. The SDK does the Zod conversion, so
// only the wire format is worth asserting.
const ajv = new Ajv2020({ strict: false, validateFormats: false });
for (const tool of listedTools) {
assert.doesNotThrow(
() => ajv.compile(tool.inputSchema),
`${tool.name} has an invalid inputSchema`
);
const { outputSchema } = tool;
if (outputSchema) {
assert.doesNotThrow(
() => ajv.compile(outputSchema),
`${tool.name} has an invalid outputSchema`
);
}
}
});
});
+4
View File
@@ -2,6 +2,7 @@ import tools from './tools/index.js';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import pkg from '../package.json' with { type: 'json' };
import { isToolPermittedByUser } from './config.js';
import enforce2020Dialect from './jsonSchemaDialect.js';
export default function createMcpServer(): McpServer {
const mcpServer = new McpServer(
@@ -25,5 +26,8 @@ export default function createMcpServer(): McpServer {
tool.register(mcpServer);
}
// Advertise 2020-12 schemas; the SDK would otherwise emit draft-07
enforce2020Dialect(mcpServer);
return mcpServer;
}
+2 -2
View File
@@ -45,8 +45,8 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: params.shape,
outputSchema: OutputSchema.shape,
inputSchema: params,
outputSchema: OutputSchema,
annotations: annotations,
},
execute
+2 -2
View File
@@ -51,8 +51,8 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: LlmContextInputSchema.shape,
outputSchema: LlmContextSearchApiResponseSchema.shape,
inputSchema: LlmContextInputSchema,
outputSchema: LlmContextSearchApiResponseSchema,
annotations: annotations,
},
execute
+1 -1
View File
@@ -79,7 +79,7 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: webParams.shape,
inputSchema: webParams,
annotations: annotations,
},
execute
+1 -1
View File
@@ -58,7 +58,7 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: params.shape,
inputSchema: params,
annotations: annotations,
},
execute
+2 -2
View File
@@ -56,8 +56,8 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: PlaceSearchInputSchema.shape,
outputSchema: PlaceSearchApiResponseSchema.shape,
inputSchema: PlaceSearchInputSchema,
outputSchema: PlaceSearchApiResponseSchema,
annotations: annotations,
},
execute
+1 -1
View File
@@ -73,7 +73,7 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: summarizerQueryParams.shape,
inputSchema: summarizerQueryParams,
annotations: annotations,
},
execute
+1 -1
View File
@@ -43,7 +43,7 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: params.shape,
inputSchema: params,
annotations: annotations,
},
execute
+1 -1
View File
@@ -126,7 +126,7 @@ export const register = (mcpServer: McpServer) => {
{
title: name,
description: description,
inputSchema: params.shape,
inputSchema: params,
annotations: annotations,
},
execute