fix: correct error handling for unknown tools and resources #1389
+140
−27
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 issues in the TypeScript SDK:
Tool Error Handling
When an unknown or disabled tool is called, it throws an
McpError, which is caught and returned as aCallToolResultwithisError: 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" } }I moved the unknown/disabled tool checking to before the try/catch block so that these protocol errors propagate as JSON-RPC errors rather than being caught and returned as a
CallToolResult.Resource Error Handling
The spec also requires that unknown resources return error code
-32002(ResourceNotFound) rather than-32602(InvalidParams). This PR addsErrorCode.ResourceNotFoundto theErrorCodeenum and updates the resource handler to use it.How Has This Been Tested?
-32002)-32602)Breaking Changes
CallToolResultwithisError: truefor unknown/disabled tools will now receive rejected promises withMcpError-32602on unknown resources should update to expect-32002Types of changes
Checklist