Merge pull request #257 from koic/return_404_for_invalid_session_id

Return 404 for invalid session ID in `handle_regular_request`
This commit is contained in:
Koichi ITO 2026-03-18 04:26:57 +09:00 • committed by GitHub
commit 8823bd34ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View file

@ -261,7 +261,7 @@ module MCP
unless @stateless
# If session ID is provided, but not in the sessions hash, return an error
if session_id && !@sessions.key?(session_id)
return [400, { "Content-Type" => "application/json" }, [{ error: "Invalid session ID" }.to_json]]
return session_not_found_response
end
end

View file

@ -287,6 +287,25 @@ module MCP
assert_equal "Session not found", body["error"]
end
test "handles POST request with invalid session ID" do
request = create_rack_request(
"POST",
"/",
{
"CONTENT_TYPE" => "application/json",
"HTTP_MCP_SESSION_ID" => "invalid_id",
},
{ jsonrpc: "2.0", method: "ping", id: "456" }.to_json,
)
response = @transport.handle_request(request)
assert_equal 404, response[0]
assert_equal({ "Content-Type" => "application/json" }, response[1])
body = JSON.parse(response[2][0])
assert_equal "Session not found", body["error"]
end
test "handles DELETE request with valid session ID" do
# First create a session with initialize
init_request = create_rack_request(