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 不列工具」的設計意義。