Expand description
The MCP request loop: JSON-RPC lines in, JSON-RPC lines out, with every
tools/call running as its own task under its own deadline, so one call
that never returns can’t hold up the others
(docs/reports/ on #2614: a window call hung for 1800 s and every later call,
to any tool, hung behind it, for ten hours, until the agent restarted).
Each call runs on a thread of its own (driving the same async runtime), so a call stuck in blocking code, such as an OS window call into an app that has stopped answering, holds that thread and nothing else.
Calls to the same tool still run in the order they arrived (a type and a
read of the same shell mean something in order); calls to different tools
don’t wait for each other. A call that outlives its deadline is answered
with an error and given up on (and so is a call that is cancelled): its
future is dropped, which stops async work at its next await. A call stuck in
blocking code can’t be stopped, so its thread stays stuck, but the loop
doesn’t, and its tool’s queue moves on without it: the order holds among
the calls still being waited for.
Structs§
- Health 🔒
- What the loop is doing, for the heartbeat to srv (
mcp_health): the calls not yet answered, and whether the async runtime is still turning. - Session 🔒
- What the loop shares with the calls it starts.
- Thread
Count 🔒 - One call thread in
Health’s count, until it ends. - Tool
Queues 🔒 - The per-tool queues: calls to one tool run in arrival order.
Constants§
- DEFAULT_
DEADLINE 🔒 - MAX_
DEADLINE 🔒 - Claude Code gives up on a call that is silent for 1800 s. A tool is answered well before that.
- UI_
DEADLINE 🔒 - A window call (capture, look, click) takes seconds; a hung target window makes the OS call wait for it forever.
Traits§
- Tool
Runner 🔒 - Runs one
tools/call’sparams({name, arguments}).
Functions§
- answer_
for 🔒 - The answer to a call: its text, its error, why it gave none, or that it outlived
deadline(outcomeisNone). - call_
deadline 🔒 - How long
toolmay run withargumentsbefore the loop answers for it. The tools that wait on purpose (a shell, the user, a spawned agent) get their own wait, plus a margin; the rest get a few minutes. - error_
result 🔒 - initialize_
answer 🔒 - run_
in_ 🔒turn - Waits for the tool’s turn, then runs the call on a thread of its own. The outer error is why there was no answer from the call itself.
- serve 🔒
- Reads requests from
inputuntil it closes, answering each onoutput.deadline_ofsays how long each call may run (call_deadlinein production).initializeandtools/listare answered at once;tools/callruns throughrunneras a task.notifications/cancelledstops the call it names. Eachtools_changedwake-up tells the client the tool list changed, so it asks for it again: a client lists the tools once, when it connects, and never again unless told. - spawn_
writer 🔒 - One writer, so two answers never interleave on the pipe.
- tell_
tools_ 🔒changed - Sends
notifications/tools/list_changedeach timechangedis woken (MCP’s way to say the tools differ now; announced aslistChangedininitialize). - text_
result 🔒
Type Aliases§
- Call
Future 🔒 - Not
Send: a tool may hold an OS window handle across an await. The future is made and run on one thread (seeserve). - Call
Result 🔒 - What a tool call returns: the text for the model, or the error to show it.
- Running 🔒
- The calls running now, by request id, so a cancel can stop the one it names.