Skip to content

fix(server): emit required keys for exhaustive records in the zod JSON Schema fallback - #2457

Closed
felixweinberger wants to merge 1 commit into
mainfrom
fweinberger/record-required-enum-keys
Closed

felixweinberger wants to merge 1 commit into
mainfrom
fweinberger/record-required-enum-keys

Conversation

@felixweinberger

Copy link
Copy Markdown
Contributor

The zod fallback converter emits record schemas without required when the key type is an exhaustive enum, on zod 4.0–4.2 (zod fixed the emission in 4.3, but runtime validation has always required every enum key).

Motivation and Context

A tool declaring z.record(z.enum(['a','b']), z.number()) advertises a schema that accepts {a: 1}, but validation rejects it — so model/agent clients generate schema-valid arguments that fail at call time with a confusing validation error. The advertised schema should match what validation enforces.

How Has This Been Tested?

13 new unit tests covering enum keys, literal-union keys, nested records, mixed schemas, and a z.partialRecord negative (partial records emit byte-identical JSON pre-4.3, so detection uses the zod schema's own exhaustiveness marker rather than the emitted JSON). Also verified end-to-end over HTTP: tools/list advertises required and runtime validation matches.

Breaking Changes

None — schemas only gain a required array that matches existing validation behavior. Idempotent on zod >= 4.3 emissions.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

…back

zod 4.0.x-4.2.x z.toJSONSchema converts z.record(z.enum([...]), value) to
{type:'object', propertyNames:{enum:[...]}, additionalProperties:...} without
'required', although runtime validation rejects objects missing any of the
keys (exhaustive records; fixed upstream in zod 4.3). A tool converted through
the standardSchemaToJsonSchema fallback (taken on zod versions without
~standard.jsonSchema, i.e. exactly the ones with the gap) therefore advertises
a schema that accepts inputs its own validation then refuses.

Post-process the fallback emission: walk the zod schema for record nodes whose
key type carries a finite string key set (keyType._zod.values - the same
marker zod's own record parser uses to decide exhaustiveness; z.partialRecord
clears it), and add required:[...keys] to every emitted record-shaped JSON
node whose propertyNames key set matches. Emitted JSON alone cannot
distinguish record from partialRecord (identical shape pre-4.3), hence the
zod-side walk. Nodes already carrying 'required' are left untouched, so the
fixup is a no-op on zod versions that emit it natively.
@felixweinberger
felixweinberger requested a review from a team as a code owner July 7, 2026 16:34
@changeset-bot

changeset-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 1c2013f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2457

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@2457

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@2457

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2457

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@2457

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2457

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2457

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2457

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2457

commit: 1c2013f

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant