@@ -2739,6 +2739,82 @@ def test_send_request_does_not_check_the_body_answering_a_notification
27392739 assert_empty ( response )
27402740 end
27412741
2742+ def test_connect_succeeds_when_the_initialized_notification_is_acknowledged_with_any_2xx
2743+ # The spec has the server answer a notification with 202 and no body; the TypeScript and Python
2744+ # clients accept any 2xx, whatever the body, and some hosted servers answer an empty 200.
2745+ acknowledgements = [
2746+ { status : 200 , headers : { } , body : "" } ,
2747+ { status : 204 , headers : { } , body : "" } ,
2748+ { status : 200 , headers : { "Content-Type" => "text/plain" } , body : "OK" } ,
2749+ ]
2750+ acknowledgements . each do |acknowledgement |
2751+ WebMock . reset!
2752+
2753+ stub_request ( :post , url ) . with { |req | JSON . parse ( req . body ) [ "method" ] == "initialize" } . to_return do |req |
2754+ {
2755+ status : 200 ,
2756+ headers : { "Content-Type" => "application/json" , "Mcp-Session-Id" => "session-abc" } ,
2757+ body : { jsonrpc : "2.0" , id : JSON . parse ( req . body ) [ "id" ] , result : { protocolVersion : "2025-11-25" } } . to_json ,
2758+ }
2759+ end
2760+
2761+ stub_request ( :post , url ) . with { |req |
2762+ JSON . parse ( req . body ) [ "method" ] == "notifications/initialized"
2763+ } . to_return ( **acknowledgement )
2764+
2765+ client = HTTP . new ( url : url )
2766+ client . connect ( mode : :legacy )
2767+
2768+ assert_predicate ( client , :connected? , acknowledgement . inspect )
2769+ assert_equal ( "session-abc" , client . session_id )
2770+ end
2771+ end
2772+
2773+ def test_send_request_returns_nil_for_a_notification_answered_with_an_sse_stream_carrying_no_response
2774+ # A primed stream that closes without a response would make a request reconnect; a notification has
2775+ # no response to resume for, so the stream is its acknowledgement.
2776+ stub_request ( :post , url ) . to_return (
2777+ status : 200 ,
2778+ headers : { "Content-Type" => "text/event-stream" } ,
2779+ body : "id: event-1\n retry: 100\n data:\n \n " ,
2780+ )
2781+
2782+ response = client . send_request ( request : { jsonrpc : "2.0" , method : "notifications/initialized" } )
2783+
2784+ assert_nil ( response )
2785+ assert_not_requested ( :get , url )
2786+ end
2787+
2788+ def test_send_request_still_raises_for_a_notification_answered_with_a_3xx
2789+ # Only a success status acknowledges a notification; a redirect, which the transport does not follow,
2790+ # keeps its handling, on both the body-less and the SSE path.
2791+ answers = [
2792+ { status : 302 , headers : { "Location" => "https://example.com/elsewhere" } , body : "" , error_type : :unsupported_media_type } ,
2793+ { status : 302 , headers : { "Content-Type" => "text/event-stream" } , body : "data:\n \n " , error_type : :parse_error } ,
2794+ ]
2795+ answers . each do |answer |
2796+ stub_request ( :post , url ) . to_return ( status : answer [ :status ] , headers : answer [ :headers ] , body : answer [ :body ] )
2797+
2798+ error = assert_raises ( RequestHandlerError , answer . inspect ) do
2799+ client . send_request ( request : { jsonrpc : "2.0" , method : "notifications/initialized" } )
2800+ end
2801+
2802+ assert_equal ( answer [ :error_type ] , error . error_type )
2803+ end
2804+ end
2805+
2806+ def test_send_request_raises_error_for_a_request_answered_without_a_json_or_sse_body
2807+ [ { status : 200 , headers : { } , body : "" } , { status : 204 , headers : { } , body : "" } ] . each do |answer |
2808+ stub_request ( :post , url ) . to_return ( **answer )
2809+
2810+ error = assert_raises ( RequestHandlerError , answer . inspect ) do
2811+ client . send_request ( request : { jsonrpc : "2.0" , id : "test_id" , method : "tools/list" } )
2812+ end
2813+
2814+ assert_equal ( :unsupported_media_type , error . error_type )
2815+ end
2816+ end
2817+
27422818 def test_send_request_raises_error_for_an_sse_response_that_is_not_json_rpc_2_0
27432819 # The first response-shaped event settles the exchange, so the valid one after it changes nothing.
27442820 sse_body = <<~SSE
0 commit comments