Skip to content

Commit 8348687

Browse files
committed
fix(mcpserver): return resource not found code
1 parent cf110e3 commit 8348687

3 files changed

Lines changed: 3 additions & 9 deletions

File tree

‎src/mcp/server/mcpserver/server.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ async def read_resource(
448448
try:
449449
resource = await self._resource_manager.get_resource(uri, context)
450450
except ValueError as exc:
451-
raise ResourceError(f"Unknown resource: {uri}") from exc
451+
raise MCPError(code=-32002, message=f"Resource not found: {uri}") from exc
452452

453453
try:
454454
content = await resource.read()

‎tests/interaction/_requirements.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -901,12 +901,6 @@ def __post_init__(self) -> None:
901901
"mcpserver:resource:unknown-uri": Requirement(
902902
source=f"{SPEC_BASE_URL}/server/resources#error-handling",
903903
behavior="resources/read for a URI matching no registered resource returns JSON-RPC error -32002.",
904-
divergence=Divergence(
905-
note=(
906-
"The spec reserves -32002 for resource-not-found; MCPServer raises ResourceError, which "
907-
"the low-level server converts to error code 0."
908-
),
909-
),
910904
),
911905
# ═══════════════════════════════════════════════════════════════════════════
912906
# Prompts

‎tests/interaction/mcpserver/test_resources.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def user_profile(user_id: str) -> str:
114114
async def test_read_unknown_uri_is_error(connect: Connect) -> None:
115115
"""Reading a URI that matches no registered resource fails with a JSON-RPC error.
116116
117-
The spec reserves -32002 for resource-not-found; see the divergence note on the requirement.
117+
The spec reserves -32002 for resource-not-found.
118118
"""
119119
mcp = MCPServer("library")
120120

@@ -127,7 +127,7 @@ def app_config() -> str:
127127
with pytest.raises(MCPError) as exc_info:
128128
await client.read_resource("config://missing")
129129

130-
assert exc_info.value.error == snapshot(ErrorData(code=0, message="Unknown resource: config://missing"))
130+
assert exc_info.value.error == snapshot(ErrorData(code=-32002, message="Resource not found: config://missing"))
131131

132132

133133
@requirement("mcpserver:resource:read-throws-surfaced")

0 commit comments

Comments
 (0)