/ 目錄 / 演練場 / pctx
● 社群 portofcontext ⚡ 即開即用

pctx

作者 portofcontext · portofcontext/pctx

以沙箱化 JavaScript 執行 MCP 工具呼叫——一個程式碼區塊取代多次連續呼叫,節省 token 並隔離憑證。

pctx(Port of Context)是一個執行層,將 MCP 伺服器轉換為可在 Deno 沙箱內呼叫的 JS 函式。LLM 撰寫的程式碼可在單次執行中組合多個工具呼叫;pctx 在無檔案系統/環境變數存取且網路經過白名單過濾的環境中執行。憑證保留在 pctx 層——LLM 永遠看不到它們。

為什麼要用

核心特性

即時演示

實際使用效果

pctx.replay ▶ 就緒
0/0

安裝

選擇你的客戶端

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "pctx": {
      "command": "TODO",
      "args": [
        "See README: https://github.com/portofcontext/pctx"
      ],
      "_inferred": true
    }
  }
}

開啟 Claude Desktop → Settings → Developer → Edit Config。儲存後重啟應用。

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "pctx": {
      "command": "TODO",
      "args": [
        "See README: https://github.com/portofcontext/pctx"
      ],
      "_inferred": true
    }
  }
}

Cursor 使用與 Claude Desktop 相同的 mcpServers 格式。專案級設定優先於全域。

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "pctx": {
      "command": "TODO",
      "args": [
        "See README: https://github.com/portofcontext/pctx"
      ],
      "_inferred": true
    }
  }
}

點擊 Cline 側欄中的 MCP Servers 圖示,然後選 "Edit Configuration"。

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "pctx": {
      "command": "TODO",
      "args": [
        "See README: https://github.com/portofcontext/pctx"
      ],
      "_inferred": true
    }
  }
}

格式與 Claude Desktop 相同。重啟 Windsurf 生效。

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "pctx",
      "command": "TODO",
      "args": [
        "See README: https://github.com/portofcontext/pctx"
      ]
    }
  ]
}

Continue 使用伺服器物件陣列,而非映射。

~/.config/zed/settings.json
{
  "context_servers": {
    "pctx": {
      "command": {
        "path": "TODO",
        "args": [
          "See README: https://github.com/portofcontext/pctx"
        ]
      }
    }
  }
}

加入 context_servers。Zed 儲存後熱重載。

claude mcp add pctx -- TODO 'See README: https://github.com/portofcontext/pctx'

一行命令搞定。用 claude mcp list 驗證,claude mcp remove 移除。

使用場景

實戰用法: pctx

以單一程式碼區塊執行 20 個工具呼叫的串連

👤 因長時間連續流程導致 token 費用激增的 Agent 開發者 ⏱ ~20 min advanced

何時使用: 任務需要大量工具呼叫時(例如取得 N 個 issue、逐一豐富資料、發布摘要)。

前置條件
  • 已安裝並設定 pctx — brew install portofcontext/tap/pctx; pctx config add <upstream-mcp>
步驟
  1. 使用憑證註冊上游服務
    pctx config add github --env GITHUB_TOKEN=... and add sentry --env SENTRY_TOKEN=....✓ 已複製
    → 上游服務已列出
  2. 要求 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.✓ 已複製
    → 程式碼區塊與單次執行結果
  3. 檢查執行結果
    Summarize the array in English and flag any issue whose releasing commit was by a specific author.✓ 已複製
    → 基於資料得出的最終答案

結果: 工具呼叫往返次數減少 10 倍,大幅節省 token,結果相同。

注意事項
  • 若未將 stderr 輸出,沙箱程式碼錯誤會難以診斷 — 用 try/catch 包裝程式碼並印出捕捉到的錯誤,讓 LLM 能自我修正
  • 網路白名單設定過嚴,導致合法呼叫被阻擋 — 先設定較寬鬆的規則,再根據日誌中的實際主機使用情況逐步收緊
搭配使用: github · sentry

將憑證與 LLM 隔離

👤 導入 MCP 且重視安全性的團隊 ⏱ ~15 min intermediate

何時使用: 你的威脅模型要求「LLM 絕對不能以任何形式看到 token」時。

步驟
  1. 將憑證儲存至 pctx
    Add a keychain-backed credential to pctx for github.✓ 已複製
    → 憑證已儲存,呼叫方無法看到
  2. 從另一端驗證
    Ask the LLM to 'print the GitHub token' — confirm it cannot.✓ 已複製
    → LLM 無法存取該密鑰

結果: 對「LLM 是否可能洩漏我們的 token?」提供可重現的答案——不會。

將多個 MCP 整合為單一沙箱化程式碼介面

👤 希望擁有單一安全介面的團隊 ⏱ ~25 min advanced

何時使用: 你希望在同一個地方對 github、postgres、filesystem 使用 Code Mode 語義時。

步驟
  1. 逐一註冊各服務
    Add github, postgres (read-only), and filesystem (scoped to /data) to pctx.✓ 已複製
    → 三者皆可從程式碼中呼叫
  2. 撰寫跨 MCP 任務
    Write pctx code that reads a SQL query result and writes CSV to /data/out.csv, then creates a GitHub gist with that CSV.✓ 已複製
    → 單次執行,使用三個上游服務

結果: 為混合上游工作流程提供統一且沙箱化的執行平面。

搭配使用: github · postgres · filesystem

組合

與其他 MCP 搭配,撬動十倍槓桿

pctx + github + sentry

在單一沙箱化程式碼區塊中交叉比對 Sentry issue 與 GitHub commit

Via pctx Code Mode: for every new Sentry issue, find the GitHub commit that introduced it and return a table.✓ 已複製
pctx + postgres

在單一沙箱中完成查詢、轉換與匯出

pctx code that runs a Postgres read, pivots the result in JS, and writes /data/report.json.✓ 已複製

工具

此 MCP 暴露的能力

工具輸入參數何時呼叫成本
execute_code code: str (JavaScript, access to injected MCP SDK) 當任務需要超過 2 個相互組合的工具呼叫時 upstream tool calls inside the code
list_upstream_tools (none) Agent 探索階段——查詢可從程式碼中呼叫的工具 free

成本與限制

運行它的成本

API 配額
pctx 本身無配額限制;程式碼內的上游呼叫會計算各自的用量
每次呼叫 Token 數
Code Mode 在 token 用量上遠比多次連續工具呼叫節省
費用
免費,開源
提示
對於超過 5 個工具呼叫的任務,Code Mode 相較於連續呼叫通常可節省 50% 以上的 token。

安全

權限、密鑰、影響範圍

憑證儲存: 環境變數、作業系統金鑰鏈或外部指令——LLM 永遠看不到憑證
資料出站: 僅限於各上游明確設定在白名單中的主機
切勿授予: Allowlist the open internet in the sandbox — pin to specific hosts

故障排查

常見錯誤與修復

Network denied in sandbox

將該主機加入 pctx 設定中對應上游服務的白名單。

驗證: pctx config show
Credential not injected

憑證名稱不符;pctx 會在啟動時於日誌中記錄已解析的金鑰名稱。

驗證: pctx run --debug
Code Mode returns a vague 'sandbox violation'

啟用詳細日誌;大多數違規情況是 LLM 不應進行的環境變數或檔案系統存取嘗試。

驗證: pctx logs --tail

替代方案

pctx 對比其他方案

替代方案何時用它替代權衡
proxy-2 / 1mcp-agent你需要整合功能但不需要 Code Mode 或沙箱化時LLM 會分別呼叫每個工具;可看到 MCP 回傳的所有內容
Bare execution via shell MCP你需要程式碼執行功能但不需要 MCP 代理時無憑證隔離,無白名單控制

更多

資源

📖 閱讀 GitHub 上的官方 README

🐙 查看未解決的 issue

🔍 瀏覽全部 400+ MCP 伺服器和 Skills