mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Adding a few small sample improvements (#914)
* A few small sample improvements * Address PR comments. * Simplify Mcp sample further.
This commit is contained in:
committed by
GitHub
Unverified
parent
ef9c072eab
commit
863c8d7471
-1
@@ -11,7 +11,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
-1
@@ -11,7 +11,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Agents.AI;
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,18 @@
|
||||
// This sample shows how to expose an AI agent as an MCP tool.
|
||||
|
||||
using System;
|
||||
using Agent_Step10_AsMcpTool;
|
||||
using Azure.AI.Agents.Persistent;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
|
||||
var deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
||||
|
||||
const string JokerName = "Joker";
|
||||
const string JokerDescription = "An agent that tells jokes.";
|
||||
const string JokerInstructions = "You are good at telling jokes, and you always start each joke with 'Aye aye, captain!'.";
|
||||
|
||||
var persistentAgentsClient = new PersistentAgentsClient(endpoint, new AzureCliCredential());
|
||||
@@ -22,16 +23,21 @@ var persistentAgentsClient = new PersistentAgentsClient(endpoint, new AzureCliCr
|
||||
var agentMetadata = await persistentAgentsClient.Administration.CreateAgentAsync(
|
||||
model: deploymentName,
|
||||
name: JokerName,
|
||||
description: JokerDescription,
|
||||
instructions: JokerInstructions);
|
||||
|
||||
// Retrieve the server side persistent agent as an AIAgent.
|
||||
AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(agentMetadata.Value.Id);
|
||||
|
||||
// Register the MCP server with StdIO transport and expose the agent as an MCP tool.
|
||||
// Convert the agent to an AIFunction and then to an MCP tool.
|
||||
// The agent name and description will be used as the mcp tool name and description.
|
||||
McpServerTool tool = McpServerTool.Create(agent.AsAIFunction());
|
||||
|
||||
// Register the MCP server with StdIO transport and expose the tool via the server.
|
||||
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
|
||||
builder.Services
|
||||
.AddMcpServer()
|
||||
.WithStdioServerTransport()
|
||||
.WithTools([agent.AsMcpTool()]);
|
||||
.WithTools([tool]);
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
||||
Reference in New Issue
Block a user