66from mcp import MCPError
77from mcp .server .mcpserver import MCPServer
88from mcp .types import (
9+ RESOURCE_NOT_FOUND ,
910 ErrorData ,
1011 ListResourcesResult ,
1112 ListResourceTemplatesResult ,
@@ -114,7 +115,7 @@ def user_profile(user_id: str) -> str:
114115async def test_read_unknown_uri_is_error (connect : Connect ) -> None :
115116 """Reading a URI that matches no registered resource fails with a JSON-RPC error.
116117
117- The spec reserves -32002 for resource-not-found; see the divergence note on the requirement .
118+ The spec reserves -32002 for resource-not-found.
118119 """
119120 mcp = MCPServer ("library" )
120121
@@ -127,7 +128,9 @@ def app_config() -> str:
127128 with pytest .raises (MCPError ) as exc_info :
128129 await client .read_resource ("config://missing" )
129130
130- assert exc_info .value .error == snapshot (ErrorData (code = 0 , message = "Unknown resource: config://missing" ))
131+ assert exc_info .value .error == snapshot (
132+ ErrorData (code = RESOURCE_NOT_FOUND , message = "Unknown resource: config://missing" )
133+ )
131134
132135
133136@requirement ("mcpserver:resource:read-throws-surfaced" )
@@ -151,6 +154,24 @@ def boom() -> str:
151154 assert exc_info .value .error == snapshot (ErrorData (code = 0 , message = "Error reading resource res://boom" ))
152155
153156
157+ @requirement ("mcpserver:resource:read-throws-surfaced" )
158+ async def test_templated_resource_function_that_raises_is_not_reported_as_missing (connect : Connect ) -> None :
159+ """A matching resource template that raises is a read failure, not a missing resource."""
160+ mcp = MCPServer ("library" )
161+
162+ @mcp .resource ("users://{user_id}/profile" )
163+ def user_profile (user_id : str ) -> str :
164+ raise RuntimeError (f"profile unavailable for { user_id } " )
165+
166+ async with connect (mcp ) as client :
167+ with pytest .raises (MCPError ) as exc_info :
168+ await client .read_resource ("users://42/profile" )
169+
170+ assert exc_info .value .error == snapshot (
171+ ErrorData (code = 0 , message = "Error reading resource users://42/profile" )
172+ )
173+
174+
154175@requirement ("mcpserver:resource:duplicate-name" )
155176async def test_registering_a_duplicate_resource_uri_warns_and_keeps_the_first (connect : Connect ) -> None :
156177 """Registering a second static resource at an already-used URI keeps the first registration.
0 commit comments