# Claude Code Docs

> Chapter-by-chapter teardown of a production coding agent — agent loop, context engineering, compaction, permissions, MCP. 13 chapters, Chinese and English.

- **Type:** MCP server
- **Install:** `agentstack add mcp-anneheartrecord-claude-code-docs`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [anneheartrecord](https://agentstack.voostack.com/s/anneheartrecord)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [anneheartrecord](https://github.com/anneheartrecord)
- **Source:** https://github.com/anneheartrecord/claude-code-docs
- **Website:** https://anneheartrecord.github.io/claude-code-docs/

## Install

```sh
agentstack add mcp-anneheartrecord-claude-code-docs
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

[English](./README_EN.md)

# Claude Code 源码解剖

[](./LICENSE)
[](https://github.com/anneheartrecord/claude-code-docs/actions/workflows/docs-check.yml)

[](./CONTRIBUTING.md)

> 📖 **在线阅读：** https://anneheartrecord.github.io/claude-code-docs/

2026 年 3 月 31日，有人发现 Anthropic 发布在 npm 上的 Claude Code 客户端包里带了 sourcemap 文件。

Sourcemap 是前端构建工具生成的调试辅助文件，记录了编译后代码和原始源码之间的映射关系。正常发布时应该把它排除掉，但 Anthropic 的构建流程里漏了这一步。

于是完整的 TypeScript 源码被反向还原了出来，有51.5 万行代码，2,766 个文件。

**需要明确的是：泄露的只是客户端侧的代码。** Claude Code 是典型的客户端-服务端(Client-Server)分离架构。客户端跑在你的终端里，负责用户交互、工具执行、权限管理、上下文组装。服务端是 Anthropic 的 API，负责模型推理，模型本身和服务端逻辑没有泄露。

但客户端的这部分代码已经足够有价值了。因为 Agent 产品的核心竞争力不只在模型调用侧，也在于**怎么通过设计 Agent 将模型的能力安全、高效、稳定地释放出来**。

有意思的是，从 git 历史看，这份代码本身很可能就是 AI 写的：20 个 commit 全部来自同一个账号 claude-code-best，其中三个带着 `Co-Authored-By: Claude Opus 4.6`。51.5 万行代码一次编译零报错。**AI 把功能写得挑不出毛病，却栽在一个基础的发布配置细节上。**

## 这个仓库做了什么

我用 Claude Code 对这份源码做了系统化的技术分析，逐模块拆解，写了 13 篇技术文档，中英双语。

从架构设计到 Agent 循环的六阶段实现，从三层消息压缩体系到权限校验系统，从五层记忆加载到工具的执行流水线。

同时从 82 个 feature flag 里挖出了 Anthropic 还没发布的未来功能蓝图：Kairos 自主运行模式、Context Collapse 上下文折叠、Voice Mode 语音交互。

最后几篇是我自己的思考：这份代码到底值不值钱、AI Coding 时代工程师怎么做 Code Review、以及从源码里能看到 Claude 封号机制的哪些线索。

**目标读者：** 正在做 Agent 产品的工程师、对 AI Agent 架构感兴趣的开发者、想了解顶级 Agent 系统如何工程化落地的技术人，非技术背景的同学也能从中了解到 Agent 产品的运作方式和行业趋势。

## 文档目录

### 概览篇

| 文档 | 内容 |
|------|------|
| [01-架构总览](./docs/01-架构总览.md) | 整体架构、技术栈、核心文件、一次请求的完整旅程 |
| [02-源码泄露的价值之争](./docs/02-源码泄露的价值之争.md) | 产物 vs Harness 能力，两派观点分析，代码是快照能力是动态的 |

### 核心模块篇

| 文档 | 内容 |
|------|------|
| [03-Agent 循环](./docs/03-Agent循环.md) | 六阶段 ReAct 循环、AsyncGenerator 设计、状态管理、思维链保留 |
| [04-上下文工程](./docs/04-上下文工程.md) | System Prompt 构建、CLAUDE.md 加载、分层优先级、预取缓存、Prompt Cache 优化 |
| [05-消息压缩系统](./docs/05-消息压缩系统.md) | 三层压缩：微压缩、Session Memory、Full Compact，熔断器、递归保护 |
| [06-权限系统](./docs/06-权限系统.md) | 三模式权限、YOLO 分类器、42 条拦截规则、文件沙箱、Dangerous Rule Stripping |
| [07-记忆管理](./docs/07-记忆管理.md) | 五层记忆加载、@include 指令、MEMORY.md 管理、Session Memory |
| [08-工具与 Skill 系统](./docs/08-工具与Skill系统.md) | 40+ 工具注册、执行流水线、Pre/Post Hook、Skill fork 机制 |
| [09-MCP 集成](./docs/09-MCP集成.md) | 六种传输协议、OAuth、七种配置作用域 |

### 前瞻篇

| 文档 | 内容 |
|------|------|
| [10-未来功能蓝图](./docs/10-未来功能蓝图.md) | 82 个 feature flag 解析、Kairos 自主模式、Context Collapse、语音模式 |
| [11-AI Coding 时代的 Code Review](./docs/11-AI-Coding时代的Code-Review.md) | 个人/团队/CICD 三层 Review 范式、Review Agent 设想 |
| [12-从权限系统学 Agent 安全设计](./docs/12-从Claude%20Code权限系统学Agent安全设计.md) | 三层防御体系拆解、L0-L4 安全成熟度模型、落地建议 |
| [13-啃完源码之后的一些发现](./docs/13-啃完源码之后的一些发现.md) | AI 工程化短板、生产事故、Claude 封号机制分析 |

## 上手复现

被还原出来的源码仓库可以直接跑：

```bash
git clone https://github.com/anthropics/claude-code.git
cd claude-code

bun install
bun run build
# ✓ Bundled 5344 modules in 554ms
#   cli.js  25.89 MB

bun run dev --version
# 2.1.888 (Claude Code)
```

## 关键数据

| 指标 | 数据 |
|------|------|
| 代码总量 | 515,498 行 TypeScript/TSX |
| 文件数 | 2,766 |
| 构建产物 | 25.89 MB，5,344 模块 |
| 内置工具 | 40+ |
| Feature Flag | 82 个 |
| 权限拦截规则 | 42 条硬编码危险模式 |
| 消息压缩阈值 | 上下文窗口 - 13,000 token |
| npm 依赖 | 583 个包 |

## 版本覆盖范围

源码解读一定会过时，所以这里把「分析基于哪个快照」写清楚，而不是让读者去猜。

| 项 | 值 |
|---|---|
| 源码快照 | 2026-03-31 的 sourcemap 还原产物 |
| 该构建自报版本 | `2.1.888` |
| 章节内容最近复核 | 2026-04-24（v1.0.0） |
| 仓库最近维护 | 2026-08-11（v1.1.0，链接与工具链，未改章节结论） |

上游改动导致某章描述失效，请开 [版本漂移 issue](https://github.com/anneheartrecord/claude-code-docs/issues/new?template=version-drift.yml)。这是本仓库最欢迎的一类 issue。

## 参与贡献

最有价值的贡献不是加内容，是**指出哪里写错了**。事实纠错优先级最高，issue 7 天内首次回应。

- 纠错与提议：见 [贡献指南](./CONTRIBUTING.md)（事实纠错必须带证据）
- 变更记录：见 [CHANGELOG.md](./CHANGELOG.md)
- 本地自检：`python3 scripts/check_links.py && python3 scripts/check_bilingual.py`

## License

[MIT](./LICENSE)，覆盖本仓库的原创内容：13 篇分析、插图、站点配置与 `scripts/`。

本仓库不再分发 Claude Code 源码本身；引用的代码片段、商标归属与准确性声明见 [NOTICE.md](./NOTICE.md)。

## Source & license

This open-source MCP server is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [anneheartrecord](https://github.com/anneheartrecord)
- **Source:** [anneheartrecord/claude-code-docs](https://github.com/anneheartrecord/claude-code-docs)
- **License:** MIT
- **Homepage:** https://anneheartrecord.github.io/claude-code-docs/

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/mcp-anneheartrecord-claude-code-docs
- Seller: https://agentstack.voostack.com/s/anneheartrecord
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
