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。
MCP(Model Context Protocol)是 AI agent 與外部工具溝通的標準協議。CodeGraph 作為 MCP server,讓 agent 可以「像人類一樣」詢問程式碼結構問題,而不是靠 grep 重建結構。
「只暴露一個工具」的設計哲學:
假設你在 code review,想知道「修改 buildContext 會影響哪些地方?」
# 在 agent session 中直接問
你:「buildContext 這個函式被誰呼叫?改動它會影響什麼?」
agent 呼叫 codegraph_explore:
{
"query": "buildContext callers and impact"
}
# 回傳(節錄):
**Exploration: buildContext callers and impact**
Found 35 symbols across 2 files.
**Callers of "buildContext" (3):**
- method buildContext
src/index.ts:1842
- file context-ranking.test.ts
__tests__/context-ranking.test.ts:1
- file context.test.ts
__tests__/context.test.ts:1
**Impact radius (depth 2):**
- 12 affected symbols
- 3 test files
- 2 downstream services
關鍵觀察:一次呼叫就拿到「誰呼叫它」+「影響半徑」+「相關測試」,不需要多次 grep + Read。
| 症狀 | 原因 | 解法 |
|---|---|---|
| MCP server 連不上 | 專案未初始化或未索引 | 執行 codegraph init 確認 .codegraph/ 存在 |
| 工具未列出 | 無索引或 CODEGRAPH_MCP_TOOLS 設定錯誤 | 執行 codegraph status 檢查索引,確認環境變數格式 |
| 回傳結果不完整 | 索引過期 | 執行 codegraph sync 更新索引 |
| 回應包含 staleness 警告 | 檔案剛修改,auto-sync 尚未完成 | 等待 2 秒或手動 codegraph sync |
CODEGRAPH_MCP_TOOLS 重新啟用隱藏工具codegraph_explore 回答架構問題前面的範例是單一 agent 問單一專案。這裡是更真實的協作:主 agent 在同一個 session 內分析兩個專案,而 heavy lifting 交給 sub-agent(它看不到 MCP initialize 的指引)。
# ① 主 agent:用 projectPath 切到另一個專案
codegraph_explore {
"query": "payment 服務的入口與依賴",
"projectPath": "/workspace/billing-service"
}
# ② sub-agent(看不到 initialize response):靠 instructions 檔
# CLAUDE.md / AGENTS.md 已被安裝器寫入 marker-fenced 段落:
# "使用 `codegraph explore <query>` CLI 對等指令回答結構性問題"
# ③ sub-agent 用 CLI 對等指令完成 deep-dive
codegraph explore "payment 的 blast radius"
codegraph callers charge --limit 20
# ④ 主 agent 用 CODEGRAPH_MCP_TOOLS 精簡工具面
# CODEGRAPH_MCP_TOOLS=explore,status # 只保留兩個
為什麼走這條路徑:MCP 的 initialize response 只有主 agent 看得到,sub-agent 與非 MCP harness 收不到——所以安裝器在每個 agent 的 instructions 檔寫 marker-fenced 段落指向 CLI 對等指令。知道「explore 與 CLI 輸出逐字元一致」,你就能信任「sub-agent 用 CLI 得到的答案 = 主 agent 用 MCP 得到的答案」。
內部架構:工具面是 allowlist 而非靜態清單。tools.ts 定義工具清單與 CODEGRAPH_MCP_TOOLS allowlist 邏輯;engine.ts 用 ContextBuilder + traverser 組 explore 的答案。關鍵設計:7 個未列工具回的東西已內嵌在 explore 回應(blast-radius、關係圖、本體與 callee 清單),所以「只列一個」不是閹割,是把「多工具調度」的成本省掉。server 在無索引的 workspace 會宣告 inactive 且不列任何工具——agent 自然退回內建工具。
| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| 工具列表只有 explore,想要更多 | 預設 allowlist 只有 explore | CODEGRAPH_MCP_TOOLS=explore,node,search 加回來 |
| sub-agent 不會用 explore | 看不到 initialize response | 確認 CLAUDE.md/AGENTS.md 有 marker-fenced 段落 |
| explore 連不上 | 無索引或未 init | codegraph init + codegraph status 確認 |
| 回應附 staleness banner | debounce 窗口內,檔案剛改 | 等同步或直接 Read 該檔 |
| WSL 上 socket 連線不穩 | local socket 在 Windows 磁碟路徑不可靠 | CODEGRAPH_NO_DAEMON=1 每個 session 自己跑 |
projectPath 參數解決了什麼問題?它與「每個專案各跑一個 server」在資源與一致性上的 trade-off 是什麼?在一個跨三個 repo 的微服務架構中,主 agent 用 MCP 同時分析多個專案,sub-agent 用 CLI 對等指令完成 deep-dive。以下是完整流程:
# ① 主 agent:切到另一個專案分析
codegraph_explore {
"query": "payment 服務的入口、API 端點與依賴",
"projectPath": "/workspace/billing-service"
}
# ② 主 agent:精簡工具面,只保留最必要的
# CODEGRAPH_MCP_TOOLS=explore,status
# ③ sub-agent(看不到 initialize response):
# CLAUDE.md 已被安裝器寫入 marker-fenced 段落:
# "使用 `codegraph explore <query>` CLI 對等指令回答結構性問題"
codegraph explore "payment 的 blast radius"
codegraph callers charge --limit 20
# ④ 主 agent 用 status 確認索引新鮮度
codegraph_explore { "query": "check index freshness", "tool": "codegraph_status" }
# ⑤ 驗證 MCP 與 CLI 的輸出一致性
# 兩邊驅動同一個 CodeGraph class,答案一定一致
關鍵觀察:initialize response 只有主 agent 看得到,sub-agent 靠 instructions 檔的 marker-fenced 段落。MCP 與 CLI 驅動同一個 CodeGraph class,答案 100% 一致。
| 面向 | 考量 | 實務建議 |
|---|---|---|
| 效能 | explore 的 dedup 機制省 token | 同一對話已顯示過的檔案改回傳 path + symbols pointer,不重送全文 |
| 品質 | 一個強工具 vs 多個窄工具的 agent 決策成本 | 一個瞄得準的工具減少 mis-pick、省上下文、讓 agent 在回答與編輯時都會用它 |
| 安全 | 無索引時 server 不列工具 | agent 自然退回內建工具,不會拿到半成品答案;projectPath 只能在有索引的專案間切換 |
| 面向 | 本文(MCP 工具) | 相關文 | 差異說明 |
|---|---|---|---|
| explore 輸出 | MCP 工具的回應格式與設計理由 | CLI 指令 | MCP 有 initialize 指引,CLI 沒有;兩者 explore 輸出逐字元一致 |
| 工具面管理 | CODEGRAPH_MCP_TOOLS allowlist | 設定 | MCP 工具頁講工具選擇,設定頁講環境變數全貌 |
| 無索引行為 | server 宣告 inactive,不列工具 | 隱私與安全 | MCP 頁講工具行為,安全頁講資料保護 |
| sub-agent | 靠 instructions 檔的 marker-fenced 段落 | installer 程式碼 | MCP 頁講使用場景,installer 講怎麼寫入 marker |
CODEGRAPH_MCP_TOOLS=explore,status 精簡工具面
- [ ] 能區分 MCP initialize response 的指引 vs instructions 檔的 marker-fenced 段落
- [ ] 能解釋 projectPath 參數如何讓一個 server 服務多個專案
- [ ] 能驗證 MCP 與 CLI 的 explore 輸出一致性(逐字元比對)
- [ ] 能解釋「無索引時 server 不列工具」如何保護 agent 不拿到半成品答案