fix: return INVALID_PARAMS (-32602) for resource-not-found errors#2325
Closed
leoneperdigao wants to merge 2 commits intomodelcontextprotocol:mainfrom
Closed
fix: return INVALID_PARAMS (-32602) for resource-not-found errors#2325leoneperdigao wants to merge 2 commits intomodelcontextprotocol:mainfrom
leoneperdigao wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Previously, when a resource was not found or failed to read, MCPServer raised ResourceError (an MCPServerError subclass) which was caught by the generic Exception handler in the low-level server and returned with error code 0. This made it difficult for clients to programmatically distinguish resource errors from other failures. The fix raises MCPError with INVALID_PARAMS (-32602) directly, which is consistent with the TypeScript SDK and the emerging spec consensus (modelcontextprotocol/modelcontextprotocol#1545). Closes modelcontextprotocol#1579
2 tasks
Author
|
Closing in favor of #1582. As @maxisbey noted there, the spec discussion (modelcontextprotocol/modelcontextprotocol#1545) needs to settle first before merging a fix for the error code. Happy to revisit once that's resolved. |
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
read_resource()to raiseMCPError(code=INVALID_PARAMS)instead ofResourceError, so resource-not-found errors return-32602instead of0Root cause
ResourceErrorextendsMCPServerErrorwhich extendsException, it is not anMCPError. When the low-level server's request handler catches it, it falls into the genericexcept Exceptionbranch which returnsErrorData(code=0, ...). The fix raisesMCPErrordirectly at the protocol boundary inread_resource(), following the same pattern used elsewhere in the codebase (validation errors, session errors,UrlElicitationRequiredError).Test plan
test_1579_resource_error_code.pyverifies error code is-32602for unknown resources and read failurestest_141_resource_templates.pyto catchMCPErrorinstead ofResourceErrorCloses #1579