Skip to content

Commit 2c3c307

Browse files
authored
Merge pull request #492 from koic/require_mcp_method_header_on_the_modern_path
Require the `Mcp-Method` header on the modern path
2 parents 02aed75 + 5f3a131 commit 2c3c307

2 files changed

Lines changed: 73 additions & 16 deletions

File tree

‎lib/mcp/server/transports/streamable_http_transport.rb‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,23 +634,43 @@ def validate_modern_headers(request, body, header_version)
634634
)
635635
end
636636

637+
# `Mcp-Method` is required on every modern POST and `Mcp-Name` on the name-bearing methods:
638+
# the spec lists a missing required standard header among the validation failures that MUST be rejected,
639+
# and the TypeScript SDK enforces presence the same way. Absence cannot be treated as "nothing to compare":
640+
# the headers exist so intermediaries can route without parsing bodies, and a client that omits them
641+
# defeats that contract silently.
637642
method_header = request.env["HTTP_MCP_METHOD"]
638-
if method_header && method_header != body[:method]
643+
if method_header.to_s.empty?
644+
return header_mismatch_response(
645+
"Mcp-Method header is required on the modern path",
646+
body[:id],
647+
)
648+
end
649+
if method_header != body[:method]
639650
return header_mismatch_response(
640651
"Mcp-Method header value '#{method_header}' does not match body value '#{body[:method]}'",
641652
body[:id],
642653
)
643654
end
644655

645-
name_header = request.env["HTTP_MCP_NAME"]
646-
if name_header && NAME_BEARING_METHODS.include?(body[:method])
656+
if NAME_BEARING_METHODS.include?(body[:method])
647657
body_name = params.is_a?(Hash) ? params[:name] || params[:uri] : nil
648-
decoded_name = decode_header_value(name_header)
649-
if body_name && decoded_name != body_name
650-
return header_mismatch_response(
651-
"Mcp-Name header value '#{decoded_name}' does not match body value '#{body_name}'",
652-
body[:id],
653-
)
658+
name_header = request.env["HTTP_MCP_NAME"]
659+
if body_name
660+
if name_header.to_s.empty?
661+
return header_mismatch_response(
662+
"Mcp-Name header is required for `#{body[:method]}`",
663+
body[:id],
664+
)
665+
end
666+
667+
decoded_name = decode_header_value(name_header)
668+
if decoded_name != body_name
669+
return header_mismatch_response(
670+
"Mcp-Name header value '#{decoded_name}' does not match body value '#{body_name}'",
671+
body[:id],
672+
)
673+
end
654674
end
655675
end
656676

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)