Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>

<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>$(NoWarn);CA1812</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Azure.AI.Projects" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.AzureAI\Microsoft.Agents.AI.AzureAI.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft. All rights reserved.

// This sample demonstrates how to use a Hosted MCP (Model Context Protocol) server tool with Azure Foundry Agents.
// The MCP server runs remotely and is invoked by the Azure Foundry service when needed.
// This sample uses the Microsoft Learn MCP endpoint to search documentation.

using Azure.AI.Projects;
using Azure.AI.Projects.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";

const string AgentInstructions = "You are a helpful assistant that can help with Microsoft documentation questions. Use the Microsoft Learn MCP tool to search for documentation.";
const string AgentName = "DocsAgent";

// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents.
AIProjectClient aiProjectClient = new(new Uri(endpoint), new AzureCliCredential());

// Create a Hosted MCP tool that the agent can use.
// The MCP tool is hosted at Microsoft Learn and provides documentation search capabilities.
// Setting ApprovalMode to NeverRequire allows the tool to be called without user approval.
var mcpTool = new HostedMcpServerTool(
serverName: "microsoft_learn",
serverAddress: "https://learn.microsoft.com/api/mcp")
{
AllowedTools = ["microsoft_docs_search"],
ApprovalMode = HostedMcpServerToolApprovalMode.NeverRequire
};

// Create the server side agent with the MCP tool
AIAgent agent = await aiProjectClient.CreateAIAgentAsync(
model: deploymentName,
name: AgentName,
instructions: AgentInstructions,
tools: [mcpTool]);

Console.WriteLine($"Agent '{agent.Name}' created successfully.");

// Ask a question that will use the MCP tool to search documentation
string prompt = "How to create an Azure storage account using az cli?";
Console.WriteLine($"\nUser: {prompt}\n");

// Invoke the agent and output the result
AgentResponse response = await agent.RunAsync(prompt);
Console.WriteLine($"Agent: {response}");

// Cleanup by removing the agent when done
await aiProjectClient.Agents.DeleteAgentAsync(agent.Name);
Console.WriteLine($"\nAgent '{agent.Name}' deleted.");
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Using Hosted MCP Tools with Azure Foundry Agents

This sample demonstrates how to use a Hosted MCP (Model Context Protocol) server tool with Azure Foundry Agents. The MCP server runs remotely and is invoked by the Azure Foundry service when needed.

## What this sample demonstrates

- Creating an agent with a Hosted MCP tool
- Using the Microsoft Learn MCP endpoint for documentation search
- Configuring MCP tool approval modes
- Managing agent lifecycle (creation and deletion)

## Prerequisites

Before you begin, ensure you have the following prerequisites:

- .NET 10 SDK or later
- Azure Foundry service endpoint and deployment configured
- Azure CLI installed and authenticated (for Azure credential authentication)

**Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure Foundry resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively).

Set the following environment variables:

```powershell
$env:AZURE_FOUNDRY_PROJECT_ENDPOINT="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project" # Replace with your Azure Foundry resource endpoint
$env:AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini
```

## Run the sample

Navigate to the FoundryAgents sample directory and run:

```powershell
cd dotnet/samples/GettingStarted/FoundryAgents
dotnet run --project .\FoundryAgents_Step27_LocalMCP
```

## Expected behavior

The sample will:

1. Create an agent with the Microsoft Learn MCP tool configured
2. Ask a question about creating an Azure storage account using az cli
3. The agent will use the MCP tool to search Microsoft Learn documentation
4. Display the agent's response with information from the documentation
5. Clean up resources by deleting the agent
Loading