From d75057f72e678f9a9a5e9f152ba6455defec03ea Mon Sep 17 00:00:00 2001 From: guoyangzhen Date: Fri, 20 Mar 2026 14:55:16 +0800 Subject: [PATCH 1/2] fix: add required field to empty object schemas for OpenAI strict mode (#1659) --- packages/core/src/util/schema.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/core/src/util/schema.ts b/packages/core/src/util/schema.ts index adecee361..156aa76f1 100644 --- a/packages/core/src/util/schema.ts +++ b/packages/core/src/util/schema.ts @@ -26,7 +26,22 @@ export type SchemaOutput = z.output; * Converts a Zod schema to JSON Schema. */ export function schemaToJson(schema: AnySchema, options?: { io?: 'input' | 'output' }): Record { - return z.toJSONSchema(schema, options) as Record; + const jsonSchema = z.toJSONSchema(schema, options) as Record; + + // OpenAI strict JSON schema mode requires the `required` field to always + // be present on object schemas, even when empty. Zod's toJSONSchema omits + // it for empty objects (e.g. z.object({}).strict()), causing tool + // registration to fail with OpenAI's strict mode. + // See: https://github.com/modelcontextprotocol/typescript-sdk/issues/1659 + if ( + jsonSchema.type === 'object' && + jsonSchema.properties !== undefined && + !Array.isArray(jsonSchema.required) + ) { + jsonSchema.required = []; + } + + return jsonSchema; } /** From 23c5598f003a61a9e0569aa7feb07ad7876b2ac7 Mon Sep 17 00:00:00 2001 From: guoyangzhen Date: Sat, 21 Mar 2026 06:12:09 +0800 Subject: [PATCH 2/2] chore: add changeset --- .changeset/fix-openai-strict-required.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fix-openai-strict-required.md diff --git a/.changeset/fix-openai-strict-required.md b/.changeset/fix-openai-strict-required.md new file mode 100644 index 000000000..96a162fb7 --- /dev/null +++ b/.changeset/fix-openai-strict-required.md @@ -0,0 +1,5 @@ +--- +"@modelcontextprotocol/sdk": patch +--- + +fix: include required field in JSON Schema when Zod .optional() is absent