MCPによるコード実行:より効率的なエージェントの構築
直接的なツール呼び出しは定義と結果ごとにコンテキストを消費する。代わりにコードを書いてツールを呼び出すことで、エージェントはより効率的にスケールする。MCPを用いたその仕組みを説明する。
キーポイント
MCP(Model Context Protocol)はAIエージェントと外部システムを接続するオープン標準として業界デファクトとなっている
多数のツール接続時にツール定義や中間結果がコンテキストウィンドウを圧迫し、エージェントの効率低下とコスト増加を招く課題がある
コード実行によるツール呼び出しが、直接ツール呼び出しよりもコンテキスト消費を削減し、大規模ツール接続時のスケーラビリティを向上させる
AnthropicがMCP導入後の実運用課題と効率化ソリューションを公式に共有している点が業界にとって貴重な知見
影響分析・編集コメントを表示
影響分析
この記事は、AIエージェントの実用化が進む中で表面化したスケーラビリティ課題に対する具体的な解決アプローチを示しており、エージェント開発の効率化とコスト削減に直接寄与する技術的洞察を提供している。MCPの広範な採用を背景に、大規模ツール接続時の実運用ノウハウが業界全体の開発ベストプラクティス形成に影響を与える可能性が高い。
編集コメント
AIエージェントの実用化が進む中で表面化する「効率とコスト」という現実的な課題に焦点を当てた実践的な技術記事。理論ではなく運用ノウハウを共有する点が開発者にとって価値が高い。
MCPによるコード実行:より効率的なエージェントの構築
Anthropicが2024年11月に発表したモデルコンテキストプロトコル(MCP)は、AIエージェントと外部システム(ツールやデータ)を接続するためのオープンな標準規格である。従来は接続の組み合わせごとに個別の統合作業が必要だったが、MCPを採用すれば一度実装するだけで広範な統合エコシステムを利用できる。発表後、コミュニティによる数千のMCPサーバー開発や主要プログラミング言語向けSDKの提供が進み、業界では事実上の標準として急速に採用が拡大している。
現在、開発者は数十のMCPサーバーを介して数百から数千ものツールにアクセス可能なエージェントを日常的に構築している。しかし、接続ツール数が増大するにつれて、新たな課題が浮上した。それは、全てのツール定義を事前に読み込み、中間結果をコンテキストウィンドウ(処理可能なテキスト量に限界がある領域)を通じてやり取りする従来の方法が、エージェントの速度低下とコスト増加を招くことである。
この非効率性は主に二つのパターンで生じる。
第一に、ツール定義によるコンテキストウィンドウの圧迫である。多くのMCPクライアントは、直接ツール呼び出し構文を使用するため、全てのツールの説明文(機能、必須パラメータ、戻り値など)を最初にコンテキストにロードする。ツールが数千に及ぶ場合、エージェントはユーザーのリクエストを処理する前に、何十万トークンもの定義情報を読み込む必要があり、応答時間とコストを増大させる。
第二に、中間結果による追加トークン消費である。エージェントが複数のツールを連鎖的に使用するタスク(例:「Google Driveから会議議事録をダウンロードし、それをSalesforceのリードに添付せよ」)を実行する際、各ツールの出力結果がそのままコンテキストに追加される。この中間結果の蓄積が、さらなるトークン消費とコスト増につながる。
これらの課題に対する解決策が、ツールの直接呼び出しではなく、コードを記述してツールを呼び出すアプローチである。エージェントに、必要なツールを呼び出すための短いコード(関数)を生成させ、それを実行エンジンで処理する。この方法には大きな利点がある。ツールの詳細な定義をコンテキストに常駐させる必要がなくなり、代わりに簡潔なコード関数だけを提示すればよくなる。さらに、複雑なマルチステップのワークフローも、中間結果をコンテキストに逐一載せずに、コード内の変数として効率的に処理できる。
要約すると、MCPの大規模採用によりツール接続の汎用性が飛躍的に高まった一方で、従来の「直接呼び出し」方式ではコンテキストの逼迫がボトルネックとなっていた。コード実行を介した間接的なツール利用は、ツール定義と中間結果の両方に由来する過剰なトークン消費を削減し、より多くのツールを扱いながらも
原文を表示
Engineering at AnthropicCode execution with MCP: Building more efficient agents
Direct tool calls consume context for each definition and result. Agents scale better by writing code to call tools instead. Here's how it works with MCP.
The Model Context Protocol (MCP) is an open standard for connecting AI agents to external systems. Connecting agents to tools and data traditionally requires a custom integration for each pairing, creating fragmentation and duplicated effort that makes it difficult to scale truly connected systems. MCP provides a universal protocol—developers implement MCP once in their agent and it unlocks an entire ecosystem of integrations.
Since launching MCP in November 2024, adoption has been rapid: the community has built thousands of MCP servers, SDKs are available for all major programming languages, and the industry has adopted MCP as the de-facto standard for connecting agents to tools and data.
Today developers routinely build agents with access to hundreds or thousands of tools across dozens of MCP servers. However, as the number of connected tools grows, loading all tool definitions upfront and passing intermediate results through the context window slows down agents and increases costs.
In this blog we'll explore how code execution can enable agents to interact with MCP servers more efficiently, handling more tools while using fewer tokens.
Excessive token consumption from tools makes agents less efficient
As MCP usage scales, there are two common patterns that can increase agent cost and latency:
Tool definitions overload the context window;
Intermediate tool results consume additional tokens.
- Tool definitions overload the context window
Most MCP clients load all tool definitions upfront directly into context, exposing them to the model using a direct tool-calling syntax. These tool definitions might look like:
gdrive.getDocument Description: Retrieves a document from Google Drive Parameters: documentId (required, string): The ID of the document to retrieve fields (optional, string): Specific fields to return Returns: Document object with title, body content, metadata, permissions, etc.Copysalesforce.updateRecord Description: Updates a record in Salesforce Parameters: objectType (required, string): Type of Salesforce object (Lead, Contact, Account, etc.) recordId (required, string): The ID of the record to update data (required, object): Fields to update with their new values Returns: Updated record object with confirmationCopyTool descriptions occupy more context window space, increasing response time and costs. In cases where agents are connected to thousands of tools, they’ll need to process hundreds of thousands of tokens before reading a request.
- Intermediate tool results consume additional tokens
Most MCP clients allow models to directly call MCP tools. For example, you might ask your agent: "Download my meeting transcript from Google Drive and attach it to the Salesforce lead."
The model will make calls like:
TOOL CALL: gdrive.getDocument(documentId: "abc123") → returns "Discussed Q4 goals...\n[full transcript text]" (loaded into model context) TOOL CALL: salesforce.updateRecord( objectType: "SalesMeeting", recordId: "00Q5f000001abcXYZ", data: { "Notes": "Discussed Q4 goals...\n[full transcript text written out]" } ) (model needs to write entire transcript into context again)CopyEvery intermediate result must pass through the model. In this example, the full call transcript flows through twice. For a 2-hour sales meeting, that could mean processing an additional 50,000 tokens. Even larger documents may exceed context window limits, breaking the workflow.
With large documents or complex data structures, models may be more likely to make mistakes when copying data between tool calls.
Code execution with MCP improves context efficiency
With code execution environments becoming more common for agents, a solution is to present MCP servers as code APIs rather than direct tool calls. The agent can then write code to interact with MCP servers. This approach addresses both challenges: agents can load only the tools they need and process data in the execution environment before passing results back to the model.
There are a number of ways to do this. One approach is to generate a file tree of all available tools from connected MCP servers. Here's an implementation using TypeScript:
servers ├── google-drive │ ├── getDocument.ts │ ├── ... (other tools) │ └── index.ts ├── salesforce │ ├── updateRecord.ts │ ├── ... (other tools) │ └── index.ts └── ... (other servers)CopyThen each tool corresponds to a file, something like:
// ./servers/google-drive/getDocument.ts import { callMCPTool } from "../../../client.js"; interface GetDocumentInput { documentId: string; } interface GetDocumentResponse { content: string; } /* Read a document from Google Drive */ export async function getDocument(input: GetDocumentInput): Promise<GetDocumentResponse> { r
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み