mcp: return input validation errors as tool results, not JSON-RPC errors (#863)
This implements [SEP-1303](https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1303). Input validation errors (missing required fields, wrong types) during tool calls are now returned as `CallToolResult` with `IsError=true` instead of JSON-RPC `-32602` errors. This allows LLMs to see the validation error and self-correct, per the MCP spec. The existing pattern for handler-returned errors (lines 347-350 in server.go) already converts regular errors to tool results with `IsError=true`. This change applies the same pattern to argument schema validation and unmarshal errors, resolving the `TODO(#450)` in the code. Thanks to @iwyrkore for filing the issue and proposing the approach, and @findleyr for the cross-SDK analysis in the discussion. Fixes #450 Co-authored-by: Maciej Kisiel <mkisiel@google.com>
This commit is contained in:
parent
b71a8391a6
commit
74d2751d88
1 changed files with 73 additions and 23 deletions
22
mcp/testdata/conformance/server/tools.txtar
vendored
22
mcp/testdata/conformance/server/tools.txtar
vendored
|
|
@ -222,9 +222,14 @@ inc
|
|||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 8,
|
||||
"error": {
|
||||
"code": -32602,
|
||||
"message": "invalid params: validating \"arguments\": validating root: required: missing properties: [\"In\"]"
|
||||
"result": {
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "validating \"arguments\": validating root: required: missing properties: [\"In\"]"
|
||||
}
|
||||
],
|
||||
"isError": true
|
||||
}
|
||||
}
|
||||
{
|
||||
|
|
@ -245,9 +250,14 @@ inc
|
|||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 10,
|
||||
"error": {
|
||||
"code": -32602,
|
||||
"message": "invalid params: validating \"arguments\": validating root: required: missing properties: [\"Name\"]"
|
||||
"result": {
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "validating \"arguments\": validating root: required: missing properties: [\"Name\"]"
|
||||
}
|
||||
],
|
||||
"isError": true
|
||||
}
|
||||
}
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue