Skip to content

Commit 45e5be4

Browse files
committed
[Doc] Update Rails controller example in README.md
The following two points have been updated: ## 1. Use ActionController::API in README controller example `ActionController::Base` includes CSRF protection which rejects POST requests without an authenticity token. MCP clients do not send CSRF tokens, so the controller example should inherit from `ActionController::API` instead. ## 2. Use `stateless: true` for `StreamableHTTPTransport.new` The controller creates a new transport per request, so the session stored on the previous transport is lost. Without `stateless: true`, the second request with `Mcp-Session-Id` returns 404 because the new transport has an empty session map. To share sessions via `Mcp-Session-Id` across requests, there are two approaches. One is persisting the transport in a class variable. The other is mounting the transport as a Rack app via #263. Both approaches maintain sessions, so features that depend on `server_context` within the SDK (Progress, Sampling) work correctly. However, per-request user-specific context such as `server_context: { user_id: current_user.id }` cannot be passed since the server is shared across all requests.
1 parent 40b048b commit 45e5be4

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ You can use `StreamableHTTPTransport#handle_request` to handle requests with pro
526526
status codes (e.g., 202 Accepted for notifications).
527527

528528
```ruby
529-
class McpController < ActionController::Base
529+
class McpController < ActionController::API
530530
def create
531531
server = MCP::Server.new(
532532
name: "my_server",
@@ -537,7 +537,8 @@ class McpController < ActionController::Base
537537
prompts: [MyPrompt],
538538
server_context: { user_id: current_user.id },
539539
)
540-
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
540+
# Since the `MCP-Session-Id` is not shared across requests, `stateless: true` is set.
541+
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server, stateless: true)
541542
server.transport = transport
542543
status, headers, body = transport.handle_request(request)
543544

0 commit comments

Comments
 (0)