fix: correct unknown tool/prompt/resource error handling #1872
+71
−30
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.
While developing my Swift SDK for MCP, I encountered the following MCP spec compliance issue in the Python SDK:
When an unknown tool is called, it throws a
ToolError, which is caught by the server's exception handler and converted to a response withisError: true.The spec distinguishes between protocol errors and tool execution errors:
Protocol Errors:
Tool Execution Errors:
Protocol error example for unknown tool:
{ "jsonrpc": "2.0", "id": 3, "error": { "code": -32602, "message": "Unknown tool: invalid_tool_name" } }The same issue applies to prompts and resources. Per the spec:
-32602(INVALID_PARAMS): spec reference-32002(RESOURCE_NOT_FOUND): spec referenceChanges
McpErrorwithINVALID_PARAMS(-32602) for unknown toolsMcpErrorwithINVALID_PARAMS(-32602) for unknown promptsMcpErrorwithRESOURCE_NOT_FOUND(-32002) for unknown resourcesRESOURCE_NOT_FOUNDconstant tomcp.typesHow Has This Been Tested?
I updated the tests to check for the correct error handling.
Breaking Changes
Users who expected the incorrect error handling may need to change their code.
Types of changes
Checklist