fix(server): return -32602 for resource not found (SEP-2164)#2344
Draft
fix(server): return -32602 for resource not found (SEP-2164)#2344
Conversation
The SDK previously returned error code 0 for resource-not-found, making it impossible for clients to distinguish this from other errors. Per SEP-2164, servers should return -32602 (Invalid Params) with the uri in the error data field. Adds ResourceNotFoundError subclass of ResourceError so existing code catching ResourceError continues to work. The internal handler converts it to MCPError with INVALID_PARAMS before it reaches the JSON-RPC layer. Spec: modelcontextprotocol/modelcontextprotocol#2164
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
Implements SEP-2164 — standardizes the resource-not-found error code to
-32602(Invalid Params).Problem
The Python SDK currently returns error code
0for resource-not-found, making it impossible for clients to reliably distinguish this from other errors. The spec now requires-32602with the URI in thedatafield.Changes
ResourceNotFoundError(ResourceError)subclass inmcpserver/exceptions.pyMCPServer.read_resource()now raisesResourceNotFoundError(instead of genericResourceError) when the resource doesn't exist_handle_read_resource()catchesResourceNotFoundErrorand converts it toMCPError(code=INVALID_PARAMS, ..., data={"uri": ...})The subclass approach keeps backward compatibility — existing code catching
ResourceErrorcontinues to work unchanged.Wire format (before → after)
{ "jsonrpc": "2.0", "id": 2, "error": { - "code": 0, + "code": -32602, "message": "Unknown resource: file:///nonexistent.txt", - "data": null + "data": {"uri": "file:///nonexistent.txt"} } }Spec: modelcontextprotocol/modelcontextprotocol#2164