src/mcp — MCP server

agent 怎麼跟圖對話:單一工具 + 自動傳遞的使用指引
檔案:src/mcp/ · server-instructions.ts 是主 agent 指引的單一來源

大方向

MCP server 把 CodeGraph 暴露給 agent。兩個關鍵設計:預設只列 codegraph_explore(其它 7 個工具可用但不列出),以及使用指引直接塞進 MCP initialize response——主 agent 不用看文件就知道怎麼用。sub-agent 與非 MCP harness 看不到那個 response,所以安裝器另寫 marker-fenced 段落進 instructions 檔(見 installer)。


server-instructions.ts src/mcp/MCP initialize 回傳的使用指引——主 agent 的單一來源。

內容重點:結構性問題直接答(圖就是 pre-built 索引,grep/read 是重複做工);codegraph_explore 幾乎什麼都能用;相信結果別再 grep 驗證;編輯後看 staleness banner。README 說「The exact text is src/mcp/server-instructions.ts — the single source of truth for the main agent」,所以它必須與 user-facing tool guidance 保持同步(CLAUDE.md 特別註記)。

tools.ts / engine.ts src/mcp/工具定義與實作(explore 的組裝 + 7 個薄工具)。

tools.ts 定義工具清單與 CODEGRAPH_MCP_TOOLS allowlist 邏輯;engine.ts 組 explore 的答案(用 ContextBuilder + traverser)。7 個未列工具回的東西已內嵌在 explore 回應裡。projectPath 參數讓「server 自己 root 沒有索引」時也能查別的專案。

transport / daemon 連線 src/mcp/ + src/sync/stdio transport + 共享背景 server(local socket)。

agent 以 stdio 啟動 server。daemon 機制用 local socket 讓多個 session 共享一個背景 server(省資源);WSL2 / Windows 磁碟路徑上 socket 不可靠時,server fallback 為 in-process 服務。CODEGRAPH_NO_DAEMON=1 強制每個 session 自己跑(見 疑難排解)。connect-time catch-up 在每次(重)連線時做快速對帳(見 自動同步)。

staleness banner / pending files src/mcp/debounce 窗口內,把「未入索引的檔」誠實告訴 agent。

explore 回應前檢查 CodeGraph.getPendingFiles()(見 main):引用到 pending 檔 → 前置 ⚠️ banner 叫 agent 直接 Read;沒引用的 pending 檔 → 小 footer。已用 Claude Code 驗證 agent 會照做。

看完這頁你應該能說出:為什麼只有 explore 被列(其它工具已內嵌)、server-instructions 為什麼要與工具指引同步、daemon/socket 的 fallback 邏輯、以及 staleness banner 的資料來源。

🎓 教學解說

MCP server 是 CodeGraph 與 agent 溝通的「橋樑」——它把圖譜的能力暴露給 AI agent,讓 agent 可以像人類一樣詢問程式碼結構問題。

兩個關鍵設計:

Worked Example:MCP Server 的運作流程

MCP 實測
# 步驟 1:啟動 MCP server
codegraph serve --mcp

# 步驟 2:查看 initialize response
# Agent 收到:
# {
#   "tools": [
#     {
#       "name": "codegraph_explore",
#       "description": "Ask natural-language questions about code...",
#       "inputSchema": { "type": "object", "properties": { "query": {...} } }
#     }
#   ],
#   "instructions": "CodeGraph is a pre-built knowledge graph..."
# }

# 步驟 3:Agent 呼叫工具
# Agent: "How does buildContext work?"
# MCP: { "query": "how does buildContext work" }

# 步驟 4:Server 組裝回應
# 1. 用 GraphQueryManager 找相關符號
# 2. 用 ContextBuilder 組出原始碼 + 關係
# 3. 加上 staleness banner(如有 pending 檔)
# 4. 回傳給 Agent

關鍵觀察:整個流程是自動的——agent 問問題,server 回答,中間不需要人工介入。

常見錯誤與診斷

症狀原因解法
MCP server 連不上專案未初始化或未索引執行 codegraph init 確認 .codegraph/ 存在
工具未列出無索引或 CODEGRAPH_MCP_TOOLS 設定錯誤執行 codegraph status 檢查索引
回應包含 staleness 警告檔案剛修改,auto-sync 尚未完成等待 2 秒或手動 codegraph sync

練習:驗收清單


🔍 進階真實情境 Worked Example:多 agent 環境的 MCP 除錯 + 自訂 allowlist

前面的範例是單一 MCP server 的運作流程。這裡是除錯場景:主 agent + 多個 sub-agent 混用,有的會用 explore、有的不會,你要定位問題並用 allowlist 收斂工具面。

多 agent 除錯工作流
# ① 主 agent:initialize response 會帶 instructions
#    若主 agent 會用 explore → server-instructions 有進去

# ② sub-agent:看不到 initialize response
#    它該從 instructions 檔(CLAUDE.md/AGENTS.md)讀 marker-fenced 段落
grep -A 3 "CodeGraph" CLAUDE.md    # 確認段落存在

# ③ 若段落不存在:重跑安裝器補上
codegraph install --agent claude

# ④ 收斂工具面:只留要用的
export CODEGRAPH_MCP_TOOLS=explore,status

# ⑤ 檢查 daemon/socket 是否導致不穩
export CODEGRAPH_NO_DAEMON=1   # WSL/Windows 路徑不穩時的 fallback

為什麼走這條路徑:MCP 的 initialize response 只有主 agent 看得到,所以「指引傳遞」有兩條路:主 agent 走 response、sub-agent 走 instructions 檔。除錯時先確認「該走哪條路」的載體存在,再查工具面與連線——順序很重要。

🔬 深入原理擴充

內部架構:allowlist 是「取代」不是「加入」。tools.ts 的 CODEGRAPH_MCP_TOOLS 是逗號分隔的 allowlist,取代預設——如果你只想加一個工具卻忘了列 explore,explore 就會消失。這是一個「精簡是預設哲學」的延伸。engine.ts 用 ContextBuilder + traverser 組 explore 答案;transport 走 stdio,daemon 用 local socket 共享背景 server,WSL2/Windows 磁碟路徑上 socket 不可靠時 fallback 到 in-process。projectPath 參數讓「server 自己 root 沒索引」時也能查別的專案。staleness banner 的資料來源是 CodeGraph.getPendingFiles()。

大家以為分析正確但其實有誤:以為 tools.ts 是靜態工具清單。實際有完整的 allowlist 邏輯——預設只有 explore,其它 7 個要環境變數才重新暴露。另一個誤解:以為 daemon 一定「更快更好」。在 WSL2 + Windows 磁碟路徑上,local socket 不穩,server 會 fallback 到 in-process——CODEGRAPH_NO_DAEMON=1 不是退步,是「避開已知不穩路徑」的正確工具。

🩺 診斷式疑難排解表

症狀可能原因解決方案
sub-agent 不用 explore看不到 initialize response,instructions 檔沒段落確認 CLAUDE.md/AGENTS.md 的 marker-fenced 段落;重跑 install
工具列表不符合預期allowlist 寫錯或取代了預設檢查 CODEGRAPH_MCP_TOOLS 格式,確認 explore 有列
連線斷斷續續daemon socket 不穩(WSL/Windows 路徑)CODEGRAPH_NO_DAEMON=1 強制 in-process
查別專案失敗未用 projectPath呼叫時傳 projectPath 參數
第一個問題特別慢connect-time catch-up 對帳這是設計行為;第一次完成後即正常

🧩 進階挑戰題

  1. 為什麼 server-instructions 必須與 user-facing 工具指引同步?不同步時,主 agent 行為與文件描述不符的具體後果是什麼?
  2. daemon 共享 vs in-process fallback 的取捨:何時該強制 CODEGRAPH_NO_DAEMON=1?列 WSL、權限、多專案三個場景。
  3. allowlist 是「取代預設」而非「加入」——這個語意設計有何後果?如果使用者只想加 status 卻忘了列 explore,會發生什麼?