Skip to content
Merged
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
14 changes: 11 additions & 3 deletions lib/mcp/server/transports/streamable_http_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +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
elsif notification?(body) || response?(body)
handle_accepted
else
handle_regular_request(body_string, session_id)
end
Expand Down Expand Up @@ -168,6 +168,14 @@ def parse_request_body(body_string)
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
end

def notification?(body)
!body["id"] && !!body["method"]
end

def response?(body)
!!body["id"] && !body["method"]
end

def handle_initialization(body_string, body)
session_id = SecureRandom.uuid

Expand All @@ -187,7 +195,7 @@ def handle_initialization(body_string, body)
[200, headers, [response]]
end

def handle_notification_initialized
def handle_accepted
[202, {}, []]
end

Expand Down
26 changes: 26 additions & 0 deletions test/mcp/server/transports/streamable_http_transport_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,32 @@ class StreamableHTTPTransportTest < ActiveSupport::TestCase
assert_empty(response[2])
end

test "handles POST request with body including JSON-RPC response object and returns with no body" do
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"]

request = create_rack_request(
"POST",
"/",
{
"CONTENT_TYPE" => "application/json",
"HTTP_MCP_SESSION_ID" => session_id,
},
{ jsonrpc: "2.0", result: "success", id: "123" }.to_json,
)

response = @transport.handle_request(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