Skip to content

Return HTTP status code 202 for notification initialized method #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions lib/mcp/server/transports/streamable_http_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def handle_post(request)

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
Expand Down Expand Up @@ -185,6 +187,10 @@ def handle_initialization(body_string, body)
[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 && [email protected]?(session_id)
Expand Down
27 changes: 27 additions & 0 deletions test/mcp/server/transports/streamable_http_transport_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,33 @@ class StreamableHTTPTransportTest < ActiveSupport::TestCase
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)
Expand Down