Merge pull request #263 from koic/make_streamable_http_transport_a_standard_rack_app

Make `StreamableHTTPTransport` a Rack application
This commit is contained in:
Koichi ITO 2026-04-15 11:01:40 +09:00 • committed by GitHub
commit 66d0722e41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,15 @@
require "json"
require_relative "../../transport"
# This file is autoloaded only when `StreamableHTTPTransport` is referenced,
# so the `rack` dependency does not affect `StdioTransport` users.
begin
require "rack"
rescue LoadError
raise LoadError, "The 'rack' gem is required to use the StreamableHTTPTransport. " \
"Add it to your Gemfile: gem 'rack'"
end
module MCP
class Server
module Transports
@ -39,6 +48,11 @@ module MCP
STREAM_WRITE_ERRORS = [IOError, Errno::EPIPE, Errno::ECONNRESET].freeze
SESSION_REAP_INTERVAL = 60
# Rack app interface. This transport can be mounted as a Rack app.
def call(env)
handle_request(Rack::Request.new(env))
end
def handle_request(request)
case request.env["REQUEST_METHOD"]
when "POST"
@ -546,7 +560,7 @@ module MCP
end
end
[200, SSE_HEADERS, body]
[200, SSE_HEADERS.dup, body]
end
# Returns the SSE stream available for server-to-client messages.
@ -628,7 +642,7 @@ module MCP
def setup_sse_stream(session_id)
body = create_sse_body(session_id)
[200, SSE_HEADERS, body]
[200, SSE_HEADERS.dup, body]
end
def create_sse_body(session_id)