MCP 工具全解

codegraph_explore 一個就夠?——為什麼只暴露一個強工具是刻意的設計
codegraph serve --mcp · 無索引時不暴露工具 · CODEGRAPH_MCP_TOOLS 可重新啟用

CodeGraph 如何作為 MCP server

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 摘要。一次呼叫通常就回答完整個問題。

英文原文(官方 docs · mcp-server)

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.

為什麼刻意只暴露一個:實測顯示,一個瞄得準的工具比一整排窄工具更能引導 agent 直接答對(更少 mis-pick),而且每次 session 都省上下文。一個強工具也讓 agent 在回答問題與編輯程式時都會去用它。

其他 7 個工具(預設隱藏)

以下工具全部仍可用,只是預設不列出——它們回的東西已經內嵌在 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 用。

Agent 該怎麼用它

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 對等指令。

本站實測:explore 的真實輸出

對 codegraph 自己問「codegraph_explore 怎麼組出答案」(節錄):

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。

看完這頁你應該能說出:為什麼只暴露 codegraph_explore、7 個隱藏工具各自做什麼、如何用 CODEGRAPH_MCP_TOOLS 重新啟用、以及「無索引時 server 不列工具」的設計意義。

🎓 教學解說

MCP(Model Context Protocol)是 AI agent 與外部工具溝通的標準協議。CodeGraph 作為 MCP server,讓 agent 可以「像人類一樣」詢問程式碼結構問題,而不是靠 grep 重建結構。

「只暴露一個工具」的設計哲學:

Worked Example:用 MCP 工具回答架構問題

假設你在 code review,想知道「修改 buildContext 會影響哪些地方?」

MCP 工具實測
# 在 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

練習:驗收清單


🔍 進階真實情境 Worked Example:多專案 + sub-agent 的 MCP 協作

前面的範例是單一 agent 問單一專案。這裡是更真實的協作:主 agent 在同一個 session 內分析兩個專案,而 heavy lifting 交給 sub-agent(它看不到 MCP initialize 的指引)。

多專案 MCP 工作流
# ① 主 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 自然退回內建工具。

大家以為分析正確但其實有誤:以為 7 個隱藏工具是「進階專用」或「效能較好」。它們只是 explore 回應組成部分的獨立入口——對 agent 來說直接用 explore 通常更快。另一個誤解:以為 explore 每次呼叫都從磁碟重讀全量回傳。實際上探索 dedup 已上線:同一對話已顯示過的檔案,改回傳 path + symbols + 行範圍 pointer(確認檔案沒變)——省下的空間給沒看過的程式。

🩺 診斷式疑難排解表

症狀可能原因解決方案
工具列表只有 explore,想要更多預設 allowlist 只有 exploreCODEGRAPH_MCP_TOOLS=explore,node,search 加回來
sub-agent 不會用 explore看不到 initialize response確認 CLAUDE.md/AGENTS.md 有 marker-fenced 段落
explore 連不上無索引或未 initcodegraph init + codegraph status 確認
回應附 staleness bannerdebounce 窗口內,檔案剛改等同步或直接 Read 該檔
WSL 上 socket 連線不穩local socket 在 Windows 磁碟路徑不可靠CODEGRAPH_NO_DAEMON=1 每個 session 自己跑

🧩 進階挑戰題

  1. 「一個瞄得準的工具 vs 一整排窄工具」的機制是什麼?試著量化 agent 在「工具選擇」上的決策成本,並說明為什麼 mis-pick 少能省上下文。
  2. 假設你要只開放 explore + status 給某個 harness。allowlist 要怎麼寫?為什麼把 status 加進去是合理的(想想 staleness 檢查)?
  3. projectPath 參數解決了什麼問題?它與「每個專案各跑一個 server」在資源與一致性上的 trade-off 是什麼?

🏗️ 專案級端到端 Worked Example:多專案 MCP 協作 + sub-agent 整合

在一個跨三個 repo 的微服務架構中,主 agent 用 MCP 同時分析多個專案,sub-agent 用 CLI 對等指令完成 deep-dive。以下是完整流程:

多專案 MCP 工作流
# ① 主 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

✅ 進階互動式檢核清單

- [ ] 能解釋為什麼「只暴露一個工具」是刻意的設計,以及它如何減少 agent 的認知負擔 - [ ] 能用 CODEGRAPH_MCP_TOOLS=explore,status 精簡工具面 - [ ] 能區分 MCP initialize response 的指引 vs instructions 檔的 marker-fenced 段落 - [ ] 能解釋 projectPath 參數如何讓一個 server 服務多個專案 - [ ] 能驗證 MCP 與 CLI 的 explore 輸出一致性(逐字元比對) - [ ] 能解釋「無索引時 server 不列工具」如何保護 agent 不拿到半成品答案