Repository navigation
fix(server): preserve inputSchema for z.discriminatedUnion / z.union - #2017
ankitvirdi4 wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 7b808a9 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: |
|
gentle bump on this one. happy to rebase against v1.x if its drifted, or rescope to drop the ensureObjectRoot helper if youd prefer the narrower change (just the normalizeObjectSchema pass-through). let me know if anything needs revising and ill turn it around quickly. |
|
any update on when/if this will get merged? |
…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.
0ce5cf5 to
7b808a9
Compare
|
@felixweinberger quick context since #1643 shows as closed: it was closed by #1796, which fixed this on This PR is the v1 backport. It does the same two things #1796 does on
re my earlier offer to drop the Just rebased onto latest @joselvelez thanks for the nudge. it's rebased and ready on my side, now waiting on maintainer review. in the meantime you can try it with |
|
Confirming this in production on 1.29.0: a What we do while waiting for this: One trap for anyone switching to Thanks for working on the v1.x backport. |
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