How jkspeaks/claude-code works
Purpose and entry points
This repository archives the TypeScript source of Claude Code and adds exploration tooling around it. The primary product is a Bun-based terminal coding agent; companion surfaces include an MCP source-explorer server and a Next.js web UI.
• CLI/bootstrap: src/main.tsx, src/entrypoints/cli.tsx, src/entrypoints/init.ts
• MCP explorer executable/server: mcp-server/src/index.ts, mcp-server/src/server.ts
• Web surface: web/app/page.tsx, web/app/api/chat/route.ts
Interactive CLI and application state
The CLI parses invocation modes, initializes session/runtime dependencies, then renders a React terminal application using the repository’s Ink implementation. Application state, contexts, hooks, screens, commands, and components form the interactive control plane.
• Top-level UI and REPL: src/components/App.tsx, src/screens/REPL.tsx, src/replLauncher.tsx
• State and shared contexts: src/state/AppState.tsx, src/state/AppStateStore.ts, src/context.ts
• Custom terminal renderer: src/ink/ink.tsx, src/ink/renderer.ts, src/ink/layout/engine.ts
Model query pipeline
QueryEngine is the central agent loop: it builds model context, calls Claude-facing API services, handles streaming responses, coordinates tool calls/results, transitions, token budgets, retries, and session progression. Conversation history, compaction, memory extraction, and cost tracking constrain and preserve the session.
• Engine and query mechanics: src/QueryEngine.ts, src/query/transitions.ts, src/query/tokenBudget.ts
• API boundary: src/services/api/claude.ts, src/services/api/client.ts
• Context lifecycle: src/context.ts, src/services/compact/compact.ts, src/services/SessionMemory/sessionMemory.ts
Tools, safety, and execution
The model operates through a registered tool system spanning local files, shell/PowerShell, search, web access, LSP, MCP, tasks, and subagents. Tool orchestration is separated from permission decisions and concrete execution. Permission rules, path/shell validation, UI approvals, and sandbox support are the critical safety boundary before effects occur.
• Tool abstractions/registry: src/Tool.ts, src/tools.ts, src/services/tools/toolOrchestration.ts
• Representative effectful tools: src/tools/BashTool/BashTool.tsx, src/tools/FileEditTool/FileEditTool.ts, src/tools/AgentTool/AgentTool.tsx
• Authorization/sandbox boundary: src/hooks/toolPermission/PermissionContext.ts, src/utils/permissions/permissions.ts, src/utils/sandbox/sandbox-adapter.ts
Commands and extensibility
Slash commands provide user-directed operations separate from model tool calls: session control, configuration, authentication, review, MCP, plugins, tasks, and memory. Plugins, skills, and user-defined agents extend available commands, prompts, tools, output styles, hooks, and integrations.
• Command registry and implementations: src/commands.ts, src/commands/mcp/mcp.tsx, src/commands/plugin/plugin.tsx
• Plugin loading/install lifecycle: src/services/plugins/PluginInstallationManager.ts, src/utils/plugins/pluginLoader.ts
• Skills and agents: src/skills/loadSkillsDir.ts, src/tools/AgentTool/loadAgentsDir.ts
Remote, IDE, and multi-agent boundaries
The bridge subsystem runs bidirectional CLI–IDE sessions, including transport, JWT/trusted-device handling, inbound messages, and permission callbacks. Remote sessions and agent/team tasks provide alternate execution locations and asynchronous work while preserving task/session state.
• IDE bridge: src/bridge/bridgeMain.ts, src/bridge/bridgeMessaging.ts, src/bridge/bridgePermissionCallbacks.ts
• Remote control/session transport: src/remote/RemoteSessionManager.ts, src/remote/SessionsWebSocket.ts
• Task/teammate implementations: src/tasks/LocalMainSessionTask.ts, src/tasks/RemoteAgentTask/RemoteAgentTask.tsx, src/utils/swarm/spawnInProcess.ts
External protocols, runtime, and deployment
Core runtime is Bun with strict TypeScript, React, and Ink; Zod schemas validate configuration/protocol inputs. External integrations include Anthropic APIs, OAuth/JWT and secure storage, MCP servers, LSP servers, GrowthBook/analytics, OpenTelemetry-style telemetry, GitHub, and optional cloud/remote services. Docker artifacts support container deployment; MCP server deployment includes Vercel and Railway-oriented files.
• Protocol services: src/services/mcp/MCPConnectionManager.tsx, src/services/lsp/LSPServerManager.ts, src/services/oauth/client.ts
• Telemetry/flags: src/services/analytics/growthbook.ts, src/utils/telemetry/instrumentation.ts
• Deployment: Dockerfile, docker/docker-compose.yml, mcp-server/api/index.ts
Main entry points: QueryEngine.ts Query engine · App.tsx Terminal app · REPL.tsx REPL · toolOrchestration.ts Tool orchestration
Components 22
CLI Runtime
Bun terminal-agent control plane.
- CLI bootstrap
src/main.tsx - CLI modes
src/entrypoints/cli.tsx - Terminal app · Top-level interactive application.
src/components/App.tsx - REPL
src/screens/REPL.tsx - App state store · Shared session and UI state.
src/state/AppStateStore.ts - Custom Ink renderer
src/ink/renderer.ts
Agent Pipeline
Model context, querying, and session lifecycle.
- Query engine · Coordinates streaming, tools, retries, budgets, and progression.
src/QueryEngine.ts - Context lifecycle
src/context.ts - Compaction & memory
src/services/compact/compact.ts - Claude API
src/services/api/claude.ts
Tools & Safety
Effect execution is gated by permissions.
- Tool registry
src/tools.ts - Tool orchestration
src/services/tools/toolOrchestration.ts - Permission boundary · Validates paths and shell effects before execution.
src/utils/permissions/permissions.ts - Effectful tools
src/tools/BashTool/BashTool.tsx
Extensions & Protocols
Commands and external integration managers.
- Slash commands
src/commands.ts - Plugin lifecycle · Loads plugins, skills, and user agents.
src/services/plugins/PluginInstallationManager.ts - MCP & LSP managers
src/services/mcp/MCPConnectionManager.tsx
Companion Surfaces
Explorer, web, IDE, and remote entry points.
- MCP explorer server · Source-explorer executable and server.
mcp-server/src/index.ts - Web UI & chat API
web/app/page.tsx - IDE bridge · CLI–IDE transport with permission callbacks.
src/bridge/bridgeMain.ts - Remote sessions & tasks
src/remote/RemoteSessionManager.ts - Container deployment
Dockerfile
Connections 22
- CLI bootstrap → CLI modes: starts
- CLI modes → Terminal app: renders
- Terminal app → REPL: hosts
- Terminal app → App state store: uses
- Terminal app → Custom Ink renderer: renders through
- REPL → Query engine: submits prompts
- REPL → Slash commands: dispatches slash commands
- Query engine → Context lifecycle: builds
- Query engine → Compaction & memory: maintains history
- Query engine → Claude API: streams queries
- Query engine → Tool orchestration: coordinates tool calls
- Tool orchestration → Tool registry: resolves tools
- Tool orchestration → Permission boundary: requests authorization
- Permission boundary → Effectful tools: gates effects
- Slash commands → Plugin lifecycle: manages extensions
- Plugin lifecycle → Tool registry: extends
- MCP & LSP managers → Tool registry: provides protocol tools
- Slash commands → MCP & LSP managers: configures
- IDE bridge → Permission boundary: forwards approvals
- Remote sessions & tasks → Query engine: hosts alternate sessions
- Web UI & chat API → Query engine: chat surface
- Container deployment → CLI bootstrap: packages CLI runtime