code-graph
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Graph Skill
代码图技能
Purpose: Use the code graph (codebase-memory-mcp) for sub-millisecond
symbol lookup, function search, dependency analysis, and blast radius
detection. This replaces brute-force grep and file reading for code
navigation.
用途: 使用代码图(codebase-memory-mcp)实现亚毫秒级的符号查找、函数搜索、依赖分析和影响范围检测。这替代了暴力grep和文件读取的代码导航方式。
Core Principle
核心原则
Graph first, file second. Before reading files or grepping, query the
code graph. Only read full files when you need to modify them or need
context beyond what the graph provides.
Consider graph when planning. When planning any change — feature,
refactor, bug fix — start by querying the graph to understand scope,
dependencies, and blast radius. This applies to thinking and planning
phases, not just implementation. Grep is still the right tool for
searching string literals, log messages, config values, and content
that lives outside code structure.
┌────────────────────────────────────────────────────────────────┐
│ GRAPH FIRST, FILE SECOND │
│ ─────────────────────────────────────────────────────────────│
│ The code graph indexes your entire codebase as a persistent │
│ knowledge graph. Claude queries it via MCP for instant │
│ symbol lookup, dependency chains, and blast radius — instead │
│ of reading hundreds of files. │
│ │
│ 14 MCP tools │ 64 languages │ sub-ms queries │ zero deps │
│ ~99% fewer tokens for navigation vs brute-force file reads │
├────────────────────────────────────────────────────────────────┤
│ AUTO-UPDATED │
│ ─────────────────────────────────────────────────────────────│
│ File watcher keeps graph in sync. Post-commit hook ensures │
│ freshness. No manual rebuild needed. │
└────────────────────────────────────────────────────────────────┘先查图,再读文件。 在读取文件或使用grep之前,先查询代码图。仅当需要修改文件或需要图中未提供的上下文时,才读取完整文件。
规划时考虑图的使用。 在规划任何变更——功能开发、重构、bug修复——时,先查询图以了解范围、依赖关系和影响范围。这适用于思考和规划阶段,而不仅仅是实现阶段。Grep仍然是搜索字符串字面量、日志消息、配置值和代码结构之外内容的合适工具。
┌────────────────────────────────────────────────────────────────┐
│ 先查图,再读文件 │
│ ─────────────────────────────────────────────────────────────│
│ 代码图将整个代码库索引为持久化知识图谱。Claude通过MCP查询它,以实现即时的│
│ 符号查找、依赖链和影响范围检测——无需读取数百个文件。 │
│ │
│ 14种MCP工具 │ 64种语言 │ 亚毫秒级查询 │ 零依赖 │
│ 与暴力读取文件相比,导航所用令牌减少约99% │
├────────────────────────────────────────────────────────────────┤
│ 自动更新 │
│ ─────────────────────────────────────────────────────────────│
│ 文件监视器保持图同步。提交后钩子确保 │
│ 图的新鲜度。无需手动重建。 │
└────────────────────────────────────────────────────────────────┘When to Use Graph vs Direct Read
何时使用图工具 vs 直接读取文件
| Task | Use Graph Tool | Use Direct Read? |
|---|---|---|
| Find function/class definition | | No |
| Get function signature + docs | | No |
| Find all callers of a function | | No |
| Trace dependency chain | | No |
| Determine blast radius of change | | No |
| Understand project architecture | | No |
| Search for code patterns | | No |
| Read full implementation to modify | | Yes |
| Understand business logic context | | Yes |
Rule: If a graph tool can answer the question, use it. Only open files
when you need the full source to make edits.
| 任务 | 使用图工具 | 是否直接读取? |
|---|---|---|
| 查找函数/类定义 | | 否 |
| 获取函数签名+文档 | | 否 |
| 查找函数的所有调用者 | | 否 |
| 追踪依赖链 | | 否 |
| 确定变更的影响范围 | | 否 |
| 理解项目架构 | | 否 |
| 搜索代码模式 | | 否 |
| 读取完整实现以进行修改 | | 是 |
| 理解业务逻辑上下文 | | 是 |
规则: 如果图工具可以回答问题,就使用它。仅当需要完整源代码进行编辑时才打开文件。
Available MCP Tools
可用的MCP工具
Indexing & Status
索引与状态
| Tool | Purpose | When to Use |
|---|---|---|
| Build/rebuild graph for a project | First setup, or after major restructure |
| Check if graph is current | Before querying, if unsure of freshness |
| List all indexed projects | Multi-project navigation |
| 工具 | 用途 | 使用场景 |
|---|---|---|
| 为项目构建/重建代码图 | 首次设置,或重大重构后 |
| 检查代码图是否为最新状态 | 查询前不确定图的新鲜度时 |
| 列出所有已索引的项目 | 多项目导航时 |
Querying & Navigation
查询与导航
| Tool | Purpose | When to Use |
|---|---|---|
| Find symbols by name (fuzzy) | "Find auth-related functions" |
| Text search across indexed codebase | "Find TODO comments", pattern matching |
| Get source code for a specific symbol | Need signature, docstring, implementation |
| Understand graph structure and relationships | Exploring what data is available |
| Run structured graph queries | Complex dependency/relationship queries |
| 工具 | 用途 | 使用场景 |
|---|---|---|
| 按名称模糊查找符号 | "查找与认证相关的函数" |
| 在已索引代码库中进行文本搜索 | "查找TODO注释"、模式匹配 |
| 获取特定符号的源代码 | 需要签名、文档字符串、实现细节时 |
| 了解代码图的结构和关系 | 探索可用数据时 |
| 运行结构化图查询 | 复杂的依赖/关系查询时 |
Analysis
分析
| Tool | Purpose | When to Use |
|---|---|---|
| Trace caller/callee chains | "Who calls sendEmail?", "What does init() trigger?" |
| Identify changed files and blast radius | Before/after code changes, PR review |
| High-level module/package structure | Onboarding, understanding project layout |
| 工具 | 用途 | 使用场景 |
|---|---|---|
| 追踪调用者/被调用者链 | "谁调用了sendEmail?"、"init()触发了什么?" |
| 识别变更文件和影响范围 | 代码变更前后、PR评审时 |
| 获取高层次模块/包结构 | 新员工入职、理解项目布局时 |
Management
管理
| Tool | Purpose | When to Use |
|---|---|---|
| Remove a project from the graph | Cleanup, project restructure |
| Architecture decision records | Document architectural decisions |
| Import runtime traces | Performance analysis, dead code detection |
| 工具 | 用途 | 使用场景 |
|---|---|---|
| 从代码图中移除项目 | 清理、项目重构时 |
| 架构决策记录 | 记录架构决策时 |
| 导入运行时追踪数据 | 性能分析、死代码检测时 |
Workflow: Before Any Code Change
工作流:任何代码变更之前
0. PLAN → get_architecture + search_graph to understand scope before planning
1. LOCATE → search_graph to find the symbol
2. UNDERSTAND → get_code_snippet for context
3. BLAST → detect_changes to assess impact
4. TRACE → trace_call_path to find all affected callers
5. CHANGE → Read file, make edit
6. VERIFY → detect_changes again to confirm scopeStep 0 applies to planning, not just coding. When the user asks you to
plan a feature, refactor, or fix — query the graph first to understand
what exists, what depends on what, and what the scope looks like. This
prevents plans based on wrong assumptions about the codebase.
Never skip step 3. Blast radius analysis prevents unexpected breakage
from changes to shared code.
0. 规划 → 使用get_architecture + search_graph在规划前了解范围
1. 定位 → 使用search_graph查找符号
2. 理解 → 使用get_code_snippet获取上下文
3. 影响范围 → 使用detect_changes评估影响
4. 追踪 → 使用trace_call_path查找所有受影响的调用者
5. 变更 → 读取文件,进行编辑
6. 验证 → 再次使用detect_changes确认范围步骤0适用于规划,而非仅编码阶段。 当用户要求你规划功能、重构或修复bug时——先查询代码图,了解现有内容、依赖关系和范围。这能避免基于对代码库错误假设的规划。
永远不要跳过步骤3。 影响范围分析可防止共享代码变更导致意外故障。
Graph Data & Freshness
图数据与新鲜度
The graph stays fresh automatically through 3 layers — no manual rebuild needed:
| Layer | Trigger | What Happens |
|---|---|---|
| File watcher | Every file save | codebase-memory-mcp detects changes and re-indexes affected files in real-time |
| Auto-index | Session start | |
| Post-commit hook | Every | Touches |
You do NOT need to manually re-index unless you do a major restructure
(rename entire directories, switch branches with massive diffs). In that
case: once, then the 3 layers keep it fresh.
index_repository- Storage: directory (auto-created, gitignored)
.code-graph/ - MCP config: at project root (committed, shared with team)
.mcp.json
代码图通过三层机制自动保持新鲜——无需手动重建:
| 层级 | 触发条件 | 操作内容 |
|---|---|---|
| 文件监视器 | 每次文件保存 | codebase-memory-mcp检测变更并实时重新索引受影响的文件 |
| 自动索引 | 会话启动 | |
| 提交后钩子 | 每次 | 标记 |
除非进行重大重构(重命名整个目录、切换差异巨大的分支),否则无需手动重新索引。 这种情况下:运行一次,之后三层机制会保持图的新鲜度。
index_repository- 存储:目录(自动创建,已加入git忽略)
.code-graph/ - MCP配置:项目根目录的(已提交,团队共享)
.mcp.json
MCP Configuration
MCP配置
The code graph MCP server is configured in at project root:
.mcp.jsonjson
{
"mcpServers": {
"codebase-memory": {
"command": "codebase-memory-mcp",
"args": []
}
}
}Installation:
~/.claude/install-graph-tools.sh代码图MCP服务器在项目根目录的中配置:
.mcp.jsonjson
{
"mcpServers": {
"codebase-memory": {
"command": "codebase-memory-mcp",
"args": []
}
}
}安装:
~/.claude/install-graph-tools.shDecision Framework
决策框架
Need to find a symbol/function?
→ search_graph (sub-ms, structured result)
→ NOT: grep -r "functionName" (slow, unstructured)
Need to understand dependencies?
→ query_graph or trace_call_path (complete, traversable)
→ NOT: manually reading import statements
Need to assess change impact?
→ detect_changes (comprehensive, instant)
→ NOT: searching for usages manually across files
Need to understand architecture?
→ get_architecture (high-level overview)
→ NOT: reading every directory listing
Need to read/modify code?
→ search_graph to locate, then Read the specific file
→ NOT: reading entire directories hoping to find it需要查找符号/函数?
→ 使用search_graph(亚毫秒级,结构化结果)
→ 不要使用:grep -r "functionName"(缓慢,非结构化)
需要理解依赖关系?
→ 使用query_graph或trace_call_path(完整,可遍历)
→ 不要使用:手动读取导入语句
需要评估变更影响?
→ 使用detect_changes(全面,即时)
→ 不要使用:手动跨文件搜索用法
需要理解架构?
→ 使用get_architecture(高层次概述)
→ 不要使用:读取每个目录列表
需要读取/修改代码?
→ 使用search_graph定位,然后读取特定文件
→ 不要使用:读取整个目录试图找到目标Anti-Patterns
反模式
| Anti-Pattern | Do This Instead |
|---|---|
| Grepping for function names | |
| Reading entire files to find a signature | |
| Manually tracing import chains | |
| Making changes without checking impact | |
| Reading all files in a directory | |
| Ignoring graph staleness warnings | Check |
| Re-indexing on every query | Trust the file watcher; only manual re-index after major restructure |
| 反模式 | 正确做法 |
|---|---|
| 用grep搜索函数名 | 使用 |
| 读取整个文件以查找签名 | 使用 |
| 手动追踪导入链 | 使用 |
| 未检查影响就进行变更 | 在修改共享代码前使用 |
| 读取目录中的所有文件 | 使用 |
| 忽略图过期警告 | 检查 |
| 每次查询都重新索引 | 信任文件监视器;仅在重大重构后手动重新索引 |