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)。
內容重點:結構性問題直接答(圖就是 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 定義工具清單與 CODEGRAPH_MCP_TOOLS allowlist 邏輯;engine.ts 組 explore 的答案(用 ContextBuilder + traverser)。7 個未列工具回的東西已內嵌在 explore 回應裡。projectPath 參數讓「server 自己 root 沒有索引」時也能查別的專案。
explore 回應前檢查 CodeGraph.getPendingFiles()(見 main):引用到 pending 檔 → 前置 ⚠️ banner 叫 agent 直接 Read;沒引用的 pending 檔 → 小 footer。已用 Claude Code 驗證 agent 會照做。
MCP server 是 CodeGraph 與 agent 溝通的「橋樑」——它把圖譜的能力暴露給 AI agent,讓 agent 可以像人類一樣詢問程式碼結構問題。
兩個關鍵設計:
codegraph_explore,減少 agent 的認知負擔# 步驟 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 |
codegraph_explore 回答問題前面的範例是單一 MCP server 的運作流程。這裡是除錯場景:主 agent + 多個 sub-agent 混用,有的會用 explore、有的不會,你要定位問題並用 allowlist 收斂工具面。
# ① 主 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()。
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 對帳 | 這是設計行為;第一次完成後即正常 |
CODEGRAPH_NO_DAEMON=1?列 WSL、權限、多專案三個場景。