CodeGraph 以 Model Context Protocol server 形式運行。安裝器設定的 agent 會自動啟動它,你不用手動啟動:
codegraph serve --mcp
有 .codegraph/ 索引時,agent 拿到下面的工具;沒有索引的 workspace,server 宣告 inactive 且不列任何工具——agent 照常用自己的內建工具,索引與否是你的決定。
codegraph_explore預設只暴露一個工具。它是 Read 的等價物:丟給它自然語言問題或一堆符號/檔名,它回傳逐行編號、逐位元組對齊 on-disk 的原始碼(依檔案分組,與 Read 工具相同格式),加上之間的呼叫路徑(含 dynamic-dispatch 跳躍:callback、React re-render、JSX children——grep 追不到的那些)與一個「什麼依賴這些」的 blast-radius 摘要。一次呼叫通常就回答完整個問題。
By default the server exposes a single tool, codegraph_explore. It's Read-equivalent: give it a natural-language question or a bag of symbol and file names, and it returns the verbatim, line-numbered source of the relevant symbols grouped by file — the same shape the Read tool gives you — plus the call paths between them (including dynamic-dispatch hops like callbacks, React re-render, and JSX children that grep can't follow) and a blast-radius summary of what depends on them.
以下工具全部仍可用,只是預設不列出——它們回的東西已經內嵌在 codegraph_explore 的回應裡(blast-radius 區、關係圖、符號本體與 callee 清單):
| 工具 | 用途 |
|---|---|
codegraph_node | 單一符號的原始碼 + caller/callee 軌跡,或帶行號的整個檔案讀取(Read 對等)。模糊名稱會回傳每個 overload 的本體。 |
codegraph_search | 依名稱在整個 codebase 找符號(只有位置)。 |
codegraph_callers | 找誰呼叫某個函數。 |
codegraph_callees | 找某個函數呼叫誰。 |
codegraph_impact | 分析改一個符號會影響哪些程式。 |
codegraph_files | 拿索引過的檔案結構(比掃描檔案系統快)。 |
codegraph_status | 檢查索引健康度與統計。 |
用 CODEGRAPH_MCP_TOOLS 環境變數重新啟用(逗號分隔的 allowlist,取代預設):
CODEGRAPH_MCP_TOOLS=explore,node,search,callers
每個也都有 CLI 對應(codegraph node / query / callers / callees / impact / files / status),給腳本與非 MCP harness 用。
CodeGraph 就是 pre-built 搜尋索引。對「X 怎麼運作」「架構」「A 怎麼到 B」以及編輯中的問題,agent 應該用 codegraph_explore 答完就停,通常零檔案讀取。直接答 = 一到幾次呼叫;grep+Read 探索 = 幾十次。
MCP server 會自動在 initialize response 把這份指引交給主 agent;sub-agent 與非 MCP harness 看不到那 response,所以安裝器另外在每個 agent 的 instructions 檔寫 marker-fenced 段落,指向 codegraph explore CLI 對等指令。
對 codegraph 自己問「codegraph_explore 怎麼組出答案」(節錄):
$ codegraph explore "how does codegraph_explore build its answer"
**Exploration: how does codegraph_explore build its answer**
Found 35 symbols across 2 files.
**Blast radius — what depends on these ...**
- `CodeGraph` (src/index.ts:139) — 136 callers in `__tests__/evaluation/runner.ts`,
`src/mcp/engine.ts`, `src/mcp/tools.ts`, `src/index.ts`; tests: ... +96
**Relationships**
instantiates:
- init → CodeGraph · initSync → CodeGraph
- open → CodeGraph · recreate → CodeGraph · openSync → CodeGraph
calls:
- constructor → wireLayers · sync → sync · watch → sync
- open → sync · init → indexAll · ... and 3 more
**Source Code**
> The code below is the verbatim, current on-disk source of these files ...
完整輸出見 dogfood.md。