コンテンツにスキップ

Model context protocol (MCP)

Model context protocol (MCP) は、アプリケーションがツールやコンテキストを言語モデルに公開する方法を標準化します。公式ドキュメントより:

MCP は、アプリケーションが LLM にコンテキストを提供する方法を標準化するオープンプロトコルです。MCP は AI アプリケーション向けの USB-C ポートのようなものだと考えてください。USB-C がデバイスをさまざまな周辺機器やアクセサリに接続するための標準化された方法を提供するのと同様に、MCP は AI モデルをさまざまなデータソースやツールに接続するための標準化された方法を提供します。

Agents Python SDK は複数の MCP トランスポートに対応しています。これにより、既存の MCP サーバーを再利用したり、独自のサーバーを構築して、ファイルシステム、HTTP、またはコネクタに裏打ちされたツールをエージェントに公開できます。

MCP 統合の選択

MCP サーバーをエージェントに接続する前に、ツール呼び出しをどこで実行するか、どのトランスポートに到達できるかを決めます。以下のマトリクスは Python SDK がサポートするオプションの概要です。

必要なこと 推奨オプション
OpenAI の Responses API がモデルに代わって外部から到達可能な MCP サーバーを呼び出せるようにする Hosted MCP server tools 経由の HostedMCPTool
ローカルまたはリモートで稼働する Streamable HTTP サーバーに接続する Streamable HTTP MCP servers 経由の MCPServerStreamableHttp
Server-Sent Events を用いた HTTP を実装するサーバーと通信する HTTP with SSE MCP servers 経由の MCPServerSse
ローカルプロセスを起動し stdin/stdout 越しに通信する stdio MCP servers 経由の MCPServerStdio

以下のセクションでは、それぞれのオプションの設定方法と、どのトランスポートを選ぶべきかを説明します。

1. Hosted MCP server tools

ホスト型ツールは、ツールの往復処理全体を OpenAI のインフラに任せます。あなたのコードがツールを列挙・呼び出す代わりに、HostedMCPTool は サーバーラベル(および任意のコネクタメタデータ)を Responses API に転送します。モデルはリモートサーバーのツールを一覧し、あなたの Python プロセスへの追加のコールバックなしでそれらを呼び出します。ホスト型ツールは現在、Responses API の hosted MCP 統合をサポートする OpenAI モデルで動作します。

基本的な hosted MCP ツール

エージェントの tools リストに HostedMCPTool を追加してホスト型ツールを作成します。tool_config の dict は、REST API に送信する JSON を反映します:

import asyncio

from agents import Agent, HostedMCPTool, Runner

async def main() -> None:
    agent = Agent(
        name="Assistant",
        tools=[
            HostedMCPTool(
                tool_config={
                    "type": "mcp",
                    "server_label": "gitmcp",
                    "server_url": "https://gitmcp.io/openai/codex",
                    "require_approval": "never",
                }
            )
        ],
    )

    result = await Runner.run(agent, "Which language is this repository written in?")
    print(result.final_output)

asyncio.run(main())

ホストされたサーバーは自動的にそのツールを公開します。mcp_servers に追加する必要はありません。

ストリーミングする hosted MCP の実行結果

ホスト型ツールは、関数ツールと全く同じ方法で ストリーミング をサポートします。Runner.run_streamedstream=True を渡して、モデルがまだ処理中でも MCP の増分出力を消費します:

result = Runner.run_streamed(agent, "Summarise this repository's top languages")
async for event in result.stream_events():
    if event.type == "run_item_stream_event":
        print(f"Received: {event.item}")
print(result.final_output)

任意の承認フロー

サーバーが機微な操作を実行できる場合、各ツール実行前に人間またはプログラムによる承認を必須にできます。tool_configrequire_approval を、単一のポリシー("always""never")またはツール名からポリシーへの dict マッピングで設定します。判断を Python 内で行うには、on_approval_request コールバックを指定します。

from agents import MCPToolApprovalFunctionResult, MCPToolApprovalRequest

SAFE_TOOLS = {"read_project_metadata"}

def approve_tool(request: MCPToolApprovalRequest) -> MCPToolApprovalFunctionResult:
    if request.data.name in SAFE_TOOLS:
        return {"approve": True}
    return {"approve": False, "reason": "Escalate to a human reviewer"}

agent = Agent(
    name="Assistant",
    tools=[
        HostedMCPTool(
            tool_config={
                "type": "mcp",
                "server_label": "gitmcp",
                "server_url": "https://gitmcp.io/openai/codex",
                "require_approval": "always",
            },
            on_approval_request=approve_tool,
        )
    ],
)

コールバックは同期・非同期のどちらでもよく、モデルが実行を続けるために承認データを必要とするたびに呼び出されます。

コネクタ対応の hosted サーバー

Hosted MCP は OpenAI コネクタにも対応しています。server_url を指定する代わりに、connector_id とアクセストークンを指定します。Responses API が認証を処理し、ホストされたサーバーがコネクタのツールを公開します。

import os

HostedMCPTool(
    tool_config={
        "type": "mcp",
        "server_label": "google_calendar",
        "connector_id": "connector_googlecalendar",
        "authorization": os.environ["GOOGLE_CALENDAR_AUTHORIZATION"],
        "require_approval": "never",
    }
)

ストリーミング、承認、コネクタを含む完全なホスト型ツールのサンプルは examples/hosted_mcp にあります。

2. Streamable HTTP MCP servers

ネットワーク接続を自分で管理したい場合は、 MCPServerStreamableHttp を使用します。Streamable HTTP サーバーは、トランスポートを自分で制御したい場合や、サーバーを自社インフラ内で稼働させつつレイテンシを低く保ちたい場合に最適です。

import asyncio
import os

from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp
from agents.model_settings import ModelSettings

async def main() -> None:
    token = os.environ["MCP_SERVER_TOKEN"]
    async with MCPServerStreamableHttp(
        name="Streamable HTTP Python Server",
        params={
            "url": "http://localhost:8000/mcp",
            "headers": {"Authorization": f"Bearer {token}"},
            "timeout": 10,
        },
        cache_tools_list=True,
        max_retry_attempts=3,
    ) as server:
        agent = Agent(
            name="Assistant",
            instructions="Use the MCP tools to answer the questions.",
            mcp_servers=[server],
            model_settings=ModelSettings(tool_choice="required"),
        )

        result = await Runner.run(agent, "Add 7 and 22.")
        print(result.final_output)

asyncio.run(main())

コンストラクターは追加のオプションを受け付けます:

  • client_session_timeout_seconds は HTTP の読み取りタイムアウトを制御します。
  • use_structured_content は、テキスト出力よりも tool_result.structured_content を優先するかどうかを切り替えます。
  • max_retry_attemptsretry_backoff_seconds_base は、list_tools()call_tool() に自動リトライを追加します。
  • tool_filter は、一部のツールのみを公開できます(Tool filtering を参照)。

3. HTTP with SSE MCP servers

MCP サーバーが HTTP with SSE トランスポートを実装している場合は、 MCPServerSse をインスタンス化します。トランスポート以外の点では、API は Streamable HTTP サーバーと同一です。

from agents import Agent, Runner
from agents.model_settings import ModelSettings
from agents.mcp import MCPServerSse

workspace_id = "demo-workspace"

async with MCPServerSse(
    name="SSE Python Server",
    params={
        "url": "http://localhost:8000/sse",
        "headers": {"X-Workspace": workspace_id},
    },
    cache_tools_list=True,
) as server:
    agent = Agent(
        name="Assistant",
        mcp_servers=[server],
        model_settings=ModelSettings(tool_choice="required"),
    )
    result = await Runner.run(agent, "What's the weather in Tokyo?")
    print(result.final_output)

4. stdio MCP servers

ローカルのサブプロセスとして実行する MCP サーバーには、MCPServerStdio を使用します。SDK はプロセスを起動し、パイプを開いたまま維持し、コンテキストマネージャの終了時に自動でクローズします。このオプションは、迅速なプロトタイプや、サーバーがコマンドラインのエントリポイントのみを公開している場合に便利です。

from pathlib import Path
from agents import Agent, Runner
from agents.mcp import MCPServerStdio

current_dir = Path(__file__).parent
samples_dir = current_dir / "sample_files"

async with MCPServerStdio(
    name="Filesystem Server via npx",
    params={
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-filesystem", str(samples_dir)],
    },
) as server:
    agent = Agent(
        name="Assistant",
        instructions="Use the files in the sample directory to answer questions.",
        mcp_servers=[server],
    )
    result = await Runner.run(agent, "List the files available to you.")
    print(result.final_output)

Tool filtering

各 MCP サーバーはツールフィルターをサポートしており、エージェントに必要な機能のみを公開できます。フィルタリングは、構築時または実行ごとに動的に行えます。

静的なツールフィルタリング

create_static_tool_filter を使用して、シンプルな許可/ブロックのリストを設定します:

from pathlib import Path

from agents.mcp import MCPServerStdio, create_static_tool_filter

samples_dir = Path("/path/to/files")

filesystem_server = MCPServerStdio(
    params={
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-filesystem", str(samples_dir)],
    },
    tool_filter=create_static_tool_filter(allowed_tool_names=["read_file", "write_file"]),
)

allowed_tool_namesblocked_tool_names の両方が指定された場合、SDK はまず許可リストを適用し、その後残りの集合からブロックされたツールを取り除きます。

動的なツールフィルタリング

より高度なロジックには、ToolFilterContext を受け取る呼び出し可能オブジェクトを渡します。コール可能オブジェクトは同期・非同期のどちらでもよく、ツールを公開すべき場合に True を返します。

from pathlib import Path

from agents.mcp import MCPServerStdio, ToolFilterContext

samples_dir = Path("/path/to/files")

async def context_aware_filter(context: ToolFilterContext, tool) -> bool:
    if context.agent.name == "Code Reviewer" and tool.name.startswith("danger_"):
        return False
    return True

async with MCPServerStdio(
    params={
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-filesystem", str(samples_dir)],
    },
    tool_filter=context_aware_filter,
) as server:
    ...

フィルターコンテキストは、アクティブな run_context、ツールを要求している agent、および server_name を公開します。

Prompts

MCP サーバーは、エージェントの instructions を動的に生成するプロンプトも提供できます。プロンプトをサポートするサーバーは 2 つのメソッドを公開します:

  • list_prompts() は利用可能なプロンプトテンプレートを列挙します。
  • get_prompt(name, arguments) は、任意のパラメーター付きで具体的なプロンプトを取得します。
from agents import Agent

prompt_result = await server.get_prompt(
    "generate_code_review_instructions",
    {"focus": "security vulnerabilities", "language": "python"},
)
instructions = prompt_result.messages[0].content.text

agent = Agent(
    name="Code Reviewer",
    instructions=instructions,
    mcp_servers=[server],
)

Caching

すべてのエージェント実行は、各 MCP サーバーに対して list_tools() を呼び出します。リモートサーバーは顕著なレイテンシをもたらす可能性があるため、すべての MCP サーバークラスは cache_tools_list オプションを公開します。ツール定義が頻繁に変更されないと確信できる場合にのみ True に設定してください。後で新しい一覧を強制するには、サーバーインスタンスで invalidate_tools_cache() を呼び出します。

Tracing

Tracing は MCP のアクティビティを自動的に捕捉します。含まれるもの:

  1. MCP サーバーへのツール一覧取得の呼び出し。
  2. ツール呼び出しに関する MCP 関連情報。

MCP Tracing Screenshot

参考資料