Repository navigation
fix(server): emit required keys for exhaustive records in the zod JSON Schema fallback - #2457
Closed
felixweinberger wants to merge 1 commit into
Closed
felixweinberger wants to merge 1 commit into
felixweinberger wants to merge 1 commit into
Conversation
…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.
|
6 tasks done
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The zod fallback converter emits record schemas without
requiredwhen 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.partialRecordnegative (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 advertisesrequiredand runtime validation matches.Breaking Changes
None — schemas only gain a
requiredarray that matches existing validation behavior. Idempotent on zod >= 4.3 emissions.Types of changes
Checklist