fix: accept null arguments in tools/call and return -32602 for validation errors#2013
Open
blackwell-systems wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
Conversation
…tion errors Two issues: 1. CallToolRequestParamsSchema uses .optional() on the arguments field, which accepts undefined but rejects null. Clients that serialize missing fields as null (common in Go, Java, C# JSON libraries) get an internal error. Fixed with z.preprocess() to coerce null to undefined before validation, preserving the existing type signature. 2. The protocol request handler uses schema.parse() which throws on validation failure. The thrown ZodError lacks a numeric "code" property, so the error handler defaults to -32603 (InternalError). Changed to schema.safeParse() with an explicit ProtocolError using -32602 (InvalidParams) on failure. Before: null arguments → -32603 with raw Zod error message. After: null arguments → accepted (coerced to undefined). Any remaining validation errors → -32602 with structured message.
🦋 Changeset detectedLatest commit: b33d687 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CallToolRequestParamsSchemarejectsnullarguments with-32603(InternalError) and a raw Zod error message. Fixed by addingz.preprocess()to coercenulltoundefinedbefore validation, preserving the existing type signature.schema.parse()which throws on validation failure. The thrown ZodError lacks a numericcode, so the error handler defaults to-32603. Changed toschema.safeParse()with an explicitProtocolError(InvalidParams)on failure, returning-32602as the JSON-RPC spec requires.Motivation
Clients that serialize missing fields as
null(common in Go, Java, and C# JSON libraries) get an opaque internal error when calling any tool without arguments. Tested against@modelcontextprotocol/server-everything: all 13 tools fail this way.Changes
schemas.ts:1420z.preprocess(val => val ?? undefined, ...)onargumentsfieldprotocol.ts:390-397schema.parse()→schema.safeParse()+ProtocolError(InvalidParams)types.test.tsTest plan
pnpm run test:allpasses (1,536 tests, 0 failures)pnpm -r typecheckpasses (no type changes, preprocess preserves existing signature)pnpm -r lintpasses (prettier formatting applied)@modelcontextprotocol/server-everythingover raw stdio: null arguments now acceptedFixes #2012