mirror of
https://github.com/we-promise/sure.git
synced 2026-12-03 10:49:07 +08:00
fix(mcp): answer GET and DELETE /mcp with 405 Method Not Allowed (#4920)
## What broke
After #4780 made notifications/initialized return 202, Streamable-HTTP
clients like OpenClaw open GET /mcp for the SSE stream and terminate
with DELETE /mcp. Both verbs were unrouted, so Rails answered 404 and
the client tore the session down and reported 0 tools, even though POST
/mcp works.
## What changed
- GET and DELETE /mcp now route to a new
McpController#method_not_allowed action that answers 405 with an Allow:
POST header, per the MCP Streamable HTTP spec.
- The endpoint stays token-gated: without a bearer token both verbs
still return 401, same as POST.
## How verified
- Added regression tests asserting 405 (not 404) for authenticated GET
and DELETE /mcp, plus a 401 check for an unauthenticated GET.
- A local harness confirmed the routes were unrouted before the fix and
resolve to the new action after. No Ruby in this environment, so the
Rails suite is the gate.
Fixes #4913
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Fixes Streamable-HTTP clients like OpenClaw tearing down their MCP
session and reporting 0 tools. GET and DELETE `/mcp` were unrouted, so
Rails answered 404 and dropped the session even though POST works.
- Routes GET and DELETE `/mcp` to a `method_not_allowed` action that
returns 405 with an `Allow: POST` header, per the MCP Streamable HTTP
spec.
- A supplied `Mcp-Session-Id` that is no longer in the cache returns 404
instead, so the client knows to initialize a new session.
- Keeps the endpoint token-gated: unauthenticated GET and DELETE still
return 401, same as POST.
- Adds regression tests for 405 on authenticated GET/DELETE, 404 for
expired session ids, and 401 for an unauthenticated GET.
<sup>Written for commit 36e8bf6579.
Summary will update on new commits.</sup>
<a
href="https://cubic.dev/pr/we-promise/sure/pull/4920?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Requests using unsupported GET or DELETE methods on the MCP endpoint
now receive a “405 Method Not Allowed” response that identifies POST as
the accepted method.
* Authentication remains required; requests without a valid token
receive an authentication error, including GET requests.
* Requests with an invalid or expired session ID receive a “404 Not
Found” response.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -40,6 +40,19 @@ class McpController < ApplicationController
|
||||
render json: { jsonrpc: "2.0", id: request_id, result: result }
|
||||
end
|
||||
|
||||
# MCP's Streamable HTTP transport also uses GET (SSE stream) and DELETE
|
||||
# (session termination) on this endpoint. Sure serves neither, so answer
|
||||
# 405 Method Not Allowed: an unrouted 404 makes clients treat the session
|
||||
# as dropped. A supplied session id that is no longer in the cache is
|
||||
# answered 404 instead, so the client knows to initialize a new session.
|
||||
def method_not_allowed
|
||||
session_id = mcp_request_header("Mcp-Session-Id").presence
|
||||
return head :not_found if session_id && !valid_mcp_session?(session_id)
|
||||
|
||||
response.set_header("Allow", "POST")
|
||||
head :method_not_allowed
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_request_body
|
||||
@@ -198,7 +211,7 @@ class McpController < ApplicationController
|
||||
session_id = mcp_request_header("Mcp-Session-Id").presence
|
||||
return true unless session_id
|
||||
|
||||
unless Rails.cache.read(mcp_session_cache_key(session_id)) == mcp_user.id
|
||||
unless valid_mcp_session?(session_id)
|
||||
render_jsonrpc_error(request_id, -32600, t("mcp.errors.invalid_session_id"), status: :not_found)
|
||||
return false
|
||||
end
|
||||
@@ -214,6 +227,10 @@ class McpController < ApplicationController
|
||||
PROTOCOL_VERSION
|
||||
end
|
||||
|
||||
def valid_mcp_session?(session_id)
|
||||
Rails.cache.read(mcp_session_cache_key(session_id)) == mcp_user.id
|
||||
end
|
||||
|
||||
def mcp_session_cache_key(session_id)
|
||||
"mcp:session:#{session_id}"
|
||||
end
|
||||
|
||||
@@ -1066,6 +1066,7 @@ Rails.application.routes.draw do
|
||||
|
||||
# MCP server endpoint for external AI assistants (JSON-RPC 2.0)
|
||||
post "mcp", to: "mcp#handle"
|
||||
match "mcp", to: "mcp#method_not_allowed", via: %i[get delete]
|
||||
|
||||
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
||||
# Can be used by load balancers and uptime monitors to verify that the app is live.
|
||||
|
||||
@@ -280,6 +280,84 @@ class McpControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
# -- Unsupported methods --
|
||||
|
||||
test "GET /mcp returns 405 Method Not Allowed instead of 404" do
|
||||
with_mcp_env do
|
||||
get "/mcp", headers: mcp_headers(@token).merge("Accept" => "text/event-stream")
|
||||
|
||||
assert_response :method_not_allowed
|
||||
assert_equal "POST", response.headers["Allow"]
|
||||
end
|
||||
end
|
||||
|
||||
test "DELETE /mcp returns 405 Method Not Allowed instead of 404" do
|
||||
with_mcp_env do
|
||||
delete "/mcp", headers: mcp_headers(@token)
|
||||
|
||||
assert_response :method_not_allowed
|
||||
assert_equal "POST", response.headers["Allow"]
|
||||
end
|
||||
end
|
||||
|
||||
test "GET /mcp with an expired session id returns 404" do
|
||||
with_mcp_cache do
|
||||
with_mcp_env do
|
||||
session_id = initialize_mcp_session
|
||||
Rails.cache.delete("mcp:session:#{session_id}")
|
||||
|
||||
get "/mcp", headers: mcp_headers(@token).merge("Accept" => "text/event-stream", "Mcp-Session-Id" => session_id)
|
||||
|
||||
assert_response :not_found
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "DELETE /mcp with an expired session id returns 404" do
|
||||
with_mcp_cache do
|
||||
with_mcp_env do
|
||||
session_id = initialize_mcp_session
|
||||
Rails.cache.delete("mcp:session:#{session_id}")
|
||||
|
||||
delete "/mcp", headers: mcp_headers(@token).merge("Mcp-Session-Id" => session_id)
|
||||
|
||||
assert_response :not_found
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "GET /mcp with a valid session id returns 405" do
|
||||
with_mcp_cache do
|
||||
with_mcp_env do
|
||||
session_id = initialize_mcp_session
|
||||
|
||||
get "/mcp", headers: mcp_headers(@token).merge("Accept" => "text/event-stream", "Mcp-Session-Id" => session_id)
|
||||
|
||||
assert_response :method_not_allowed
|
||||
assert_equal "POST", response.headers["Allow"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "DELETE /mcp with a valid session id returns 405" do
|
||||
with_mcp_cache do
|
||||
with_mcp_env do
|
||||
session_id = initialize_mcp_session
|
||||
|
||||
delete "/mcp", headers: mcp_headers(@token).merge("Mcp-Session-Id" => session_id)
|
||||
|
||||
assert_response :method_not_allowed
|
||||
assert_equal "POST", response.headers["Allow"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "GET /mcp without a token still returns 401" do
|
||||
get "/mcp", headers: { "Accept" => "text/event-stream" }
|
||||
|
||||
assert_response :unauthorized
|
||||
end
|
||||
|
||||
# -- initialize --
|
||||
|
||||
test "initialize returns server info and capabilities" do
|
||||
@@ -758,6 +836,12 @@ class McpControllerTest < ActionDispatch::IntegrationTest
|
||||
}
|
||||
end
|
||||
|
||||
def initialize_mcp_session
|
||||
post "/mcp", params: jsonrpc_request("initialize").to_json, headers: mcp_headers(@token)
|
||||
assert_response :ok
|
||||
response.headers["Mcp-Session-Id"]
|
||||
end
|
||||
|
||||
def jsonrpc_request(method, params = {}, id: 1)
|
||||
{ jsonrpc: "2.0", id: id, method: method, params: params }
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user