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: https://github.com/modelcontextprotocol/ruby-sdk/issues/111
This commit is contained in:
parent
e168292876
commit
cd7a54a5c9
2 changed files with 37 additions and 3 deletions
|
|
@ -108,8 +108,8 @@ module MCP
|
||||||
|
|
||||||
if body["method"] == "initialize"
|
if body["method"] == "initialize"
|
||||||
handle_initialization(body_string, body)
|
handle_initialization(body_string, body)
|
||||||
elsif body["method"] == MCP::Methods::NOTIFICATIONS_INITIALIZED
|
elsif notification?(body) || response?(body)
|
||||||
handle_notification_initialized
|
handle_accepted
|
||||||
else
|
else
|
||||||
handle_regular_request(body_string, session_id)
|
handle_regular_request(body_string, session_id)
|
||||||
end
|
end
|
||||||
|
|
@ -168,6 +168,14 @@ module MCP
|
||||||
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
|
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def notification?(body)
|
||||||
|
!body["id"] && !!body["method"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def response?(body)
|
||||||
|
!!body["id"] && !body["method"]
|
||||||
|
end
|
||||||
|
|
||||||
def handle_initialization(body_string, body)
|
def handle_initialization(body_string, body)
|
||||||
session_id = SecureRandom.uuid
|
session_id = SecureRandom.uuid
|
||||||
|
|
||||||
|
|
@ -187,7 +195,7 @@ module MCP
|
||||||
[200, headers, [response]]
|
[200, headers, [response]]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_notification_initialized
|
def handle_accepted
|
||||||
[202, {}, []]
|
[202, {}, []]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -620,6 +620,32 @@ module MCP
|
||||||
assert_empty(response[2])
|
assert_empty(response[2])
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def create_rack_request(method, path, headers, body = nil)
|
def create_rack_request(method, path, headers, body = nil)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue