Skip to content

Commit 76ab822

Browse files
authored
Merge pull request #245 from koic/fix_sse_response_status
Return 202 Accepted for SSE responses per MCP spec
2 parents 145dc1f + a02a457 commit 76ab822

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/mcp/server/transports/streamable_http_transport.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def get_session_stream(session_id)
288288
def send_response_to_stream(stream, response, session_id)
289289
message = JSON.parse(response)
290290
send_to_stream(stream, message)
291-
[200, { "Content-Type" => "application/json" }, [{ accepted: true }.to_json]]
291+
handle_accepted
292292
rescue IOError, Errno::EPIPE => e
293293
MCP.configuration.exception_reporter.call(
294294
e,

test/mcp/server/transports/streamable_http_transport_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,47 @@ class StreamableHTTPTransportTest < ActiveSupport::TestCase
853853
assert_nil(body)
854854
end
855855

856+
test "send_response_to_stream returns 202 when message is sent to stream" do
857+
# Create and initialize a session
858+
init_request = create_rack_request(
859+
"POST",
860+
"/",
861+
{ "CONTENT_TYPE" => "application/json" },
862+
{ jsonrpc: "2.0", method: "initialize", id: "123" }.to_json,
863+
)
864+
init_response = @transport.handle_request(init_request)
865+
session_id = init_response[1]["Mcp-Session-Id"]
866+
867+
# Connect with SSE
868+
io = StringIO.new
869+
get_request = create_rack_request(
870+
"GET",
871+
"/",
872+
{ "HTTP_MCP_SESSION_ID" => session_id },
873+
)
874+
response = @transport.handle_request(get_request)
875+
response[2].call(io) if response[2].is_a?(Proc)
876+
877+
# Give the stream time to set up
878+
sleep(0.1)
879+
880+
# Make a regular request that will be routed through send_response_to_stream
881+
request = create_rack_request(
882+
"POST",
883+
"/",
884+
{
885+
"CONTENT_TYPE" => "application/json",
886+
"HTTP_MCP_SESSION_ID" => session_id,
887+
},
888+
{ jsonrpc: "2.0", method: "ping", id: "456" }.to_json,
889+
)
890+
891+
response = @transport.handle_request(request)
892+
assert_equal 202, response[0]
893+
assert_empty response[1]
894+
assert_empty response[2]
895+
end
896+
856897
test "handle post request with a standard error" do
857898
request = create_rack_request(
858899
"POST",

0 commit comments

Comments
 (0)