Skip to content

fix: emit JSON Schema draft 2020-12 for tools/list (#32) - #33

Merged
Siumauricio merged 1 commit into
Dokploy:mainfrom
kk-michael:bugfix/issue-32-json-schema-draft-2020-12
May 13, 2026
Merged

Siumauricio merged 1 commit into
Dokploy:mainfrom
kk-michael:bugfix/issue-32-json-schema-draft-2020-12

Conversation

@kk-michael

Copy link
Copy Markdown
Contributor

Problem

Fixes #32.

When an MCP client (e.g. Anthropic's Claude API) calls tools/list, the server currently returns tool schemas tagged as "$schema": "http://json-schema.org/draft-07/schema#". Claude's API strictly requires JSON Schema draft 2020-12 and rejects the whole response with a 400 InvalidRequestError, making every tool in this server unusable.

Root cause: McpServer.tool() auto-registers a tools/list handler that converts each Zod schema through the SDK's built-in toJsonSchemaCompat(). That helper delegates to zod-to-json-schema which emits draft-07 by default. Zod v3's z.toJSONSchema (draft 2020-12) lives only in @modelcontextprotocol/[email protected], so every consumer of the stable SDK inherits the draft-07 output.

Fix (Option A — pragmatic, ships today)

This PR overrides the auto-registered handler with a custom one that does the Zod → JSON Schema conversion in this package, then stamps the correct $schema draft. It does not touch SDK internals and does not require any alpha dependency.

Concretely, src/server.ts:

  1. Registers all tools via server.tool(...) as before (so call handlers keep working).
  2. Adds its own ListToolsRequestSchema handler that replaces the SDK default:
    • Convert each Zod schema with zodToJsonSchema(..., { target: "jsonSchema2019-09", strictUnions: true }).
    • Recursively strip any nested $schema keys.
    • Set the top-level $schema to https://json-schema.org/draft/2020-12/schema.

Draft 2019-09 is intentional: the zod-to-json-schema package doesn't expose a "draft-2020-12" target, but 2019-09 is a strict subset of 2020-12 for the features emitted here (type, properties, required, enum, anyOf, description, etc.), so relabeling is safe.

Verification

Built the fixed server and connected a fresh Claude Code MCP client to it. All 87 tools now load:

✓ tools/list returns 87 tools
✓ every inputSchema has "$schema": "https://json-schema.org/draft/2020-12/schema"
✓ no draft-07 entries
✓ test calls on application-*, deployment-*, domain-*, postgres-*, project-*, server-* resolve cleanly

Checks:

pnpm install       # clean
pnpm type-check    # ok
pnpm lint          # ok
pnpm run build     # ok

Dependencies

Adds zod-to-json-schema as a direct dependency (it was already transitive via the SDK) so the explicit import is covered.

Notes / Option B

An alternative would be to route through Standard Schema, which is the path newer MCP SDKs are taking. That support lives in @modelcontextprotocol/[email protected], which is an alpha release with breaking API changes. Once that lands in a stable SDK, this local conversion can be removed. Until then, Option A keeps the project on the current stable SDK and unblocks Claude clients immediately.

🤖 Generated with Claude Code

Claude's API rejects the default draft-07 output from the MCP SDK's
Zod→JSON Schema converter with:

  tools.N.custom.input_schema: JSON schema is invalid.
  It must match JSON Schema draft 2020-12

Bypass the SDK's auto-registered tools/list handler with one that
pre-generates schemas via zod-to-json-schema (targeting
jsonSchema2019-09), strips any nested $schema keys, and sets the
top-level $schema to https://json-schema.org/draft/2020-12/schema.

The per-tool CallTool dispatch registered by server.tool() is
untouched; only the schema serialization path changes.
@work-evan

Copy link
Copy Markdown

+1 This solved my issues in claude code desktop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v2.0 does not fix draft 2020-12 schema error (reopen #10)

3 participants