Skip to content

Commit ae627e9

Browse files
committed
Fix to return a response code 202 if the server accepts JSON-RPC notifications and responses
If the server accepts JSON-RPC notifications and responses, it must return an HTTP status code of 202 without a body. refs - https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#sending-messages-to-the-server - https://www.jsonrpc.org/specification Follow up: #111
1 parent eb0d9c0 commit ae627e9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/mcp/server/transports/streamable_http_transport.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ 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
111+
elsif notification_or_response?(body)
112+
# For notifications and responses only, return 202 Accepted
113+
handle_notification_or_response
113114
else
114115
handle_regular_request(body_string, session_id)
115116
end
@@ -168,6 +169,10 @@ def parse_request_body(body_string)
168169
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
169170
end
170171

172+
def notification_or_response?(body)
173+
!(body["id"] && body["method"])
174+
end
175+
171176
def handle_initialization(body_string, body)
172177
session_id = SecureRandom.uuid
173178

@@ -187,7 +192,7 @@ def handle_initialization(body_string, body)
187192
[200, headers, [response]]
188193
end
189194

190-
def handle_notification_initialized
195+
def handle_notification_or_response
191196
[202, {}, []]
192197
end
193198

0 commit comments

Comments
 (0)