-
Notifications
You must be signed in to change notification settings - Fork 56
Add stateless
feature to Streamable HTTP protocol
#101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bigH
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
1debit:stateless
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−13
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,13 @@ module MCP | |
class Server | ||
module Transports | ||
class StreamableHTTPTransport < Transport | ||
def initialize(server) | ||
super | ||
def initialize(server, stateless: false) | ||
super(server) | ||
# { session_id => { stream: stream_object } | ||
@sessions = {} | ||
@mutex = Mutex.new | ||
|
||
@stateless = stateless | ||
end | ||
|
||
def handle_request(request) | ||
|
@@ -24,7 +26,7 @@ def handle_request(request) | |
when "DELETE" | ||
handle_delete(request) | ||
else | ||
[405, { "Content-Type" => "application/json" }, [{ error: "Method not allowed" }.to_json]] | ||
method_not_allowed_response | ||
end | ||
end | ||
|
||
|
@@ -35,6 +37,9 @@ def close | |
end | ||
|
||
def send_notification(method, params = nil, session_id: nil) | ||
# Stateless mode doesn't support notifications | ||
raise "Stateless mode does not support notifications" if @stateless | ||
|
||
notification = { | ||
jsonrpc: "2.0", | ||
method:, | ||
|
@@ -117,6 +122,10 @@ def handle_post(request) | |
end | ||
|
||
def handle_get(request) | ||
if @stateless | ||
return method_not_allowed_response | ||
end | ||
|
||
session_id = extract_session_id(request) | ||
|
||
return missing_session_id_response unless session_id | ||
|
@@ -126,6 +135,13 @@ def handle_get(request) | |
end | ||
|
||
def handle_delete(request) | ||
success_response = [200, { "Content-Type" => "application/json" }, [{ success: true }.to_json]] | ||
|
||
if @stateless | ||
# Stateless mode doesn't support sessions, so we can just return a success response | ||
return success_response | ||
end | ||
|
||
session_id = request.env["HTTP_MCP_SESSION_ID"] | ||
|
||
return [ | ||
|
@@ -135,7 +151,7 @@ def handle_delete(request) | |
] unless session_id | ||
|
||
cleanup_session(session_id) | ||
[200, { "Content-Type" => "application/json" }, [{ success: true }.to_json]] | ||
success_response | ||
end | ||
|
||
def cleanup_session(session_id) | ||
|
@@ -167,40 +183,56 @@ def parse_request_body(body_string) | |
end | ||
|
||
def handle_initialization(body_string, body) | ||
session_id = SecureRandom.uuid | ||
session_id = nil | ||
|
||
@mutex.synchronize do | ||
@sessions[session_id] = { | ||
stream: nil, | ||
} | ||
unless @stateless | ||
session_id = SecureRandom.uuid | ||
|
||
@mutex.synchronize do | ||
@sessions[session_id] = { | ||
stream: nil, | ||
} | ||
end | ||
end | ||
|
||
response = @server.handle_json(body_string) | ||
|
||
headers = { | ||
"Content-Type" => "application/json", | ||
"Mcp-Session-Id" => session_id, | ||
} | ||
|
||
headers["Mcp-Session-Id"] = session_id if session_id | ||
|
||
[200, headers, [response]] | ||
end | ||
|
||
def handle_regular_request(body_string, session_id) | ||
# If session ID is provided, but not in the sessions hash, return an error | ||
if session_id && [email protected]?(session_id) | ||
return [400, { "Content-Type" => "application/json" }, [{ error: "Invalid session ID" }.to_json]] | ||
unless @stateless | ||
# If session ID is provided, but not in the sessions hash, return an error | ||
if session_id && [email protected]?(session_id) | ||
return [400, { "Content-Type" => "application/json" }, [{ error: "Invalid session ID" }.to_json]] | ||
end | ||
end | ||
|
||
response = @server.handle_json(body_string) | ||
|
||
# Stream can be nil since stateless mode doesn't retain streams | ||
stream = get_session_stream(session_id) if session_id | ||
|
||
if stream | ||
send_response_to_stream(stream, response, session_id) | ||
elsif response.nil? && notification_request?(body_string) | ||
[202, { "Content-Type" => "application/json" }, [response]] | ||
else | ||
[200, { "Content-Type" => "application/json" }, [response]] | ||
end | ||
end | ||
|
||
def notification_request?(body_string) | ||
body = parse_request_body(body_string) | ||
body.is_a?(Hash) && body["method"].start_with?("notifications/") | ||
end | ||
|
||
def get_session_stream(session_id) | ||
@mutex.synchronize { @sessions[session_id]&.fetch(:stream, nil) } | ||
end | ||
|
@@ -222,6 +254,10 @@ def session_exists?(session_id) | |
@mutex.synchronize { @sessions.key?(session_id) } | ||
end | ||
|
||
def method_not_allowed_response | ||
[405, { "Content-Type" => "application/json" }, [{ error: "Method not allowed" }.to_json]] | ||
end | ||
|
||
def missing_session_id_response | ||
[400, { "Content-Type" => "application/json" }, [{ error: "Missing session ID" }.to_json]] | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.