fix(server): handle ZodObject in RegisteredTool.update (#1960)#1971
Open
MukundaKatta wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Open
fix(server): handle ZodObject in RegisteredTool.update (#1960)#1971MukundaKatta wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
MukundaKatta wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Conversation
|
commit: |
…rotocol#1960) The create path uses `getZodSchemaObject()` which accepts both raw shapes and ZodObject instances. The update path was calling `objectFromShape()` directly, which iterates `Object.values(shape)` and blows up on internal Zod fields when given a ZodObject (e.g. `z.object({...}).passthrough()`): TypeError: Cannot read properties of null (reading '_zod') Mirror the create path so updating with either form works. Also widen the `paramsSchema` / `outputSchema` generics on `RegisteredTool.update` to match `registerTool` (`ZodRawShapeCompat | AnySchema`), fixing the type-level inconsistency called out in modelcontextprotocol#1960. Fixes modelcontextprotocol#1960
0435482 to
d7d87a3
Compare
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.
Fixes #1960.
RegisteredTool.update({ paramsSchema })crashes withTypeError: Cannot read properties of null (reading '_zod')whenparamsSchemais a ZodObject (e.g.z.object({...}).passthrough()) instead of a raw shape.Why
The create path in
_createRegisteredToolusesgetZodSchemaObject(), which handles both raw shapes and ZodObject instances:The update path was calling
objectFromShape()directly.objectFromShapedoesObject.values(shape), which on a ZodObject iterates internal fields like_cached(which isnull), and downstreamisZ4Schema(null)accessesnull._zodand throws.Fix
RegisteredTool.update, routeparamsSchemaandoutputSchemathroughgetZodSchemaObject()(the same helper the create path uses) instead ofobjectFromShape()directly.update<InputArgs, OutputArgs>generic constraints fromZodRawShapeCompattoZodRawShapeCompat | AnySchema, matchingregisterTool. This resolves the type-level inconsistency mentioned in the issue.Net diff is ~3 lines of code; no new public API.
Test
Added
should update tool with ZodObject paramsSchemaintest/server/mcp.test.ts. It registers a tool withz.object({...}).passthrough(), updates it with a fresh ZodObject, and assertstool.update(...)does not throw and the new schema is reflected intools/listand the call result. The test runs under the existingzodTestMatrix, so it covers both Zod v3 and v4.Reproduction (before this PR)
After the patch this is a no-op update with no crash.