다음을 통해 공유


에이전트를 MCP 도구로 노출

이 자습서에서는 MCP(모델 컨텍스트 프로토콜)를 통해 에이전트를 도구로 노출하여 MCP 도구를 지원하는 다른 시스템에서 사용할 수 있도록 하는 방법을 보여줍니다.

필수 조건

필수 구성 요소는 이 자습서의 간단한 에이전트 만들기 및 실행을 참조하세요.

NuGet 패키지 설치

Azure OpenAI에서 Microsoft 에이전트 프레임워크를 사용하려면 다음 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 에이전트와 같은 MCP 호환 클라이언트에서 사용할 수 있습니다.

다음 단계