diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Agent_With_AzureOpenAIChatCompletion.csproj b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Agent_With_AzureOpenAIChatCompletion.csproj index 8f7d672d81..a420a388dd 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Agent_With_AzureOpenAIChatCompletion.csproj +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Agent_With_AzureOpenAIChatCompletion.csproj @@ -11,7 +11,6 @@ - diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Agent_With_AzureOpenAIResponses.csproj b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Agent_With_AzureOpenAIResponses.csproj index 8f7d672d81..a420a388dd 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Agent_With_AzureOpenAIResponses.csproj +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Agent_With_AzureOpenAIResponses.csproj @@ -11,7 +11,6 @@ - diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/AIAgentExtensions.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/AIAgentExtensions.cs deleted file mode 100644 index 9243764fcd..0000000000 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/AIAgentExtensions.cs +++ /dev/null @@ -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; - -/// -/// Contains extension methods for . -/// -internal static class AIAgentExtensions -{ - /// - /// Exposes an as a that can be registered with an MCP server. - /// - /// The agent to expose as an MCP tool. - /// 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. - /// The tool name to use. If not provided, the agent's name will be used. - /// The tool description to use. If not provided, the agent's description will be used. - /// The that wraps the agent. - public static McpServerTool AsMcpTool(this AIAgent agent, string? title = null, string? name = null, string? description = null) - { - async Task 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 - }); - } -} diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/Program.cs index c8da07b63a..fe10562ba9 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/Program.cs @@ -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();