Skip to content

Commit 4322ec0

Browse files
committed
Default missing MCP-Protocol-Version to 2025-03-26 in StreamableHTTPTransport
## Motivation and Context MCP 2025-11-25 (basic/transports#protocol-version-header) specifies: > For backwards compatibility, if the server does not receive an > `MCP-Protocol-Version` header, and has no other way to identify the > version - for example, by relying on the protocol version negotiated > during initialization - the server SHOULD assume protocol version `2025-03-26`. PR #347 satisfied only the MUST requirement (reject invalid/unsupported values with 400). The SHOULD requirement to default a missing header to `2025-03-26` was deferred to this follow-up. This change brings the Ruby SDK in line with the Python SDK (`src/mcp/server/streamable_http.py`), which falls back to `DEFAULT_NEGOTIATED_VERSION` and then revalidates against the supported list. ## Behavior A new constant `MCP::Configuration::DEFAULT_NEGOTIATED_PROTOCOL_VERSION` holds `"2025-03-26"`. `validate_protocol_version_header` now substitutes this value when the `MCP-Protocol-Version` header is absent, then runs the same supported-version check. Because `"2025-03-26"` is currently in `SUPPORTED_STABLE_PROTOCOL_VERSIONS`, missing headers continue to pass through as before. Once `"2025-03-26"` is dropped from the supported list in a future change, missing headers will start returning 400 with a message naming the defaulted value. ## How Has This Been Tested? Existing test `POST request without MCP-Protocol-Version header succeeds` continues to pass (observable behavior unchanged today). Added a focused regression test that stubs the supported-version list to exclude `"2025-03-26"` and verifies that a header-less request is rejected with the defaulted value reported in the error message; this exercises the fallback branch directly. ## Breaking Changes None today. The fallback is future-proofing: when `"2025-03-26"` is eventually dropped, header-less requests will start returning 400. That is the intended spec-driven behavior.
1 parent cf44475 commit 4322ec0

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

lib/mcp/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Configuration
66
SUPPORTED_STABLE_PROTOCOL_VERSIONS = [
77
LATEST_STABLE_PROTOCOL_VERSION, "2025-06-18", "2025-03-26", "2024-11-05",
88
]
9+
DEFAULT_NEGOTIATED_PROTOCOL_VERSION = "2025-03-26"
910

1011
attr_writer :exception_reporter, :around_request
1112

lib/mcp/server/transports/streamable_http_transport.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,7 @@ def initialize_request?(body)
543543
end
544544

545545
def validate_protocol_version_header(request)
546-
header_value = request.env["HTTP_MCP_PROTOCOL_VERSION"]
547-
return if header_value.nil?
546+
header_value = request.env["HTTP_MCP_PROTOCOL_VERSION"] || MCP::Configuration::DEFAULT_NEGOTIATED_PROTOCOL_VERSION
548547
return if MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.include?(header_value)
549548

550549
supported = MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.join(", ")

test/mcp/server/transports/streamable_http_transport_test.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,50 @@ def string
17781778
assert_equal 200, response[0]
17791779
end
17801780

1781+
test "missing MCP-Protocol-Version header falls back to default for validation" do
1782+
MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.stubs(:include?).returns(false)
1783+
1784+
request = Rack::Request.new(
1785+
"REQUEST_METHOD" => "POST",
1786+
"PATH_INFO" => "/",
1787+
"rack.input" => StringIO.new(""),
1788+
)
1789+
1790+
response = @transport.send(:validate_protocol_version_header, request)
1791+
assert_equal 400, response[0]
1792+
1793+
body = JSON.parse(response[2][0])
1794+
assert_includes body["error"]["message"], MCP::Configuration::DEFAULT_NEGOTIATED_PROTOCOL_VERSION
1795+
end
1796+
1797+
test "POST request with empty MCP-Protocol-Version header returns 400" do
1798+
init_request = create_rack_request(
1799+
"POST",
1800+
"/",
1801+
{ "CONTENT_TYPE" => "application/json" },
1802+
{ jsonrpc: "2.0", method: "initialize", id: "init" }.to_json,
1803+
)
1804+
init_response = @transport.handle_request(init_request)
1805+
session_id = init_response[1]["Mcp-Session-Id"]
1806+
1807+
request = create_rack_request(
1808+
"POST",
1809+
"/",
1810+
{
1811+
"CONTENT_TYPE" => "application/json",
1812+
"HTTP_MCP_SESSION_ID" => session_id,
1813+
"HTTP_MCP_PROTOCOL_VERSION" => "",
1814+
},
1815+
{ jsonrpc: "2.0", method: "tools/list", id: "list" }.to_json,
1816+
)
1817+
1818+
response = @transport.handle_request(request)
1819+
assert_equal 400, response[0]
1820+
1821+
body = JSON.parse(response[2][0])
1822+
assert_equal JsonRpcHandler::ErrorCode::INVALID_REQUEST, body["error"]["code"]
1823+
end
1824+
17811825
test "POST request with array body and unsupported MCP-Protocol-Version returns 400" do
17821826
init_request = create_rack_request(
17831827
"POST",

0 commit comments

Comments
 (0)