@@ -5340,6 +5340,31 @@ def string
53405340 assert_equal ( -32020 , JSON . parse ( response [ 2 ] [ 0 ] ) . dig ( "error" , "code" ) )
53415341 end
53425342
5343+ test "modern POST without the Mcp-Method header returns 400 with -32020" do
5344+ # The header is required on every modern POST; a missing required standard header is
5345+ # a validation failure, matching the TypeScript SDK's enforcement.
5346+ response = @transport . handle_request ( modern_rack_request (
5347+ modern_body ( "ping" , { } ) ,
5348+ method_header : nil ,
5349+ ) )
5350+
5351+ assert_equal 400 , response [ 0 ]
5352+ body = JSON . parse ( response [ 2 ] [ 0 ] )
5353+ assert_equal ( -32020 , body . dig ( "error" , "code" ) )
5354+ assert_includes body . dig ( "error" , "message" ) , "Mcp-Method"
5355+ end
5356+
5357+ test "modern POST without the Mcp-Name header on a name-bearing method returns 400 with -32020" do
5358+ response = @transport . handle_request ( modern_rack_request (
5359+ modern_body ( "tools/call" , { name : "some_tool" , arguments : { } } ) ,
5360+ ) )
5361+
5362+ assert_equal 400 , response [ 0 ]
5363+ body = JSON . parse ( response [ 2 ] [ 0 ] )
5364+ assert_equal ( -32020 , body . dig ( "error" , "code" ) )
5365+ assert_includes body . dig ( "error" , "message" ) , "Mcp-Name"
5366+ end
5367+
53435368 test "modern POST decodes the base64 Mcp-Name sentinel and enforces the match" do
53445369 # `Mcp-Name` mirrors `params.uri` for `resources/read`; a non-ASCII value arrives
53455370 # wrapped in the `=?base64?...?=` sentinel produced by `MCP::Client::HTTP`.
@@ -5410,6 +5435,7 @@ def string
54105435
54115436 response = @transport . handle_request ( modern_rack_request (
54125437 modern_body ( "tools/call" , { name : "guarded_tool" , arguments : { } } ) ,
5438+ headers : { "HTTP_MCP_NAME" => "guarded_tool" } ,
54135439 ) )
54145440
54155441 assert_equal 400 , response [ 0 ]
@@ -5762,13 +5788,24 @@ def create_rack_request_without_accept(method, path, headers, body = nil)
57625788 end
57635789
57645790 # Builds a POST request routed to the modern path via the `MCP-Protocol-Version` header.
5765- def modern_rack_request ( body , version : "2026-07-28" , headers : { } )
5766- create_rack_request (
5767- "POST" ,
5768- "/" ,
5769- { "CONTENT_TYPE" => "application/json" , "HTTP_MCP_PROTOCOL_VERSION" => version } . merge ( headers ) ,
5770- body ,
5771- )
5791+ # Mirrors what a conforming modern client sends: `Mcp-Method` is required on every modern POST,
5792+ # so it defaults to the body's method. Pass `method_header: nil` to omit it
5793+ # (for tests of the requirement itself) or a String to send a specific value.
5794+ def modern_rack_request ( body , version : "2026-07-28" , headers : { } , method_header : :from_body )
5795+ derived_method = if method_header == :from_body
5796+ begin
5797+ parsed = JSON . parse ( body )
5798+ parsed [ "method" ] if parsed . is_a? ( Hash )
5799+ rescue JSON ::ParserError
5800+ nil
5801+ end
5802+ else
5803+ method_header
5804+ end
5805+
5806+ base_headers = { "CONTENT_TYPE" => "application/json" , "HTTP_MCP_PROTOCOL_VERSION" => version }
5807+ base_headers [ "HTTP_MCP_METHOD" ] = derived_method if derived_method
5808+ create_rack_request ( "POST" , "/" , base_headers . merge ( headers ) , body )
57725809 end
57735810
57745811 # Builds a JSON-RPC body carrying the SEP-2575 modern `_meta` envelope.
0 commit comments