Microsoft Agent Framework——智能体教程—— 将智能体作为 MCP 工具暴露出来
This tutorial shows you how to expose an agent as a tool over the Model Context Protocol (MCP), so it can be used by other systems that support MCP tools.
本教程将向您展示如何通过模型上下文协议(Model Context Protocol,MCP)将一个智能体作为工具暴露出来,以便其他支持 MCP 工具的系统能够使用它。
Prerequisites 前置条件
For prerequisites see the Create and run a simple agent step in this tutorial.
有关前置条件,请参阅本教程中的“创建并运行一个简单智能体”步骤。
Installing Nuget packages 安装 NuGet 包
To use the Microsoft Agent Framework with Azure OpenAI, you need to install the following NuGet packages:
要在 Azure OpenAI 中使用 Microsoft 智能体框架,您需要安装以下 NuGet 包:
dotnet add package Azure.Identity
dotnet add package Azure.AI.OpenAI
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
To add support for hosting a tool over the Model Context Protocol (MCP), add the following Nuget packages
若要添加对通过模型上下文协议(MCP)托管工具的支持,请添加以下 NuGet 包。
dotnet add package Microsoft.Extensions.Hosting --prerelease
dotnet add package ModelContextProtocol --prerelease
Exposing an agent as an MCP tool 将智能体作为 MCP 工具暴露出来
You can expose an AIAgent as an MCP tool by wrapping it in a function and using McpServerTool. You then need to register it with an MCP server. This allows the agent to be invoked as a tool by any MCP-compatible client.
您可以通过将 AIAgent 包装在一个函数中,并使用 McpServerTool,将其作为 MCP 工具暴露出来。然后,您需要将其注册到一个 MCP 服务器上。这样,任何兼容 MCP 的客户端都可以将该智能体作为工具进行调用。
First, create an agent that we will expose as an MCP tool.
首先,创建一个我们将作为 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");
Turn the agent into a function tool and then an MCP tool. The agent name and description will be used as the mcp tool name and description.
将智能体转换为函数工具,然后再转换为 MCP 工具。该智能体的名称和描述将用作 MCP 工具的名称和描述。
using ModelContextProtocol.Server;
McpServerTool tool = McpServerTool.Create(agent.AsAIFunction());
Setup the MCP server to listen for incoming requests over standard input/output and expose the MCP tool:
设置 MCP 服务器,通过标准输入/输出监听传入的请求,并暴露该 MCP 工具:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithTools([tool]);
await builder.Build().RunAsync();
This will start an MCP server that exposes the agent as a tool over the MCP protocol.
这将启动一个MCP服务器,通过MCP协议将该智能体作为工具暴露出来。
自己使用的实际代码如下:
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Server;
using OpenAI;
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
#pragma warning disable MEAI001 // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
AIAgent agent = new AzureOpenAIClient(new Uri("https://models.inference.ai.azure.com"), new AzureKeyCredential(token))
.GetChatClient("gpt-4o-mini")
.CreateAIAgent(instructions: "You are good at telling jokes.", name: "Joker");
#pragma warning restore MEAI001 // 类型仅用于评估,在将来的更新中可能会被更改或删除。取消此诊断以继续。
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithTools([McpServerTool.Create(agent.AsAIFunction())]);
await builder.Build().RunAsync();

浙公网安备 33010602011771号