Skip to content

New mcp client quickstart #46450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 29, 2025
Merged
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
93 changes: 93 additions & 0 deletions docs/ai/quickstarts/build-mcp-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
title: Quickstart - Create a minimal MCP client using .NET
description: Learn to create a minimal MCP client and connect it to an MCP server using .NET
ms.date: 05/27/2025
ms.topic: quickstart
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
author: alexwolfmsft
---

# Create a minimal MCP client using .NET

In this quickstart, you build a minimal [Model Context Protocol (MCP)](../get-started-mcp.md) client using the [C# SDK for MCP](https://github.com/modelcontextprotocol/csharp-sdk). 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](build-mcp-server.md) quickstart.

## Prerequisites

- [.NET 8.0 SDK or higher](https://dotnet.microsoft.com/download)
- [Visual Studio Code](https://code.visualstudio.com/)

> [!NOTE]
> The MCP client you build in the sections ahead connects to the sample MCP server from the [Build a minimal MCP server](build-mcp-server.md) 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:

```console
dotnet new console -n MCPHostApp
```

1. Navigate into the newly created project folder:

```console
cd MCPHostApp
```

1. Run the following commands to add the necessary NuGet packages:

```console
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 --prerelease
dotnet add package ModelContextProtocol --prerelease
```

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

```console
code .
```

### Add the app code

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

:::code language="csharp" source="snippets/mcp-client/program.cs" :::

The preceding code accomplishes the following tasks:

- Initializes an `IChatClient` abstraction using the [`Microsoft.Extensions.AI`](/dotnet/ai/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:

```console
dotnet run
```

1. Once the app is running, enter a prompt to run the **ReverseEcho** tool:

```console
Reverse the following: "Hello, minimal MCP server!"
```

1. Verify that the server responds with the echoed message:

```output
!revres PCM laminim ,olleH
```

## Related content

[Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md)
1 change: 1 addition & 0 deletions docs/ai/quickstarts/build-mcp-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,5 @@ Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:

## Related content

[Build a minimal MCP client](build-mcp-client.md)
[Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md)
18 changes: 18 additions & 0 deletions docs/ai/quickstarts/snippets/mcp-client/MinimalMCPClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.4" />
<PackageReference Include="Azure.Identity" Version="1.13.2" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.5.0" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.3-preview.1.25230.7" />
<PackageReference Include="ModelContextProtocol" Version="0.1.0-preview.11" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions docs/ai/quickstarts/snippets/mcp-client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;

// 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 mcpClient = await McpClientFactory.CreateAsync(
new StdioClientTransport(new()
{
Command = "dotnet run",
Arguments = ["--project", "<path-to-your-mcp-server-project>"],
Name = "Minimal MCP Server",
}));

// List 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 via prompts.
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);
}
2 changes: 2 additions & 0 deletions docs/ai/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ items:
href: quickstarts/ai-templates.md
- name: Build a minimal MCP server
href: quickstarts/build-mcp-server.md
- name: Build a minimal MCP client
href: quickstarts/build-mcp-client.md
- name: Concepts
items:
- name: How generative AI and LLMs work
Expand Down