The personal finance app for everyone (by everyone)
Ruby
fix(mcp): answer GET and DELETE /mcp with 405 Method Not Allowed (#4920)
Saim Shafiqued3e3206d518f
diff --git a/app/controllers/mcp_controller.rb b/app/controllers/mcp_controller.rbindex e89dd7f276..ee5fb5e918 100644--- a/app/controllers/mcp_controller.rb+++ b/app/controllers/mcp_controller.rb@@ -40,6 +40,19 @@ class McpController < ApplicationControllerrender 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+privatedef parse_request_body@@ -198,7 +211,7 @@ class McpController < ApplicationControllersession_id = mcp_request_header("Mcp-Session-Id").presencereturn 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 falseend@@ -214,6 +227,10 @@ class McpController < ApplicationControllerPROTOCOL_VERSIONend+ 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}"enddiff --git a/config/routes.rb b/config/routes.rbindex 40176a354b..3fa96361d7 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -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.diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rbindex abf6eec81b..71db85ecaa 100644--- a/test/controllers/mcp_controller_test.rb+++ b/test/controllers/mcp_controller_test.rb@@ -280,6 +280,84 @@ class McpControllerTest < ActionDispatch::IntegrationTestendend+ # -- 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