Reject POST requests without session ID in stateful mode
Per the MCP specification (Streamable HTTP > Session Management): > Servers that require a session ID SHOULD respond to requests without an `Mcp-Session-Id` header > (other than initialization) with HTTP 400 Bad Request. https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-management Previously, non-initialize POST requests without `Mcp-Session-Id` in stateful mode were processed with HTTP 200 (for regular requests) or HTTP 202 (for notifications/responses). This change adds an explicit check in `handle_post` to return HTTP 400 Bad Request when the session ID is missing in stateful mode for all non-initialize requests, aligning with the specification.
This commit is contained in:
parent
a9e45148f6
commit
88d9bed69d
1 changed files with 63 additions and 3 deletions
|
|
@ -272,6 +272,62 @@ module MCP
|
|||
assert_equal "Missing session ID", body["error"]
|
||||
end
|
||||
|
||||
test "rejects POST request without session ID in stateful mode" do
|
||||
request = create_rack_request(
|
||||
"POST",
|
||||
"/",
|
||||
{ "CONTENT_TYPE" => "application/json" },
|
||||
{ jsonrpc: "2.0", method: "ping", id: "1" }.to_json,
|
||||
)
|
||||
|
||||
response = @transport.handle_request(request)
|
||||
assert_equal 400, response[0]
|
||||
body = JSON.parse(response[2][0])
|
||||
assert_equal "Missing session ID", body["error"]
|
||||
end
|
||||
|
||||
test "rejects notification without session ID in stateful mode" do
|
||||
request = create_rack_request(
|
||||
"POST",
|
||||
"/",
|
||||
{ "CONTENT_TYPE" => "application/json" },
|
||||
{ jsonrpc: "2.0", method: "notifications/initialized" }.to_json,
|
||||
)
|
||||
|
||||
response = @transport.handle_request(request)
|
||||
assert_equal 400, response[0]
|
||||
body = JSON.parse(response[2][0])
|
||||
assert_equal "Missing session ID", body["error"]
|
||||
end
|
||||
|
||||
test "rejects response without session ID in stateful mode" do
|
||||
request = create_rack_request(
|
||||
"POST",
|
||||
"/",
|
||||
{ "CONTENT_TYPE" => "application/json" },
|
||||
{ jsonrpc: "2.0", id: "1", result: {} }.to_json,
|
||||
)
|
||||
|
||||
response = @transport.handle_request(request)
|
||||
assert_equal 400, response[0]
|
||||
body = JSON.parse(response[2][0])
|
||||
assert_equal "Missing session ID", body["error"]
|
||||
end
|
||||
|
||||
test "allows POST request without session ID in stateless mode" do
|
||||
stateless_transport = StreamableHTTPTransport.new(@server, stateless: true)
|
||||
|
||||
request = create_rack_request(
|
||||
"POST",
|
||||
"/",
|
||||
{ "CONTENT_TYPE" => "application/json" },
|
||||
{ jsonrpc: "2.0", method: "ping", id: "1" }.to_json,
|
||||
)
|
||||
|
||||
response = stateless_transport.handle_request(request)
|
||||
assert_equal 200, response[0]
|
||||
end
|
||||
|
||||
test "rejects duplicate SSE connection with 409" do
|
||||
# Create a session
|
||||
init_request = create_rack_request(
|
||||
|
|
|
|||
Loading…
Reference in a new issue