Skip to content

Commit 8823bd3

Browse files
authored
Merge pull request #257 from koic/return_404_for_invalid_session_id
Return 404 for invalid session ID in `handle_regular_request`
2 parents 6e35d13 + cf1d578 commit 8823bd3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/mcp/server/transports/streamable_http_transport.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def handle_regular_request(body_string, session_id)
261261
unless @stateless
262262
# If session ID is provided, but not in the sessions hash, return an error
263263
if session_id && !@sessions.key?(session_id)
264-
return [400, { "Content-Type" => "application/json" }, [{ error: "Invalid session ID" }.to_json]]
264+
return session_not_found_response
265265
end
266266
end
267267

test/mcp/server/transports/streamable_http_transport_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,25 @@ class StreamableHTTPTransportTest < ActiveSupport::TestCase
287287
assert_equal "Session not found", body["error"]
288288
end
289289

290+
test "handles POST request with invalid session ID" do
291+
request = create_rack_request(
292+
"POST",
293+
"/",
294+
{
295+
"CONTENT_TYPE" => "application/json",
296+
"HTTP_MCP_SESSION_ID" => "invalid_id",
297+
},
298+
{ jsonrpc: "2.0", method: "ping", id: "456" }.to_json,
299+
)
300+
301+
response = @transport.handle_request(request)
302+
assert_equal 404, response[0]
303+
assert_equal({ "Content-Type" => "application/json" }, response[1])
304+
305+
body = JSON.parse(response[2][0])
306+
assert_equal "Session not found", body["error"]
307+
end
308+
290309
test "handles DELETE request with valid session ID" do
291310
# First create a session with initialize
292311
init_request = create_rack_request(

0 commit comments

Comments
 (0)