ruby-sdk icon indicating copy to clipboard operation
ruby-sdk copied to clipboard

Rails controller example doesn't return 202 on notifications/initialized

Open bolmaster2 opened this issue 2 months ago • 2 comments

Problem

When using the Rails controller example in the README the notifications/initialized request returns 200 instead of 202.

It doesn't seem like a major thing but when adding the MCP server to Claude it's impossible to add it when the notifications/initialized request returns 200 instead of 202. When updating it to return 202 it works fine.

Proposed solution

Getting the status from the transport object instead of server.handle_json like this makes it work though, so the updated example would then look like this:

class ApplicationController < ActionController::Base
  def index
    server = MCP::Server.new(
      name: "my_server",
      title: "Example Server Display Name", # WARNING: This is a `Draft` and is not supported in the `Version 2025-06-18 (latest)` specification.
      version: "1.0.0",
      instructions: "Use the tools of this server as a last resort",
      tools: [SomeTool, AnotherTool],
      prompts: [MyPrompt],
      server_context: { user_id: current_user.id },
    )
    transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
    server.transport = transport
    response = transport.handle_request(request)
    status, headers, body = response
    json = JSON.parse(body.first) if body.present? && body.is_a?(Array)
    render(json:, status:, headers:)
  end
end

Let me know if you want me to create a PR for it!

bolmaster2 avatar Oct 09 '25 08:10 bolmaster2

On the other hand, it is also mentioned as follows:

When added to a Rails controller on a route that handles POST requests, your server will be compliant with non-streaming Streamable HTTP transport requests.

You can use the Server#handle_json method to handle requests.

https://github.com/modelcontextprotocol/ruby-sdk/tree/v0.4.0?tab=readme-ov-file#rails-controller

Therefore, the example shown in this issue appears to be one that uses StreamableHTTPTransport#handle_request, rather than Server#handle_json.

koic avatar Oct 20 '25 03:10 koic

Does this work with Claude?

class ApplicationController < ActionController::Base
  def index
    return head :accepted if params[:method] == 'notifications/initialized'

    # ... etc.

lawrencepit avatar Oct 22 '25 23:10 lawrencepit