Editéieren

Create a minimal MCP client using .NET

In this quickstart, you build a minimal Model Context Protocol (MCP) client using the C# SDK for MCP. You also learn how to configure the client to connect to an MCP server, such as the one created in the Build a minimal MCP server quickstart.

Prerequisites

Note

The MCP client you build in the sections ahead connects to the sample MCP server from the Build a minimal MCP server quickstart. You can also use your own MCP server if you provide your own connection configuration.

Create the .NET host app

Complete the following steps to create a .NET console app. The app acts as a host for an MCP client that connects to an MCP server.

Create the project

  1. In a terminal window, navigate to the directory where you want to create your app, and create a new console app with the dotnet new command:

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

    cd MCPHostApp
    
  3. 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
    dotnet add package Microsoft.Extensions.AI.OpenAI
    dotnet add package ModelContextProtocol
    
  4. Open the project folder in your editor of choice, such as Visual Studio Code:

    code .
    

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;

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

// Create the MCP client.
// Configure it to start and connect to your MCP server.
var transport = new StdioClientTransport(new()
{
    Command = "dotnet run",
    Arguments = ["--project", "<path-to-your-mcp-server-project>"],
    Name = "Minimal MCP Server",
});
McpClient mcpClient = await McpClient.CreateAsync(transport);

// List all available tools from the MCP server.
Console.WriteLine("Available tools:");
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
foreach (McpClientTool tool in tools)
{
    Console.WriteLine($"{tool}");
}
Console.WriteLine();

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

    List<ChatResponseUpdate> updates = [];
    await foreach (ChatResponseUpdate 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 and configures it to connect to your 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 a prompt to run the ReverseEcho tool:

    Reverse the following: "Hello, minimal MCP server!"
    
  3. Verify that the server responds with the echoed message:

    !revres PCM laminim ,olleH
    

Get started with .NET AI and the Model Context Protocol