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

300.0 seconds timeout issue

Open masudhossain opened this issue 6 months ago • 2 comments

I'm not fairly sure if the issue is on the MCP server side or not, but i consistently get a timeout issue with no errors showing on our MCP server. The typical response I get with the error is below when i execute an LLM request using our MCP server on our rails app.

{
  "type": "mcp_tool_result",
  "content": [
    {
      "text": "An error occurred while executing the MCP tool: Timed out while waiting for response to ClientRequest. Waited 300.0 seconds.",
      "type": "text"
    }
  ],
  "is_error": true,
  "tool_use_id": "mcptoolu_01Mnr9euTnMRBWicLnSSAsbk"
}

Any guidance would be helpful. I've exhausted claude >.<

masudhossain avatar May 20 '26 17:05 masudhossain

I checked the current Ruby SDK path, and this error is more likely “the tool request never returned before the client's 300s budget” than a Ruby SDK exception being swallowed.

Relevant code path in this repo:

  • StreamableHTTPTransport#handle_regular_request dispatches the JSON-RPC request and keeps the POST open until Server#handle_json returns.
  • Server#call_tool then invokes the registered tool block synchronously via call_tool_with_args(...). If that block blocks, the MCP request blocks too.
  • There is no server-side per-tool timeout in this path by default. Exceptions should be wrapped as a JSON-RPC error; a clean 300s client timeout usually means no response was written.

A useful triage slice:

  1. Add explicit start/finish logging around the exact tool handler, including elapsed time and the arguments shape, not secrets:
server.define_tool(name: "your_tool", ...) do |args|
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  Rails.logger.info("MCP tool start name=your_tool")
  begin
    # existing tool code
  ensure
    elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
    Rails.logger.info("MCP tool finish name=your_tool elapsed=#{elapsed.round(2)}s")
  end
end
  1. If you see “start” but no “finish”, the hang is inside the tool code, not in tools/list or initialization. Wrap the slow section with a shorter timeout so the MCP client gets a real error before 300s:
Timeout.timeout(55) do
  # DB/API/agent call
end

Then return a normal MCP error response or raise a clear exception with the downstream operation name.

  1. Since this is Rails, specifically check for same-process self-calls. A common failure mode is: the MCP request occupies a Puma thread, the tool then calls back into the same Rails app over HTTP, but Puma/thread/database-pool capacity is exhausted, so the inner request can never run. Quick checks:
  • temporarily run Puma with multiple threads/workers and ensure RAILS_MAX_THREADS <= DB pool size;
  • replace any Net::HTTP call back to the same app with a direct service-object call;
  • log Thread.current.object_id and ActiveRecord checkout waits around the tool.
  1. If the tool legitimately needs longer than a couple minutes, don't keep one tools/call open. Return a job id quickly (status: "running") and add a second tool to poll/cancel the job. That avoids the 300s client boundary entirely and makes retries safe.

For this issue, the most helpful next data would be: transport (StreamableHTTPTransport vs stdio), whether the tool log gets past handler start, Puma thread/worker + DB pool settings, and whether the tool calls back into the same Rails server.

Baijack-star avatar May 21 '26 23:05 Baijack-star

This might be a timeout problem on Ai Api. npx ai-doctor can help check missing timeout guards and unprotected requests.

miguelmanlyx avatar May 21 '26 23:05 miguelmanlyx