Return HTTP status code 202 for notification initialized method

This commit is contained in:
Siddhant Bajaj 2025-08-08 03:08:12 -04:00
commit 4a65f62158
2 changed files with 33 additions and 0 deletions

View file

@ -108,6 +108,8 @@ module MCP
if body["method"] == "initialize"
handle_initialization(body_string, body)
elsif body["method"] == MCP::Methods::NOTIFICATIONS_INITIALIZED
handle_notification_initialized
else
handle_regular_request(body_string, session_id)
end
@ -185,6 +187,10 @@ module MCP
[200, headers, [response]]
end
def handle_notification_initialized
[202, {}, []]
end
def handle_regular_request(body_string, session_id)
# If session ID is provided, but not in the sessions hash, return an error
if session_id && !@sessions.key?(session_id)

View file

@ -595,6 +595,33 @@ module MCP
assert_equal "Internal server error", body["error"]
end
test "POST notifications/initialized returns 202 with no body" do
# Create a session first (optional for notification, but keep consistent with flow)
init_request = create_rack_request(
"POST",
"/",
{ "CONTENT_TYPE" => "application/json" },
{ jsonrpc: "2.0", method: "initialize", id: "init" }.to_json,
)
init_response = @transport.handle_request(init_request)
session_id = init_response[1]["Mcp-Session-Id"]
notif_request = create_rack_request(
"POST",
"/",
{
"CONTENT_TYPE" => "application/json",
"HTTP_MCP_SESSION_ID" => session_id,
},
{ jsonrpc: "2.0", method: MCP::Methods::NOTIFICATIONS_INITIALIZED }.to_json,
)
response = @transport.handle_request(notif_request)
assert_equal 202, response[0]
assert_empty(response[1])
assert_empty(response[2])
end
private
def create_rack_request(method, path, headers, body = nil)