Skip to content

fix: use getLiteralValue for Zod v3 method literal extraction#1876

Open
JosephDoUrden wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
JosephDoUrden:fix/zod-v3-method-literal
Open

fix: use getLiteralValue for Zod v3 method literal extraction#1876
JosephDoUrden wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
JosephDoUrden:fix/zod-v3-method-literal

Conversation

@JosephDoUrden
Copy link
Copy Markdown

Summary

Fix "Schema method literal must be a string" error when constructing McpServer with late Zod v3 releases (e.g., zod@3.25.1) where the literal value is stored under _def.values[0] instead of _def.value.

Closes #1380

Root cause

Server.setRequestHandler and Client.setRequestHandler had inline Zod literal extraction that only checked _def.value and .value. In zod@3.25.x, the literal is stored under _def.values[0] instead. The existing getLiteralValue() helper in zod-compat.ts already handles this fallback correctly, but it wasn't being used in these two locations.

Changes

  • src/server/index.ts: Replace inline v3/v4 literal extraction with getLiteralValue() call
  • src/client/index.ts: Same replacement

This is a v1.x backport as discussed in #1380 — the issue resolves itself in v2 with the Zod v3 removal.

Test plan

  • All 1579 tests pass across 48 test files (npm test)
  • Build passes (npm run build)

AI Disclosure

AI assistance (Claude) was used for issue research and codebase exploration. The implementation was written and reviewed by the author.

@JosephDoUrden JosephDoUrden requested a review from a team as a code owner April 10, 2026 11:17
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 10, 2026

⚠️ No Changeset found

Latest commit: f456351

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 10, 2026

Open in StackBlitz

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

commit: f456351

…andler

Replace inline Zod v3/v4 literal extraction in Server.setRequestHandler
and Client.setRequestHandler with the existing getLiteralValue() helper
from zod-compat.ts, which already handles the _def.values[0] fallback
for late Zod v3 releases (e.g., zod@3.25.1) where the literal value is
stored under _def.values instead of _def.value.

This fixes "Schema method literal must be a string" errors when
constructing McpServer with zod@3.25.x.

Fixes modelcontextprotocol#1380
@JosephDoUrden JosephDoUrden force-pushed the fix/zod-v3-method-literal branch from d07003c to f456351 Compare April 10, 2026 11:21
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.

1 participant