fix: Raise resource not found error #1582
Open
+50
−10
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.
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
McpErrorto 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:
ValueErrorwhen a resource is not founderrorattribute were converted toErrorData(code=0, ...)python-sdk/src/mcp/server/lowlevel/server.py
Lines 705 to 708 in 3390e49
Solution:
ResourceErrorexception class with anerrorattribute containingErrorDatawith the appropriate error codeResourceManagerto raiseResourceErrorwithRESOURCE_NOT_FOUNDcode for unknown resourcesHow Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context