Skip to content

Commit 0d38ce6

Browse files
Merge pull request #46457 from dotnet/main
Merge main into live
2 parents d2c907b + 7a7380a commit 0d38ce6

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

docs/ai/media/mcp/available-tools.png

118 KB
Loading
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
12+
<PackageReference Include="ModelContextProtocol" Version="0.2.0-preview.1" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using ModelContextProtocol.Server;
5+
using System.ComponentModel;
6+
7+
// Create a generic host builder for
8+
// dependency injection, logging, and configuration.
9+
var builder = Host.CreateApplicationBuilder(args);
10+
11+
// Configure logging for better integration with MCP clients.
12+
builder.Logging.AddConsole(consoleLogOptions =>
13+
{
14+
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
15+
});
16+
17+
// Register the MCP server and configure it to use stdio transport.
18+
// Scan the assembly for tool definitions.
19+
builder.Services
20+
.AddMcpServer()
21+
.WithStdioServerTransport()
22+
.WithToolsFromAssembly();
23+
24+
// Build and run the host. This starts the MCP server.
25+
await builder.Build().RunAsync();
26+
27+
// Define a static class to hold MCP tools.
28+
[McpServerToolType]
29+
public static class EchoTool
30+
{
31+
// Expose a tool that echoes the input message back to the client.
32+
[McpServerTool, Description("Echoes the message back to the client.")]
33+
public static string Echo(string message) => $"Hello from C#: {message}";
34+
35+
// Expose a tool that returns the input message in reverse.
36+
[McpServerTool, Description("Echoes in reverse the message sent by the client.")]
37+
public static string ReverseEcho(string message) => new string(message.Reverse().ToArray());
38+
}

docs/ai/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ items:
3333
href: quickstarts/create-assistant.md
3434
- name: Get started using the AI app templates
3535
href: quickstarts/ai-templates.md
36+
- name: Build a minimal MCP server
37+
href: quickstarts/build-mcp-server.md
3638
- name: Concepts
3739
items:
3840
- name: How generative AI and LLMs work

0 commit comments

Comments
 (0)