Skip to content

Commit d1c6a46

Browse files
authored
Merge pull request #105 from siddhantbajaj/handle-notification-initialized-method
Return HTTP status code 202 for notification initialized method
2 parents 3325862 + 4a65f62 commit d1c6a46

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/mcp/server/transports/streamable_http_transport.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def handle_post(request)
108108

109109
if body["method"] == "initialize"
110110
handle_initialization(body_string, body)
111+
elsif body["method"] == MCP::Methods::NOTIFICATIONS_INITIALIZED
112+
handle_notification_initialized
111113
else
112114
handle_regular_request(body_string, session_id)
113115
end
@@ -185,6 +187,10 @@ def handle_initialization(body_string, body)
185187
[200, headers, [response]]
186188
end
187189

190+
def handle_notification_initialized
191+
[202, {}, []]
192+
end
193+
188194
def handle_regular_request(body_string, session_id)
189195
# If session ID is provided, but not in the sessions hash, return an error
190196
if session_id && !@sessions.key?(session_id)

test/mcp/server/transports/streamable_http_transport_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,33 @@ class StreamableHTTPTransportTest < ActiveSupport::TestCase
595595
assert_equal "Internal server error", body["error"]
596596
end
597597

598+
test "POST notifications/initialized returns 202 with no body" do
599+
# Create a session first (optional for notification, but keep consistent with flow)
600+
init_request = create_rack_request(
601+
"POST",
602+
"/",
603+
{ "CONTENT_TYPE" => "application/json" },
604+
{ jsonrpc: "2.0", method: "initialize", id: "init" }.to_json,
605+
)
606+
init_response = @transport.handle_request(init_request)
607+
session_id = init_response[1]["Mcp-Session-Id"]
608+
609+
notif_request = create_rack_request(
610+
"POST",
611+
"/",
612+
{
613+
"CONTENT_TYPE" => "application/json",
614+
"HTTP_MCP_SESSION_ID" => session_id,
615+
},
616+
{ jsonrpc: "2.0", method: MCP::Methods::NOTIFICATIONS_INITIALIZED }.to_json,
617+
)
618+
619+
response = @transport.handle_request(notif_request)
620+
assert_equal 202, response[0]
621+
assert_empty(response[1])
622+
assert_empty(response[2])
623+
end
624+
598625
private
599626

600627
def create_rack_request(method, path, headers, body = nil)

0 commit comments

Comments
 (0)