Skip to content

fix(server): return -32602 for resource not found (SEP-2164)#2344

Draft
pja-ant wants to merge 1 commit intomainfrom
feat/sep-2164-resource-not-found-error-code
Draft

fix(server): return -32602 for resource not found (SEP-2164)#2344
pja-ant wants to merge 1 commit intomainfrom
feat/sep-2164-resource-not-found-error-code

Conversation

@pja-ant
Copy link
Copy Markdown
Contributor

@pja-ant pja-ant commented Mar 25, 2026

Summary

Implements SEP-2164 — standardizes the resource-not-found error code to -32602 (Invalid Params).

Problem

The Python SDK currently returns error code 0 for resource-not-found, making it impossible for clients to reliably distinguish this from other errors. The spec now requires -32602 with the URI in the data field.

Changes

  • Add ResourceNotFoundError(ResourceError) subclass in mcpserver/exceptions.py
  • MCPServer.read_resource() now raises ResourceNotFoundError (instead of generic ResourceError) when the resource doesn't exist
  • _handle_read_resource() catches ResourceNotFoundError and converts it to MCPError(code=INVALID_PARAMS, ..., data={"uri": ...})

The subclass approach keeps backward compatibility — existing code catching ResourceError continues 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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant