Ch 2 — Architecture Under the Hood

Connection lifecycle, message routing, client state machines, tool dispatch, and context aggregation internals
Under the Hood
-
Click play or press Space to begin...
Step- / 10
AClient Connection State MachineEvery client follows this lifecycle
1
power_settings_new
DisconnectedNo transport
initial state
connect
hourglass_top
InitializingHandshake in
progress
ready
2
check_circle
ConnectedNormal operation
requests flow
error/close
error
FailedTransport lost
or init rejected
3
arrow_downward Host startup: spawning clients from config
BHost Startup SequenceRead config → spawn transports → initialize each client
settings
Read ConfigParse mcp.json
server entries
for each
terminal
Spawn TransportStart process (stdio)
or open HTTP conn
then
4
handshake
InitializeHandshake + discover
tools/resources
5
arrow_downward Message routing: how the host dispatches tool calls
CMessage Routing & Tool DispatchLLM calls a tool → host finds the right client → forwards
psychology
LLMCalls tool:
search_issues
tool_call
route
Host RouterLookup: which client
owns this tool?
forward
6
swap_horiz
Client 1tools/call →
GitHub Server
7
arrow_downward Context aggregation: merging resources into LLM context
DContext AggregationHost merges tools + resources from all servers into one LLM prompt
list
Collect Toolstools/list from
every client
merge
merge
Unified ListDeduplicate, prefix
if name collisions
inject
psychology
LLM ContextSystem prompt with
all available tools
8
arrow_downward Handling server-initiated requests (reverse flow)
EServer → Client RequestsSampling, elicitation, and roots — the reverse direction
dns
ServerNeeds LLM help
sends sampling req
sampling
swap_horiz
ClientReceives request,
forwards to host
route
9
laptop
HostCalls LLM, may ask
user, returns result
10
arrow_downward Error handling and reconnection strategies
FError Handling & ReconnectionWhat happens when things go wrong
error
Server CrashTransport dies
client detects
then
refresh
ReconnectHost retries with
backoff strategy
or
do_not_disturb
DegradeMark tools unavailable
other servers continue
1
Detail