|
| 1 | +--- |
| 2 | +title: Quickstart - Create a minimal MCP Server using .NET |
| 3 | +description: Learn to create and connect to a minimal MCP server using .NET |
| 4 | +ms.date: 05/21/2025 |
| 5 | +ms.topic: quickstart |
| 6 | +ms.custom: devx-track-dotnet, devx-track-dotnet-ai |
| 7 | +author: alexwolfmsft |
| 8 | +ms.author: alexwolf |
| 9 | +--- |
| 10 | + |
| 11 | +# Create and connect to a minimal MCP server using .NET |
| 12 | + |
| 13 | +In this quickstart, you create a minimal Model Context Protocol (MCP) server using the [C# SDK for MCP](https://github.com/modelcontextprotocol/csharp-sdk) and connect to it using GitHub Copilot. MCP servers are services that expose capabilities to clients through the Model Context Protocol (MCP). |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- [.NET 8.0 SDK or higher](https://dotnet.microsoft.com/download) |
| 18 | +- [Visual Studio Code](https://code.visualstudio.com/) |
| 19 | +- [GitHub Copilot extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) for Visual Studio Code |
| 20 | + |
| 21 | +## Create the project |
| 22 | + |
| 23 | +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: |
| 24 | + |
| 25 | + ```bash |
| 26 | + dotnet new console -n MinimalMcpServer |
| 27 | + ``` |
| 28 | + |
| 29 | +1. Navigate to the `MinimalMcpServer` directory: |
| 30 | + |
| 31 | + ```bash |
| 32 | + cd MinimalMcpServer |
| 33 | + ``` |
| 34 | + |
| 35 | +1. Add the following NuGet packages to your app: |
| 36 | + |
| 37 | + ```bash |
| 38 | + dotnet add package ModelContextProtocol --prerelease |
| 39 | + dotnet add package Microsoft.Extensions.Hosting |
| 40 | + ``` |
| 41 | + |
| 42 | + - The [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol) package is the official C# SDK for working with the Model Context Protocol. |
| 43 | + - The [Microsoft.Extensions.Hosting](https://www.nuget.org/packages/Microsoft.Extensions.Hosting) package provides the generic .NET `HostBuilder` and services for logging and dependency injection. |
| 44 | + |
| 45 | +## Add the app code |
| 46 | + |
| 47 | +Replace the contents of `Program.cs` with the following code to implement a minimal MCP server that exposes simple echo tools. The AI model invokes these tools as necessary to generate responses to user prompts. |
| 48 | + |
| 49 | +:::code language="csharp" source="snippets/mcp-server/program.cs" ::: |
| 50 | + |
| 51 | +The preceding code: |
| 52 | + |
| 53 | +- Creates a generic host builder for dependency injection, logging, and configuration. |
| 54 | +- Configures logging for better integration with MCP clients. |
| 55 | +- Registers the MCP server, configures it to use stdio transport, and scans the assembly for tool definitions. |
| 56 | +- Builds and runs the host, which starts the MCP server. |
| 57 | +- Defines a static class to hold two MCP tools that echo values back to the client. |
| 58 | + |
| 59 | +## Configure the MCP server in Visual Studio Code |
| 60 | + |
| 61 | +Configure GitHub Copilot for Visual Studio Code to use your custom MCP server: |
| 62 | + |
| 63 | +1. If you haven't already, open your project folder in Visual Studio Code. |
| 64 | +1. Create a `.vscode` folder at the root of your project. |
| 65 | +1. Add an `mcp.json` file in the `.vscode` folder with the following content: |
| 66 | + |
| 67 | + ```json |
| 68 | + { |
| 69 | + "inputs": [], |
| 70 | + "servers": { |
| 71 | + "MinimalMcpServer": { |
| 72 | + "type": "stdio", |
| 73 | + "command": "dotnet", |
| 74 | + "args": [ |
| 75 | + "run", |
| 76 | + "--project", |
| 77 | + "${workspaceFolder}/MinimalMcpServer.csproj" |
| 78 | + ] |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | +1. Save the file. |
| 85 | + |
| 86 | +## Test the MCP server |
| 87 | + |
| 88 | +1. Open GitHub Copilot in Visual Studio Code and switch to agent mode. |
| 89 | +1. Select the **Select tools** icon to verify your **MinimalMcpServer** is available with both tools listed. |
| 90 | + |
| 91 | + :::image type="content" source="../media/mcp/available-tools.png" alt-text="A screenshot showing the available MCP tools."::: |
| 92 | + |
| 93 | +1. Enter a prompt to run the **ReverseEcho** tool: |
| 94 | + |
| 95 | + ```console |
| 96 | + Reverse the following: "Hello, minimal MCP server!" |
| 97 | + ``` |
| 98 | + |
| 99 | +1. GitHub Copilot requests permission to run the **ReverseEcho** tool for your prompt. Select **Continue** or use the arrow to select a more specific behavior: |
| 100 | + |
| 101 | + - **Current session** always runs the operation in the current GitHub Copilot Agent Mode session. |
| 102 | + - **Current workspace** always runs the command for the current Visual Studio Code workspace. |
| 103 | + - **Always allow** sets the operation to always run for any GitHub Copilot Agent Mode session or any Visual Studio Code workspace. |
| 104 | + |
| 105 | +1. Verify that the server responds with the echoed message: |
| 106 | + |
| 107 | + ```output |
| 108 | + !revres PCM laminim ,olleH |
| 109 | + ``` |
| 110 | + |
| 111 | +## Related content |
| 112 | + |
| 113 | +[Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md) |
0 commit comments