以單一程式碼區塊執行 20 個工具呼叫的串連
何時使用: 任務需要大量工具呼叫時(例如取得 N 個 issue、逐一豐富資料、發布摘要)。
前置條件
- 已安裝並設定 pctx — brew install portofcontext/tap/pctx; pctx config add <upstream-mcp>
步驟
-
使用憑證註冊上游服務pctx config add github --env GITHUB_TOKEN=... and add sentry --env SENTRY_TOKEN=....✓ 已複製→ 上游服務已列出
-
要求 Agent 撰寫 Code Mode 程式碼In pctx Code Mode, write a function that fetches the 10 newest Sentry issues, for each finds the releasing commit via github, and returns a summary array.✓ 已複製→ 程式碼區塊與單次執行結果
-
檢查執行結果Summarize the array in English and flag any issue whose releasing commit was by a specific author.✓ 已複製→ 基於資料得出的最終答案
結果: 工具呼叫往返次數減少 10 倍,大幅節省 token,結果相同。
注意事項
- 若未將 stderr 輸出,沙箱程式碼錯誤會難以診斷 — 用 try/catch 包裝程式碼並印出捕捉到的錯誤,讓 LLM 能自我修正
- 網路白名單設定過嚴,導致合法呼叫被阻擋 — 先設定較寬鬆的規則,再根據日誌中的實際主機使用情況逐步收緊