Skip to content

Conversation

@vincent0426
Copy link

@vincent0426 vincent0426 commented Nov 6, 2025

Introduce resource not found error handling following the MCP specification.

This change ensures that when a resource is not found, the server returns error code -32002 (RESOURCE_NOT_FOUND) as specified in the Resources Error Handling.

Concern:
There is currently no explicit error handling for FastMCP exceptions in the low-level server. I'm not sure if we want to wrap fastmcp exceptions under McpError to raise the correct error code which avoid changing the low-level server code, otherwise, sdk might need to create a new exception type to handle non-connection error?

Motivation and Context

Closes #1579

Previously, when a resource was not found, the server returned error code 0 (generic error) instead of the spec-compliant -32002 (RESOURCE_NOT_FOUND).

Root Cause:

  • FastMCP raises ValueError when a resource is not found
  • low-level server's exception handler was not properly recognizing these exceptions
  • Exceptions without an error attribute were converted to ErrorData(code=0, ...)

except Exception as err:
if raise_exceptions:
raise err
response = types.ErrorData(code=0, message=str(err), data=None)

Solution:

  • Added ResourceError exception class with an error attribute containing ErrorData with the appropriate error code
  • Updated ResourceManager to raise ResourceError with RESOURCE_NOT_FOUND code for unknown resources

How Has This Been Tested?

  • unit test
  • Reproduced and verified the fix for the original issue
data: {"jsonrpc":"2.0","id":7,"error":{"code":-32002,"message":"Unknown resource: https://example.com/does-not-exist"}} 

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@maxisbey maxisbey added bug Something isn't working P3 Nice to haves, rare edge cases labels Nov 6, 2025
@maxisbey
Copy link
Contributor

maxisbey commented Nov 6, 2025

Related: modelcontextprotocol/modelcontextprotocol#1545

Due to ongoing discussions around exactly what errors to use here we should wait on that before merging any fixes for this.

@maxisbey maxisbey added needs more eyes Needs alignment among maintainers whether this is something we want to add breaking change Will break existing deployments when updated without changes labels Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Will break existing deployments when updated without changes bug Something isn't working needs more eyes Needs alignment among maintainers whether this is something we want to add P3 Nice to haves, rare edge cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants