Repository navigation
Conversation
…from 2025-11-25 on SEP-1303, part of revision 2025-11-25, moved input validation errors from protocol errors to tool execution errors, so that the model reads what was wrong and can retry. Sessions up to 2025-06-18 keep -32602. Fixes modelcontextprotocol#763
soyuka
left a comment
There was a problem hiding this comment.
A few comments, mostly the cast / missing argument path that should follow the same rule imo.
|
|
||
| // SEP-1303: an input validation error is a tool execution error, | ||
| // which the model reads and can answer with adjusted arguments. | ||
| if ($context->getProtocolVersion()->reportsInvalidToolArgumentsAsToolError()) { |
There was a problem hiding this comment.
SEP-1303 lists "value out of range" as a tool execution error too, but the RegistryException::isInvalidParams() catch below (cast failure, missing argument) still answers -32602. So {"qty": 0} gets isError and {"qty": 9300000000000000000} doesn't. Can we put it under the same predicate in this PR? I'd rather have the BC break once.
There was a problem hiding this comment.
Done, tools only: cast failures and missing arguments are under the same predicate. Prompts and resources keep -32602.
| 'validation_errors' => $validationErrors, | ||
| ]); | ||
|
|
||
| // SEP-1303: an input validation error is a tool execution error, |
There was a problem hiding this comment.
the predicate name already says it, can we drop this comment?
|
|
||
| public function testValidationError(): void | ||
| /** | ||
| * @dataProvider provideRevisionsReportingRefusedArgumentsAsToolErrors |
There was a problem hiding this comment.
can you use #[DataProvider] like the rest of the suite? (same below)
87f0e83 to
6110779
Compare
|
Pushed as a second commit:
|
Review: a tool argument that cannot be cast, or that is missing, is an input validation error too. Prompts and resources keep -32602.
Fixes #763.
Problem
CallToolHandlervalidates the arguments oftools/callagainst the tool's input schema before the tool runs, and answers a mismatch with the JSON-RPC error-32602.That matched the specification up to
2025-06-18, whose "Error Handling" section of Tools lists "Invalid arguments" under protocol errors. Revision2025-11-25moved the case (SEP-1303, changelog item 5: "Clarify that input validation errors should be returned as Tool Execution Errors rather than Protocol Errors to enable model self-correction"). Its Error Handling section now lists "Input validation errors (e.g., date in wrong format, value out of range)" under tool execution errors, reported in the result withisError: true. Clients SHOULD hand those to the model and only MAY hand it protocol errors.2026-07-28keeps that text.So on a session at
2025-11-25or later, a model that sends"a"where the schema wants^[A-Z]$may never be shown why its call failed, while a value the tool itself refuses (ToolCallException) comes back as a result it can read.Change
ProtocolVersion::reportsInvalidToolArgumentsAsToolError()is true from2025-11-25on. When the schema validation fails and the session's revision says so,CallToolHandleranswers with a tool execution error whose text is the message the protocol error carried:{ "content": [ { "type": "text", "text": "Invalid parameters for tool 'shout': Property '/letter': String does not match the required pattern: `^[A-Z]$`." } ], "isError": true }Sessions up to
2025-06-18get-32602withdata.validation_errors, exactly as before. TheRequestContextis now built before the validation, since the revision is read from it.Not changed:
CallToolRequeststay protocol errors on every revision, as the specification has them.-32602on every revision. They shareRegistryException::isInvalidParams()with tools but have their own handlers, and the specification moved the case for tools only.An argument of a tool that cannot be cast to its parameter type, or that is missing at that stage, follows the same predicate (added after review): with the fixture of
IntArgumentTest,{"qty": 0}and{"qty": 9300000000000000000}are both tool errors from2025-11-25on.How the other SDKs behave
All four answer an input-schema mismatch with a tool result, and none of the paths I read branches on the negotiated revision:
packages/server/src/server/mcp.ts:341-358,407-412validateToolInputthrows, thetools/callhandler catches and returnsisError: truesrc/mcp/server/mcpserver/tools/base.py:153-158ValidationErroris raised asToolError, returned asis_error=Truemcp-core/.../util/ToolInputValidator.java:39-51CallToolResultwithisError(true), on by defaultmcp/server.go:423-430(typedAddTool)errRes.SetError(...), returned as a resultThis pull request is more conservative than they are: it keeps
-32602for the revisions whose text asks for it, the way the handler already gatesstructuredContentand resource-not-found by revision.Compatibility
A client on
2025-11-25or later that matched on-32602to detect refused arguments now gets a result withisError: trueinstead, and no longer getsdata.validation_errors: the text holds the same summary, and the full list goes to the debug log, asvalidateStructuredContent()does for output. If you would rather keep the list on the result,_metacould carry it. The CHANGELOG entry is marked[BC Break].Tests
IntArgumentTest: over the wire, on both eras, a tool's out-of-range or non-integer argument is a tool error naming the parameter; prompts and resources still get-32602.CallToolHandlerTest: refused arguments are a tool execution error with the validation message on2025-11-25, on2026-07-28and when no revision was negotiated; they stay-32602withvalidation_errorson2024-11-05,2025-03-26and2025-06-18; the tool is never called. Written first and seen failing.ProtocolVersionTest: the predicate, revision by revision.StdioDualEraTest: the two tests that used-32602as the sign of a refused argument, on both openings, now read the tool error.Run locally:
make cs,make phpstan,make deptrac, the unit, integration and interop suites, and the server conformance suites for2025-11-25(80/80) and2026-07-28(152/152).docs/servers/schemas.md("Argument Validation") shows both answers.