次の方法で共有


エージェントを MCP ツールとして公開する

このチュートリアルでは、MCP ツールをサポートする他のシステムで使用できるように、エージェントをモデル コンテキスト プロトコル (MCP) 経由でツールとして公開する方法について説明します。

[前提条件]

前提条件については、このチュートリアルの 「エージェントの作成と実行 」の手順を参照してください。

NuGet パッケージのインストール

Azure OpenAI で Microsoft Agent Framework を使用するには、次の NuGet パッケージをインストールする必要があります。

dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease

また、モデル コンテキスト プロトコル (MCP) 経由でツールをホストするためのサポートを追加するには、次の NuGet パッケージを追加します。

dotnet add package Microsoft.Extensions.Hosting --prerelease
dotnet add package ModelContextProtocol --prerelease

エージェントを MCP ツールとして公開する

AIAgentを MCP ツールとして公開するには、関数でラップし、McpServerToolを使用します。 その後、MCP サーバーに登録する必要があります。 これにより、MCP 互換クライアントによってエージェントをツールとして呼び出すことができます。

まず、MCP ツールとして公開するエージェントを作成します。

using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;

AIAgent agent = new AzureOpenAIClient(
    new Uri("https://<myresource>.openai.azure.com"),
    new AzureCliCredential())
        .GetChatClient("gpt-4o-mini")
        .CreateAIAgent(instructions: "You are good at telling jokes.", name: "Joker");

エージェントを関数ツールに変換してから、MCP ツールに変換します。 エージェント名と説明は、mcp ツールの名前と説明として使用されます。

using ModelContextProtocol.Server;

McpServerTool tool = McpServerTool.Create(agent.AsAIFunction());

標準の入力/出力を介して受信要求をリッスンし、MCP ツールを公開するように MCP サーバーをセットアップします。

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Server;

HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithTools([tool]);

await builder.Build().RunAsync();

これにより、MCP プロトコル経由でエージェントをツールとして公開する MCP サーバーが起動します。

このチュートリアルでは、MCP ツールをサポートする他のシステムで使用できるように、エージェントをモデル コンテキスト プロトコル (MCP) 経由でツールとして公開する方法について説明します。

[前提条件]

前提条件と Python パッケージのインストールについては、このチュートリアルの 「エージェントの作成と実行 」の手順を参照してください。

エージェントを MCP サーバーとして公開する

as_mcp_server() メソッドを使用して、エージェントを MCP サーバーとして公開できます。 これにより、MCP 互換クライアントによってエージェントをツールとして呼び出すことができます。

まず、MCP サーバーとして公開するエージェントを作成します。 エージェントにツールを追加することもできます。

from typing import Annotated
from agent_framework.openai import OpenAIResponsesClient

def get_specials() -> Annotated[str, "Returns the specials from the menu."]:
    return """
        Special Soup: Clam Chowder
        Special Salad: Cobb Salad
        Special Drink: Chai Tea
        """

def get_item_price(
    menu_item: Annotated[str, "The name of the menu item."],
) -> Annotated[str, "Returns the price of the menu item."]:
    return "$9.99"

# Create an agent with tools
agent = OpenAIResponsesClient().create_agent(
    name="RestaurantAgent",
    description="Answer questions about the menu.",
    tools=[get_specials, get_item_price],
)

エージェントを MCP サーバーに変換します。 エージェント名と説明は、MCP サーバーのメタデータとして使用されます。

# Expose the agent as an MCP server
server = agent.as_mcp_server()

標準の入力/出力を介して受信要求をリッスンするように MCP サーバーを設定します。

import anyio
from mcp.server.stdio import stdio_server

async def run():
    async def handle_stdin():
        async with stdio_server() as (read_stream, write_stream):
            await server.run(read_stream, write_stream, server.create_initialization_options())

    await handle_stdin()

if __name__ == "__main__":
    anyio.run(run)

これにより、MCP プロトコル経由でエージェントを公開する MCP サーバーが起動され、VS Code GitHub Copilot Agents などの MCP 互換クライアントで使用できるようになります。

次のステップ