@@ -70,22 +70,23 @@ async def call(method: str) -> None:
7070
7171
7272@pytest .mark .anyio
73- async def test_handler_raising_exception_sends_internal_error_with_str_message ():
74- """Per design: INTERNAL_ERROR carries str(e), not a scrubbed message ."""
73+ async def test_handler_raising_exception_sends_code_zero_with_str_message ():
74+ """Matches the existing server's `_handle_request`: code=0, message= str(e)."""
7575
7676 async def server_on_request (ctx : DCtx , method : str , params : Mapping [str , Any ] | None ) -> dict [str , Any ]:
7777 raise RuntimeError ("kaboom" )
7878
7979 async with running_pair (jsonrpc_pair , server_on_request = server_on_request ) as (client , * _ ):
8080 with anyio .fail_after (5 ), pytest .raises (MCPError ) as exc :
8181 await client .send_raw_request ("tools/list" , None )
82- assert exc .value .error .code == INTERNAL_ERROR
82+ assert exc .value .error .code == 0
8383 assert exc .value .error .message == "kaboom"
8484 assert exc .value .__cause__ is None # cause does not survive the wire
8585
8686
8787@pytest .mark .anyio
88- async def test_peer_cancel_interrupt_mode_sets_cancel_requested_and_sends_no_response ():
88+ async def test_peer_cancel_interrupt_mode_writes_cancelled_error_response ():
89+ """Matches the existing server: a peer-cancelled request is answered with code=0."""
8990 handler_started = anyio .Event ()
9091 handler_exited = anyio .Event ()
9192 seen_ctx : list [DCtx ] = []
@@ -99,23 +100,22 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
99100 handler_exited .set ()
100101 raise NotImplementedError
101102
103+ seen_error : list [ErrorData ] = []
102104 async with running_pair (jsonrpc_pair , server_on_request = server_on_request ) as (client , * _ ):
103105 with anyio .fail_after (5 ):
104106 async with anyio .create_task_group () as tg : # pragma: no branch
105107
106108 async def call_then_record () -> None :
107- with pytest .raises (MCPError ): # we'll cancel via tg below
109+ with pytest .raises (MCPError ) as exc :
108110 await client .send_raw_request ("slow" , None )
111+ seen_error .append (exc .value .error )
109112
110113 tg .start_soon (call_then_record )
111114 await handler_started .wait ()
112- # cancel just the handler (peer-cancel), not our caller
113115 await client .notify ("notifications/cancelled" , {"requestId" : 1 })
114116 await handler_exited .wait ()
115- # Handler torn down, no response was written; caller is still parked.
116- # Cancel the caller's task to end the test.
117- tg .cancel_scope .cancel ()
118117 assert seen_ctx [0 ].cancel_requested .is_set ()
118+ assert seen_error == [ErrorData (code = 0 , message = "Request cancelled" )]
119119
120120
121121@pytest .mark .anyio
@@ -266,7 +266,7 @@ async def on_notify(ctx: DCtx, method: str, params: Mapping[str, Any] | None) ->
266266 sent = s2c_recv .receive_nowait ()
267267 assert isinstance (sent , SessionMessage )
268268 assert isinstance (sent .message , JSONRPCError )
269- assert sent .message .error .code == INTERNAL_ERROR
269+ assert sent .message .error .code == 0
270270 finally :
271271 for s in (c2s_send , c2s_recv , s2c_send , s2c_recv ):
272272 s .close ()
@@ -408,7 +408,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
408408 async with running_pair (jsonrpc_pair , server_on_request = server_on_request ) as (client , * _ ):
409409 with anyio .fail_after (5 ), pytest .raises (MCPError ) as exc :
410410 await client .send_raw_request ("t" , None )
411- assert exc .value .error . code == INVALID_PARAMS
411+ assert exc .value .error == ErrorData ( code = INVALID_PARAMS , message = "Invalid request parameters" , data = "" )
412412
413413
414414@pytest .mark .anyio
0 commit comments