diff --git a/.changeset/fix-zod3-json-schema-2020-12.md b/.changeset/fix-zod3-json-schema-2020-12.md new file mode 100644 index 000000000..4ede7b49c --- /dev/null +++ b/.changeset/fix-zod3-json-schema-2020-12.md @@ -0,0 +1,7 @@ +--- +"@modelcontextprotocol/sdk": patch +--- + +Fix JSON Schema output to use draft-2020-12 for Zod v3 schemas + +The Zod v3 branch of `toJsonSchemaCompat` was not passing a `target` option to `zod-to-json-schema`, causing it to default to draft-07. This breaks compatibility with Claude's API which requires draft-2020-12 compliance. diff --git a/packages/core/src/util/zodJsonSchemaCompat.ts b/packages/core/src/util/zodJsonSchemaCompat.ts index 12e5e88c4..54bad03bd 100644 --- a/packages/core/src/util/zodJsonSchemaCompat.ts +++ b/packages/core/src/util/zodJsonSchemaCompat.ts @@ -38,7 +38,11 @@ export function toJsonSchemaCompat(schema: AnyObjectSchema, opts?: CommonOpts): } // v3 branch — use vendored converter + // Use 'jsonSchema2020-12' to omit the $schema field, which prevents Claude's API + // from rejecting the schema due to draft-07 declaration. The actual schema syntax + // produced is compatible with draft-2020-12. return zodToJsonSchema(schema as z3.ZodTypeAny, { + target: 'jsonSchema2020-12' as 'jsonSchema2019-09', strictUnions: opts?.strictUnions ?? true, pipeStrategy: opts?.pipeStrategy ?? 'input' }) as JsonSchema;