Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Answer MCP notifications with 202 Accepted (#4780)

Josh Sanderse3d3eda3bffb
diff --git a/app/controllers/mcp_controller.rb b/app/controllers/mcp_controller.rb
index 2e191925b2..4591a6a9ca 100644
--- a/app/controllers/mcp_controller.rb
+++ b/app/controllers/mcp_controller.rb
@@ -27,9 +27,11 @@ class McpController < ApplicationController
request_id = body["id"]
- # JSON-RPC notifications omit the id field — server must not respond
+ # JSON-RPC notifications omit the id field, so there's nothing to answer. MCP's Streamable
+ # HTTP transport says a server that accepts one returns 202 Accepted with no body; proxies
+ # such as Pipelock's MCP reverse proxy reject any other status, which ends the handshake.
unless body.key?("id")
- return head(:no_content)
+ return head(:accepted)
end
result = dispatch_jsonrpc(request_id, body["method"], body["params"])
diff --git a/docs/hosting/mcp.md b/docs/hosting/mcp.md
index 24fa7b9d29..c0a332ca92 100644
--- a/docs/hosting/mcp.md
+++ b/docs/hosting/mcp.md
@@ -127,6 +127,10 @@ Sure implements the following JSON-RPC 2.0 methods:
| `tools/list` | Lists available financial tools with schemas |
| `tools/call` | Executes a tool with provided arguments |
+A JSON-RPC notification (a message without an `id`, such as `notifications/initialized`) gets
+`202 Accepted` with an empty body, as MCP's Streamable HTTP transport requires. A `tools/call`
+sent as a notification doesn't run.
+
### Available Tools
The MCP endpoint exposes the same tool registry used by Sure's built-in assistant. Clients should treat `tools/list` as the source of truth.
diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rb
index e66b24d3c6..d52f8f4ccd 100644
--- a/test/controllers/mcp_controller_test.rb
+++ b/test/controllers/mcp_controller_test.rb
@@ -222,13 +222,13 @@ class McpControllerTest < ActionDispatch::IntegrationTest
# -- Notifications (requests without id) --
- test "notifications receive no response body" do
+ test "notifications are accepted with 202 and no body, as MCP's Streamable HTTP transport requires" do
with_mcp_env do
post "/mcp", params: jsonrpc_notification("notifications/initialized").to_json,
headers: mcp_headers(@token)
- assert_response :no_content
- assert response.body.blank?, "Notification must not produce a response body"
+ assert_response :accepted
+ assert_equal "", response.body, "Notification must not produce a response body"
end
end
@@ -238,18 +238,34 @@ class McpControllerTest < ActionDispatch::IntegrationTest
post "/mcp", params: jsonrpc_notification("tools/call", { name: "get_balance_sheet", arguments: {} }).to_json,
headers: mcp_headers(@token)
- assert_response :no_content
- assert response.body.blank?, "Notification-style tools/call must not execute or respond"
+ assert_response :accepted
+ assert_equal "", response.body, "Notification-style tools/call must not execute or respond"
end
end
- test "unknown notification method still returns no content" do
+ test "an unknown notification method is accepted with no body too" do
with_mcp_env do
post "/mcp", params: jsonrpc_notification("notifications/unknown").to_json,
headers: mcp_headers(@token)
- assert_response :no_content
- assert response.body.blank?
+ assert_response :accepted
+ assert_equal "", response.body
+ end
+ end
+
+ test "the handshake's notifications/initialized, sent with the negotiated session, is accepted" do
+ with_mcp_cache do
+ with_mcp_env do
+ post "/mcp", params: jsonrpc_request("initialize").to_json, headers: mcp_headers(@token)
+ assert_response :ok
+ session_id = response.headers["Mcp-Session-Id"]
+
+ post "/mcp", params: jsonrpc_notification("notifications/initialized").to_json,
+ headers: mcp_headers(@token).merge("Mcp-Protocol-Version" => MCP_PROTOCOL_VERSION, "Mcp-Session-Id" => session_id)
+
+ assert_response :accepted
+ assert_equal "", response.body
+ end
end
end