指令用途
在進行代碼除錯或重構時,AI 助手最常見的瓶頸是需要在多個 MCP 工具(如檢索、讀檔、追蹤)之間來回呼叫。這不僅耗費時間,還會因為多次對話往返(Round-trips)而消耗大量 Token。
codegraph_explore 是 Codegraph 專案中最核心的全方位代碼探索工具。它基於本地輕量級的圖模型,讓 AI 助手只需進行單一一次工具呼叫,就能同時獲取:
- 實體原始碼 (Verbatim Source Code):該 Symbol 的精確代碼片段與行號。
- 入站邊 (Inbound Callers):誰呼叫了該 Symbol。
- 出站邊 (Outbound Dependencies):該 Symbol 調用了哪些外部方法或類別。
這種「三合一」的返回格式,使得 AI 助手能夠在一個 Turn 內徹底理清代碼邊界,效率大幅提升。
運作流程
- 參數接收:接收
symbol(欲探索的方法、類別或屬性名稱)與file(可選,指定範圍)。 - 圖尋索:在 Codegraph 本地嵌入式圖結構中,定位該 Symbol 的 AST 定義。
- 數據聚合:
- 提取其實體代碼字串。
- 拉取所有直接呼叫它的 Inbound 節點。
- 拉取它所有相依的 Outbound 節點。
- 輸出呈現:將這三部分資訊整合成一個 JSON 對象返回。
實戰對話範例
範例:探索前端事件處理器的完整上下文
- You:
/opsx:explore "請幫我用 codegraph_explore 查詢 React 元件中的 handleReconnect 方法。" - AI:
好的,我將呼叫
codegraph_explore工具,為您一次性獲取handleReconnect的代碼實作、上游呼叫者與外部依賴。CG 呼叫:
codegraph_explore(symbol: "handleReconnect")
CG 回傳:{ "symbol": "handleReconnect", "filePath": "src/components/BluetoothButton.tsx", "verbatim": "const handleReconnect = async () => {\n try {\n await NativeModules.BluetoothModule.startScan();\n } catch (err) {\n console.error(err);\n }\n};", "inboundCallers": [ { "caller": "render", "file": "src/components/BluetoothButton.tsx", "line": 52 } ], "outboundDependencies": [ { "dependency": "NativeModules.BluetoothModule.startScan", "type": "NativeModuleMethod" } ] }分析:
僅用一次工具呼叫,我就獲得了完整的邊界脈絡:- 實作:
handleReconnect是一個非同步方法,內部調用了原生 iOS 橋接方法startScan()。 - 上游:它被同一個檔案中的
render按鈕 onClick 事件綁定(第 52 行)。 - 下游:強相依於原生的
BluetoothModule模組。
接下來,我們需要繼續探索 Objective-C 或 Swift 端暴露的
BluetoothModule實作嗎? - 實作: