fix: correct error handling for unknown tools and resources#1389
Open
DePasqualeOrg wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix: correct error handling for unknown tools and resources#1389DePasqualeOrg wants to merge 2 commits intomodelcontextprotocol:mainfrom
DePasqualeOrg wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 28b6268 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
6571c9b to
59500f6
Compare
The error message differs depending on the environment.
59500f6 to
e68454d
Compare
e68454d to
28b6268
Compare
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.
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