300.0 seconds timeout issue
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 >.<
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_requestdispatches the JSON-RPC request and keeps the POST open untilServer#handle_jsonreturns.Server#call_toolthen invokes the registered tool block synchronously viacall_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:
- 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
- If you see “start” but no “finish”, the hang is inside the tool code, not in
tools/listor 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.
- 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::HTTPcall back to the same app with a direct service-object call; - log
Thread.current.object_idand ActiveRecord checkout waits around the tool.
- If the tool legitimately needs longer than a couple minutes, don't keep one
tools/callopen. 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.
This might be a timeout problem on Ai Api. npx ai-doctor can help check missing timeout guards and unprotected requests.