@@ -7,6 +7,7 @@ class Server
77 module Transports
88 class StdioTransportTest < ActiveSupport ::TestCase
99 include InstrumentationTestHelper
10+ include InitializeParamsTestHelper
1011
1112 setup do
1213 configuration = MCP ::Configuration . new
@@ -550,6 +551,140 @@ class StdioTransportTest < ActiveSupport::TestCase
550551 $stdout = original_stdout
551552 end
552553 end
554+
555+ test "locks the legacy era on a successful initialize and rejects a later modern envelope" do
556+ responses = run_transport_session ( [
557+ initialize_request ( id : 1 ) ,
558+ modern_ping_request ( id : 2 ) ,
559+ ] )
560+
561+ refute responses [ 0 ] . key? ( :error )
562+ assert_equal :legacy , session_era
563+ assert_equal JsonRpcHandler ::ErrorCode ::INVALID_REQUEST , responses [ 1 ] . dig ( :error , :code )
564+ end
565+
566+ test "initialize negotiating 2026-07-28 still locks the legacy era" do
567+ # 2026-07-28 serves both lifecycles of the dual-era model: negotiating it through
568+ # the legacy handshake locks `:legacy`, so a later modern envelope is still rejected.
569+ responses = run_transport_session ( [
570+ initialize_request ( id : 1 , protocol_version : "2026-07-28" ) ,
571+ modern_ping_request ( id : 2 ) ,
572+ ] )
573+
574+ assert_equal "2026-07-28" , responses [ 0 ] . dig ( :result , :protocolVersion )
575+ assert_equal :legacy , session_era
576+ assert_equal JsonRpcHandler ::ErrorCode ::INVALID_REQUEST , responses [ 1 ] . dig ( :error , :code )
577+ end
578+
579+ test "locks the modern era on a successful server/discover and rejects a later initialize with -32022" do
580+ responses = run_transport_session ( [
581+ { jsonrpc : "2.0" , method : "server/discover" , id : 1 } ,
582+ initialize_request ( id : 2 ) ,
583+ modern_ping_request ( id : 3 ) ,
584+ ] )
585+
586+ assert_equal Configuration ::SUPPORTED_STABLE_PROTOCOL_VERSIONS , responses [ 0 ] . dig ( :result , :supportedVersions )
587+ assert_equal :modern , session_era
588+ assert_equal ErrorCodes ::UNSUPPORTED_PROTOCOL_VERSION , responses [ 1 ] . dig ( :error , :code )
589+ assert_equal Configuration ::SUPPORTED_MODERN_PROTOCOL_VERSIONS , responses [ 1 ] . dig ( :error , :data , :supported )
590+ refute responses [ 2 ] . key? ( :error )
591+ end
592+
593+ test "locks the modern era on a successful request carrying the modern envelope" do
594+ responses = run_transport_session ( [ modern_ping_request ( id : 1 ) ] )
595+
596+ refute responses [ 0 ] . key? ( :error )
597+ assert_equal :modern , session_era
598+ end
599+
600+ test "does not lock an era when the era-distinctive request fails" do
601+ # An unsupported envelope version fails with -32022, so the connection stays unlocked
602+ # and a legacy initialize can still succeed afterwards.
603+ responses = run_transport_session ( [
604+ modern_ping_request ( id : 1 , version : "2027-01-01" ) ,
605+ initialize_request ( id : 2 ) ,
606+ ] )
607+
608+ assert_equal ErrorCodes ::UNSUPPORTED_PROTOCOL_VERSION , responses [ 0 ] . dig ( :error , :code )
609+ refute responses [ 1 ] . key? ( :error )
610+ assert_equal :legacy , session_era
611+ end
612+
613+ test "requires the modern envelope after a modern era lock" do
614+ responses = run_transport_session ( [
615+ modern_ping_request ( id : 1 ) ,
616+ { jsonrpc : "2.0" , method : "ping" , id : 2 } ,
617+ ] )
618+
619+ refute responses [ 0 ] . key? ( :error )
620+ assert_equal JsonRpcHandler ::ErrorCode ::INVALID_REQUEST , responses [ 1 ] . dig ( :error , :code )
621+ end
622+
623+ test "#send_request raises on a modern-locked session" do
624+ run_transport_session ( [ modern_ping_request ( id : 1 ) ] )
625+
626+ error = assert_raises ( RuntimeError ) do
627+ @transport . send_request ( "roots/list" )
628+ end
629+ assert_match ( /modern lifecycle/ , error . message )
630+ end
631+
632+ private
633+
634+ def initialize_request ( id :, protocol_version : "2025-11-25" )
635+ {
636+ jsonrpc : "2.0" ,
637+ method : "initialize" ,
638+ id : id ,
639+ params : initialize_params (
640+ protocolVersion : protocol_version ,
641+ clientInfo : { name : "legacy_client" , version : "1.0" } ,
642+ ) ,
643+ }
644+ end
645+
646+ def modern_ping_request ( id :, version : "2026-07-28" )
647+ {
648+ jsonrpc : "2.0" ,
649+ method : "ping" ,
650+ id : id ,
651+ params : {
652+ _meta : {
653+ "io.modelcontextprotocol/protocolVersion" : version ,
654+ "io.modelcontextprotocol/clientInfo" : { name : "modern_client" , version : "2.0" } ,
655+ "io.modelcontextprotocol/clientCapabilities" : { } ,
656+ } ,
657+ } ,
658+ }
659+ end
660+
661+ # Feeds the frames to a fresh transport session over swapped stdio and returns the parsed responses in order.
662+ def run_transport_session ( frames )
663+ input = StringIO . new ( frames . map { |frame | JSON . generate ( frame ) } . join ( "\n " ) + "\n " )
664+ output = StringIO . new
665+
666+ original_stdin = $stdin
667+ original_stdout = $stdout
668+
669+ begin
670+ $stdin = input
671+ $stdout = output
672+
673+ thread = Thread . new { @transport . open }
674+ sleep ( 0.1 )
675+ @transport . close
676+ thread . join
677+ ensure
678+ $stdin = original_stdin
679+ $stdout = original_stdout
680+ end
681+
682+ output . string . each_line . map { |line | JSON . parse ( line , symbolize_names : true ) }
683+ end
684+
685+ def session_era
686+ @transport . instance_variable_get ( :@session ) . era
687+ end
553688 end
554689 end
555690 end
0 commit comments