@@ -3022,6 +3022,108 @@ def describe(table: str) -> str:
30223022 assert not [r for r in caplog .records if r .levelno >= logging .WARNING ]
30233023
30243024
3025+ async def test_resource_template_parameter_validation_failure_is_not_found_logged_at_info (
3026+ caplog : pytest .LogCaptureFixture ,
3027+ ):
3028+ """SDK-defined: a URI parameter that fails the template handler's type annotation names no
3029+ resource, so it is answered with -32602 Unknown resource and logged once at INFO without a
3030+ traceback, matching an unmatched URI or a path-safety rejection."""
3031+ mcp = MCPServer ()
3032+
3033+ @mcp .resource ("users://{user_id}/profile" )
3034+ def profile (user_id : int ) -> str :
3035+ return f"user { user_id } "
3036+
3037+ caplog .set_level (logging .INFO )
3038+ async with Client (mcp ) as client :
3039+ valid = await client .read_resource ("users://42/profile" )
3040+ with pytest .raises (MCPError ) as exc :
3041+ await client .read_resource ("users://abc/profile" )
3042+ with pytest .raises (ResourceNotFoundError ) as direct :
3043+ await mcp .read_resource ("users://abc/profile" )
3044+
3045+ assert valid .contents == [TextResourceContents (uri = "users://42/profile" , mime_type = "text/plain" , text = "user 42" )]
3046+ assert exc .value .error == snapshot (
3047+ ErrorData (
3048+ code = INVALID_PARAMS ,
3049+ message = "Unknown resource: users://abc/profile" ,
3050+ data = {"uri" : "users://abc/profile" },
3051+ )
3052+ )
3053+ assert type (direct .value ) is ResourceNotFoundError
3054+ assert isinstance (direct .value .__cause__ , ValidationError )
3055+ assert _server_records (caplog ) == snapshot (
3056+ [("INFO" , "Resource 'users://abc/profile' failed: 'Unknown resource: users://abc/profile'" , False )]
3057+ )
3058+ assert not [r for r in caplog .records if r .levelno >= logging .WARNING ]
3059+
3060+
3061+ async def test_validation_error_raised_inside_the_resource_template_body_is_a_crash (
3062+ caplog : pytest .LogCaptureFixture ,
3063+ ):
3064+ """SDK-defined: only parameter validation against the template signature is treated as not-found;
3065+ a pydantic ValidationError raised inside the handler body is still logged and answered as a crash."""
3066+ mcp = MCPServer ()
3067+
3068+ class Row (BaseModel ):
3069+ n : int
3070+
3071+ @mcp .resource ("rows://{row_id}" )
3072+ def get_row (row_id : int ) -> str :
3073+ Row .model_validate ({"n" : "x" })
3074+ raise NotImplementedError
3075+
3076+ caplog .set_level (logging .INFO )
3077+ async with Client (mcp ) as client :
3078+ with pytest .raises (MCPError ) as exc :
3079+ await client .read_resource ("rows://1" )
3080+
3081+ assert exc .value .error == snapshot (
3082+ ErrorData (
3083+ code = INTERNAL_ERROR ,
3084+ message = "Error creating resource from template rows://1" ,
3085+ data = {"uri" : "rows://1" },
3086+ )
3087+ )
3088+ assert _server_records (caplog ) == snapshot ([("ERROR" , "Resource 'rows://1' raised an unexpected exception" , True )])
3089+ assert isinstance (_cause_chain (_logged_exception (caplog ))[- 1 ], ValidationError )
3090+
3091+
3092+ async def test_resource_template_parameter_validator_that_crashes_is_a_crash (
3093+ caplog : pytest .LogCaptureFixture ,
3094+ ):
3095+ """SDK-defined: a parameter validator raising anything other than ValueError/AssertionError
3096+ (or ResourceError/MCPError) is a bug in the template's schema and is wrapped and logged as a crash."""
3097+ mcp = MCPServer ()
3098+ raised = TypeError ("ids are compared as integers" )
3099+
3100+ def check (user_id : str ) -> str :
3101+ raise raised
3102+
3103+ @mcp .resource ("users://{user_id}" )
3104+ def get_user (user_id : Annotated [str , AfterValidator (check )]) -> str :
3105+ raise NotImplementedError
3106+
3107+ caplog .set_level (logging .INFO )
3108+ async with Client (mcp ) as client :
3109+ with pytest .raises (MCPError ) as exc :
3110+ await client .read_resource ("users://42" )
3111+ with pytest .raises (UnexpectedResourceError ) as direct :
3112+ await mcp .read_resource ("users://42" )
3113+
3114+ assert exc .value .error == snapshot (
3115+ ErrorData (
3116+ code = INTERNAL_ERROR ,
3117+ message = "Error creating resource from template users://42" ,
3118+ data = {"uri" : "users://42" },
3119+ )
3120+ )
3121+ assert direct .value .__cause__ is raised
3122+ assert _server_records (caplog ) == snapshot (
3123+ [("ERROR" , "Resource 'users://42' raised an unexpected exception" , True )]
3124+ )
3125+
3126+
30253127async def test_prompt_raising_unexpected_exception_is_logged_once (caplog : pytest .LogCaptureFixture ):
30263128 """SDK-defined: a prompt crash is logged exactly once, by the dispatcher boundary that turns it
30273129 into the JSON-RPC error, and not a second time by MCPServer."""
0 commit comments