mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
* add sample demostrating how to expose ai-agent as an mcp tool * Update dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/Program.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/samples/GettingStarted/Agents/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove threadId parameter --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
43 lines
1.8 KiB
C#
43 lines
1.8 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.ComponentModel;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI.Agents;
|
|
using ModelContextProtocol.Server;
|
|
|
|
namespace Agent_Step10_AsMcpTool;
|
|
|
|
/// <summary>
|
|
/// Contains extension methods for <see cref="AIAgent"/>.
|
|
/// </summary>
|
|
internal static class AIAgentExtensions
|
|
{
|
|
/// <summary>
|
|
/// Exposes an <see cref="AIAgent"/> as a <see cref="McpServerTool"/> that can be registered with an MCP server.
|
|
/// </summary>
|
|
/// <param name="agent">The agent to expose as an MCP tool.</param>
|
|
/// <param name="title">A human-readable title for the tool that can be displayed to users. If not provided, the tool name or agent name will be used.</param>
|
|
/// <param name="name">The tool name to use. If not provided, the agent's name will be used.</param>
|
|
/// <param name="description">The tool description to use. If not provided, the agent's description will be used.</param>
|
|
/// <returns>The <see cref="McpServerTool"/> that wraps the agent.</returns>
|
|
public static McpServerTool AsMcpTool(this AIAgent agent, string? title = null, string? name = null, string? description = null)
|
|
{
|
|
async Task<string> RunAgentAsync(
|
|
[Description("Available information that will guide in performing this operation.")] string query,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
AgentRunResponse response = await agent.RunAsync(query, cancellationToken: cancellationToken);
|
|
|
|
return response.ToString();
|
|
}
|
|
|
|
return McpServerTool.Create(RunAgentAsync, new McpServerToolCreateOptions()
|
|
{
|
|
Title = title ?? name ?? agent.Name,
|
|
Name = name ?? agent.Name,
|
|
Description = description ?? agent.Description
|
|
});
|
|
}
|
|
}
|