Abstract

Tool Attention 是一個 Middleware 層的注意力機制,旨在消除 MCP 在多工具部署時帶來的系統性負擔(MCP Tax)。核心主張是 MCP 的 stateless 設計在每次請求中強制重新注入完整工具綱要,造成每次對話來回消耗 15k–55k 個 token 的隱藏開銷。Tool Attention 將「Attention Is All You Need」的思想從 token 層級推廣到工具層級,在 120 工具、六伺服器的合成基準上測得 95.0% 的 per-turn token 削減(47.3k → 2.4k),並將有效上下文利用率從 24% 提升至 91%。

Tool Attention MCP Tax

Overview

Tool Attention 是一個 Middleware 層的注意力機制,旨在消除 MCP (Model Context Protocol) 在多工具部署時帶來的系統性負擔——所謂的 MCP TaxTools Tax。論文的核心主張是:MCP 的 stateless 設計在每次请求中強制重新注入完整工具綱要(JSON Schema),造成每次對話來回(per-turn)消耗 15k–55k 個 token 的隱藏開銷,不僅大幅推高 API 成本,更在上下文利用率超過約 70% 時導致 LLM 推理品質崩潰(hallucination、參數混淆、任務記憶斷裂)。

Tool Attention 將「Attention Is All You Need」的思想從 token 層級推廣到工具層級:讓每個使用者回合動態選擇最相關的少數工具,而非每次都將整個工具目錄塞入 prompt。實驗在 120 工具、六伺服器的合成基準上測得 95.0% 的 per-turn token 削減(47.3k → 2.4k),並將有效上下文利用率從 24% 提升至 91%。^[raw/papers/tool-attention-mcp-tax.md]

The Problem: MCP/Tools Tax

What is the Tools Tax?

MCP 將 LLM Agent 連接到外部工具(檔案系統、Git、資料庫、Slack、GitHub 等),但底層 chat-completions API 是 stateless 的。Host 客戶端(Claude Desktop、Cursor、VS Code、Claude Code)在每次請求都必須重新序列化整個工具目錄。根據社群實測 [14, 18, 22]:

部署規模Token/turn
典型 4 伺服器主機15k–20k
Enterprise DB (106 tools)~54,600
GitHub MCP (93 tools)~55,000
惡化擴展 (tool sprawl)>150k

論文給出封閉式表達:

T_tax(N, K) ≈ K · α · N     (α ∈ [200, 500] tokens/tool)

在 N=120、K=30 的代表性企業設定下,僅系統提示就消耗約 1.42M tokens——在使用者說任何話之前。^[raw/papers/tool-attention-mcp-tax.md]

Three Cascading Failures

  1. 經濟層面:Stateless re-injection 使每次 session 成本增加一個數量級。社群報告 CLI 等效工作流 $3.20 vs. MCP $55.20(相同 10,000 次操作)[14, 26]。
  2. 認知層面:上下文利用率超過 ~70% 時,LLM 推理品質急遽下降——模型開始 hallucinate 參數混淆相似工具、喪失多步驟任務的記憶線索 [19, 22]。這是觀察到的「mid-session drift」的根本原因。
  3. 安全層面:同一份描述工具的 schema text 同時塑造模型的 attention mask,使 tool-poisoning-attack(TPA)得以透過在工具描述中注入惡意指令來劫持控制流,且無需該工具被實際調用 [29, 30]。

Core Contributions

論文聲稱四項貢獻:

  1. 形式化量化:給出 Tools Tax 的封閉表達式,並推導其在何種條件下主導有效上下文窗口。
  2. 機制創新:提出 Tool Attention——結合意圖-綱要重疊分數(ISO)、狀態感知閘控、雙相位懶惰綱要載入的通用中介層。
  3. 參考實作:發布基於 LangGraph middleware、FAISS、sentence-transformers、tiktoken 的生產級 Python 實作。^[raw/papers/tool-attention-mcp-tax.md]
  4. 校準模擬評估:在 token counts 已對標公開部署審計的 120 工具、六伺服器合成測試平台上進行測量,報告投影的任務成功率、延遲、成本與推理品質。

Architecture & Approach

1. Intent–Schema Overlap (ISO) Score

使用 sentence encoder(預設 all-MiniLM-L6-v2,d=384)將每個工具的壓縮摘要(≤60 tokens)預先編碼為向量 e_ti。每輪查詢時,將用戶消息編碼為 e_q,計算餘弦相似度:

ISO(q, ti) = (e_q · e_ti) / (||e_q||₂ · ||e_ti||₂)

這作為該工具對當前查詢意圖相關性的cheap proxy。^[raw/papers/tool-attention-mcp-tax.md]

2. Stateful Gating Function

每個工具附帶一組前置條件(preconditions)——如 requires_authonly_after_search。閘控函數定義為:

g(ti; q, state_t) = 1  iff  ISO(q, ti) ≥ θ  AND  state_t |= pre_i

只有滿足閾值θ且狀態允許的工具才能入選。候選集再取 top-k(按 ISO 分數排序)。閾值 θ 需對每個部署進行校準(典型 θ* ∈ [0.22, 0.32])。^[raw/papers/tool-attention-mcp-tax.md]

3. Two-Phase Lazy Schema Loading

這是節省 token 的關鍵設計:

  • Phase 1 — Summary Pool:所有 N 個壓縮摘要(每個 ≤60 tokens,總計約 4.8k tokens for N=120)始終駐留於上下文,讓模型知道工具的存在,屬於靜態內容,可被 prompt caching 完全快取。
  • Phase 2 — Schema Promotion:每輪僅針對 gated top-k 工具(即 At)從外部註冊表懶惰載入完整 JSON Schema,隨消息主體傳遞。

這個雙相位設計的洞見在於:摘要讓工具可被發現,完整 Schema 才能讓模型正確填充參數——兩者缺一不可,但只有後者需要每次完整傳遞。^[raw/papers/tool-attention-mcp-tax.md]

4. Hallucination Rejection Gate

after_model hook 中,若模型試圖調用不在 At 中的工具,請求被拒絕並返回結構化錯誤 {"error": "tool_not_available", "available": [...]},促使模型重新選擇可用工具或向用戶尋求澄清。實測中此 gate 在 2.3% 的輪次觸發,其中 78% 模型能在下一輪成功恢復。^[raw/papers/tool-attention-mcp-tax.md]

5. Theoretical Grounding: Total Attention Energy

論文將 ISO 閘控與 mindguard 的 Total Attention Energy (TAE) 框架掛鉤。TAE 測量生成token(如 tool-call action)與上下文元數據token之間的 attention weight 總能量。關鍵觀察:一個成功的工具調用在該工具的 Schema tokens 上累積高 TAE;如果 Schema 不在 prompt 中,高 TAE 不可能達成。

因此,Tool Attention 的邏輯閘控相當於消極排除那些「無論如何都無法對 tool-call logit 產生顯著貢獻」的工具——既消除稅收,也縮小 tool-poisoning-attack 攻擊面(任何 semantic fingerprint 不匹配當前查詢意圖的毒化描述會被 gate out,永遠不會接觸模型的 attention layers)。^[raw/papers/tool-attention-mcp-tax.md]

Key Results

Main Benchmark (120 tools, 6 servers, 500 tasks)

所有 token 數量以 tiktoken(cl100k_base)直接測量;帶 † 的欄位為結合已發布遙測數據的投影值,非 live agent 測量。^[raw/papers/tool-attention-mcp-tax.md]

MethodTool Tokens/turnρ (Context Util.)Success % †P50 latency (s) †$/task †
Full-Schema (B1)47,3120.24≈72≈4.2≈0.21
Static Pruning (B2)11,8650.56≈58≈3.8≈0.09
Simple Retrieval (B3)4,0820.78≈81≈2.2≈0.04
CLI Lazy (B4)4800.94≈88≈2.4≈0.03
Tool Attention2,3680.91≈94≈2.0≈0.03

關鍵數據

  • 95.0% token 削減(47.3k → 2.4k)
  • 3.8× 上下文利用率提升(0.24 → 0.91)
  • ∼22pp 任務成功率提升(投影值)
  • ∼52% P50 延遲降低
  • ∼86% 成本降低($0.21 → $0.03)

Reasoning Quality (Projected LLM-Judge, 1–5)

MethodMeanSD% scoring ≥4
Full-Schema3.211.0443.2%
Tool Attention4.430.6287.6%

Full-Schema 在長時段任務(turn 30)推理品質預測降至 ~2.78,而 Tool Attention 維持 ~4.31。^[raw/papers/tool-attention-mcp-tax.md]

Ablation Study

VariantTool TokensSuccess % ∆
Full Tool Attention2,368
− Hallucination gate2,368−3.2
− Preconditions (ISO only)2,462−3.6
− Lazy loading (summaries only)0−10.3
+ Phase-1 only, k=04,820−15.0
k=5 instead of k=101,320−2.8
θ=0.40 instead of θ=0.281,480−6.0

Lazy loading 是最大單一貢獻者(+10.3pp),確認模型需要完整 Schema 而非僅摘要才能正確填充參數。^[raw/papers/tool-attention-mcp-tax.md]

Scaling Behavior

Full-Schema 的有效上下文利用率 ρ 衰減為 1/(1 + 400N/Cmax),在 N≈50 時即穿越 70% fracture point。Tool Attention 在 k=10 時維持 ρ≥0.87 直至 N=1,000,僅因 Phase-1 生長而有對數衰減。^[raw/papers/tool-attention-mcp-tax.md]

Implementation

參考實作基於四個協作模組 tool-attention GitHub

  • IntentRouter:封裝 encoder + FAISS index,實作 ISO scoring + top-k 提取
  • ToolVectorStore:FAISS 或 ChromaDB 後端的工具摘要持久化
  • LazySchemaLoader:LRU cache 的按需完整 Schema 獲取
  • ToolAttention:頂層協調器,暴露 before_modelafter_model hooks,符合 LangGraph middleware 合約

技術選擇:默認 encoder all-MiniLM-L6-v2(22M 參數,384-d),可在 CPU 上 ∼30–60ms 完成典型 50-token 查詢編碼。Router 每輪 O(N log N) 複雜度,N≤10,000 在 commodity CPU 上 sub-millisecond。^[raw/papers/tool-attention-mcp-tax.md]

Security Implications

Tool Attention 的閘控機制可作為 tool-poisoning-attack(TPA)的消極防禦:在 50 個模擬毒化工具描述的實驗中,Tool Attention 的 gate 排除了其中 46 個(因為 query intent 的 semantic fingerprint 與 poisoning payload 的 cosine 匹配度低)。不過論文強調真正的防御需搭配 mindguard 的 TAE runtime monitor,兩者是互補關係。^[raw/papers/tool-attention-mcp-tax.md]

Limitations

  1. 應用層補救:Tool Attention 無法修復協議層缺陷(如缺乏 session-scoped 能力協商)。屬於中期緩解,與 MCP-over-MOQT 傳輸層改進互補。
  2. 摘要品質依賴:工具摘要如果名稱隱晦、描述含糊,會直接損害檢索精確度。 curator effort 無法完全消除。
  3. 模擬評估局限:所有下游指標(任務成功率、推理品質)為投影值,非 live agent 測量。社群需要類似 SWE-bench 的標準化 MCP benchmark 來驗證。^[raw/papers/tool-attention-mcp-tax.md]
  4. Adversarial paraphrase:攻擊者可構造與 benign queries semantic fingerprint 高度匹配的毒化描述。需配對 MindGuard TAE monitor 檢測新晉升 Schema 上的異常 attention energy。

Future Work

  • Cross-turn state-aware gating:目前 query embedding 僅使用最新用戶消息(可選加滾動摘要)。更強版本可學習結合中間工具輸出和演化任務計劃的狀態表徵。
  • Learned gating:用輕量蒸餾分類器(2-layer MLP)取代固定閾值,估計可額外提升 1–3pp 成功率。
  • Composition with code execution:Tool Attention 優化工具定義端;Anthropic 的 code execution 模式優化工具輸出端。兩者組合預計可在數據密集工作流上進一步削減一個數量級的端到端上下文消耗。^[raw/papers/tool-attention-mcp-tax.md]
  • openhands — 開源 Agent 平台,強調長期自主任務執行,與 Tool Attention 的工具選擇效率問題高度相關
  • mindguard — TAE (Total Attention Energy) 攻擊檢測框架,Tool Attention 的閘控機制以其理論為基礎
  • mcp — Model Context Protocol,Tool Attention 旨在消除的協議層稅收的源頭
  • Anthropic Code Execution — Anthropic 的 code execution 模式,與 Tool Attention 在「定義 vs. 輸出」兩個方向上互補

References

  • [Paper] Anuj Sadani, Deepak Kumar. Tool Attention Is All You Need: Dynamic Tool Gating and Lazy Schema Loading for Eliminating the MCP/Tools Tax in Scalable Agentic Workflows. arXiv:2604.21816, April 2026. ^[raw/papers/tool-attention-mcp-tax.md]
  • [GitHub] https://github.com/asadani/tool-attention