Skip to content

fix: return INVALID_PARAMS (-32602) for resource-not-found errors#2325

Closed
leoneperdigao wants to merge 2 commits intomodelcontextprotocol:mainfrom
leoneperdigao:fix/resource-error-code-1579
Closed

fix: return INVALID_PARAMS (-32602) for resource-not-found errors#2325
leoneperdigao wants to merge 2 commits intomodelcontextprotocol:mainfrom
leoneperdigao:fix/resource-error-code-1579

Conversation

@leoneperdigao
Copy link

@leoneperdigao leoneperdigao commented Mar 21, 2026

Summary

  • Fix read_resource() to raise MCPError(code=INVALID_PARAMS) instead of ResourceError, so resource-not-found errors return -32602 instead of 0
  • Aligns with the TypeScript SDK behavior and emerging spec consensus (modelcontextprotocol#1545)
  • Adds regression test verifying the error code for both unknown resources and resource read failures

Root cause

ResourceError extends MCPServerError which extends Exception, it is not an MCPError. When the low-level server's request handler catches it, it falls into the generic except Exception branch which returns ErrorData(code=0, ...). The fix raises MCPError directly at the protocol boundary in read_resource(), following the same pattern used elsewhere in the codebase (validation errors, session errors, UrlElicitationRequiredError).

Test plan

  • New test test_1579_resource_error_code.py verifies error code is -32602 for unknown resources and read failures
  • Updated test_141_resource_templates.py to catch MCPError instead of ResourceError
  • All 364 existing tests pass with no regressions

Closes #1579

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
@leoneperdigao
Copy link
Author

leoneperdigao commented Mar 21, 2026

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.

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.

FastMCP read_resource() returns incorrect error code when resource not found

1 participant