Part VIII · Entrypoints, Startup, SDK, and Remote Control
Chapter 23
Machine-Readable Modes: Headless CLI, Agent SDK, and Control Protocols
Layer: Layer 8 (Entrypoints, Startup, SDK & Remote Control)
Prerequisites: Chapter 08, Chapter 11, Chapter 15, Chapter 22
Primary Source Files:
- cli/print.ts — headless runner (
runHeadless), full-duplex streaming engine (runHeadlessStreaming), control-request dispatcher, and resume/replay orchestration- cli/structuredIO.ts — NDJSON line framer, bidirectional RPC multiplexer (
StructuredIO), and permission/hook/elicitation/SDK-MCP bridges- cli/ndjsonSafeStringify.ts — ECMA-262
U+2028/U+2029line-terminator sanitizer for NDJSON wire framing- utils/streamJsonStdoutGuard.ts — runtime
process.stdout.writeinterceptor diverting stray non-JSON output tostderr- entrypoints/agentSdkTypes.ts — public Agent SDK surface declarations and re-export hub
- entrypoints/sdk/coreSchemas.ts & entrypoints/sdk/coreTypes.ts — Zod single source of truth for serializable
SDKMessagewire types- entrypoints/sdk/controlSchemas.ts — Zod schemas for bidirectional
control_request/control_responseenvelopes- entrypoints/mcp.ts — inverted entrypoint exposing built-in tools as an MCP stdio server (
claude mcp serve)- services/mcp/SdkControlTransport.ts — MCP transport tunneling JSON-RPC over
mcp_messagecontrol frames- cli/remoteIO.ts —
StructuredIOsubclass bridging the control protocol onto remote WebSocket/SSE transports
Why This Layer Exists
In Chapter 22, main.tsx finishes startup and hits a fork in the road. Interactive users mount the Ink React tree (screens/REPL.tsx), where a human watches ANSI spinners, hits Shift+Tab to toggle permission modes, and clicks through permission dialogs. Non-interactive callers have none of that—no TTY, no React tree, no modal UI, and often another program sitting on both ends of stdin and stdout.
Those programmatic consumers span four very different workloads:
- One-shot shell pipes (
git diff | claude -p "summarize"), expecting plain text and a clean exit code. - Structured batch automation (
-p --output-format=json --json-schema=...), expecting a single machine-validated JSON payload. - Full-duplex Agent SDK hosts (the TypeScript and Python SDKs, VS Code, Desktop, and CI orchestrators) spawning Claude Code as a long-lived child process with
--input-format=stream-json --output-format=stream-json --verbose, exchanging multi-turn prompts, partial token streams, mid-turn permission prompts, host-side hooks, and in-process MCP tool calls over one pair of pipes. - Inverted tool hosting (
claude mcp serve), where another agent owns the LLM loop and wants to call Claude Code's workspace tools over standard MCP JSON-RPC.
The architectural insight of this layer is that workloads 1, 2, and 3 share a single streaming engine: runHeadlessStreaming() driven by StructuredIO. A plain string passed to -p is wrapped into a one-element AsyncIterable containing one JSON user line (getStructuredIO in cli/print.ts), then executed through the exact same pipeline as a multi-hour bidirectional SDK session. Only workload 4 steps outside QueryEngine entirely, mounting getTools() directly onto an MCP server.
We build this layer from the bytes up: wire hygiene first, then wire schemas, then full-duplex RPC multiplexing, then the headless coordinator in cli/print.ts, and finally the inverted MCP server.
1. The Wire Discipline: Keeping stdout Pure NDJSON
The Problem
When --output-format=stream-json is active, file descriptor 1 (stdout) is no longer a human terminal—it is a framed binary-adjacent RPC channel parsed line-by-line as NDJSON by the parent SDK process. Two production failure modes routinely destroy NDJSON streams in Node/Bun applications:
- Unicode line-terminator truncation (
gh-28405):JSON.stringifyemits rawU+2028(LINE SEPARATOR) andU+2029(PARAGRAPH SEPARATOR) inside strings, which is legal JSON per ECMA-404. Any receiver that splits lines using JavaScript line-terminator semantics (ECMA-262 §11.3:\n,\r,U+2028,U+2029) slices the JSON object in half mid-string. Even if the receiver skips malformed lines instead of crashing, the message is silently lost. - Stray
stdoutwrites: A single strayconsole.log, third-party library startup banner, or forgotten debug print anywhere in ~1,900 source files ornode_moduleslands onstdoutmid-stream and breaks the SDK client's JSON parser with no recovery path.
The Real Implementation
Two small, surgical modules eliminate both failure classes. First, every NDJSON serializer routes through ndjsonSafeStringify() instead of raw JSON.stringify:
// cli/ndjsonSafeStringify.ts:16-32
const JS_LINE_TERMINATORS = /\u2028|\u2029/g
function escapeJsLineTerminators(json: string): string {
return json.replace(JS_LINE_TERMINATORS, c =>
c === '\u2028' ? '\\u2028' : '\\u2029',
)
}
export function ndjsonSafeStringify(value: unknown): string {
return escapeJsLineTerminators(jsonStringify(value))
}
A single alternating regex avoids scanning large serialized tool outputs twice; the \u2028/\u2029 escape sequences parse back to identical strings under every JSON parser while being invisible to every line splitter.
Second, before runHeadless() emits a single message in stream-json mode (cli/print.ts:594–596), it installs a runtime gate on process.stdout.write:
// utils/streamJsonStdoutGuard.ts:59-91
process.stdout.write = function (
chunk: string | Uint8Array,
encodingOrCb?: BufferEncoding | ((err?: Error) => void),
cb?: (err?: Error) => void,
): boolean {
const text =
typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf-8')
buffer += text
let newlineIdx: number
let wrote = true
while ((newlineIdx = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, newlineIdx)
buffer = buffer.slice(newlineIdx + 1)
if (isJsonLine(line)) {
wrote = originalWrite!(line + '\n')
} else {
process.stderr.write(`${STDOUT_GUARD_MARKER} ${line}\n`)
logForDebugging(
`streamJsonStdoutGuard diverted non-JSON stdout line: ${line.slice(0, 200)}`,
)
}
}
const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb
if (callback) {
queueMicrotask(() => callback())
}
return wrote
} as typeof process.stdout.write
isJsonLine() (utils/streamJsonStdoutGuard.ts:14–26) treats empty lines as valid (blank separators are tolerated in NDJSON) and tests every non-empty complete line with JSON.parse(line). Anything that fails to parse is diverted to stderr prefixed with STDOUT_GUARD_MARKER ('[stdout-guard]') so it stays debuggable in logs without poisoning the wire.
Hidden Invariants & Failure Modes
- Shutdown flush:
installStreamJsonStdoutGuard()registers a cleanup hook viaregisterCleanup()(utils/streamJsonStdoutGuard.ts:93–109). Any partial line sitting inbufferat exit without a trailing\nis flushed tostdoutif valid JSON, or diverted tostderrwith[stdout-guard]if it is a fragment, before restoringoriginalWrite. - Callback liveness: Notice lines 86–89: even when a write is diverted to
stderr, the caller's completion callback still fires viaqueueMicrotask(() => callback()). Skipping the callback would deadlock callers awaiting stream drain. - One extra
JSON.parseper frame: The blessed path (structuredIO.write→writeToStdout→process.stdout.write) always emitsndjsonSafeStringify(msg) + '\n', so it passes straight through—at the deliberate cost of oneJSON.parsevalidation pass per outbound message instream-jsonmode. - Non-TTY
stdinhygiene (main.tsx:857–883): On the input side,getInputPrompt()guards against two classic pipe traps. First, it checks!process.argv.includes('mcp')before touchingprocess.stdin—reading stdin during startup would steal bytes from MCP's JSON-RPC framing. Second, intextinput mode with a non-TTYstdin, it waits at most 3 seconds viapeekForStdinData(process.stdin, 3000)before warning onstderrand proceeding, preventing silent hangs when a parent spawns Claude Code with an inherited, open, silent pipe.
2. Schema-First Contracts and the Snapshot Boundary
The Problem
At least five independent codebases consume Claude Code's NDJSON wire format: the CLI itself, the TypeScript Agent SDK (@anthropic-ai/claude-agent-sdk), the Python SDK, the VS Code extension, and the claude.ai remote bridge. Hand-maintaining duplicate type declarations and runtime validators across those boundaries guarantees schema drift—especially across 24+ message types and 20+ control request subtypes.
The Real Implementation
The repository treats Zod v4 schemas in entrypoints/sdk/ as the single source of truth:
- entrypoints/sdk/coreSchemas.ts (1,889 lines) defines every serializable SDK data type (
ModelUsageSchema,PermissionUpdateSchema,PermissionResultSchema,HookInputSchema,SDKResultSuccessSchema,SDKResultErrorSchema,SDKSystemMessageSchema,SDKPartialAssistantMessageSchema,SDKSessionStateChangedMessageSchema, and the 24-variantSDKMessageSchemaunion at lines 1854–1881). Every schema is wrapped inlazySchema()(Chapter 01) so importing schema modules incurs zero startup allocation. - entrypoints/sdk/controlSchemas.ts (663 lines) defines the bidirectional control protocol layered on top of
coreSchemas.ts, culminating in the two aggregate wire schemas:
// entrypoints/sdk/controlSchemas.ts:642-663
export const StdoutMessageSchema = lazySchema(() =>
z.union([
SDKMessageSchema(),
SDKStreamlinedTextMessageSchema(),
SDKStreamlinedToolUseSummaryMessageSchema(),
SDKPostTurnSummaryMessageSchema(),
SDKControlResponseSchema(),
SDKControlRequestSchema(),
SDKControlCancelRequestSchema(),
SDKKeepAliveMessageSchema(),
]),
)
export const StdinMessageSchema = lazySchema(() =>
z.union([
SDKUserMessageSchema(),
SDKControlRequestSchema(),
SDKControlResponseSchema(),
SDKKeepAliveMessageSchema(),
SDKUpdateEnvironmentVariablesMessageSchema(),
]),
)
- entrypoints/sdk/coreTypes.ts re-exports generated TypeScript types derived from
coreSchemas.tsviabun scripts/generate-sdk-types.ts, alongside runtimeconstarrays (HOOK_EVENTS—27 lifecycle events—andEXIT_REASONS). - entrypoints/agentSdkTypes.ts is the public SDK declaration barrel: it re-exports core types, runtime callback interfaces, generated settings types, tool types, and
@alphacontrol types (SDKControlRequest,SDKControlResponse). Its exported functions (query(),tool(),createSdkMcpServer(), the@alphaV2 session APIunstable_v2_createSession/resumeSession/prompt, transcript utilities likelistSessions/forkSession, and@internaldaemon helperswatchScheduledTasks/connectRemoteControl) all throw'not implemented'stubs here—this file is the type-checked contract against which the external SDK package is built and published.
Snapshot Absences in
entrypoints/sdk/: This source checkout containscoreSchemas.ts,controlSchemas.ts,coreTypes.ts,entrypoints/agentSdkTypes.ts, andentrypoints/sandboxTypes.ts, but six imported sibling modules are absent from the snapshot:
entrypoints/sdk/coreTypes.generated.ts(generated fromcoreSchemas.ts)entrypoints/sdk/controlTypes.ts(TypeScript types corresponding tocontrolSchemas.ts)entrypoints/sdk/runtimeTypes.ts(non-serializable SDK callback andQuery/SDKSessioninterfaces)entrypoints/sdk/settingsTypes.generated.ts(generated from settings JSON schema)entrypoints/sdk/toolTypes.ts(@internalper-tool input/output types)entrypoints/sdk/sdkUtilityTypes.ts(NonNullableUsage)Because
coreSchemas.tsandcontrolSchemas.tsare present in full, every serializable wire type is directly inspectable, and the missing runtime/control TypeScript interfaces are unambiguously observed at call sites incli/structuredIO.ts,cli/print.ts, andentrypoints/agentSdkTypes.ts.
Hidden Invariants & Failure Modes
- A second hidden code generator (Zod → Protobuf): Look at the comment on
SDKControlGetSettingsResponseSchemain entrypoints/sdk/controlSchemas.ts:506–510:// String levels only — numeric effort is ant-only and the Zod→proto generator can't emit enum∪number unions.The Zod schemas aren't only generating TypeScript.d.tsfiles; they also feed a Protobuf generator for web/mobile clients, constraining schema authors away from heterogeneous unions. StdinMessageSchemavs. runtimeprocessLine(): WhileStdinMessageSchemalists five inbound message types,StructuredIO.processLine()(cli/structuredIO.ts:431–450) also acceptsassistantandsystemmessages onstdinso the remote bridge can inject prior conversation history directly intomutableMessagesduring replay (cli/print.ts:4042–4052).- Internal control subtypes beyond
controlSchemas.ts:controlSchemas.tsspecifies 21 public/builderSDKControlRequestInnerSchemasubtypes, whilecli/print.tsdispatches additional internal/IDE subtypes (end_session,channel_enable,mcp_authenticate,mcp_oauth_callback_url,claude_authenticate,claude_oauth_callback,claude_oauth_wait_for_completion,mcp_clear_auth,generate_session_title,side_question,set_proactive,remote_control). Any unknown subtype gets an explicitcontrol_responseerror (cli/print.ts:4021–4028) so callers never hang waiting for an unrecognized request.
3. StructuredIO: Full-Duplex RPC Multiplexing Over stdin/stdout
The Problem
A single pair of unidirectional pipes (stdin in, stdout out) has to carry two asymmetric protocols simultaneously:
- Event streaming: User turns flow in;
assistantmessages,stream_eventdeltas,tool_progress,systemevents, andresultsummaries flow out. - Bidirectional RPC:
- Host → CLI: The host sends
control_requestframes (initialize,interrupt,set_model,set_permission_mode,rewind_files, etc.) and awaits matchingcontrol_responseframes—even while a turn is blocked mid-tool. - CLI → Host: Mid-turn, Claude Code sends blocking
control_requestframes (can_use_tool,hook_callback,mcp_message,elicitation) and must park the calling tool coroutine until acontrol_responsewith the matchingrequest_idarrives onstdin—even while other concurrent tools are also blocked on their own prompts.
- Host → CLI: The host sends
Without careful framing, two nasty bugs appear immediately:
- Reordering: If
sendRequest()writes directly tostdoutwhileprint.tsdrainsQueryEnginethrough a separate queue, acan_use_toolpermission prompt can overtake theassistantmessage that announced thetool_useblock, confusing host UIs. - Duplicate / orphaned
control_responsecorruption: If a WebSocket reconnect or worker restart redelivers an already-handledcan_use_toolresponse after itspendingRequestsentry was removed, treating it as a new orphaned approval re-executes the tool and appends duplicatetool_useIDs intomutableMessages—causing an unrecoverable Anthropic API400 "tool_use ids must be unique"error.
The Real Implementation
StructuredIO (cli/structuredIO.ts:135–774) solves multiplexing with four interlocking primitives.
1. One Shared Outbound FIFO (outbound: Stream<StdoutMessage>)
// cli/structuredIO.ts:160-162
// sendRequest() and print.ts both enqueue here; the drain loop is the
// only writer. Prevents control_request from overtaking queued stream_events.
readonly outbound = new Stream<StdoutMessage>()
Stream<T> (utils/stream.ts:1–76) is a minimal single-consumer async FIFO queue (enqueue(), done(), error()). Both StructuredIO.sendRequest() and runHeadlessStreaming() enqueue into structuredIO.outbound, and runHeadless()'s outer for await loop is the only code that calls structuredIO.write(). Every stream_event, assistant frame, control_request, and control_response hits stdout in strict causal order.
2. Line Framing, prependedLines, and Inline Response Interception
StructuredIO.read() (cli/structuredIO.ts:215–261) splits chunks from this.input on \n, calling processLine() on each line. Notice the subtle prependedLines mechanism (cli/structuredIO.ts:204–242): SessionStart hooks (takeInitialUserMessage()) and SDK-defined --agent initialPrompt values can call structuredIO.prependUserMessage(text) before iteration starts or mid-stream. splitAndProcess() checks this.prependedLines on every iteration of its inner for (;;) loop, so a prepended turn lands ahead of the next message even when two messages arrived in the same read buffer. When this.input reaches EOF, read() sets this.inputClosed = true and rejects every promise still waiting in this.pendingRequests with 'Tool permission stream closed before response received'.
Inside processLine() (cli/structuredIO.ts:333–463), incoming frames are classified before yielding to cli/print.ts:
// cli/structuredIO.ts:341-429 (abridged for clarity)
const message = normalizeControlMessageKeys(jsonParse(line)) as
| StdinMessage
| SDKMessage
if (message.type === 'keep_alive') {
return undefined
}
if (message.type === 'update_environment_variables') {
for (const [key, value] of Object.entries(message.variables)) {
process.env[key] = value
}
return undefined
}
if (message.type === 'control_response') {
// ... notifyCommandLifecycle(uuid, 'completed') ...
const request = this.pendingRequests.get(message.response.request_id)
if (!request) {
const responsePayload =
message.response.subtype === 'success'
? message.response.response
: undefined
const toolUseID = responsePayload?.toolUseID
if (
typeof toolUseID === 'string' &&
this.resolvedToolUseIds.has(toolUseID)
) {
return undefined // Duplicate delivery — do not re-process as orphan!
}
if (this.unexpectedResponseCallback) {
await this.unexpectedResponseCallback(message)
}
return undefined
}
this.trackResolvedToolUseId(request.request)
this.pendingRequests.delete(message.response.request_id)
if (
request.request.request.subtype === 'can_use_tool' &&
this.onControlRequestResolved
) {
this.onControlRequestResolved(message.response.request_id)
}
if (message.response.subtype === 'error') {
request.reject(new Error(message.response.error))
return undefined
}
// Zod-validate response payload against request.schema, then resolve
// Yield message only if this.replayUserMessages is enabled
}
Two real battle scars sit right at the top of processLine():
normalizeControlMessageKeys()(utils/controlMessageCompat.ts:13–32): Older iOS app builds lacked a SwiftCodingKeysmapping and emitted camelCaserequestIdinstead of snake_caserequest_idon both top-level control requests and nestedresponseobjects. Without this in-place normalizer,structuredIO.tsreadmessage.response.request_idasundefinedand silently dropped mobile permission approvals.update_environment_variables: Mutatesprocess.envdirectly inside the CLI process—not just child shells—because bridge session runners use it to rotateCLAUDE_CODE_SESSION_ACCESS_TOKENin-flight for the running agent itself.
3. Five Outbound RPC Bridges Built on sendRequest()
sendRequest<Response>() (cli/structuredIO.ts:469–531) generates a requestId = randomUUID(), enqueues { type: 'control_request', request_id, request } onto this.outbound, stores { resolve, reject, schema, request } in this.pendingRequests, and attaches a one-shot AbortSignal listener. If the signal aborts, it enqueues { type: 'control_cancel_request', request_id }, records the tool_use_id in resolvedToolUseIds before rejecting (so a late reply from the host won't trip the orphan handler), and rejects immediately with AbortError without waiting for host acknowledgment.
All five CLI→Host RPC capabilities build directly on sendRequest():
- Tool Permissions (
createCanUseTool, cli/structuredIO.ts:533–659): First evaluates local rules and modes viahasPermissionsToUseTool()(Chapter 11). Only when the local engine returns'ask'doesStructuredIOgo over the wire—and it races localPermissionRequesthooks against the remote SDK prompt in parallel:
// cli/structuredIO.ts:576-638 (abridged)
const hookPromise = executePermissionRequestHooksForSDK(
tool.name, toolUseID, input, toolUseContext, mainPermissionResult.suggestions,
).then(decision => ({ source: 'hook' as const, decision }))
const requestId = randomUUID()
onPermissionPrompt?.(buildRequiresActionDetails(tool, input, toolUseID, requestId))
const sdkPromise = this.sendRequest<PermissionToolOutput>(
{
subtype: 'can_use_tool',
tool_name: tool.name,
input,
permission_suggestions: mainPermissionResult.suggestions,
blocked_path: mainPermissionResult.blockedPath,
decision_reason: serializeDecisionReason(mainPermissionResult.decisionReason),
tool_use_id: toolUseID,
agent_id: toolUseContext.agentId,
},
permissionToolOutputSchema(),
hookAbortController.signal,
requestId,
).then(result => ({ source: 'sdk' as const, result }))
const winner = await Promise.race([hookPromise, sdkPromise])
if (winner.source === 'hook') {
if (winner.decision) {
sdkPromise.catch(() => {})
hookAbortController.abort() // emits control_cancel_request to dismiss host UI
return winner.decision
}
const sdkResult = await sdkPromise
return permissionPromptToolResultToPermissionDecision(
sdkResult.result, tool, input, toolUseContext,
)
}
return permissionPromptToolResultToPermissionDecision(
winner.result, tool, input, toolUseContext,
)
Why race instead of awaiting hooks first? A slow PermissionRequest hook (e.g., an external approval service taking 20 seconds) would freeze the VS Code or Desktop permission dialog. Racing lets the host show the prompt immediately and dismiss it via control_cancel_request if the hook decides first.
When the SDK host responds, permissionPromptToolResultToPermissionDecision() (utils/permissions/PermissionPromptToolResultSchema.ts:84–127) applies and persists any updatedPermissions, aborts toolUseContext.abortController if behavior === 'deny' && result.interrupt, and embeds two production scars:
- Mobile empty-object fallback (lines 107–111): Mobile push notifications don't carry the full tool input payload, so they send
updatedInput: {}to satisfy the schema. An emptyupdatedInputobject is silently replaced with the originalinputso the tool isn't invoked with stripped arguments. - Lenient telemetry/permission-update parsing (lines 37–62,
#29440): BothdecisionClassificationandupdatedPermissionsuse Zod.catch(undefined)so a malformed permission suggestion from an older SDK host degrades gracefully instead of rejecting an entireallowdecision.
- Sandbox Network Approval (
createSandboxAskCallback, cli/structuredIO.ts:731–753): Piggybacks OS-level sandbox network prompts onto the existingcan_use_toolprotocol using a synthetic tool name:SANDBOX_NETWORK_ACCESS_TOOL_NAME = 'SandboxNetworkAccess'. SDK hosts already know how to rendercan_use_tool, so they get network approval dialogs with zero new protocol subtypes. Failure or stream closure fails closed (return false). - Host-Side Hooks (
createHookCallback, cli/structuredIO.ts:661–689): When the host sendshooksininitialize, eachcallbackIdis wrapped in aHookCallback(type: 'callback') that callssendRequest({ subtype: 'hook_callback', callback_id, input, tool_use_id }, hookJSONOutputSchema(), abort). Transport errors log tostderrand fail open to{}. - MCP Elicitations (
handleElicitation, cli/structuredIO.ts:694–721): Wired into connected non-SDK MCP clients byregisterElicitationHandlers()(cli/print.ts:1263–1387). LocalElicitationhooks get first crack; if none respond,structuredIO.handleElicitation()emits{ subtype: 'elicitation', mcp_server_name, message, mode, url, elicitation_id, requested_schema }validated againstSDKControlElicitationResponseSchema(), then passes the result throughrunElicitationResultHooks(). Transport failures return{ action: 'cancel' }. URL-mode completion notifications (ElicitationCompleteNotificationSchema) are surfaced outbound assystem/elicitation_completeSDK events. - In-Process SDK MCP Tunneling (
sendMcpMessage, cli/structuredIO.ts:758–773 + services/mcp/SdkControlTransport.ts): When an SDK caller creates an in-process MCP server viacreateSdkMcpServer()(living inside the host's own Node/Python process rather than a separate child binary), the CLI connects to it usingSdkControlClientTransport. Every JSON-RPC request from the CLI's MCP client is wrapped bysendMcpMessage()into{ subtype: 'mcp_message', server_name, message }, shipped overstdout, handled by the host'sSdkControlServerTransport, and returned in{ mcp_response }. Unsolicited MCP server notifications travel the opposite direction as host→CLImcp_messagecontrol requests, whichcli/print.ts:2979–2994injects intosdkClient.client.transport.onmessage.
Hidden Invariants & Failure Modes
Bounded Deduplication Against Orphaned Permission Replay:
resolvedToolUseIdsis capped atMAX_RESOLVED_TOOL_USE_IDS = 1000(cli/structuredIO.ts:133, 176–187), evicting the oldest entry viaSetinsertion order. It is updated on all three resolution paths: normalcontrol_response, bridgeinjectControlResponse(), andAbortSignalcancellation. Together withhandledOrphanedToolUseIdsincli/print.ts:2766, 5272, this guarantees a redeliveredcontrol_responsenever triggers duplicate tool execution. Meanwhile, a genuine orphan (e.g., user clicked Approve onclaude.aiafter the worker restarted and lost its in-memorypendingRequestsmap) falls throughunexpectedResponseCallback→handleOrphanedPermissionResponse()(cli/print.ts:5241–5304), locates the unresolvedtool_useblock in the persisted transcript viafindUnresolvedToolUse(toolUseID), and enqueues a syntheticmode: 'orphaned-permission'command to complete the turn.
- Concurrent permission state transition (
cli/structuredIO.ts:653–655): IncreateCanUseTool()'sfinallyblock,notifySessionStateChanged('running')fires only ifthis.getPendingPermissionRequests().length === 0. Because parallel tool execution (Chapter 06) can have multiplecan_use_toolprompts in flight at once, prematurely emitting'running'while another prompt is still open would hide the host's remaining approval UI. - Dual-host permission race (
injectControlResponse,cli/structuredIO.ts:283–331): When a local SDK session activatesremote_control,onControlRequestSentmirrors everycan_use_toolrequest to theclaude.aibridge. If the remote web/mobile user answers first,injectControlResponse()resolves the local promise and sendscontrol_cancel_requestonstdoutso the local SDK host'scanUseToolcallback aborts cleanly; if the local SDK host answers first,onControlRequestResolvedtells the bridge to cancel the remote prompt.
4. cli/print.ts: Driving QueryEngine Across Text, JSON, and stream-json
The Problem
Headless execution looks deceptively simple until you remove the React REPL. Every concern that lived in React hooks in screens/REPL.tsx (useSettingsChange, useManageMCPConnections, useQueueProcessor, useInboxPoller, useScheduledTasks, useReplBridge) still has to work in a 5,594-line non-React module—while simultaneously supporting three output formats, two input formats, structured JSON output enforcement, background agent completion hold-back, session resume/rewind, and ~30 control request subtypes.
The Real Implementation
Step 1: Mode Validation and Unified Stream Normalization
Before calling runHeadless(), main.tsx:1237–1243 and 1818–1861 enforce the CLI flag invariants:
--sdk-urlautomatically sets both--input-format=stream-jsonand--output-format=stream-json.--input-format=stream-jsonrequires--output-format=stream-json;--replay-user-messagesrequires both;--include-partial-messages(or envCLAUDE_CODE_INCLUDE_PARTIAL_MESSAGES) requires--printand--output-format=stream-json.- Inside
runHeadless()(cli/print.ts:787–793),--output-format=stream-jsonadditionally requires--verbose.
Then getStructuredIO() (cli/print.ts:5199–5233) collapses single-shot and streaming inputs into one abstraction:
// cli/print.ts:5199-5233
function getStructuredIO(
inputPrompt: string | AsyncIterable<string>,
options: {
sdkUrl: string | undefined
replayUserMessages?: boolean
},
): StructuredIO {
let inputStream: AsyncIterable<string>
if (typeof inputPrompt === 'string') {
if (inputPrompt.trim() !== '') {
inputStream = fromArray([
jsonStringify({
type: 'user',
session_id: '',
message: { role: 'user', content: inputPrompt },
parent_tool_use_id: null,
} satisfies SDKUserMessage),
])
} else {
inputStream = fromArray([])
}
} else {
inputStream = inputPrompt
}
return options.sdkUrl
? new RemoteIO(options.sdkUrl, inputStream, options.replayUserMessages)
: new StructuredIO(inputStream, options.replayUserMessages)
}
Step 2: runHeadless() — Process Setup and Output-Format Dispatch
runHeadless() (cli/print.ts:455–974) prepares the non-React runtime before draining runHeadlessStreaming():
- Headless subscriptions & GC: Subscribes directly to
settingsChangeDetector(lines 520–532), triggersdownloadUserSettings()iffeature('DOWNLOAD_USER_SETTINGS')in remote mode, initializes GrowthBook (void initializeGrowthBook()), and startssetInterval(Bun.gc, 1000).unref()under Bun to keep memory bounded in long-lived workers. - Guard, sandbox, and hook events: Installs
installStreamJsonStdoutGuard()forstream-json; initializesSandboxManager.initialize(structuredIO.createSandboxAskCallback())(or exits ifisSandboxRequired()and unavailable,#34044); and registersregisterHookEventHandler()to emithook_started/hook_progress/hook_responseNDJSON frames whenstream-json+verbose. - Resume / Rewind (
loadInitialMessages,cli/print.ts:4893–5197): Loads prior history for--continue,--teleport(checked againstisPolicyAllowed('allow_remote_sessions')), or--resume <uuid|.jsonl|url>(URL resume is[ANT-ONLY]; hydrates remote logs underCLAUDE_CODE_USE_CCR_V2orENABLE_SESSION_PERSISTENCE). Supports--resume-session-at <uuid>(slices loaded messages toindex + 1),--fork-session(keeps a fresh session ID), and standalone--rewind-files <user-uuid>(rewinds disk viahandleRewindFiles()and exits immediately). - Output format switching (
cli/print.ts:851–973): Consumes everyStdoutMessageyielded byrunHeadlessStreaming():stream-json: Writes each frame immediately viaawait structuredIO.write(message). Whenfeature('STREAMLINED_OUTPUT')is compiled in andCLAUDE_CODE_STREAMLINED_OUTPUT=true, frames pass throughcreateStreamlinedTransformer()(utils/streamlinedTransform.ts:130–193), a "distillation-resistant" filter that strips thinking blocks, system/init metadata, and raw tool calls, replacing consecutive tool-only turns with cumulative counts (streamlined_tool_use_summary, e.g.,"Searched 2 patterns, read 3 files") and emitting text asstreamlined_textwhile preservingresult.json: Only accumulatesmessages[]whenoptions.outputFormat === 'json' && options.verbose(needsFullArray), avoiding O(session) RAM retention intextandstream-jsonmodes. At stream end, writes eitherjsonStringify(messages)(--verbose) orjsonStringify(lastMessage).text(default): WriteslastMessage.resulton'success', or a human-readable diagnostic for'error_during_execution','error_max_turns','error_max_budget_usd', or'error_max_structured_output_retries'.- Exit code & cleanup: Drains pending background memory extraction (
feature('EXTRACT_MEMORIES') && isExtractModeActive()), then callsgracefulShutdownSync(lastMessage?.type === 'result' && lastMessage.is_error ? 1 : 0). Note lines 892–909:lastMessageexplicitly ignorescontrol_*,stream_event,session_state_changed,task_*,post_turn_summary, andprompt_suggestionso late tail events afterresultnever clobber the exit code.
Step 3: runHeadlessStreaming() — Dual-Loop Concurrency Over QueryEngine
Inside runHeadlessStreaming() (cli/print.ts:976–4143), two asynchronous loops run concurrently against messageQueueManager and output = structuredIO.outbound:
-
The Inbound Reader IIFE (
cli/print.ts:2813–4140): Iteratesfor await (const message of structuredIO.structuredInput):control_request: Handled inline without waiting for an active turn to finish. Long-running auxiliary requests (generate_session_title,side_question) spawn fire-and-forget IIFEs (lines 3783–3874) specifically so their Haiku/forked-agent API round-trips don't block subsequentinterruptframes onstdin.user: Marksinitialized = true, deduplicatesmessage.uuidagainst both disk (doesMessageExistInSession) and an in-memory 10,000-entry ring (receivedMessageUuids, emitting anisReplay: trueack for duplicates ifreplayUserMessagesis set), resolves any web-composerfile_attachments, enqueues apromptcommand, and callsvoid run().assistant/system: History replay from bridge; converts viatoInternalMessages()and pushes straight intomutableMessages.control_response: Only arrives here whenreplayUserMessagesis enabled; re-enqueued ontooutputfor logging/UI parity.- Stream close (
lines 4124–4139): SetsinputClosed = true, stops thecronScheduler(iffeature('AGENT_TRIGGERS')), and—if!running—waits up to 5 seconds for any in-flightprompt_suggestion, finalizes pending async hooks, removes listeners, and closesoutput.done().
-
The Turn Consumer
run()(cli/print.ts:1865–2681): Guarded by a boolean mutex (if (running) return; running = true).- Ensures SDK MCP servers are synced (
updateSdkMcp()) and optionally awaits synchronous plugin installation (CLAUDE_CODE_SYNC_PLUGIN_INSTALL, raced againstCLAUDE_CODE_SYNC_PLUGIN_INSTALL_TIMEOUT_MS). - Enters a
do { ... } while (waitingForAgents)loop callingdrainCommandQueue():- Dequeues main-thread commands (
cmd.agentId === undefined). Consecutivepromptcommands with matchingworkloadandisMetacoalesce viacanBatchWith()+joinPromptValues()(cli/print.ts:428–453, 1949–1982) into oneask()call; whenreplayUserMessagesis enabled, merged intermediate UUIDs still receive syntheticSDKUserMessageReplayacks so hosts tracking per-UUID delivery don't stall. - Calls
ask()(QueryEngine.ts:1185–1295), which constructs a freshQueryEnginesharingmutableMessagesby reference and cloningreadFileState, yieldsengine.submitMessage(prompt, { uuid, isMeta }), and writes backengine.getReadFileState()infinally. - Streams yielded
SDKMessagerecords intooutput, flushingdrainSdkEvents()before every message andresult.
- Dequeues main-thread commands (
- Ensures SDK MCP servers are synced (
Step 4: The Control Protocol Surface
The inbound reader dispatches the full host→CLI control grammar. Grouping the handlers reveals how an external host treats Claude Code as a controllable stateful engine:
| Category | Subtypes (request.subtype) |
Key Implementation Details & Scars |
|---|---|---|
| Lifecycle | initialize, interrupt, end_session |
initialize accepts systemPrompt, appendSystemPrompt, agents, and jsonSchema over stdin to avoid OS ARG_MAX limits (cli/print.ts:4369–4384), registers SDK hook callbacks, and returns commands, agents, models, account info, output styles, and pid. Re-initializing returns an error carrying pending_permission_requests. end_session aborts and breaks the stdin loop. |
| Runtime Config | set_permission_mode, set_model, set_max_thinking_tokens, apply_flag_settings, get_settings |
set_permission_mode enforces policy/launch gates for bypassPermissions and feature('TRANSCRIPT_CLASSIFIER') auto (cli/print.ts:4568–4642). set_model updates setMainLoopModelOverride() and injects model-switch breadcrumbs. apply_flag_settings converts JSON null values to key deletions, fans out via settingsChangeDetector.notifyChange('flagSettings') (post-#20625 cache reset), and syncs model override (lines 3700–3755). |
| History & State | rewind_files, seed_read_state, cancel_async_message, get_context_usage, stop_task |
rewind_files supports dry_run diff stats via fileHistory.ts. cancel_async_message removes an unstarted prompt from messageQueueManager by UUID. seed_read_state restores readFileState entries removed by snip/compaction (see invariant below). |
| MCP & Plugins | mcp_status, mcp_message, mcp_set_servers, mcp_reconnect, mcp_toggle, reload_plugins, plus internal IDE OAuth/channel subtypes (mcp_authenticate, mcp_oauth_callback_url, mcp_clear_auth, claude_authenticate, channel_enable) |
mcp_set_servers serializes mutations through a promise chain (mcpChangesPromise) and sends control_response before calling void updateSdkMcp() (lines 3059–3064)—otherwise connecting new SDK MCP servers over stdout deadlocks while the host is still awaiting mcp_set_servers! |
| Remote & Auxiliary | remote_control, side_question, generate_session_title, set_proactive (PROACTIVE/KAIROS) |
side_question forks from getLastCacheSafeParams() (swapping in a fresh AbortController if the prior turn was aborted!) for prompt-cache hits (lines 3815–3874). remote_control dynamically imports initReplBridge (lines 3892–4020). |
Step 5: Partial Messages, Structured Output, and Permission Delegation
Three headless features cross the boundary between cli/print.ts and QueryEngine.ts:
- Partial Messages (
--include-partial-messages): When enabled,QueryEngine.ts:818–826wraps every raw Anthropic SSEstream_eventinto{ type: 'stream_event', event, parent_tool_use_id: null, uuid, session_id }, letting SDK UIs render token-by-token deltas without waiting for completeassistantblocks. - Structured Output (
--json-schemaorinitialize.jsonSchema): Rather than relying on raw constrained decoding alone, the harness injects a synthetic built-in tool named'StructuredOutput'(createSyntheticOutputTool()in tools/SyntheticOutputTool/SyntheticOutputTool.ts:20–120, cached in aWeakMap<object, CreateResult>so repeated workflow invocations skip ~1.4 ms of Ajv JIT compilation). It is appended aftergetTools()filtering (main.tsx:1885–1892for CLI flags;buildAllTools()incli/print.ts:1492–1498forinitialize.jsonSchema).QueryEngine.ts:331–333registers a stop-hook enforcement guard (registerStructuredOutputEnforcement), captures the tool's output via astructured_outputattachment (QueryEngine.ts:838–840), caps retries atMAX_STRUCTURED_OUTPUT_RETRIES(default5, emittingerror_max_structured_output_retriesatQueryEngine.ts:1005–1048), and surfaces the validated object onSDKResultSuccess.structured_output(QueryEngine.ts:1149). - Three Permission Modes (
getCanUseToolFn, cli/print.ts:4267–4334):permissionPromptToolName === 'stdio'(forced whenever--sdk-urlis set) →structuredIO.createCanUseTool(onPermissionPrompt).- Unset → pure static
hasPermissionsToUseTool()with no interactive fallback. - Custom MCP tool name (
--permission-prompt-tool) → lazily resolved on first prompt (lines 4294–4333) because MCP servers connect incrementally in print mode and aren't inappState.mcp.toolsat startup;createCanUseToolWithPermissionPrompt()races the MCP tool call againstcreateCombinedAbortSignal()soCtrl+C/interrupts break out of blocking permission tools immediately (lines 4149–4263).
Hidden Invariants & Failure Modes
heldBackResultand the Teammate Deadlock (gh-30008): Whenask()yields aresultwhile a backgroundlocal_agentorlocal_workflowis still running,cli/print.ts:2222–2236captures the message inheldBackResult(and defers anyprompt_suggestion) until thedo { drainCommandQueue(); ... } while (waitingForAgents)loop finishes draining background task completions. Critically, lines 2392–2394 explicitly excludein_process_teammatetasks:// cli/print.ts:2392-2394 const hasRunningBg = getRunningTasks(state).some( t => isBackgroundTask(t) && t.type !== 'in_process_teammate', )Teammates remain
runninguntil explicitly shut down—waiting on them insidewaitingForAgentslooped forever. Instead, onceinputClosedis true with an active swarm (lines 2617–2667),print.tsinjectsSHUTDOWN_TEAM_PROMPTso the team lead tears down its workers cleanly before closingoutput.done().
- Lost-wakeup guard on the
run()mutex (cli/print.ts:2487–2495): If a user message arrives between the lastdequeue()returningundefinedandrunning = falseinfinally, the producer'svoid run()seesrunning === trueand returns.run()closes this race by checkingif (peek(isMainThread) !== undefined) { void run(); return }after releasingrunning = false. - Authoritative turn-over boundary (
session_state_changed: 'idle'): Inrun()'sfinallyblock (cli/print.ts:2453–2469),print.tsflushesstructuredIO.flushInternalEvents(), emitsnotifySessionStateChanged('idle'), and runs one finaldrainSdkEvents()loop before blocking on the next command. BecauseheldBackResultflushed beforefinally,'idle'is the authoritative signal that a turn (and all its background agents) is completely finished. - Surviving
ask()'s clone-and-replace onseed_read_state(cli/print.ts:1157–1167, 2168–2181, 3017–3054): Because thestdinIIFE runs concurrently withask(), writing aseed_read_stateentry directly intoreadFileStatemid-turn would be overwritten whenask()'sfinallyreplacesreadFileStatewith its clone. Seeds instead buffer inpendingSeeds(one-shot survival across one clone-replace cycle), normalize paths withexpandPath()plus BOM/CRLF stripping, and skip seeding ifMath.floor(stat.mtimeMs) > request.mtime—otherwise caching current disk bytes under an older client timestamp would causegetChangedFiles()to diff current-vs-current, emit no change notification, and hide an external edit! - Auto-resume of interrupted turns (
cli/print.ts:1171–1192): WhenCLAUDE_CODE_RESUME_INTERRUPTED_TURNis set andloadInitialMessages()detects an interrupted turn (turnInterruptionState.kind !== 'none'),print.tssplices out the interrupted message and its sentinel viaremoveInterruptedMessage()and pre-enqueues it—waiting until afterinitializecompletes (lines 2913–2917) before callingrun()so SDK system prompts, agents, and hooks are wired first.
5. Inverting the Architecture: The Harness as an MCP Server (entrypoints/mcp.ts)
The Problem
Everything above assumes Claude Code owns the agentic loop—calling Claude, streaming tokens, deciding which tools to invoke. Sometimes the architecture flips inside out: another agent harness, desktop host, or IDE already owns the LLM loop and wants to consume Claude Code's built-in tools (Read, Edit, Write, Bash, Glob, Grep, etc.) over standard MCP stdio JSON-RPC.
The Real Implementation
When invoked as claude mcp serve, main.tsx:524–527 stamps process.env.CLAUDE_CODE_ENTRYPOINT = 'mcp', registers the Commander subcommand (main.tsx:3895–3906), and lazily imports mcpServeHandler() (cli/handlers/mcp.tsx:42–71), which validates cwd, calls setup(providedCwd, 'default', ...), and delegates to startMCPServer() in entrypoints/mcp.ts.
Unlike cli/print.ts, entrypoints/mcp.ts touches neither QueryEngine nor StructuredIO. It instantiates an @modelcontextprotocol/sdk Server named 'claude/tengu' bound to StdioServerTransport and registers exactly two request handlers:
// entrypoints/mcp.ts:59-97 (abridged)
server.setRequestHandler(ListToolsRequestSchema, async () => {
const toolPermissionContext = getEmptyToolPermissionContext()
const tools = getTools(toolPermissionContext)
return {
tools: await Promise.all(
tools.map(async tool => {
let outputSchema: ToolOutput | undefined
if (tool.outputSchema) {
const convertedSchema = zodToJsonSchema(tool.outputSchema)
// MCP SDK requires outputSchema to have type: "object" at root level
// Skip schemas with anyOf/oneOf at root (issues/8014)
if (
typeof convertedSchema === 'object' &&
convertedSchema !== null &&
'type' in convertedSchema &&
convertedSchema.type === 'object'
) {
outputSchema = convertedSchema as ToolOutput
}
}
return {
...tool,
description: await tool.prompt({
getToolPermissionContext: async () => toolPermissionContext,
tools,
agents: [],
}),
inputSchema: zodToJsonSchema(tool.inputSchema) as ToolInput,
outputSchema,
}
}),
),
}
})
On CallToolRequestSchema (entrypoints/mcp.ts:99–188), the server looks up the tool by name, constructs a minimal stub ToolUseContext (with getAppState: () => getDefaultAppState(), no-op state mutators, isNonInteractiveSession: true, thinkingConfig: { type: 'disabled' }, commands: [review], and the shared 100-file LRU readFileStateCache), checks tool.isEnabled() and tool.validateInput?.(), and invokes tool.call() directly:
// entrypoints/mcp.ts:136-169
// TODO: validate input types with zod
try {
if (!tool.isEnabled()) {
throw new Error(`Tool ${name} is not enabled`)
}
const validationResult = await tool.validateInput?.(
(args as never) ?? {},
toolUseContext,
)
if (validationResult && !validationResult.result) {
throw new Error(
`Tool ${name} input is invalid: ${validationResult.message}`,
)
}
const finalResult = await tool.call(
(args ?? {}) as never,
toolUseContext,
hasPermissionsToUseTool,
createAssistantMessage({ content: [] }),
)
return {
content: [
{
type: 'text' as const,
text:
typeof finalResult === 'string'
? finalResult
: jsonStringify(finalResult.data),
},
],
}
}
Hidden Invariants & Failure Modes
Reading entrypoints/mcp.ts alongside services/tools/toolExecution.ts (Chapter 06) exposes five load-bearing differences caused by bypassing runToolUse():
- Permission enforcement shifts to the MCP host:
CallToolRequestSchemapasseshasPermissionsToUseToolastool.call()'scanUseToolparameter, rather than callingcanUseTool()before invokingtool.call(). Built-in leaf tools that rely onrunToolUse()to check permissions ahead of time will execute directly once the MCP client sendstools/call—meaning the calling MCP client is the permission boundary, notentrypoints/mcp.ts. - No Zod input parsing or hooks (
entrypoints/mcp.ts:136): The inline// TODO: validate input types with zodcomment is literal:tool.inputSchema.safeParse()is skipped, and neitherPreToolUsenorPostToolUsehooks run. Onlytool.validateInput()executes beforetool.call(). - Cross-call
FileStateCachebounded by LRU (lines 40–45): Sharing a single 100-file (25 MB)FileStateCacheacross alltools/callrequests preservesFileEditTool's "must read before edit" and staleness invariants across sequential MCP tool calls while preventing unbounded memory growth in a long-lived server process. - Root-object
outputSchemafilter (issues/8014,lines 69–82): The MCP specification requires tooloutputSchemato declaretype: "object"at root. Tools whose Zod output schema is az.unionorz.discriminatedUnionconvert to top-levelanyOf/oneOf, which crashes strict MCP clients;entrypoints/mcp.tsdropsoutputSchematoundefinedfor those tools. - No upstream MCP re-export & different startup path: Note the
// TODO: Also re-expose any MCP toolscomments (lines 62, 103)—only built-in tools fromgetTools()are exposed. And unlike the specialized fast-path MCP servers in entrypoints/cli.tsx:72–95 (--claude-in-chrome-mcpandfeature('CHICAGO_MCP')--computer-use-mcp) that bypassmain.tsxcompletely,claude mcp serveroutes throughmain.tsxCommander dispatch and runs fullsetup().
Putting the Layer Together
The Three Machine-Readable Topologies
(1) Single-Shot (-p text / json) (2) Bidirectional SDK (stream-json)
prompt string / piped stdin Host (TS/Py SDK, IDE, Bridge)
| | ^
getStructuredIO() stdin | NDJSON lines | stdout
(wraps into 1 user JSON) (raw) v | (guarded)
| +-------+-----------------+-------+
v | StructuredIO <-> outbound FIFO |
+-------------------------+ | - processLine() |
| runHeadlessStreaming() | <----> | - pendingRequests Map |
| (cli/print.ts) | | - ndjsonSafeStringify() |
+------------+------------+ | - streamJsonStdoutGuard |
| +---------------------------------+
v
QueryEngine.ask() (3) Inverted Server (claude mcp serve)
External MCP Host
| StdioServerTransport
v (JSON-RPC)
entrypoints/mcp.ts ('claude/tengu')
| direct tool.call()
v
getTools() + 100-file cache
Full-Duplex Multiplexing During a Single SDK Turn
sequenceDiagram
participant Host as "SDK Host (TS/Py/IDE)"
participant Guard as "stdout Guard + ndjsonSafeStringify"
participant SIO as "StructuredIO (stdin/outbound FIFO)"
participant Print as "cli/print.ts (stdin IIFE + run())"
participant QE as "QueryEngine.ask()"
participant MCP as "SdkControlClientTransport"
Host->>SIO: "control_request (initialize: hooks, agents, jsonSchema, sdkMcpServers)"
SIO->>Print: "yield control_request"
Print->>SIO: "outbound.enqueue(control_response: commands, models, pid)"
SIO->>Guard: "write(control_response)"
Guard->>Host: "valid NDJSON line (non-JSON -> stderr [stdout-guard])"
Host->>SIO: "user message (uuid)"
SIO->>Print: "enqueue(prompt) -> void run()"
Print->>QE: "ask(prompt, mutableMessages, readFileState)"
QE-->>Print: "stream_event / assistant (tool_use)"
Print->>SIO: "outbound.enqueue(assistant)"
SIO->>Host: "assistant line (strict FIFO before permission prompt)"
QE->>SIO: "canUseTool() -> races PermissionRequest hooks vs sendRequest()"
SIO->>Host: "control_request (can_use_tool, request_id)"
Host->>SIO: "control_response (allow + updatedInput, request_id)"
SIO-->>QE: "processLine() resolves pendingRequests[request_id]"
QE->>MCP: "SDK MCP tool call -> sendMcpMessage()"
MCP->>SIO: "sendRequest(mcp_message, server_name, jsonrpc)"
SIO->>Host: "control_request (mcp_message)"
Host->>SIO: "control_response (mcp_response)"
SIO-->>MCP: "transport.onmessage(mcp_response)"
QE-->>Print: "result (usage, cost, structured_output)"
Print->>SIO: "outbound.enqueue(result) + session_state_changed(idle)"
SIO->>Host: "result + idle lines"
How This Snaps Into the Next Layer
Notice that nothing in cli/print.ts assumes the SDK host sits on local stdin/stdout pipes—everything speaks to the StructuredIO interface. That abstraction is the exact hinge into Chapter 24, snapping in at two concrete seams:
- Outbound worker transport (
cli/print.ts:5229–5232→ cli/remoteIO.ts): When--sdk-urlis passed,getStructuredIO()swapsnew StructuredIO(...)fornew RemoteIO(options.sdkUrl, inputStream, options.replayUserMessages).RemoteIO extends StructuredIOreplacesstdinwith aPassThroughstream fed bygetTransportForUrl()(cli/transports/transportUtils.ts:16–45)—selectingSSETransport(+CCRClientlifecycle/heartbeats/internal-event persistence) underCLAUDE_CODE_USE_CCR_V2,HybridTransportunderCLAUDE_CODE_POST_FOR_SESSION_INGRESS_V2, orWebSocketTransportby default.RemoteIO.write()ships frames over the network transport (still echoingcontrol_requestframes tostdoutwhenCLAUDE_CODE_ENVIRONMENT_KIND === 'bridge'so a supervising bridge process detects permission prompts) and adds an Envoy idle keep-alive timer (#21931). - Mid-session remote control (
cli/print.ts:3892–4020): Even a local stdio SDK session can dynamically attach aclaude.airemote-control bridge by sending{ subtype: 'remote_control', enabled: true }, which importssrc/bridge/initReplBridge.jsand wiresstructuredIO.setOnControlRequestSent,setOnControlRequestResolved, andinjectControlResponseso local SDK and remote web/mobile clients race safely over the same session.
Chapter 24 picks up directly on the far side of RemoteIO and initReplBridge to dissect the remote transports, always-on bridge daemons, Direct Connect, and the upstream credential proxy.
Reading Exercises & Code Checkpoints
- Audit every control subtype handled in
cli/print.tsvs.controlSchemas.ts: Run:Compare the two lists. Which subtypes exist inrg -n "request\.subtype ===" cli/print.ts rg -n "subtype: z\.literal\(" entrypoints/sdk/controlSchemas.tscli/print.ts(for IDE extensions, OAuth flows, or internal daemons) that are intentionally omitted fromSDKControlRequestInnerSchema? - Trace the
mcp_set_serversdeadlock prevention: Inspectcli/print.ts:3055–3065alongsidecli/print.ts:1389–1461(updateSdkMcp) andcli/structuredIO.ts:758–773(sendMcpMessage). Explain why awaitingupdateSdkMcp()before sendingsendControlResponseSuccess(message)would deadlock a synchronous SDK host. - Verify the two-layer duplicate-response shield:
Read
cli/structuredIO.ts:176–187,374–401, and490–504, thencli/print.ts:5241–5304. Trace what happens when aPermissionRequesthook wins the race against a slow host (hookAbortController.abort()), and the host's stalecontrol_responsearrives 200 ms later onstdin. Which data structure catches it and preventshandleOrphanedPermissionResponse()from running? - Contrast
entrypoints/mcp.tswithservices/tools/toolExecution.ts: Readentrypoints/mcp.ts:99–188and list every step from Chapter 06'srunToolUse()lifecycle (Zod parse,PreToolUsehooks,canUseToolpre-check,toolResultStoragebudgeting,PostToolUsehooks) that is absent when a tool is invoked viaclaude mcp serve. What security implication does this carry for an MCP host mountingclaude mcp serve?
Manuscript chapters/23-machine-readable-modes-headless-sdk-and-control-protocols.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.