@@ -797,7 +797,7 @@ def test_connect_is_idempotent
797797 def test_connect_raises_on_jsonrpc_error_response
798798 stub_request ( :post , url ) . to_return (
799799 status : 200 ,
800- headers : { "Content-Type" => "application/json" } ,
800+ headers : { "Content-Type" => "application/json" , "Mcp-Session-Id" => "session-abc" } ,
801801 body : { error : { code : -32602 , message : "Unsupported protocol version" } } . to_json ,
802802 )
803803
@@ -807,12 +807,15 @@ def test_connect_raises_on_jsonrpc_error_response
807807
808808 assert_includes ( error . message , "Unsupported protocol version" )
809809 refute_predicate ( client , :connected? )
810+ assert_nil ( client . session_id )
811+ assert_nil ( client . protocol_version )
812+ assert_nil ( client . server_info )
810813 end
811814
812815 def test_connect_raises_on_missing_result
813816 stub_request ( :post , url ) . to_return (
814817 status : 200 ,
815- headers : { "Content-Type" => "application/json" } ,
818+ headers : { "Content-Type" => "application/json" , "Mcp-Session-Id" => "session-abc" } ,
816819 body : { jsonrpc : "2.0" , id : "x" } . to_json ,
817820 )
818821
@@ -822,6 +825,45 @@ def test_connect_raises_on_missing_result
822825
823826 assert_includes ( error . message , "missing result in response" )
824827 refute_predicate ( client , :connected? )
828+ assert_nil ( client . session_id )
829+ assert_nil ( client . protocol_version )
830+ assert_nil ( client . server_info )
831+ end
832+
833+ def test_connect_raises_on_unsupported_negotiated_protocol_version
834+ stub_request ( :post , url )
835+ . with { |req | JSON . parse ( req . body ) [ "method" ] == "initialize" }
836+ . to_return (
837+ status : 200 ,
838+ headers : { "Content-Type" => "application/json" , "Mcp-Session-Id" => "session-abc" } ,
839+ body : { result : { protocolVersion : "2099-01-01" } } . to_json ,
840+ )
841+
842+ error = assert_raises ( RequestHandlerError ) do
843+ client . connect
844+ end
845+
846+ assert_includes ( error . message , 'unsupported protocol version "2099-01-01"' )
847+ refute_predicate ( client , :connected? )
848+ assert_nil ( client . session_id )
849+ assert_nil ( client . protocol_version )
850+ assert_nil ( client . server_info )
851+ end
852+
853+ def test_connect_clears_session_when_initialized_notification_fails
854+ stub_initialize
855+ stub_request ( :post , url )
856+ . with { |req | JSON . parse ( req . body ) [ "method" ] == "notifications/initialized" }
857+ . to_return ( status : 500 )
858+
859+ assert_raises ( RequestHandlerError ) do
860+ client . connect
861+ end
862+
863+ refute_predicate ( client , :connected? )
864+ assert_nil ( client . session_id )
865+ assert_nil ( client . protocol_version )
866+ assert_nil ( client . server_info )
825867 end
826868
827869 def test_connected_lifecycle
0 commit comments