Validate Content-Type on POST requests

## Motivation and Context

The MCP Streamable HTTP specification requires POST request bodies to
be JSON-RPC messages with `Content-Type: application/json`. The Ruby
SDK did not validate Content-Type, accepting requests with any or no
Content-Type. The Python SDK validates this and returns 415
Unsupported Media Type for non-JSON Content-Types.

## How Has This Been Tested?

Added tests for Content-Type validation:

- POST without Content-Type returns 415
- POST with `Content-Type: text/plain` returns 415
- POST with `Content-Type: application/json; charset=utf-8` succeeds

## Breaking Changes

POST requests without `Content-Type: application/json` now return
415 Unsupported Media Type. This is a spec compliance fix.
This commit is contained in:
Koichi ITO 2026-04-04 20:52:50 +09:00
commit 81e81c359e

View file

@ -1143,6 +1143,51 @@ module MCP
assert_equal "Method not allowed", body["error"]
end
test "POST request without Content-Type returns 415" do
request = create_rack_request_without_accept(
"POST",
"/",
{ "HTTP_ACCEPT" => "application/json, text/event-stream" },
{ jsonrpc: "2.0", method: "initialize", id: "123" }.to_json,
)
response = @transport.handle_request(request)
assert_equal 415, response[0]
body = JSON.parse(response[2][0])
assert_equal "Unsupported Media Type: Content-Type must be application/json", body["error"]
end
test "POST request with wrong Content-Type returns 415" do
request = create_rack_request_without_accept(
"POST",
"/",
{
"CONTENT_TYPE" => "text/plain",
"HTTP_ACCEPT" => "application/json, text/event-stream",
},
{ jsonrpc: "2.0", method: "initialize", id: "123" }.to_json,
)
response = @transport.handle_request(request)
assert_equal 415, response[0]
end
test "POST request with Content-Type including charset succeeds" do
request = create_rack_request_without_accept(
"POST",
"/",
{
"CONTENT_TYPE" => "application/json; charset=utf-8",
"HTTP_ACCEPT" => "application/json, text/event-stream",
},
{ jsonrpc: "2.0", method: "initialize", id: "123" }.to_json,
)
response = @transport.handle_request(request)
assert_equal 200, response[0]
end
test "POST request without Accept header returns 406" do
request = create_rack_request_without_accept(
"POST",