Skip to content

fix: add required field to empty object schemas for OpenAI strict mode#1719

Open
guoyangzhen wants to merge 2 commits intomodelcontextprotocol:mainfrom
guoyangzhen:fix/openai-strict-required
Open

fix: add required field to empty object schemas for OpenAI strict mode#1719
guoyangzhen wants to merge 2 commits intomodelcontextprotocol:mainfrom
guoyangzhen:fix/openai-strict-required

Conversation

@guoyangzhen
Copy link

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.

@guoyangzhen guoyangzhen requested a review from a team as a code owner March 20, 2026 07:21
@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2026

🦋 Changeset detected

Latest 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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 20, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1719

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1719

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1719

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1719

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1719

commit: 23c5598

Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was removed, probably needs to go to standardSchemaToJsonSchema now

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.

2 participants