@@ -2792,6 +2792,68 @@ def string
27922792 assert_equal "456" , body [ "id" ]
27932793 end
27942794
2795+ test "a reply that cannot be generated is answered with Internal error on the legacy SSE path" do
2796+ reported = [ ]
2797+ configuration = MCP ::Configuration . new
2798+ configuration . exception_reporter = -> ( e , context ) { reported << [ e . class , context ] }
2799+ @server = Server . new ( name : "test_server" , configuration : configuration )
2800+ @server . define_tool ( name : "invalid_text" ) do
2801+ Tool ::Response . new ( [ { type : "text" , text : "\xFF " . dup . force_encoding ( Encoding ::UTF_8 ) } ] )
2802+ end
2803+ @transport . close
2804+ @transport = StreamableHTTPTransport . new ( @server , listen_keepalive_interval : nil )
2805+ session_id = initialize_test_session
2806+
2807+ response = @transport . handle_request ( create_rack_request (
2808+ "POST" ,
2809+ "/" ,
2810+ { "CONTENT_TYPE" => "application/json" , "HTTP_MCP_SESSION_ID" => session_id } ,
2811+ { jsonrpc : "2.0" , id : 2 , method : "tools/call" , params : { name : "invalid_text" , arguments : { } } } . to_json ,
2812+ ) )
2813+ io = StringIO . new
2814+ response [ 2 ] . call ( io )
2815+
2816+ assert_equal 200 , response [ 0 ]
2817+ assert_equal (
2818+ { "jsonrpc" => "2.0" , "id" => 2 , "error" => { "code" => -32603 , "message" => "Internal error" } } ,
2819+ JSON . parse ( io . string [ /^data: (.+)$/ , 1 ] ) ,
2820+ )
2821+ assert_equal ( [ [ JSON ::GeneratorError , { error : "Failed to generate response" } ] ] , reported )
2822+ end
2823+
2824+ test "a reply that cannot be generated is answered with Internal error in JSON response mode" do
2825+ reported = [ ]
2826+ configuration = MCP ::Configuration . new
2827+ configuration . exception_reporter = -> ( e , context ) { reported << [ e . class , context ] }
2828+ server = Server . new ( name : "test" , configuration : configuration )
2829+ server . define_tool ( name : "invalid_text" ) do
2830+ Tool ::Response . new ( [ { type : "text" , text : "\xFF " . dup . force_encoding ( Encoding ::UTF_8 ) } ] )
2831+ end
2832+ transport = StreamableHTTPTransport . new ( server , enable_json_response : true )
2833+ session_id = transport . handle_request ( create_rack_request (
2834+ "POST" ,
2835+ "/" ,
2836+ { "CONTENT_TYPE" => "application/json" } ,
2837+ { jsonrpc : "2.0" , method : "initialize" , id : "init" , params : initialize_params } . to_json ,
2838+ ) ) [ 1 ] [ "mcp-session-id" ]
2839+
2840+ response = transport . handle_request ( create_rack_request (
2841+ "POST" ,
2842+ "/" ,
2843+ { "CONTENT_TYPE" => "application/json" , "HTTP_MCP_SESSION_ID" => session_id } ,
2844+ { jsonrpc : "2.0" , id : 2 , method : "tools/call" , params : { name : "invalid_text" , arguments : { } } } . to_json ,
2845+ ) )
2846+
2847+ assert_equal 200 , response [ 0 ]
2848+ assert_equal (
2849+ { "jsonrpc" => "2.0" , "id" => 2 , "error" => { "code" => -32603 , "message" => "Internal error" } } ,
2850+ JSON . parse ( response [ 2 ] [ 0 ] ) ,
2851+ )
2852+ assert_equal ( [ [ JSON ::GeneratorError , { error : "Failed to generate response" } ] ] , reported )
2853+ ensure
2854+ transport . close
2855+ end
2856+
27952857 test "JSON response mode returns application/json for POST requests" do
27962858 server = Server . new ( name : "test" , tools : [ ] , prompts : [ ] , resources : [ ] )
27972859 transport = StreamableHTTPTransport . new ( server , enable_json_response : true )
@@ -6280,6 +6342,27 @@ def string
62806342 assert_equal 405 , @transport . handle_request ( delete_request ) [ 0 ]
62816343 end
62826344
6345+ test "modern POST answers a reply that cannot be generated with Internal error" do
6346+ reported = [ ]
6347+ original_reporter = MCP . configuration . exception_reporter
6348+ MCP . configuration . exception_reporter = -> ( e , context ) { reported << [ e . class , context ] }
6349+ @server . define_tool ( name : "invalid_text" ) do
6350+ Tool ::Response . new ( [ { type : "text" , text : "\xFF " . dup . force_encoding ( Encoding ::UTF_8 ) } ] )
6351+ end
6352+
6353+ response = @transport . handle_request ( modern_rack_request (
6354+ modern_body ( "tools/call" , { name : "invalid_text" , arguments : { } } ) ,
6355+ ) )
6356+
6357+ assert_equal 200 , response [ 0 ]
6358+ body = JSON . parse ( response [ 2 ] [ 0 ] )
6359+ assert_equal 1 , body [ "id" ]
6360+ assert_equal ( { "code" => -32603 , "message" => "Internal error" } , body [ "error" ] )
6361+ assert_equal ( [ [ JSON ::GeneratorError , { error : "Failed to generate response" } ] ] , reported )
6362+ ensure
6363+ MCP . configuration . exception_reporter = original_reporter
6364+ end
6365+
62836366 test "modern POST maps an unknown method to 404 with -32601" do
62846367 response = @transport . handle_request ( modern_rack_request ( modern_body ( "no/such_method" , { } ) ) )
62856368
0 commit comments