-
Notifications
You must be signed in to change notification settings - Fork 6k
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
New mcp client quickstart #46450
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e8e09c3
New mcp client quickstart
alexwolfmsft 10be2eb
fixes
alexwolfmsft 96e97e7
metadata
alexwolfmsft 2d0a407
tweaks
alexwolfmsft b094a91
fix toc
alexwolfmsft ce22463
Apply suggestions from code review
alexwolfmsft acd5cba
PR fixes
alexwolfmsft 61e35e3
Apply suggestions from code review
alexwolfmsft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
docs/ai/quickstarts/snippets/mcp-client/MinimalMCPClient.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.