fix(server): handle ZodEffects in normalizeObjectSchema and getObjectShape#1865
Open
pch007 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Open
fix(server): handle ZodEffects in normalizeObjectSchema and getObjectShape#1865pch007 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
pch007 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Conversation
…Shape Schemas wrapped with `.superRefine()`, `.refine()`, or `.transform()` become ZodEffects (v3) or pipe types (v4) that lack `.shape`, causing `normalizeObjectSchema()` to return `undefined` and `tools/list` to fall back to `EMPTY_OBJECT_JSON_SCHEMA`. This fix: - Adds `unwrapZodEffects()` helper that walks `_def.schema` chains (v3 ZodEffects) with a depth bound of 10 to prevent infinite loops - Updates `normalizeObjectSchema()` to detect v3 ZodEffects and v4 pipe types wrapping ZodObjects, returning the original schema so `zodToJsonSchema()` / `toJSONSchema()` can extract correct JSON Schema - Updates `getObjectShape()` to extract shape through ZodEffects/pipe wrappers for both Zod versions Adds 4 new tests verifying `tools/list` returns correct JSON Schema properties for `.superRefine()`, `.refine()`, `.transform()`, and nested ZodEffects chains. All tests run against both Zod v3 and v4. Fixes: tools registered with refined/transformed schemas now correctly advertise their input properties to MCP clients.
🦋 Changeset detectedLatest commit: ac01b14 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
Schemas wrapped with
.superRefine(),.refine(), or.transform()become ZodEffects (v3) or pipe types (v4) that lack.shape, causingnormalizeObjectSchema()to returnundefinedandtools/listto fall back toEMPTY_OBJECT_JSON_SCHEMA.Changes
unwrapZodEffects()helper that walks_def.schemachains (v3 ZodEffects) with a depth bound of 10 to prevent infinite loopsnormalizeObjectSchema()to detect v3 ZodEffects and v4 pipe types wrapping ZodObjects, returning the original schema sozodToJsonSchema()/toJSONSchema()can extract correct JSON SchemagetObjectShape()to extract shape through ZodEffects/pipe wrappers for both Zod versionsTests
Adds 4 new tests verifying
tools/listreturns correct JSON Schema properties for.superRefine(),.refine(),.transform(), and nested ZodEffects chains. All tests run against both Zod v3 and v4.All 1587 existing tests pass. Lint + Prettier clean.
Impact
Fixes: tools registered with refined/transformed schemas now correctly advertise their input properties to MCP clients. Without this fix, any tool using Zod refinements loses its schema information in the
tools/listresponse, making it impossible for clients to know what parameters the tool accepts.