The world needs an open-source Claude Code.It will become the foundational infrastructure of AI in the future, running on every PC.
A reverse-engineered restoration based on @anthropic-ai/claude-code v2.0.76.
For educational and research purposes only.
This is an educational project for studying and learning CLI tool architecture design. This is NOT the official Claude Code source code, but a reimplementation based on public APIs and type definitions.
For the official Claude Code, please install the official version:
npm install -g @anthropic-ai/claude-code# Install dependencies
npm install
# Build the project
npm run build
# Link globally (optional)
npm link# Interactive mode
npm run dev
# Or run after building
node dist/cli.js
# With initial prompt
node dist/cli.js "Hello, please analyze this project"
# Print mode
node dist/cli.js -p "Explain this code"
# Specify model
node dist/cli.js -m opus "Complex task"
# Resume last session
node dist/cli.js --resumeSet up your API key:
Linux/macOS:
export ANTHROPIC_API_KEY=your-api-key
# or
export CLAUDE_API_KEY=your-api-keyWindows Command Prompt:
set ANTHROPIC_API_KEY=your-api-key
# or
set CLAUDE_API_KEY=your-api-keyWindows PowerShell:
$env:ANTHROPIC_API_KEY="your-api-key"
# or
$env:CLAUDE_API_KEY="your-api-key"| Variable | Description | Default |
|---|---|---|
ANTHROPIC_API_KEY |
API Key | - |
BASH_MAX_OUTPUT_LENGTH |
Max Bash output length | 30000 |
CLAUDE_CODE_MAX_OUTPUT_TOKENS |
Max output tokens | 32000 |
CLAUDE_TELEMETRY_ENABLED |
Enable telemetry | true |
src/
├── index.ts # Main export barrel
├── cli.ts # CLI entry point (Commander.js)
├── core/ # Core engine
│ ├── client.ts # Anthropic API client (streaming, retry, cost)
│ ├── session.ts # Session state management
│ ├── loop.ts # Conversation orchestrator
│ └── context.ts # Context management & summarization
├── tools/ # 25+ tools
│ ├── bash.ts # Bash execution (sandbox support)
│ ├── file.ts # Read/Write/Edit/MultiEdit
│ ├── search.ts # Glob/Grep search
│ ├── web.ts # WebFetch/WebSearch
│ ├── todo.ts # TodoWrite task management
│ ├── agent.ts # Task/TaskOutput sub-agents
│ ├── notebook.ts # Jupyter Notebook editing
│ ├── planmode.ts # EnterPlanMode/ExitPlanMode
│ ├── mcp.ts # MCP protocol (ListMcpResources/ReadMcpResource)
│ ├── ask.ts # AskUserQuestion
│ ├── tmux.ts # Tmux multi-terminal (Linux/macOS)
│ ├── skill.ts # Skill system
│ ├── lsp.ts # LSP integration (diagnostics, hover, references)
│ └── sandbox.ts # Bubblewrap sandbox (Linux)
├── ui/ # Ink/React UI framework
│ ├── App.tsx # Main app component
│ └── components/ # Reusable UI components
│ ├── Spinner.tsx
│ ├── Message.tsx
│ ├── ToolCall.tsx
│ ├── TodoList.tsx
│ ├── PermissionPrompt.tsx
│ └── StatusBar.tsx
├── agents/ # Specialized sub-agents
│ ├── explore.ts # Codebase exploration agent
│ ├── plan.ts # Implementation planning agent
│ └── guide.ts # Claude Code documentation agent
├── auth/ # Authentication
│ ├── oauth.ts # OAuth flow
│ └── api-key.ts # API key management
├── session/ # Session persistence
│ ├── manager.ts # Session lifecycle
│ ├── storage.ts # Disk persistence (~/.claude/sessions/)
│ └── export.ts # Markdown export
├── context/ # Context management
│ ├── estimator.ts # Token estimation
│ ├── compressor.ts # Message summarization
│ └── budget.ts # Token budget tracking
├── parser/ # Code parsing
│ ├── tree-sitter.ts # Tree-sitter WASM integration
│ └── languages/ # Language-specific parsers
├── search/ # Search utilities
│ ├── ripgrep.ts # Vendored ripgrep binary
│ └── glob.ts # File pattern matching
├── hooks/ # Hook system
│ ├── registry.ts # Hook registration
│ └── executor.ts # Hook execution
├── mcp/ # MCP protocol
│ ├── client.ts # MCP client
│ ├── server.ts # MCP server connection
│ └── registry.ts # MCP server registry
├── permissions/ # Permission system
│ ├── manager.ts # Permission requests
│ └── modes.ts # Permission modes (accept/bypass/plan)
├── config/ # Configuration
│ ├── loader.ts # Load from ~/.claude/settings.json
│ └── env.ts # Environment variable handling
├── telemetry/ # Telemetry
│ ├── collector.ts # Event collection
│ └── analytics.ts # Local analytics (not uploaded)
├── skills/ # Skills system
│ ├── loader.ts # Load from ~/.claude/skills/
│ └── registry.ts # Skill registration
├── commands/ # Slash commands
│ ├── registry.ts # Command registration
│ └── builtin/ # Built-in commands (/help, /clear, etc.)
├── plugins/ # Plugin system
│ ├── manager.ts # Plugin lifecycle
│ └── loader.ts # Plugin discovery
├── models/ # Model configuration
│ ├── registry.ts # Model definitions
│ └── pricing.ts # Token pricing
├── network/ # Network utilities
│ ├── proxy.ts # Proxy support
│ └── retry.ts # Retry logic
├── streaming/ # Streaming I/O
│ ├── parser.ts # JSON message streaming
│ └── writer.ts # Stream writing
├── security/ # Security features
│ ├── validator.ts # Input validation
│ └── sanitizer.ts # Output sanitization
├── types/ # TypeScript definitions
│ ├── tools.ts # Tool types
│ ├── session.ts # Session types
│ └── config.ts # Configuration types
└── utils/ # Utility functions
├── fs.ts # File system helpers
├── path.ts # Path utilities
└── time.ts # Time formatting
| Tool | Status | Description |
|---|---|---|
| File Operations | ||
| Read | ✅ Complete | File reading with image/PDF/Notebook support + external modification detection |
| Write | ✅ Complete | File writing with overwrite protection |
| Edit | ✅ Complete | File editing (string replacement) |
| MultiEdit | ✅ Complete | Batch file editing (atomic operations) |
| Search & Discovery | ||
| Glob | ✅ Complete | File pattern matching |
| Grep | ✅ Complete | Content search (ripgrep-based) with official output format |
| Execution | ||
| Bash | ✅ Complete | Command execution with background & sandbox support |
| TaskOutput | ✅ Complete | Get background command/agent output (unified UUID/task_id format) |
| KillShell | ✅ Complete | Terminate background processes |
| Web Access | ||
| WebFetch | ✅ Complete | Web page fetching with caching |
| WebSearch | Web search (requires API configuration) | |
| Task Management | ||
| TodoWrite | ✅ Complete | Task management with auto-reminder system |
| Task | ✅ Complete | Sub-agents (explore, plan, guide, etc.) |
| Planning | ||
| EnterPlanMode | ✅ Complete | Enter plan mode with permission system |
| ExitPlanMode | ✅ Complete | Exit plan mode |
| Interaction | ||
| AskUserQuestion | ✅ Complete | Ask user questions (multiSelect, options, validation) |
| Code Tools | ||
| NotebookEdit | ✅ Complete | Jupyter Notebook cell editing (replace/insert/delete) |
| LSP* | ✅ Complete | Language Server Protocol integration (diagnostics, hover, references) |
| Integration | ||
| ListMcpResources | ✅ Complete | List MCP resources |
| ReadMcpResource | ✅ Complete | Read MCP resource |
| Skill | ✅ Complete | Skill system with args parameter and permission checks |
| Terminal | ||
| Tmux | ✅ Complete | Multi-terminal session management (Linux/macOS) |
*LSP tools available when language servers are configured
Supports both API Key and OAuth authentication:
import { initAuth, startOAuthLogin, setApiKey } from './auth';
// Using API Key
setApiKey('your-api-key', true); // true for persistence
// Or using OAuth login
await startOAuthLogin({
clientId: 'your-client-id',
scope: ['read', 'write'],
});Automatic conversation saving and restoration:
import { SessionManager, listSessions, loadSession } from './session';
const manager = new SessionManager({ autoSave: true });
// Start new session or resume
const session = manager.start({
model: 'claude-sonnet-4-20250514',
resume: true, // Try to resume last session
});
// List all sessions
const sessions = listSessions({ limit: 10 });
// Export as Markdown
const markdown = manager.export();Intelligent context compression and summarization:
import { ContextManager, estimateTokens } from './context';
const context = new ContextManager({
maxTokens: 180000,
summarizeThreshold: 0.7, // Start compressing at 70%
keepRecentMessages: 10,
});
// Add conversation turn
context.addTurn(userMessage, assistantMessage);
// Get optimized messages
const messages = context.getMessages();
// Manual compaction
context.compact();Multi-language code analysis support:
import { parseFile, parseCode, detectLanguage } from './parser';
// Detect language
const lang = detectLanguage('app.tsx'); // 'typescript'
// Parse file
const parsed = parseFile('/path/to/file.ts');
console.log(parsed.classes); // Class definitions
console.log(parsed.functions); // Function definitions
console.log(parsed.imports); // Import statements
console.log(parsed.exports); // Export statementsSupported languages: JavaScript, TypeScript, Python, Go, Rust, Java, C/C++, Ruby, PHP, Swift, Kotlin, Scala, etc.
Built-in ripgrep support, no system installation required:
import { search, listFiles, getRipgrepVersion } from './search/ripgrep';
// Search content
const results = await search({
pattern: 'function.*async',
glob: '*.ts',
ignoreCase: true,
});
// List files
const files = await listFiles({
glob: '**/*.tsx',
hidden: false,
});Local usage statistics (data is not uploaded):
import { telemetry, getTelemetryStats } from './telemetry';
// Record session
telemetry.startSession('claude-sonnet-4-20250514');
telemetry.recordMessage('user', 100);
telemetry.recordToolCall('Bash', true, 50);
telemetry.endSession();
// Get statistics
const stats = getTelemetryStats();
console.log(stats.totalSessions);
console.log(stats.totalTokens);Complete terminal UI component system:
Spinner- Loading animationsToolCall- Tool call displayMessage- Message displayInput- Input boxHeader- Header barTodoList- Task listPermissionPrompt- Permission confirmationStatusBar- Status bar
Linux only: If bubblewrap is installed, Bash commands will execute in a sandbox for enhanced security:
# Ubuntu/Debian
sudo apt install bubblewrap
# Arch Linux
sudo pacman -S bubblewrapNote for Windows/macOS users:
- Bubblewrap sandbox is only available on Linux
- Windows and macOS users can use WSL (Windows Subsystem for Linux) to enable sandbox support
- Alternatively, commands will run without sandboxing (use with caution)
Sandbox can be disabled with dangerouslyDisableSandbox: true parameter.
Execute custom scripts before/after tool calls:
// .claude/settings.json
{
"hooks": [
{
"event": "PreToolUse",
"matcher": "Bash",
"command": "/path/to/script.sh", // Linux/macOS: .sh, Windows: .bat or .ps1
"blocking": true
}
]
}Supported events:
PreToolUse- Before tool callPostToolUse- After tool callPrePromptSubmit- Before submissionPostPromptSubmit- After submissionNotification- NotificationsStop- Stop
Connect to MCP (Model Context Protocol) servers:
// .claude/settings.json
{
"mcpServers": {
"filesystem": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"] // Use absolute path
}
}
}Path examples:
- Linux/macOS:
"/home/user/projects"or"/Users/user/projects" - Windows:
"C:\\Users\\user\\projects"(use double backslashes in JSON)
Linux/macOS only: Manage multiple terminal sessions:
// Create session
{ action: "new", session_name: "dev-server" }
// Send command
{ action: "send", session_name: "dev-server", command: "npm run dev" }
// Capture output
{ action: "capture", session_name: "dev-server" }Note for Windows users:
- Tmux is not available natively on Windows
- Use WSL (Windows Subsystem for Linux) to access Tmux
- Alternative: Use Windows Terminal with multiple tabs/panes
Load from the following directories:
- Linux/macOS:
~/.claude/skills/and.claude/commands/ - Windows:
%USERPROFILE%\.claude\skills\and.claude\commands\
Features:
- Skills: Reusable prompt templates
- Slash Commands: Custom command extensions
- Exponential backoff retry (up to 4 times)
- Automatic cost calculation
- Token usage statistics
- Multi-model pricing support
/help- Show help/clear- Clear conversation history/save- Save session/stats- Show statistics/tools- List tools/model- Switch model/resume- Resume session/compact- Compress context/exit- Exit
This project includes comprehensive testing:
# Run all tests
npm test
# Run with UI
npm run test:ui
# Run specific test suites
npm run test:unit # Unit tests
npm run test:integration # Integration tests
npm run test:e2e # End-to-end tests
# Run with coverage
npm run test:coverage
# Watch mode
npm run test:watch- Unit Tests (
src/**/*.test.ts) - Individual component tests - Integration Tests (
tests/integration/) - Multi-component interaction tests - E2E Tests (
tests/e2e/) - Full CLI workflow tests - Tool Tests (
tests/tools/) - Individual tool functionality tests
- ✅ Tool-level error handling & retry - Exponential backoff for transient failures
- ✅ LSP URI handling - Enhanced URI parsing and location validation
- ✅ Grep output format - 100% match with official implementation
- ✅ OAuth authentication - Streamlined auth flow and system prompt formatting
- ✅ AskUserQuestion - Full parity with official (multiSelect, validation)
- ✅ Shell ID format - Unified UUID/task_id format across all background tasks
- ✅ Tool result persistence - Automatic saving of tool execution results
- ✅ Permission dialog flow - Complete permission request workflow
- ✅ TodoWrite auto-reminders - Official reminder system for task tracking
- ✅ Plan mode permissions - Permission checks integrated into planning tools
- ✅ File modification detection - Alerts when files are modified externally
- ✅ Skill args parameter - Full skill argument passing and permission system
- ✅ NotebookEdit insert mode - Fixed cell insertion position logic
| Component | Status | Notes |
|---|---|---|
| Core Architecture | ✅ 100% | Three-layer design (Entry → Engine → Tools) |
| CLI Interface | ✅ 100% | All commands & flags implemented |
| Tool System | ✅ 100% | 25+ tools with full feature parity |
| API Client | ✅ 100% | Streaming, retry, cost calculation |
| Permission System | ✅ 100% | Accept/bypass/plan modes |
| Error Handling | ✅ 100% | Tool-level retry with exponential backoff |
| File Operations | ✅ 100% | External modification detection |
| Background Tasks | ✅ 100% | Unified UUID/task_id format |
| Output Formatting | ✅ 100% | Grep, LSP, and all tools match official |
| Sandbox | ✅ 100% | Bubblewrap isolation (Linux) |
| Hooks | ✅ 100% | Complete event system |
| MCP | ✅ 100% | Full protocol support |
| UI Components | ✅ 100% | Ink/React framework with auto-scroll |
| Skills/Commands | ✅ 100% | Args, permissions, discovery |
| Authentication | ✅ 100% | API Key + OAuth |
| Session Management | ✅ 100% | Persistence, recovery, export |
| Context Management | ✅ 100% | Auto-summarization |
| Code Parser | ✅ 100% | Tree-sitter WASM |
| Telemetry | ✅ 100% | Local analytics |
Overall Accuracy: ~100% (based on public API and behavioral analysis)
# Development mode (using tsx)
npm run dev
# Build
npm run build
# Type checking
npx tsc --noEmit- TypeScript - Type safety
- Anthropic SDK - API calls
- Ink + React - Terminal UI
- Commander - CLI framework
- Chalk - Terminal colors
- Glob - File matching
- Zod - Schema validation
- Discord: Join our Discord
- X (Twitter): @wangbingjie1989
This project is for educational purposes only. Original Claude Code is owned by Anthropic PBC.
This project is a reverse engineering study of obfuscated code and does not represent the official implementation.