Merge pull request #392 from koic/fallback_missing_protocol_version_in_streamable_http_transport

Default missing `MCP-Protocol-Version` to 2025-03-26 in StreamableHTTPTransport
This commit is contained in:
Koichi ITO 2026-06-14 00:15:55 +09:00 • committed by GitHub
commit 3d7046ab1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 2 deletions

View file

@ -6,6 +6,7 @@ module MCP
SUPPORTED_STABLE_PROTOCOL_VERSIONS = [
LATEST_STABLE_PROTOCOL_VERSION, "2025-06-18", "2025-03-26", "2024-11-05",
]
DEFAULT_NEGOTIATED_PROTOCOL_VERSION = "2025-03-26"
attr_writer :exception_reporter, :around_request

View file

@ -551,8 +551,7 @@ module MCP
end
def validate_protocol_version_header(request)
header_value = request.env["HTTP_MCP_PROTOCOL_VERSION"]
return if header_value.nil?
header_value = request.env["HTTP_MCP_PROTOCOL_VERSION"] || MCP::Configuration::DEFAULT_NEGOTIATED_PROTOCOL_VERSION
return if MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.include?(header_value)
supported = MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.join(", ")

View file

@ -1799,6 +1799,50 @@ module MCP
assert_equal 200, response[0]
end
test "missing MCP-Protocol-Version header falls back to default for validation" do
MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.stubs(:include?).returns(false)
request = Rack::Request.new(
"REQUEST_METHOD" => "POST",
"PATH_INFO" => "/",
"rack.input" => StringIO.new(""),
)
response = @transport.send(:validate_protocol_version_header, request)
assert_equal 400, response[0]
body = JSON.parse(response[2][0])
assert_includes body["error"]["message"], MCP::Configuration::DEFAULT_NEGOTIATED_PROTOCOL_VERSION
end
test "POST request with empty MCP-Protocol-Version header returns 400" do
init_request = create_rack_request(
"POST",
"/",
{ "CONTENT_TYPE" => "application/json" },
{ jsonrpc: "2.0", method: "initialize", id: "init" }.to_json,
)
init_response = @transport.handle_request(init_request)
session_id = init_response[1]["Mcp-Session-Id"]
request = create_rack_request(
"POST",
"/",
{
"CONTENT_TYPE" => "application/json",
"HTTP_MCP_SESSION_ID" => session_id,
"HTTP_MCP_PROTOCOL_VERSION" => "",
},
{ jsonrpc: "2.0", method: "tools/list", id: "list" }.to_json,
)
response = @transport.handle_request(request)
assert_equal 400, response[0]
body = JSON.parse(response[2][0])
assert_equal JsonRpcHandler::ErrorCode::INVALID_REQUEST, body["error"]["code"]
end
test "POST request with array body and unsupported MCP-Protocol-Version returns 400" do
init_request = create_rack_request(
"POST",