MODULO 7.3

🔌 Entrypoints & SDK

6
Topicos
~60
Minutos
Deep
Nivel
Source
Tipo
1

🚀 The Entrypoint Layer

src/entrypoints/ contains five files: cli.tsx, init.ts, mcp.ts, agentSdkTypes.ts, sandboxTypes.ts. cli.tsx is a thin bootstrap that fast-paths special commands BEFORE loading the 200+ module import graph.

Fast path 1: --version

Zero imports. Prints MACRO.VERSION inlined at build time.

Fast path 2: --daemon-worker

Loads only worker registry.

Fast path 3: Bridge/RC

Connects local machine as remote-controlled environment.

Fast path 4: Background sessions

ps, logs, attach, kill, --bg without loading interactive UI.

Fallthrough

Full CLI via main.tsx with 200+ module imports.

2

⚙️ init.ts — Shared Initialization

Memoized init() shared by all non-trivial entrypoints. Not called for fast-paths. 13 steps including configs, TLS certs, graceful shutdown, telemetry, OAuth, IDE detection, proxy agents, and API preconnect (~150ms in parallel with CLI parsing).

3

🔧 MCP Server Mode

When invoked with claude --mcp, Claude Code runs as a standard MCP server over stdio. Exposes all tools (Bash, Edit, Read, WebFetch) to other MCP clients. Forces isNonInteractiveSession: true and disables thinking.

💡 Symmetric Boundary

Claude Code can BE an MCP server (--mcp mode) while also CONSUMING MCP servers.

4

📦 Agent SDK Public API

V1 API (stable)

query() - headless one-shot query.

V2 API (@alpha)

unstable_v2_createSession(), resumeSession(), prompt() - persistent multi-turn sessions.

Session Management

listSessions, getSessionInfo, getSessionMessages, renameSession, tagSession, forkSession.

In-process MCP

createSdkMcpServer() and tool() for defining MCP tools.

💡 Stub Pattern

All function bodies throw 'not implemented'. Real implementations injected at runtime by SDK transport layer.

5

💬 Control Protocol

SubtypeDirectionPurpose
initializeSDK → CLIStart session with hooks, MCP servers, agents
interruptSDK → CLICancel currently running turn
can_use_toolCLI → SDKRequest permission for tool use
set_permission_modeSDK → CLIChange permission mode mid-session
set_modelSDK → CLISwitch model for subsequent turns
mcp_statusSDK → CLIQuery MCP server connection states
6

🔗 Hook Events & Daemon Mode

26 named hook events for observing/intercepting lifecycle: PreToolUse, PostToolUse, PermissionRequest, SessionStart/End, PreCompact, SubagentStart, TaskCreated, etc.

💡 Daemon vs enableRemoteControl

Daemon mode: WebSocket lives in parent process, survives agent crashes. enableRemoteControl: WebSocket in child, dies with it. Use daemon for production reliability.

📋 Resumo do Modulo

✅ cli.tsx is pure dispatcher - fast-paths 8+ commands before loading full CLI.
✅ Build-time feature() flags perform dead-code elimination in external builds.
✅ init.ts is memoized and shared - coordinates TLS, proxy, OAuth, telemetry before first API call.
✅ Agent SDK public API is stub file - implementations injected by SDK transport at runtime.
✅ 26 lifecycle hook events let SDK hosts observe tool execution, permissions, and session lifecycle.
✅ sandboxTypes.ts is single source of truth for process isolation config used by both SDK and settings.
← Voltar Proximo →