Edit

Share via


Get started using the Azure MCP Server with .NET

The Azure MCP Server uses the Model Context Protocol (MCP) to standardize integrations between AI apps and external tools and data sources, allowing for AI systems to perform operations that are context-aware of your Azure resources.

In this article, you learn how to complete the following tasks:

  • Install and authenticate to the Azure MCP Server
  • Connect to Azure MCP Server using a custom .NET client
  • Run prompts to test Azure MCP Server operations and manage Azure resources

Prerequisites

Note

The Azure resources you intend to access with Azure MCP Server must already exist within your Azure subscription. Additionally, your user account must have the necessary RBAC roles and permissions assigned for those resources.

Sign-in for local development

Azure MCP Server provides a seamless authentication experience using token-based authentication via Microsoft Entra ID. Internally, Azure MCP Server uses DefaultAzureCredential from the Azure Identity library to authenticate users.

You need to sign-in to one of the tools supported by DefaultAzureCredential locally with your Azure account to work with Azure MCP Server. Sign-in using a terminal window, such as the Visual Studio Code terminal:

az login

Once you have signed-in successfully to one of the preceding tools, Azure MCP Server can automatically discover your credentials and use them to authenticate and perform operations on Azure services.

Note

You can also sign-in to Azure through Visual Studio. Azure MCP Server is only able to run operations that the signed-in user has permissions to perform.

Create the .NET host app

Complete the following steps to create a .NET console app. The app connects to an AI model and acts as a host for an MCP client that connects to an Azure MCP Server.

Create the project

  1. Open a terminal to an empty folder where you want to create the project.

  2. Run the following command to create a new .NET console application:

    dotnet new console -n MCPHostApp
    
  3. Navigate into the newly created project folder:

    cd MCPHostApp
    
  4. Open the project folder in your editor of choice, such as Visual Studio Code:

    code .
    

Add the dependencies

  1. In the terminal, run the following commands to add the necessary NuGet packages:

    dotnet add package Azure.AI.OpenAI --prerelease
    dotnet add package Azure.Identity
    dotnet add package Microsoft.Extensions.AI --prerelease
    dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
    dotnet add package ModelContextProtocol --prerelease
    
  2. Verify that the packages were added by checking the MCPHostApp.csproj file.

  3. Run the following command to build the project and ensure everything is set up correctly:

    dotnet build
    

Add the app code

Replace the contents of Program.cs with the following code:

using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;

// Create an IChatClient
IChatClient client =
    new ChatClientBuilder(
        new AzureOpenAIClient(new Uri("<your-Azure-OpenAI-endpoint>"), 
        new DefaultAzureCredential())
        .GetChatClient("gpt-4o").AsIChatClient())
    .UseFunctionInvocation()
    .Build();

// Create the MCP client
var mcpClient = await McpClientFactory.CreateAsync(
    new StdioClientTransport(new()
    {
        Command = "npx",
        Arguments = ["-y", "@azure/mcp@latest", "server", "start"],
        Name = "Azure MCP",
    }));

// Get all available tools from the MCP server
Console.WriteLine("Available tools:");
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
    Console.WriteLine($"{tool}");
}
Console.WriteLine();

// Conversational loop that can utilize the tools
List<ChatMessage> messages = [];
while (true)
{
    Console.Write("Prompt: ");
    messages.Add(new(ChatRole.User, Console.ReadLine()));

    List<ChatResponseUpdate> updates = [];
    await foreach (var update in client
        .GetStreamingResponseAsync(messages, new() { Tools = [.. tools] }))
    {
        Console.Write(update);
        updates.Add(update);
    }
    Console.WriteLine();

    messages.AddMessages(updates);
}

The preceding code accomplishes the following tasks:

  • Initializes an IChatClient abstraction using the Microsoft.Extensions.AI libraries.
  • Creates an MCP client to interact with the Azure MCP Server using a standard I/O transport. The provided npx command and corresponding arguments download and start the Azure MCP Server.
  • Retrieves and displays a list of available tools from the MCP server, which is a standard MCP function.
  • Implements a conversational loop that processes user prompts and utilizes the tools for responses.

Run and test the app

Complete the following steps to test your .NET host app:

  1. In a terminal window open to the root of your project, run the following command to start the app:

    dotnet run
    
  2. Once the app is running, enter the following test prompt:

    List all of the resource groups in my subscription
    

    The output for the previous prompt should resemble the following text:

    The following resource groups are available for your subscription:
    
    1. **DefaultResourceGroup-EUS** (Location: `eastus`)
    2. **rg-testing** (Location: `centralus`)
    3. **rg-azd** (Location: `eastus2`)
    4. **msdocs-sample** (Location: `southcentralus`)
    14. **ai-testing** (Location: `eastus2`)
    
    Let me know if you need further details or actions related to any of these resource groups!
    
  3. Explore and test the Azure MCP operations using other relevant prompts, such as:

    List all of the storage accounts in my subscription
    Get the available tables in my storage accounts
    

Next steps