|
| 1 | +--- |
| 2 | +title: Quickstart - Create a minimal MCP client using .NET |
| 3 | +description: Learn to create a minimal MCP client and connect it to an MCP server using .NET |
| 4 | +ms.date: 05/27/2025 |
| 5 | +ms.topic: quickstart |
| 6 | +ms.custom: devx-track-dotnet, devx-track-dotnet-ai |
| 7 | +author: alexwolfmsft |
| 8 | +--- |
| 9 | + |
| 10 | +# Create a minimal MCP client using .NET |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- [.NET 8.0 SDK or higher](https://dotnet.microsoft.com/download) |
| 17 | +- [Visual Studio Code](https://code.visualstudio.com/) |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> 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. |
| 21 | +
|
| 22 | +## Create the .NET host app |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +### Create the project |
| 27 | + |
| 28 | +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: |
| 29 | + |
| 30 | + ```console |
| 31 | + dotnet new console -n MCPHostApp |
| 32 | + ``` |
| 33 | + |
| 34 | +1. Navigate into the newly created project folder: |
| 35 | + |
| 36 | + ```console |
| 37 | + cd MCPHostApp |
| 38 | + ``` |
| 39 | + |
| 40 | +1. Run the following commands to add the necessary NuGet packages: |
| 41 | + |
| 42 | + ```console |
| 43 | + dotnet add package Azure.AI.OpenAI --prerelease |
| 44 | + dotnet add package Azure.Identity |
| 45 | + dotnet add package Microsoft.Extensions.AI |
| 46 | + dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease |
| 47 | + dotnet add package ModelContextProtocol --prerelease |
| 48 | + ``` |
| 49 | + |
| 50 | +1. Open the project folder in your editor of choice, such as Visual Studio Code: |
| 51 | + |
| 52 | + ```console |
| 53 | + code . |
| 54 | + ``` |
| 55 | + |
| 56 | +### Add the app code |
| 57 | + |
| 58 | +Replace the contents of `Program.cs` with the following code: |
| 59 | + |
| 60 | +:::code language="csharp" source="snippets/mcp-client/program.cs" ::: |
| 61 | + |
| 62 | +The preceding code accomplishes the following tasks: |
| 63 | + |
| 64 | +- Initializes an `IChatClient` abstraction using the [`Microsoft.Extensions.AI`](/dotnet/ai/microsoft-extensions-ai) libraries. |
| 65 | +- Creates an MCP client and configures it to connect to your MCP server. |
| 66 | +- Retrieves and displays a list of available tools from the MCP server, which is a standard MCP function. |
| 67 | +- Implements a conversational loop that processes user prompts and utilizes the tools for responses. |
| 68 | + |
| 69 | +## Run and test the app |
| 70 | + |
| 71 | +Complete the following steps to test your .NET host app: |
| 72 | + |
| 73 | +1. In a terminal window open to the root of your project, run the following command to start the app: |
| 74 | + |
| 75 | + ```console |
| 76 | + dotnet run |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Once the app is running, enter a prompt to run the **ReverseEcho** tool: |
| 80 | + |
| 81 | + ```console |
| 82 | + Reverse the following: "Hello, minimal MCP server!" |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Verify that the server responds with the echoed message: |
| 86 | + |
| 87 | + ```output |
| 88 | + !revres PCM laminim ,olleH |
| 89 | + ``` |
| 90 | + |
| 91 | +## Related content |
| 92 | + |
| 93 | +[Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md) |
0 commit comments