AgentHistoryMCP
<div align="center">
# agent-history-mcp
**跨 AI Coding Agent 的统一本地历史检索与无缝上下文交接 MCP Server**
[](https://www.npmjs.com/package/agent-history-mcp)
[](./LICENSE)
[](https://nodejs.org/)
[](https://www.typescriptlang.org/)
[](https://modelcontextprotocol.io/)
[](#核心特性)
**简体中文** · [English](./README.en.md)
<br />
<img src="./assets/demo.gif" alt="agent-history-mcp Cross-Agent Handoff Demo" width="100%" />
</div>
---
## 为什么做 agent-history-mcp?
当你在一个项目上同时使用多个 AI Coding Agent —— 比如先在 **Codex CLI** 里讨论架构、改完第一轮代码,接着切到 **Claude Code**、**Cursor**、**Gemini CLI**、**Antigravity** 或 **Pi** 继续开发时,每个工具的对话历史都各自锁在私有目录和不同格式里,上下文瞬间断层。
**agent-history-mcp** 提供**一个统一、纯本地、只读原始数据的 MCP Server**:
- 所有客户端接入**同一套 6 个只读 MCP 工具**,无需为每个工具单独配置同步脚本。
- 切换工具后只需一句:**“接手我刚才在 Codex 里关于这个项目的工作继续开发”**,当前 Agent 即可毫秒级定位项目关联文件,现场直读最新纯对话,无缝续写。
> **偏好零常驻 MCP 开销的 Skill 形态?** 查看独立项目 **[agent-history-skills](https://github.com/luo0629/agent-history-skills)** —— 打包为单文件零依赖本地引擎,支持 `npx skills add luo0629/agent-history-skills` 一键安装。
---
## 架构一览
<p align="center">
<img src="./assets/architecture.svg" alt="AgentHistoryMCP Architecture" width="100%" />
</p>
**工作原理(三步极简闭环):**
1. **首次查询按需打标**:不常驻后台、不主动偷扫硬盘;首次查询项目 A 时,为其他工具里未登记的会话文件记录轻量“项目标签卡片”(`project_id` → 原始文件路径)。
2. **增量秒级更新**:之后在任意工具新开或续聊,下次查询仅花 ~20ms 对账新增/变更的少数文件,已打标的老文件完全不重扫。
3. **现场直读纯对话**:`get_handoff_context` 与 `read_session` 直接按卡片定位原始会话文件,**现场读取最新纯 `user`/`assistant` 对话**(彻底跳过冗长 `tool_result`),不复制正文副本。
---
## 实测性能提升
<p align="center">
<img src="./assets/benchmark.svg" alt="AgentHistoryMCP Real-World Benchmark" width="100%" />
</p>
基于真实 Windows 本机多工具历史库实测(**1,540 个会话文件**,含 **1.7 GB Codex JSONL/ZST** 与 **965 MB Cursor `state.vscdb`**):
| 实测维度 | 优化前(全量消息副本 + `LIKE` 扫描) | 优化后(项目卡片 + 现场直读 + B-Tree) | 提升效果 |
| :--- | :--- | :--- | :--- |
| **965 MB Cursor 库查询** | `LIKE` 全表扫描(>30s 超时) | 主键 B-Tree 范围索引 + 单会话 `O(1)` 直读 | **消除超时,毫秒级定位** |
| **冷启动同步吞吐** | 10.8s 处理 10 个文件 | **6.0s** 处理 33 个文件(覆盖 6 工具 105 会话) | **吞吐量提升 3.3×** |
| **Cursor 消息信噪比** | 15,569 条(混杂大量 `tool_result`) | **3,372 条**纯对话(92 个会话) | **冗余噪音减少 78.3%** |
| **本地缓存正文副本** | 完整复制 `messages` 表 | **零 `messages` 表、零 `tool_result`** | **随时可删重建,极轻占用** |
---
## 核心特性
- **零正文副本、不存 `tool_result`**:`~/.agent-history/index.db` 只存轻量项目标签目录卡片与纯对话 FTS5 倒排索引;读取与交接直接从原始文件现场直读。
- **一处接入,全工具打通**:基于 4 大可复用存储原型基类,开箱覆盖 **15+ 主流 CLI 与 IDE Coding Agent**。
- **专为“切换工具”打造的交接能力**:`get_handoff_context` 自动排除当前工具(`exclude_agent`),按字符预算精准提取目标项目的初始需求、最新几轮对话与改动文件列表。
- **中英文 + 代码标识符混合检索**:利用 Node 原生 `Intl.Segmenter("zh-CN")` 分词 + 驼峰/下划线切分 + SQLite FTS5 BM25 与时间衰减排序。
- **零 C++ 编译依赖、跨平台零踩坑**:基于 Node.js 22.5+ 内置 `node:sqlite`、原生 `Intl.Segmenter` 与纯 JS `fzstd`,告别 `node-gyp`。
- **隐私与安全优先**:100% 本地离线只读、自动脱敏常见 API Key/Token、自动跳过本 MCP 自身调用防套娃,并用 `<historical_session_data>` 隔离不可信历史文本。
---
## 支持的 AI Coding Agent
### CLI 工具
| 工具 | `agent_id` | 支持方式与特色能力 |
| :--- | :--- | :--- |
| **Codex CLI** | `codex` | JSONL 流解析,透明解压 `.jsonl.zst` / `.gz` 归档与 `cwd` 识别 |
| **Claude Code** | `claude` | 解析主会话与 `subagents`,保留树状关系与编码目录校验 |
| **Gemini CLI** | `gemini` | 兼容 JSON / JSONL 对话、`logs.json` 及 `projects.json` 映射 |
| **Pi** | `pi` | 完整保留 DAG 多分支与 `branch_summary`,支持按分支阅读 |
| **OpenCode** | `opencode` | 只读白名单 SQLite 表,严格隔离凭据与认证表 |
| **Aider** | `aider` | 自动关联已发现项目根目录的 `.aider.chat.history.md` |
| **GitHub Copilot CLI** | `copilot_cli` | 解析 `workspace.yaml` 与 `events.jsonl` 会话事件流 |
### IDE 与桌面端工具
| 工具 | `agent_id` | 支持方式与特色能力 |
| :--- | :--- | :--- |
| **Cursor** | `cursor` | B-Tree 范围索引直读 `state.vscdb`,支持单会话 `O(1)` 现场直读 |
| **Antigravity** | `antigravity` | 直达解析 `brain/<id>` 紧凑版 `transcript.jsonl` |
| **VS Code Copilot Chat** | `vscode_copilot` | 兼容新旧 JSONL / JSON 会话,自动反查 `workspace.json` 项目路径 |
| **Cline** | `cline` | 支持 VS Code / Cursor / Windsurf 多宿主任务历史解析 |
| **Roo Code** | `roo_code` | 同构支持多宿主任务目录与对话记录解析 |
| **Kiro IDE** | `kiro` | 双版本解析链:同时兼容 1.x `messages.jsonl` 与旧版执行 JSON |
| **Augment / Auggie** | `augment` | 探测本地 `sessions/` JSON / JSONL 并兼容导出文件 |
| **Windsurf Cascade** | `windsurf` | 诚实探测加密 `.pb` 文件(AES-256-GCM),自动索引 Markdown 导出 |
### 通用导入兜底
| 工具 | `agent_id` | 支持方式与特色能力 |
| :--- | :--- | :--- |
| **Universal Import** | `universal_import` | 将任意工具导出的 MD / JSON / JSONL / HTML 放入导入目录即可统一检索 |
---
## 快速开始
> **环境要求**:Node.js `>= 22.5.0`(内置 `node:sqlite`,零外部数据库与编译工具依赖)
### 方式一:CLI 工具一行命令安装(自动拉取最新版本)
```bash
# Claude Code(全局生效)
claude mcp add agent-history -s user -- npx -y agent-history-mcp@latest
# Codex CLI(自动写入 ~/.codex/config.toml)
codex mcp add agent-history -- npx -y agent-history-mcp@latest
# Gemini CLI(全局生效)
gemini mcp add -s user agent-history -- npx -y agent-history-mcp@latest
```
### 方式二:通用 `mcpServers` JSON(Cursor / Antigravity / Windsurf / Cline / Roo Code / Kiro / Augment)
将以下片段放入对应工具的 MCP 配置文件即可:
```json
{
"mcpServers": {
"agent-history": {
"command": "npx",
"args": ["-y", "agent-history-mcp@latest"]
}
}
}
```
<details>
<summary>查看 OpenCode / VS Code / Codex 手动配置文件写法</summary>
#### OpenCode (`~/.config/opencode/opencode.json`)
```json
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"agent-history": {
"type": "local",
"command": ["npx", "-y", "agent-history-mcp@latest"],
"enabled": true
}
}
}
```
#### VS Code / GitHub Copilot Chat (`.vscode/mcp.json`)
```json
{
"servers": {
"agent-history": {
"type": "stdio",
"command": "npx",
"args": ["-y", "agent-history-mcp@latest"]
}
}
}
```
#### Codex CLI (`~/.codex/config.toml`)
```toml
[mcp_servers.agent-history]
command = "npx"
args = ["-y", "agent-history-mcp@latest"]
```
</details>
<details>
<summary>从源码本地构建(开发者模式)</summary>
```bash
git clone https://github.com/luo0629/agent-history-mcp.git
cd agent-history-mcp
npm install && npm run build && npm link
```
</details>
---
## MCP 工具列表(6 个统一只读工具)
| 工具名 | 核心用途 |
| :--- | :--- |
| **`get_handoff_context`** | 一键组装目标项目的跨工具交接包(支持 `exclude_agent`,现场直读最新纯对话) |
| **`search_history`** | 跨工具中英文 + 代码标识符全文检索(仅搜纯对话,零 `tool_result` 噪音) |
| **`list_sessions`** | 按项目 / Agent / 时间列出历史会话卡片 |
| **`read_session`** | 按卡片定位原始文件,**现场直读**分页或指定分支的纯对话 |
| **`list_projects`** | 列出本机所有已聚合项目及活跃工具分布 |
| **`get_agents_status`** | 五维探测所有适配器健康状态,并支持手动触发增量同步 |
---
## 内置诊断 CLI
无参数运行默认启动 **STDIO MCP Server**;传入子命令即可作为本地诊断工具使用:
```bash
npx -y agent-history-mcp@latest doctor # 一键体检所有 15+ 工具与索引统计
npx -y agent-history-mcp@latest sync --full # 对超大历史库执行一次全量离线索引
npx -y agent-history-mcp@latest projects # 查看已聚合的本地项目列表
npx -y agent-history-mcp@latest search "VideoPipeline 工作流" # 本地验证中英文 + 代码混合检索
npx -y agent-history-mcp@latest handoff "D:/Project/Verso" --exclude claude # 预览跨工具交接上下文包
npx -y agent-history-mcp@latest print-config # 打印各客户端即拷即用配置
```
---
## 高级配置与隐私保护
<details>
<summary>点击展开 <code>~/.agent-history/config.json</code> 配置示例(默认零配置开箱即用)</summary>
空字符串的 `dataDir` 表示启用自动发现(绝不会被当作当前工作目录)。
```json
{
"general": {
"databasePath": "~/.agent-history/index.db",
"autoDiscover": true,
"readOnlySources": true,
"syncDebounceSeconds": 3,
"maxContextChars": 20000
},
"agents": {
"codex": { "enabled": true, "dataDir": "" },
"claude": { "enabled": true, "dataDir": "" },
"cursor": { "enabled": true, "dataDir": "" },
"universal_import": { "enabled": true, "dataDir": "~/.agent-history/imports" }
},
"privacy": {
"redactSecrets": true,
"excludeProjects": [],
"excludePatterns": ["*.env", "*secret*", "*credential*"]
}
}
```
</details>
- **原始文件只读**:绝不修改任何工具的原始会话文件;`index.db` 随时可删并自动重建。
- **自动密钥脱敏**:返回前遮蔽常见 API Key、GitHub Token、Bearer Token、数据库连接串密码及私钥块。
- **凭据黑名单**:严格跳过 `.env`、`auth.json`、`oauth_creds.json` 及数据库认证表。
- **Prompt Injection 隔离**:历史正文统一包裹在 `<historical_session_data ...>` 标签内。
---
## 开发与测试
```bash
npm run typecheck # TypeScript 严格类型检查
npm run build # 编译输出 dist/
npm test # 运行 25 项单元与真实 STDIO MCP 集成测试
```
## 相关项目:agent-history-skills
如果你使用支持 Agent Skills 的工具(如 Claude Code、Codex CLI、Cursor、Antigravity、Pi、OpenCode 等),且希望**平时零常驻工具 Schema 占用**并内置跨工具交接 SOP,可以使用独立发布的 **[agent-history-skills](https://github.com/luo0629/agent-history-skills)**(单文件自包含引擎,零 `npm install`,数据目录完全独立于 `~/.agent-history-skill/`):
```bash
npx skills add luo0629/agent-history-skills
```
| 选型建议 | agent-history-mcp(本项目) | [agent-history-skills](https://github.com/luo0629/agent-history-skills) |
| :--- | :--- | :--- |
| **适用场景** | 无 Shell 权限、希望 `readOnlyHint` 免终端审批弹窗、或仅支持 MCP 的客户端 | 追求零常驻上下文占用、希望内置交接 SOP 与一键 Skill 分发的工具 |
## License
本项目基于 [MIT License](./LICENSE) 开源。
---
<p align="center">
如果这个项目帮你在多个 AI Coding Agent 之间省下了不少来回复制上下文的时间,制作不易,欢迎顺手点个 ⭐ <strong>Star</strong> 支持一下~你的支持是持续适配更多工具与版本迭代的最大动力!
</p>
TDQS
Scored across 6 tools
Each tool maps to a distinct operation: agent discovery/health, project listing, session listing, single-session reading, cross-agent full-text search, and a compact handoff pack. The only near-neighbor pair is get_handoff_context vs read_session, but one aggregates recent turns across sessions while the other pages through one session file, and the descriptions make that split explicit.
All names follow a consistent verb_noun snake_case pattern (get_, list_, search_, read_). The verbs accurately signal the operation type, making the set predictable to scan.
Six tools is well-scoped for a read-only cross-agent history server; each one covers a distinct stage of the discover-to-retrieve workflow with no filler.
The lifecycle is covered end to end: discover agents, list projects, list sessions, read or search turns, and package a handoff. Minor gaps remain — index refresh is buried as an optional flag on get_agents_status rather than its own tool, and there is no way to surface omitted tool outputs or per-session metadata without a full read, though both appear to be deliberate design choices.