Skip to main content

Module serve

Module serve 

Source
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.
ThreadCount 🔒
One call thread in Health’s count, until it ends.
ToolQueues 🔒
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§

ToolRunner 🔒
Runs one tools/call’s params ({name, arguments}).

Functions§

answer_for 🔒
The answer to a call: its text, its error, why it gave none, or that it outlived deadline (outcome is None).
call_deadline 🔒
How long tool may run with arguments before 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 input until it closes, answering each on output. deadline_of says how long each call may run (call_deadline in production). initialize and tools/list are answered at once; tools/call runs through runner as a task. notifications/cancelled stops the call it names. Each tools_changed wake-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_changed each time changed is woken (MCP’s way to say the tools differ now; announced as listChanged in initialize).
text_result 🔒

Type Aliases§

CallFuture 🔒
Not Send: a tool may hold an OS window handle across an await. The future is made and run on one thread (see serve).
CallResult 🔒
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.