[v1.x backport] fix(server): validate wrapped outputSchema#1931
[v1.x backport] fix(server): validate wrapped outputSchema#1931lawrence3699 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Conversation
|
commit: |
There was a problem hiding this comment.
Pull request overview
Backports a server-side fix to ensure validateToolOutput() can validate tool structuredContent when outputSchema is a wrapped/compound Zod schema (e.g., optional(), nullable(), nullish(), or top-level union()), preventing crashes when object-schema normalization returns undefined.
Changes:
- Update
validateToolOutput()to attemptnormalizeObjectSchema()but fall back to validating against the originaloutputSchemawhen normalization fails. - Add a regression test covering wrapped
outputSchemavariants across the Zod test matrix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/server/mcp.ts |
Fixes output validation to handle non-normalizable (wrapped/compound) Zod schemas by falling back to the original schema. |
test/server/mcp.test.ts |
Adds a regression test ensuring tool calls succeed with wrapped outputSchema variants. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Fixes #1308.
On
v1.x,validateToolOutput()still assumes everyoutputSchemacan be normalized to an object schema before validation. That holds for raw shapes and plainz.object(...), but wrapped schemas like.optional(),.nullable(),.nullish(), and top-levelz.union(...)normalize toundefined, so tool calls fail withCannot read properties of undefined (reading '_zod').This keeps the existing normalization path for raw shapes / object schemas and falls back to the original schema when normalization does not return an object, matching how tool input validation already works on this branch. The regression test covers the wrapped schema variants across both Zod matrices.
Validation:
npx vitest run test/server/mcp.test.ts -t 'should support wrapped outputSchema variants'npx vitest run test/server/mcp.test.ts -t 'outputSchema'npx eslint src/server/mcp.tsnpx prettier --check src/server/mcp.ts test/server/mcp.test.ts