CodeGraph — 代码知识图谱:安装、配置与深度使用指南
1. 概述:什么要使用 CodeGraph
CodeGraph 是一个本地优先的代码智能库 + CLI + MCP 服务器,通过 @colbymchenry/codegraph 发布在 npm。它利用 tree-sitter 解析代码库,将符号(symbol)、调用边(edges)和文件结构存储于 SQLite + FTS5 数据库中,并通过 MCP(Model Context Protocol) 向 AI 编程助手暴露知识图谱查询能力。
支持的 AI 工具包括:Claude Code、Cursor、Codex CLI、OpenCode、Gemini CLI、Antigravity、Kiro、Hermes Agent 等。
核心价值
| 指标 | 效果 |
|---|---|
| 工具调用次数 | 减少约 58%–70% |
| 代码探索速度 | 提升约 50% |
| Token 消耗 | 降低约 40%–60% |
| 数据安全 | 100% 本地,零数据外泄 |
2. 架构原理
CodeGraph 采用分层处理管线:
源文件 → ExtractionOrchestrator (tree-sitter) → DB (nodes / edges / files)
↓
ReferenceResolver (import、命名匹配、框架模式)
↓
GraphQueryManager / GraphTraverser (callers, callees, impact)
↓
ContextBuilder (Markdown / JSON,供 AI 消费)
| 组件 | 技术选型 |
|---|---|
| 解析层 | tree-sitter(原生 / WASM) |
| 存储层 | SQLite + FTS5 全文检索 |
| 同步层 | 原生 OS 文件监听(FSEvents / inotify / ReadDirectoryChanges) |
| 接口层 | MCP 协议(stdio 传输) |
关键设计原则:
- 确定性解析:所有符号和边由 AST 派生,非 LLM 摘要,杜绝幻觉。
- 增量同步:文件内容哈希 + 防抖监听,仅重索引变更文件。
- 单次调用获取上下文:一个
codegraph_explore调用的效果等同于数十次 grep/glob/read 的组合。
3. 安装
3.1 方式一:Shell 安装脚本(无需 Node.js)
CodeGraph 捆绑了自己的运行时,无需预装 Node.js。
macOS / Linux:
curl -fsSL https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.sh | sh
Windows(PowerShell):
irm https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.ps1 | iex
3.2 方式二:npm 全局安装(Node.js ≥ 18,< 25)
npm install -g @colbymchenry/codegraph
3.3 方式三:npx 零安装(一次性)
npx @colbymchenry/codegraph
⚠️ 注意:安装完成后请打开新终端窗口,确保
codegraph命令在 PATH 中生效。
3.4 验证安装
codegraph --version
4. 配置——接入 AI 编程助手
安装 CLI 后,运行交互式安装程序:
codegraph install
安装程序会:
- 自动检测已安装的 AI 工具(Claude Code、Cursor、Codex CLI 等)
- 询问是否将
codegraph安装到 PATH - 提示选择配置范围:global(全局所有项目)或 local(仅当前项目)
- 写入各工具的 MCP 服务器配置
- 为 Claude Code 配置自动授权(auto-allow)权限列表
4.1 非交互 / CI 环境参数
| 参数 | 值 | 默认 |
|---|---|---|
--target |
auto、all、none 或逗号分隔列表(如 claude,cursor) |
提示选择 |
--location |
global、local |
提示选择 |
--yes |
布尔标志 | 逐步确认 |
--no-permissions |
跳过 Claude 自动授权列表 | 写入权限 |
--print-config <id> |
输出单个工具的配置片段并退出 | — |
示例:
codegraph install --yes # 自动检测,全局安装
codegraph install --target=cursor,claude --yes # 指定目标工具
codegraph install --target=auto --location=local # 检测到的工具,项目级配置
codegraph install --print-config codex # 仅打印,不写文件
4.2 验证配置
使用以下命令查看索引状态:
codegraph status
5. 项目初始化与日常使用
5.1 初始化知识图谱
cd your-project
codegraph init -i
codegraph init:创建.codegraph/目录。codegraph init -i(或--index):创建目录并立即构建初始索引。
当一个项目中存在 .codegraph/ 目录时,已配置的 AI 工具将自动启用 CodeGraph 工具。
5.2 增量同步
日常编码后,运行以下命令将变更增量同步至索引:
codegraph sync
底层机制:OS 原生文件监听器 + 防抖 + 内容哈希 → 仅重索引实际变更的文件。索引延迟通常在秒级以内。
5.3 查看状态
codegraph status
输出内容包括:索引文件数、符号总数、边总数、最后同步时间、当前项目语言分布。
5.4 强制重建索引
codegraph index --force
适用场景:切换分支后大量文件变化、索引结构升级、或怀疑索引不一致时。
6. CLI 命令速查
| 命令 | 功能 |
|---|---|
codegraph install |
交互式安装并配置 AI 工具 |
codegraph uninstall |
从所有已配置的工具中移除 CodeGraph |
codegraph init [path] |
初始化项目(-i 同时构建索引) |
codegraph uninit [path] |
从项目移除 .codegraph/(--force 跳过确认) |
codegraph index [path] |
全量索引构建(--force 强制重建) |
codegraph sync [path] |
增量更新索引 |
codegraph status [path] |
显示统计信息 |
codegraph query <search> |
符号搜索(--kind、--limit、--json) |
codegraph files [path] |
显示索引文件结构 |
codegraph context <task> |
为 AI 构建上下文 |
codegraph callers <symbol> |
查找调用方 |
codegraph callees <symbol> |
查找被调用方 |
codegraph impact <symbol> |
影响分析(--depth 控制深度) |
codegraph affected [files...] |
查找受变更影响的测试文件 |
codegraph serve --mcp |
启动 MCP 服务器 |
codegraph workspace ... |
多项目工作区管理 |
codegraph upgrade |
原地升级到最新版本 |
codegraph upgrade --check |
检查是否有可用更新 |
codegraph upgrade <version> |
升级到指定版本 |
7. MCP 工具详解(Agent 可用工具)
| 工具 | 用途 | 典型场景 |
|---|---|---|
codegraph_search |
按名称搜索符号 | “找到 AuthService 在哪里定义” |
codegraph_explore |
主工具——自然语言或符号组合探索,返回相关代码原文 | “用户登录流程是怎么工作的?” |
codegraph_callers |
列出调用目标符号的函数 | “哪些地方调用了 deleteUser?” |
codegraph_callees |
列出目标符号调用的函数 | “loginHandler 内部调用了哪些函数?” |
codegraph_impact |
变更影响传递分析(N 跳) | “重构 UserModel 会影响哪些文件?” |
codegraph_node |
获取单个符号的完整源码及位置 | “显示 encryptPassword 的完整实现” |
codegraph_files |
索引文件树及语言统计 | “项目结构概览” |
codegraph_status |
索引进度及健康检查 | “索引是否最新?” |
7.1 codegraph_explore——首选工具
对于绝大多数问题(“X 如何工作”、架构探索、Bug 定位),第一个并且通常是唯一需要调用的工具就是 codegraph_explore。它返回按文件分组的符号完整源码,单次调用即可替代数十次 grep + read 的组合操作。
支持输入形式:
- 自然语言问题:
"How does user authentication flow work?" - 符号/文件混合:
"AuthService loginUser session-manager"
7.2 codegraph_impact——重构前必用
在修改任何公共接口前,运行 codegraph_impact <symbol> 评估变更影响范围。--depth 参数控制传递跳数(默认 2,建议设置为 3–5 进行全面评估)。
8. 高级特性
8.1 支持的语言(20+)
TypeScript · JavaScript · Python · Go · Rust · Java · C# · PHP · Ruby · C · C++ · Objective-C · Swift · Kotlin · Dart · Lua · Luau · Svelte · Liquid · Pascal/Delphi
8.2 框架感知路由
自动识别 14 种框架的路由文件,建立 URL 模式 → 处理函数的映射:
Express · Django · Flask · FastAPI · Laravel · Rails · Spring · Gin · Axum · ASP.NET · React Router · SvelteKit · Vue/Nuxt · Cargo Workspaces
8.3 动态调度覆盖
合成以下非静态调用路径的边,使探索工具能追踪到 grep 无法发现的关系:
- 回调 / 观察者模式
- EventEmitter(Node.js)
- React 重渲染(
setState → render) - JSX 子组件传递
- Django ORM 描述符
8.4 跨语言追踪(iOS / React Native / Expo)
- Swift ↔ Objective-C 桥接
- React Native Bridge ↔ TurboModules ↔ Fabric
- 原生 → JS 事件发射器
- Expo Modules
8.5 可视化探索器
启动后在 http://localhost:7777 提供交互式界面:
- D3 力导向图:以图的方式浏览符号及调用关系。
- 圈复杂度树图:识别复杂度过高的热点代码区域。
9. 手动配置(不使用 codegraph install)
若需手动接入 MCP,可自行编辑配置文件。
9.1 Claude Code
在 ~/.claude.json 中添加:
{
"mcpServers": {
"codegraph": {
"type": "stdio",
"command": "codegraph",
"args": ["serve", "--mcp"]
}
}
}
在 ~/.claude/settings.json 中配置自动授权(推荐):
{
"permissions": {
"allow": [
"mcp__codegraph__codegraph_search",
"mcp__codegraph__codegraph_explore",
"mcp__codegraph__codegraph_callers",
"mcp__codegraph__codegraph_callees",
"mcp__codegraph__codegraph_impact",
"mcp__codegraph__codegraph_node",
"mcp__codegraph__codegraph_status",
"mcp__codegraph__codegraph_files"
]
}
}
9.2 Cursor
在 Cursor 设置中添加 MCP 服务器:
{
"mcpServers": {
"codegraph": {
"command": "codegraph",
"args": ["serve", "--mcp"]
}
}
}
10. 升级与卸载
10.1 升级
codegraph upgrade # 升级到最新版
codegraph upgrade --check # 仅检查更新
codegraph upgrade 0.9.9 # 升级到指定版本
10.2 卸载
# 步骤 1:从 AI 工具中移除 MCP 配置(.codegraph/ 目录不受影响)
codegraph uninstall
# 步骤 2:从项目中移除 .codegraph/ 索引
codegraph uninit --force
# 步骤 3:卸载 npm 包(如通过 npm 安装)
npm uninstall -g @colbymchenry/codegraph
11. 工作流最佳实践
- 项目接入第一步:
codegraph init -i构建初始索引,后续 AI 即可自动感知代码结构。 - 重构前的例行检查:
codegraph impact <symbol>评估变更范围,避免遗漏调用方。 - 切换分支后:运行
codegraph sync或codegraph index --force确保索引与代码一致。 - 多项目管理:使用
codegraph workspace子命令统一管理多个代码库的索引。 - CI 集成:在 CI 流程中运行
codegraph install --target=all --yes+codegraph init -i实现自动化初始化。
12. 故障排查
| 问题 | 解决方案 |
|---|---|
codegraph 命令找不到 |
打开新的终端窗口;或检查 npm 全局安装路径是否在 PATH 中 |
| AI 工具未识别 CodeGraph | 运行 codegraph install 重新配置;重启 AI 工具 |
| 索引与代码不一致 | 运行 codegraph sync 增量同步;或 codegraph index --force 强制重建 |
| 大型项目索引慢 | 初次全量索引耗时正常(取决于项目规模);后续增量同步为秒级 |
| MCP 连接错误 | 运行 codegraph serve --mcp 手动启动并观察错误日志 |
参考资源
- GitHub 仓库: github.com/colbymchenry/codegraph
- npm 包: @colbymchenry/codegraph
- Node 版本要求:
>=18.0.0 <25.0.0 - 当前版本: 0.9.9
更多推荐


所有评论(0)