Skip to content

fix: getParseErrorMessage now surfaces custom Zod v4 error messages#1663

Open
Vadaski wants to merge 2 commits intomodelcontextprotocol:mainfrom
Vadaski:fix/1415-getparseerrormessage-zod-v4-custom-errors
Open

fix: getParseErrorMessage now surfaces custom Zod v4 error messages#1663
Vadaski wants to merge 2 commits intomodelcontextprotocol:mainfrom
Vadaski:fix/1415-getparseerrormessage-zod-v4-custom-errors

Conversation

@Vadaski
Copy link

@Vadaski Vadaski commented Mar 12, 2026

Problem

In Zod v4, error.message is a JSON serialization of the issues array rather than a user-facing string. The previous implementation of getParseErrorMessage prioritized error.message, which caused custom error messages defined via .min(1, 'My custom error') or custom error maps to be completely swallowed. Instead of seeing "My custom error", callers received a raw JSON blob like:

[{"origin":"string","code":"too_small","minimum":1,"inclusive":true,"path":[],"message":"My custom error"}]

This affected tool input validation errors, output schema validation, prompt argument errors, and listChanged options validation — anywhere the SDK surfaces a Zod parse failure to the developer.

Closes #1415

Fix

Extract messages from error.issues first; fall back to error.message only when no issue messages are available.

Also updated call sites in client.ts and mcp.ts that were hand-rolling issues.map(i => i.message).join(', ') to use getParseErrorMessage for consistency.

Changes

  • packages/core/src/util/schema.ts — fix getParseErrorMessage to prefer issue.message over the JSON-serialized error.message
  • packages/core/test/util/schema.test.ts — new test file with regression tests for custom Zod v4 messages and fallback behaviour
  • packages/client/src/client/client.ts — use getParseErrorMessage instead of raw error.message
  • packages/server/src/server/mcp.ts — use getParseErrorMessage consistently (3 call sites)

Testing

All existing tests pass (pnpm build:all && pnpm test:all).

In Zod v4, `error.message` is a JSON serialization of the issues array
rather than a user-facing string. The previous implementation prioritized
`error.message`, which caused custom error messages defined via
`.min(1, 'My custom error')` or custom error maps to be swallowed —
callers saw a raw JSON blob instead of the human-readable message.

Fix by extracting messages from `error.issues` first, falling back to
`error.message` only when no issue messages are available.

Also update call sites in `client.ts` and `mcp.ts` that were
hand-rolling `issues.map(i => i.message).join(', ')` to use
`getParseErrorMessage` consistently.

Closes modelcontextprotocol#1415
@Vadaski Vadaski requested a review from a team as a code owner March 12, 2026 03:35
@changeset-bot
Copy link

changeset-bot bot commented Mar 12, 2026

⚠️ No Changeset found

Latest commit: 1534a33

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.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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

pkg-pr-new bot commented Mar 12, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1663

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1663

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1663

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1663

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1663

commit: 92df769

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.

getParseErrorMessage prioritizes error.message which bypasses custom Zod errors

1 participant