fix(mcp): answer GET and DELETE /mcp with 405 Method Not Allowed

Merged
SX
20d ago
+103-1

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


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.

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.