MarkTechPost@AI 前天 18:11
AI代理工具使用新模式:代码执行提升效率
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

传统AI代理通过模型上下文直接调用工具,在大规模工作流中易导致token超限、延迟和成本增加。Anthropic提出的“代码执行与MCP”新模式,将MCP工具转化为代码级API。模型编写并执行TypeScript代码,在沙箱环境中操作数据,显著减少token使用量(高达98.7%)。该模式支持渐进式工具发现、高效数据处理、隐私保护操作,并允许保存可复用脚本,为AI代理构建者带来诸多设计优势,使工具调用更像API接口。

💡 **核心挑战与新模式**: 传统AI代理通过将工具定义和中间结果加载到模型上下文来实现与外部系统的交互,这种方式在大规模工作流中会迅速消耗token,导致性能瓶颈和成本上升。Anthropic提出的“代码执行与MCP”新模式,将MCP工具视为代码级API,通过模型编写并执行TypeScript代码来调用这些API,从而将大量数据处理和逻辑判断移出模型上下文,实现效率的飞跃。

🚀 **显著的效率提升**: 通过将MCP工具封装为代码API并在沙箱环境中执行模型生成的代码,可以极大地减少token的使用。一个典型案例显示,token使用量从约150,000大幅下降至约2,000,降幅高达98.7%。这意味着更低的延迟、更低的成本以及更高的系统处理能力,使得AI代理能够处理更复杂、更大规模的任务。

🛠️ **代理构建者的优势**: 新模式为代理构建者带来了多重设计优势。包括:1. **渐进式工具发现**:模型无需一次性加载所有工具定义,可按需探索文件系统以发现和使用工具,减少不必要的上下文开销。2. **高效数据处理**:大型数据集可在执行环境中本地处理,模型仅接收摘要或统计信息,避免了数据在模型上下文中的传递。3. **隐私保护**:敏感数据可在执行环境中进行脱敏处理,模型仅看到占位符,确保数据在跨工具调用时的安全性。4. **状态与技能复用**:执行环境允许保存中间文件和可复用脚本,构建更高级别的能力。

⚙️ **从工具列表到可执行API**: Anthropic的新方法将MCP从一个简单的工具列表转变为一个可执行的API表面。模型不再是直接调用工具,而是通过编写代码来组合和调用这些API,这使得AI代理的行为更加可控和安全,同时也迫使开发者更加重视代码执行的安全性问题。

Agents that use the Model Context Protocol MCP have a scaling problem. Every tool definition and every intermediate result is pushed through the context window, which means large workflows burn tokens and hit latency and cost limits fast. Anthropic’s new ‘code execution with MCP’ pattern restructures this pipeline by turning MCP tools into code level APIs and asking the model to write and run code instead of calling tools directly.

The problem, MCP tools as direct model calls

MCP is an open standard that lets AI applications connect to external systems through MCP servers that expose tools. These tools let a model query databases, call APIs, or work with files through a unified interface.

In the default pattern, an agent loads many tool definitions into the model context. Each tool definition contains schema information and metadata. Intermediate results from each tool call are also streamed back into the context so the model can decide the next call.

Anthropic describes a typical case where an agent uses an MCP server for Google Drive to fetch a long sales meeting transcript and then uses another MCP server for Salesforce to update a record with that transcript. The full transcript is first returned through the model, then sent back again when the Salesforce tool is called. For a long meeting this can add tens of thousands of extra tokens that do not change the logic of the task.

When there are many MCP servers and many tools, this pattern does not scale. The model pays to read large tool catalogs and to move large payloads between tools. Latency increases, costs grow, and context limits become a hard cap on system behavior.

The shift, represent MCP servers as code APIs

Anthropic’s proposal is to place MCP inside a code execution loop. Instead of letting the model call tools directly, the MCP client exposes each server as a set of code modules in a filesystem. The model writes TypeScript code that imports and composes those modules, and this code runs in a sandboxed environment.

The pattern has three main steps.

    The MCP client generates a directory such as servers that mirrors the available MCP servers and tools.For each MCP tool, it creates a thin wrapper function implemented in a source file, for example servers/google-drive/getDocument.ts, that internally calls the MCP tool with typed parameters.The model is instructed to write TypeScript code that imports these functions, runs them, and handles control flow and data movement inside the execution environment.

The earlier Google Drive and Salesforce workflow becomes a short script. The script calls the Google Drive wrapper once, manipulates or inspects the data locally, then calls the Salesforce wrapper. The large transcript does not pass through the model, only the final status and any small samples or summaries do.

Cloudflare’s ‘Code Mode’ work uses the same idea in its Workers platform. It converts MCP tools into TypeScript APIs and runs model generated code inside an isolate with restricted bindings.

Quantitative impact, token usage drops by 98.7 percent

Anthropic reports a concrete example. A workflow that previously consumed about 150,000 tokens when tools and intermediate data were passed directly through the model was reimplemented with code execution and filesystem based MCP APIs. The new pattern used about 2,000 tokens. That is a 98.7 percent reduction in token usage for that scenario, which also reduces cost and latency.

Design benefits for agent builders

Code execution with MCP introduces several practical benefits for engineers who design agents:

Progressive tool discovery: The agent does not need all tool definitions in context. It can explore the generated filesystem, list available servers, and read specific tool modules only when needed. This shifts tool catalogs from the model context into code, so tokens are spent only on relevant interfaces.

Context efficient data handling: Large datasets remain inside the execution environment. For example, TypeScript code can read a large spreadsheet through an MCP tool, filter rows, compute aggregates, and log only small samples and summary statistics back to the model. The model sees a compact view of the data while the heavy lifting happens in code.

Privacy preserving operations: Anthropic describes a pattern where sensitive fields such as email or phone are tokenized inside the execution environment. The model sees placeholders, while the MCP client maintains a secure mapping and restores real values when calling downstream tools. This lets data move between MCP servers without exposing raw identifiers to the model.

State and reusable skills: The filesystem lets agents store intermediate files and reusable scripts. A helper script that transforms a sheet into a report can be saved in a skills directory and imported in later sessions. Anthropic connects this idea to Claude Skills, where collections of scripts and metadata define higher level capabilities.

Editorial Comments

Anthropic’s ‘code execution with MCP’ approach is a sensible next step for MCP powered agents. It directly attacks the token costs of loading tool definitions and routing large intermediate results through the context, by presenting MCP servers as code APIs and pushing work into a sandboxed TypeScript runtime. This makes agents more efficient, while also forcing teams to take code execution security seriously. This launch turns MCP from a tool list into an executable API surface.

The post Anthropic Turns MCP Agents Into Code First Systems With ‘Code Execution With MCP’ Approach appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

AI代理 MCP 代码执行 Anthropic 效率提升 AI Agent Code Execution Efficiency
相关文章