fix: throw ProtocolError as JSON-RPC error for tool not found#1567
Open
stakeswky wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: throw ProtocolError as JSON-RPC error for tool not found#1567stakeswky wants to merge 1 commit intomodelcontextprotocol:mainfrom
stakeswky wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Per MCP spec, calling a nonexistent tool should return a JSON-RPC Error (code -32602), not a JSON-RPC Result with isError: true. Previously, only UrlElicitationRequired errors were re-thrown; all other ProtocolErrors (including tool/disabled checks) were swallowed and wrapped in a CallToolResult. Now all ProtocolErrors propagate as JSON-RPC errors, which is the correct behavior per the specification. Fixes modelcontextprotocol#1510
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@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
Fix tool not found errors to return JSON-RPC Error instead of JSON-RPC Result with
isError: true, per MCP specification.Problem
When a client calls a nonexistent tool, the current implementation returns:
{ "jsonrpc": "2.0", "id": "3", "result": { "content": [ { "type": "text", "text": "MCP error -32602: Tool nonexistent_tool not found" } ], "isError": true } }But the MCP specification requires:
{ "jsonrpc": "2.0", "id": 3, "error": { "code": -32602, "message": "Unknown tool: nonexistent_tool" } }Root Cause
The
tools/callhandler's catch block only re-threwUrlElicitationRequirederrors. All otherProtocolErrorinstances (including tool not found, tool disabled, invalid params, etc.) were caught and wrapped in aCallToolResultwithisError: true.Fix
Changed the catch block to re-throw all
ProtocolErrorinstances, not justUrlElicitationRequired. This ensures protocol-level errors are returned as JSON-RPC errors per the spec.Tests
Updated the existing test for nonexistent tools to expect a thrown error instead of a
CallToolResultwithisError: true.Fixes #1510