How to navigate and study the Claude Code source code.
This is a read-only reference codebase — there's no build system or test suite. The goal is to understand how a production AI coding assistant is built.
| What | Where |
|---|---|
| CLI entrypoint | src/main.tsx |
| Core LLM engine | src/QueryEngine.ts (~46K lines) |
| Tool definitions | src/Tool.ts (~29K lines) |
| Command registry | src/commands.ts (~25K lines) |
| Tool registry | src/tools.ts |
| Context collection | src/context.ts |
| All tool implementations | src/tools/ (40 subdirectories) |
| All command implementations | src/commands/ (~85 subdirectories + 15 files) |
- Go to
src/tools/{ToolName}/ - Main implementation is
{ToolName}.tsor.tsx - UI rendering is in
UI.tsx - System prompt contribution is in
prompt.ts
Example — understanding BashTool:
src/tools/BashTool/
├── BashTool.ts ← Core execution logic
├── UI.tsx ← How bash output renders in terminal
├── prompt.ts ← What the system prompt says about bash
└── ...
- Check
src/commands/{command-name}/(directory) orsrc/commands/{command-name}.ts(file) - Look for the
getPromptForCommand()function (PromptCommands) or direct implementation (LocalCommands)
| Feature | Start Here |
|---|---|
| Permissions | src/hooks/toolPermission/ |
| IDE bridge | src/bridge/bridgeMain.ts |
| MCP client | src/services/mcp/ |
| Plugin system | src/plugins/ + src/services/plugins/ |
| Skills | src/skills/ |
| Voice input | src/voice/ + src/services/voice.ts |
| Multi-agent | src/coordinator/ |
| Memory | src/memdir/ |
| Authentication | src/services/oauth/ |
| Config schemas | src/schemas/ |
| State management | src/state/ |
Trace from user input to API response:
src/main.tsx ← CLI parsing
→ src/replLauncher.tsx ← REPL session start
→ src/QueryEngine.ts ← Core engine
→ src/services/api/ ← Anthropic SDK client
→ (Anthropic API) ← HTTP/streaming
← Tool use response
→ src/tools/{ToolName}/ ← Tool execution
← Tool result
→ (feed back to API) ← Continue the loop
Every tool uses this pattern:
export const MyTool = buildTool({
name: 'MyTool',
inputSchema: z.object({ ... }),
async call(args, context) { ... },
async checkPermissions(input, context) { ... },
})import { feature } from 'bun:bundle'
if (feature('VOICE_MODE')) {
// This code is stripped at build time if VOICE_MODE is off
}if (process.env.USER_TYPE === 'ant') {
// Anthropic employee-only features
}Most directories have an index.ts that re-exports the public API:
// src/tools/BashTool/index.ts
export { BashTool } from './BashTool.js'Heavy modules are loaded only when needed:
const { OpenTelemetry } = await import('./heavy-module.js')Bun convention — all imports use .js extensions even for .ts files:
import { something } from './utils.js' // Actually imports utils.tsThe largest files contain the most logic and are worth studying:
| File | Lines | What's Inside |
|---|---|---|
QueryEngine.ts |
~46K | Streaming, tool loops, retries, token counting |
Tool.ts |
~29K | Tool types, buildTool, permission models |
commands.ts |
~25K | Command registry, conditional loading |
main.tsx |
— | CLI parser, startup optimization |
context.ts |
— | OS, shell, git, user context assembly |
- Read
src/Tool.ts— understand thebuildToolinterface - Pick a simple tool like
FileReadToolinsrc/tools/FileReadTool/ - Trace how
QueryEngine.tscalls tools during the tool loop - See how permissions are checked in
src/hooks/toolPermission/
- Read
src/screens/REPL.tsx— the main screen - Explore
src/components/— pick a few components - See
src/hooks/useTextInput.ts— how user input is captured - Check
src/ink/— the Ink renderer wrapper
- Start at
src/bridge/bridgeMain.ts - Follow
bridgeMessaging.tsfor the message protocol - See
bridgePermissionCallbacks.tsfor how permissions route to the IDE - Check
replBridge.tsfor REPL session bridging
- Read
src/types/plugin.ts— the plugin API surface - See
src/services/plugins/— how plugins are loaded - Check
src/plugins/builtinPlugins.ts— built-in examples - Look at
src/plugins/bundled/— bundled plugin code
- Read
src/services/mcp/— the MCP client - See
src/tools/MCPTool/— how MCP tools are invoked - Check
src/entrypoints/mcp.ts— Claude Code as an MCP server - Look at
src/skills/mcpSkillBuilders.ts— skills from MCP
This repo includes a standalone MCP server (mcp-server/) that lets any MCP-compatible client explore the source code. See the MCP Server README for setup.
Once connected, you can ask an AI assistant to explore the source:
- "How does the BashTool work?"
- "Search for where permissions are checked"
- "List all files in the bridge directory"
- "Read QueryEngine.ts lines 1-100"
Useful grep/ripgrep patterns for finding things:
# Find all tool definitions
rg "buildTool\(" src/tools/
# Find all command definitions
rg "satisfies Command" src/commands/
# Find feature flag usage
rg "feature\(" src/
# Find Anthropic-internal gates
rg "USER_TYPE.*ant" src/
# Find all React hooks
rg "^export function use" src/hooks/
# Find all Zod schemas
rg "z\.object\(" src/schemas/
# Find all system prompt contributions
rg "prompt\(" src/tools/*/prompt.ts
# Find permission rule patterns
rg "checkPermissions" src/tools/- Architecture — Overall system design
- Tools Reference — Complete tool catalog
- Commands Reference — All slash commands
- Subsystems Guide — Deep dives into Bridge, MCP, Permissions, etc.