fix(server): preserve inputSchema for z.discriminatedUnion / z.union#2017
Open
ankitvirdi4 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Open
Conversation
…odelcontextprotocol#1643) normalizeObjectSchema returned undefined for any schema whose root was not z.object(...), so registerTool silently advertised an empty schema in tools/list when given z.discriminatedUnion(...). Tool calls still validated correctly via the fallback in validateToolInput, which masked the bug from end-to-end tests. Pass discriminated unions and unions through unchanged in normalizeObjectSchema; let toJsonSchemaCompat handle the actual conversion (zod-to-json-schema for v3, Mini's z.toJSONSchema for v4). Both emit oneOf/anyOf branches. Default a top-level type: "object" on the emitted JSON Schema in mcp.ts so the wire payload is spec compliant when the converter does not include one (the discriminated-union and union case). Adds three regression tests under "Tools with union and intersection schemas" exercising tools/list, callTool, and outputSchema for v3 and v4.
🦋 Changeset detectedLatest commit: b0e4b04 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 |
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.
Summary
Fixes #1643.
registerTool/registerPromptsilently advertised an empty schema intools/listwhen given az.discriminatedUnion(...)or az.union([z.object(...), ...]). Tool calls still validated correctly via the fallback invalidateToolInput, which masked the bug from end-to-end tests.Repro on
v1.xHEADRoot cause
normalizeObjectSchema()(insrc/server/zod-compat.ts) returnedundefinedfor any schema whose root was notz.object(...). Thetools/listhandler then substitutedEMPTY_OBJECT_JSON_SCHEMA. The downstreamtoJsonSchemaCompatalready handles unions correctly (zod-to-json-schema for v3, Mini'sz.toJSONSchemafor v4) but never got called.Fix
Two surgical changes:
src/server/zod-compat.ts—normalizeObjectSchema()now also passes through:_zod.def.type === 'union'(covers bothz.unionandz.discriminatedUnion)_def.typeNameofZodDiscriminatedUnionorZodUnionSchemas whose root is genuinely non-object (
z.string(),z.array(...)) still returnundefined, matching prior behavior.src/server/mcp.ts— wraps the conversion result with a smallensureObjectRoot()helper that defaultstype: "object"when the converter omits it. Discriminated unions and unions emitoneOf/anyOfwithout a roottype, but the MCP spec requires tool input/output schemas to describe objects, so we default it on the way out. Plainz.object(...)already setstype: "object", so this is a no-op for the common case. Schemas with an explicit non-object roottypeare left alone (they fall out as user errors downstream, which is the right signal).Tests
Three regression tests under
"Tools with union and intersection schemas". The file usesdescribe.each(zodTestMatrix)so each runs once for v3 and once for v4 — six actual cases:inputSchemais exposed intools/listwithtype: "object"+oneOf/anyOfoutputSchema, same, plus end-to-endcallToolround tripIncludes a changeset entry.
Test plan
npx vitest run test/server/mcp.test.ts— 238 / 238 passingnpx vitest run(full suite) — 1585 / 1585 passingnpm run typecheckcleannpm run lintcleantools/listreturns the proper schema with branches; valid input round-trips; malformed input returns proper validation error