Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mcp/server/transports/streamable_http_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def handle_regular_request(body_string, session_id)
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

Expand Down
19 changes: 19 additions & 0 deletions test/mcp/server/transports/streamable_http_transport_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,25 @@ class StreamableHTTPTransportTest < ActiveSupport::TestCase
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(
Expand Down