Skip to content

[v1.x] fix(protocol): answer -32602 for requests whose params fail the schema - #2932

Closed
Gauravtiwari31 wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
Gauravtiwari31:fix/v1-invalid-params-error-code
Closed

Gauravtiwari31 wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
Gauravtiwari31:fix/v1-invalid-params-error-code

Conversation

@Gauravtiwari31

Copy link
Copy Markdown

Part of #2916 (same bug as #2284). This is the v1.x side; the main-branch fix is in #2492, and the triage on #2284 noted v1.x needs it too.

On v1.x, Protocol.setRequestHandler parses the request with parseWithCompat, which throws the raw ZodError. That error has no numeric code, so _onrequest answers -32603 Internal error with the Zod issue dump as the message. It's a bit wider than on main, because this parse runs before the role-specific guards. Even tools/call with a bad name comes back as -32603, and the InvalidParams checks in the Client (elicitation, sampling) and Server (tools/call) wrappers are never reached.

The fix uses safeParse and throws McpError(ErrorCode.InvalidParams, "Invalid params for <method>: ..."). The message is built with getParseErrorMessage, the same helper McpServer uses for its argument errors, so it reads like Invalid option: expected one of ... at params.level rather than a JSON dump.

Over Streamable HTTP against an McpServer, with a well-formed control for each method:

                                  before                     after
prompts/get       {}              -32603  [ { "expected"...  -32602  Invalid params for prompts/get: Invalid input: expected string, received undefined at params.name
logging/setLevel  {level:"loud"}  -32603  [ { "code"...      -32602  Invalid params for logging/setLevel: Invalid option: ...
tools/call        {name:123}      -32603  [ { "expected"...  -32602  Invalid params for tools/call: Invalid input: ...
(controls)                        ok                         ok

Tests

  • Unit test in test/shared/protocol.test.ts for prompts/get with {} and logging/setLevel with { level: 'loud' }. It fails before the fix and passes after.
  • Removed the three e2e knownFailures that pinned the old behaviour (protocol:error:invalid-params, logging:set-level:invalid-level, elicitation:form:schema:restricted-subset). Those 13 cells now pass.
  • The mcpserver logging:set-level:invalid-level scenario now passes { strictValidation: false }, as it does on main. Without it the wire sniffer drops the invalid level before it reaches the server.
  • npm test (1843 passed), typecheck and eslint are clean. npm run test:e2e passes except transport:stdio:shutdown-escalation, which also fails on a clean v1.x on my Windows machine.

…e schema

Protocol.setRequestHandler parsed the request with parseWithCompat, which
throws the raw ZodError. It has no numeric code, so _onrequest answered
-32603 Internal error with the Zod issue dump as the message. Parse with
safeParse instead and throw McpError(InvalidParams) on failure, so callers
get -32602 "Invalid params for <method>: ...".

This also makes the existing InvalidParams guards in the Client and Server
wrappers (elicitation, sampling, tools/call) reachable again, since they
run after this parse.

Drops the three e2e knownFailures that pinned the old behaviour, and lets
the mcpserver logging/setLevel scenario send its invalid level past the
wire sniffer, as it already does on main.

Backport of the fix for #2916 / #2284.
@Gauravtiwari31
Gauravtiwari31 requested a review from a team as a code owner October 2, 2026 16:33
@changeset-bot

changeset-bot Bot commented Oct 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 03a2264

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 2, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 03a2264

@claude claude Bot added the v1 Issues / PRs related to v1.x label Oct 3, 2026
@Gauravtiwari31 Gauravtiwari31 closed this by deleting the head repository Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Issues / PRs related to v1.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant