Skip to content

fix: ensure object schemas always include required field for OpenAI compatibility#1696

Open
ctonneslan wants to merge 3 commits intomodelcontextprotocol:mainfrom
ctonneslan:fix/empty-zod-required-field
Open

fix: ensure object schemas always include required field for OpenAI compatibility#1696
ctonneslan wants to merge 3 commits intomodelcontextprotocol:mainfrom
ctonneslan:fix/empty-zod-required-field

Conversation

@ctonneslan
Copy link

Summary

When registering a tool with an empty Zod object (z.object({}).strict()) as inputSchema, the generated JSON schema omits the required field. OpenAI strict JSON schema mode requires required to always be present, causing:

Schema validation failed: root: Schema must have the following keys: required

Changes

packages/core/src/util/schema.ts: schemaToJson() now checks if the output is an object type schema missing required and adds required: [].

packages/server/src/server/mcp.ts: EMPTY_OBJECT_JSON_SCHEMA constant now includes required: [].

Before/After

Before:

{ "type": "object", "properties": {} }

After:

{ "type": "object", "properties": {}, "required": [] }

Both are valid JSON Schema, but the latter is compatible with OpenAI strict mode.

Fixes #1659

…ompatibility

When using z.object({}).strict() as inputSchema, the generated JSON schema
omits the required field. OpenAI strict JSON schema mode requires required
to always be present (even as an empty array), causing tool registration
to fail with: "Schema must have the following keys: required"

Fixed in two places:
- schemaToJson() now adds required: [] to object schemas missing it
- EMPTY_OBJECT_JSON_SCHEMA constant now includes required: []

Fixes modelcontextprotocol#1659
@ctonneslan ctonneslan requested a review from a team as a code owner March 17, 2026 15:48
@changeset-bot
Copy link

changeset-bot bot commented Mar 17, 2026

🦋 Changeset detected

Latest commit: b4f1b45

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@modelcontextprotocol/core Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/node Patch
@modelcontextprotocol/express Patch
@modelcontextprotocol/hono Patch

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 17, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: b4f1b45

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.

inputSchema generated from empty Zod object is incompatible with OpenAI strict JSON schema mode

1 participant