fix: add required field to empty object schemas for OpenAI strict mode#1719
fix: add required field to empty object schemas for OpenAI strict mode#1719guoyangzhen wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 23c5598 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
There was a problem hiding this comment.
Adding special handling for a specific downstream consumer seems backwards. It's the validator's (Zod, Valibot, etc.) responsibility to determine what the output should be.
I'd rather not mess with library outputs that we depend on upstream in a potentially surprising or unexpected way.
This seems like something that needs to be handled at the implementation layer if someone wants to integrate with OpenAI systems.
Do you have no way around this in whatever wrapping code you use to to integrate with OpenAI?
| @@ -26,7 +26,22 @@ export type SchemaOutput<T extends AnySchema> = z.output<T>; | |||
| * Converts a Zod schema to JSON Schema. | |||
| */ | |||
| export function schemaToJson(schema: AnySchema, options?: { io?: 'input' | 'output' }): Record<string, unknown> { | |||
There was a problem hiding this comment.
This function was removed, probably needs to go to standardSchemaToJsonSchema now
Problem
When registering a tool with an empty Zod object as inputSchema (e.g. ), the generated JSON schema omits the field. While this is valid JSON Schema, it is incompatible with OpenAI strict JSON schema mode which requires to always be present (even as an empty array ).
This causes tool registration failures when using MCP tools with OpenAI models in strict mode.
Root Cause
Zod omits the field for empty objects. The MCP SDK passes the result through without adjustment.
Fix
In , after calling , check if the result is an object type with defined but no array, and add . This ensures compatibility with OpenAI strict mode while remaining valid JSON Schema.