Skip to content

[v1.x backport] fix(server): validate wrapped outputSchema#1931

Open
lawrence3699 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
lawrence3699:fix/v1x-outputschema-validation
Open

[v1.x backport] fix(server): validate wrapped outputSchema#1931
lawrence3699 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
lawrence3699:fix/v1x-outputschema-validation

Conversation

@lawrence3699
Copy link
Copy Markdown

Fixes #1308.

On v1.x, validateToolOutput() still assumes every outputSchema can be normalized to an object schema before validation. That holds for raw shapes and plain z.object(...), but wrapped schemas like .optional(), .nullable(), .nullish(), and top-level z.union(...) normalize to undefined, so tool calls fail with Cannot 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.ts
  • npx prettier --check src/server/mcp.ts test/server/mcp.test.ts

Copilot AI review requested due to automatic review settings April 19, 2026 10:48
@lawrence3699 lawrence3699 requested a review from a team as a code owner April 19, 2026 10:48
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 19, 2026

⚠️ No Changeset found

Latest commit: e2ce8d6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 19, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@1931

commit: e2ce8d6

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 attempt normalizeObjectSchema() but fall back to validating against the original outputSchema when normalization fails.
  • Add a regression test covering wrapped outputSchema variants 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants