Merge pull request #252 from koic/fix_rails_controller_example_to_return_202_for_notifications

[Doc] Fix Rails controller example to return 202 for notifications
This commit is contained in:
Koichi ITO 2026-03-08 03:06:19 +09:00 • committed by GitHub
commit 8a97cebc33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -242,11 +242,12 @@ When added to a Rails controller on a route that handles POST requests, your ser
[Streamable HTTP](https://modelcontextprotocol.io/specification/latest/basic/transports#streamable-http) transport
requests.
You can use the `Server#handle_json` method to handle requests.
You can use `StreamableHTTPTransport#handle_request` to handle requests with proper HTTP
status codes (e.g., 202 Accepted for notifications).
```ruby
class ApplicationController < ActionController::Base
def index
class McpController < ActionController::Base
def create
server = MCP::Server.new(
name: "my_server",
title: "Example Server Display Name",
@ -256,7 +257,11 @@ class ApplicationController < ActionController::Base
prompts: [MyPrompt],
server_context: { user_id: current_user.id },
)
render(json: server.handle_json(request.body.read))
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
server.transport = transport
status, headers, body = transport.handle_request(request)
render(json: body.first, status: status, headers: headers)
end
end
```