Skip to content

Commit 90b8fd4

Browse files
committed
Derive Mcp-Name from the body in the modern transport test helper
## Motivation and Context #490 and #492 merged independently green but broke each other on main: #492 made the modern path reject a name-bearing POST whose body carries a target name without `Mcp-Name` (-32020, HTTP 400) and taught the `modern_rack_request` helper to derive `Mcp-Method` from the body, while #490, merged in between, added five modern `tools/call` tests that build their requests through that helper and therefore send no `Mcp-Name`. Since the #492 merge every CI run on main fails those five tests with 400 where 200 is expected; the library behavior itself is correct. The helper now derives `Mcp-Name` the same way it derives `Mcp-Method`: from `params.name` or `params.uri` when the body's method is one of the name-bearing three. That is what a conforming client sends per the 2026-07-28 specification, and what the TypeScript SDK's client, the bundled `MCP::Client::HTTP`, and the conformance harness's standard headers all do, so tests built through the helper model a compliant client by default. `name_header: nil` omits the header for tests of the requirement itself, symmetric with `method_header: nil`, and an explicit `headers:` entry still overrides the derived value, which keeps the base64 mismatch tests exercising their divergent names. ## How Has This Been Tested? The five failing tests pass again without being touched, and the #492 requirement tests (missing `Mcp-Method`, missing `Mcp-Name`, header mismatches, base64 decoding) still pass: the transport test file reports 217 runs with zero failures. `bundle exec rake` is green, including RuboCop and the `--requirements 2025-11-25` conformance baseline. ## Breaking Changes None. The change is confined to a test helper; the shipped gem is untouched.
1 parent b03da23 commit 90b8fd4

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

‎test/mcp/server/transports/streamable_http_transport_test.rb‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5357,6 +5357,7 @@ def string
53575357
test "modern POST without the Mcp-Name header on a name-bearing method returns 400 with -32020" do
53585358
response = @transport.handle_request(modern_rack_request(
53595359
modern_body("tools/call", { name: "some_tool", arguments: {} }),
5360+
name_header: nil,
53605361
))
53615362

53625363
assert_equal 400, response[0]
@@ -5788,23 +5789,31 @@ def create_rack_request_without_accept(method, path, headers, body = nil)
57885789
end
57895790

57905791
# Builds a POST request routed to the modern path via the `MCP-Protocol-Version` header.
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
5792+
# Mirrors what a conforming modern client sends: `Mcp-Method` is required on every modern POST
5793+
# and `Mcp-Name` on a name-bearing method whose body carries a target name, so both default to
5794+
# the body's values. Pass `method_header: nil` / `name_header: nil` to omit one
57935795
# (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
5796+
def modern_rack_request(body, version: "2026-07-28", headers: {}, method_header: :from_body,
5797+
name_header: :from_body)
5798+
parsed = begin
5799+
JSON.parse(body)
5800+
rescue JSON::ParserError
5801+
nil
5802+
end
5803+
parsed = nil unless parsed.is_a?(Hash)
5804+
5805+
derived_method = method_header == :from_body ? parsed&.dig("method") : method_header
5806+
derived_name = if name_header == :from_body
5807+
if StreamableHTTPTransport::NAME_BEARING_METHODS.include?(parsed&.dig("method"))
5808+
parsed.dig("params", "name") || parsed.dig("params", "uri")
58015809
end
58025810
else
5803-
method_header
5811+
name_header
58045812
end
58055813

58065814
base_headers = { "CONTENT_TYPE" => "application/json", "HTTP_MCP_PROTOCOL_VERSION" => version }
58075815
base_headers["HTTP_MCP_METHOD"] = derived_method if derived_method
5816+
base_headers["HTTP_MCP_NAME"] = derived_name if derived_name
58085817
create_rack_request("POST", "/", base_headers.merge(headers), body)
58095818
end
58105819

0 commit comments

Comments
 (0)