Skip to content

Commit 62229aa

Browse files
Merge pull request #47373 from dotnet/main
Merge main into live
2 parents b62c059 + 1bc2349 commit 62229aa

File tree

67 files changed

+736
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+736
-197
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ updates:
933933
patterns:
934934
- "*" # Prefer a single PR per project update.
935935
- package-ecosystem: "nuget"
936-
directory: "/docs/ai/quickstarts/snippets/mcp-server" #MinimalMcpServer.csproj
936+
directory: "/docs/ai/quickstarts/snippets/mcp-server" #SampleMcpServer.csproj
937937
schedule:
938938
interval: "weekly"
939939
day: "wednesday"

docs/ai/advanced/snippets/sample-implementations/Implementations.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.6.0" />
10+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.7.0" />
1111
</ItemGroup>
1212

1313
</Project>

docs/ai/how-to/snippets/content-filtering/AIContentFiltering.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Azure.AI.OpenAI" />
12-
<PackageReference Include="Azure.Identity" Version="1.14.1" />
13-
<PackageReference Include="Microsoft.Extensions.AI" Version="9.6.0" />
12+
<PackageReference Include="Azure.Identity" Version="1.14.2" />
13+
<PackageReference Include="Microsoft.Extensions.AI" Version="9.7.0" />
1414
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.0-preview.1.25207.5" />
1515
</ItemGroup>
1616

docs/ai/index.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ summary: Learn to build AI apps with .NET. Browse sample code, tutorials, quicks
66
metadata:
77
title: AI apps for .NET developers
88
description: Samples, tutorials, and education for building AI apps with .NET
9-
ms.topic: hub-page
9+
ms.topic: landing-page
1010
ms.service: dotnet
1111
ms.date: 12/19/2024
1212
author: alexwolfmsft
157 KB
Loading

docs/ai/media/mcp/missing-dnx.png

5.86 KB
Loading
90.3 KB
Loading
195 KB
Loading
40.7 KB
Loading
Lines changed: 188 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
11
---
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
2+
title: Quickstart - Create a minimal MCP server and publish to NuGet
3+
description: Learn to create and connect to a minimal MCP server using C# and publish it to NuGet.
4+
ms.date: 07/02/2025
55
ms.topic: quickstart
66
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
77
author: alexwolfmsft
88
ms.author: alexwolf
99
---
1010

11-
# Create and connect to a minimal MCP server using .NET
11+
# Create a minimal MCP server using C# and publish to NuGet
1212

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).
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), connect to it using GitHub Copilot, and publish it to NuGet. MCP servers are services that expose capabilities to clients through the Model Context Protocol (MCP).
14+
15+
> [!NOTE]
16+
> The `Microsoft.Extensions.AI.Templates` experience is currently in preview. The template uses the [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol/) library and the [MCP registry `server.json` schema](https://github.com/modelcontextprotocol/registry/blob/main/docs/server-json/README.md), which are both in preview.
1417
1518
## Prerequisites
1619

17-
- [.NET 8.0 SDK or higher](https://dotnet.microsoft.com/download)
20+
- [.NET 10.0 SDK](https://dotnet.microsoft.com/download/dotnet) (preview 6 or higher)
1821
- [Visual Studio Code](https://code.visualstudio.com/)
1922
- [GitHub Copilot extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) for Visual Studio Code
23+
- [NuGet.org account](https://www.nuget.org/users/account/LogOn)
2024

2125
## Create the project
2226

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:
27+
1. In a terminal window, install the MCP Server template (version 9.7.0-preview.2.25356.2 or newer):
2428

2529
```bash
26-
dotnet new console -n MinimalMcpServer
30+
dotnet new install Microsoft.Extensions.AI.Templates
2731
```
2832

29-
1. Navigate to the `MinimalMcpServer` directory:
33+
1. Create a new MCP server app with the `dotnet new mcpserver` command:
3034

3135
```bash
32-
cd MinimalMcpServer
36+
dotnet new mcpserver -n SampleMcpServer
3337
```
3438

35-
1. Add the following NuGet packages to your app:
39+
1. Navigate to the `SampleMcpServer` directory:
3640

3741
```bash
38-
dotnet add package ModelContextProtocol --prerelease
39-
dotnet add package Microsoft.Extensions.Hosting
42+
cd SampleMcpServer
4043
```
4144

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.
45+
1. Build the project:
4846

49-
:::code language="csharp" source="snippets/mcp-server/program.cs" :::
50-
51-
The preceding code:
47+
```bash
48+
dotnet build
49+
```
5250

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.
51+
1. Update the `<PackageId>` in the `.csproj` file to be unique on NuGet.org, for example `<NuGet.org username>.SampleMcpServer`.
5852

5953
## Configure the MCP server in Visual Studio Code
6054

@@ -66,15 +60,14 @@ Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:
6660

6761
```json
6862
{
69-
"inputs": [],
7063
"servers": {
71-
"MinimalMcpServer": {
64+
"SampleMcpServer": {
7265
"type": "stdio",
7366
"command": "dotnet",
7467
"args": [
7568
"run",
7669
"--project",
77-
"${workspaceFolder}/MinimalMcpServer.csproj"
70+
"<RELATIVE PATH TO PROJECT DIRECTORY>"
7871
]
7972
}
8073
}
@@ -85,30 +78,185 @@ Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:
8578

8679
## Test the MCP server
8780

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.
81+
The MCP server template includes a tool called `get_random_number` you can use for testing and as a starting point for development.
82+
83+
1. Open GitHub Copilot in Visual Studio Code and switch to chat mode.
84+
85+
1. Select the **Select tools** icon to verify your **SampleMcpServer** is available with the sample tool listed.
9086

91-
:::image type="content" source="../media/mcp/available-tools.png" alt-text="A screenshot showing the available MCP tools.":::
87+
:::image type="content" source="../media/mcp/available-tools-nuget.png" alt-text="A screenshot showing the available MCP tools.":::
9288

93-
1. Enter a prompt to run the **ReverseEcho** tool:
89+
1. Enter a prompt to run the **get_random_number** tool:
9490

9591
```console
96-
Reverse the following: "Hello, minimal MCP server!"
92+
Give me a random number between 1 and 100.
9793
```
9894

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:
95+
1. GitHub Copilot requests permission to run the **get_random_number** tool for your prompt. Select **Continue** or use the arrow to select a more specific behavior:
10096

10197
- **Current session** always runs the operation in the current GitHub Copilot Agent Mode session.
10298
- **Current workspace** always runs the command for the current Visual Studio Code workspace.
10399
- **Always allow** sets the operation to always run for any GitHub Copilot Agent Mode session or any Visual Studio Code workspace.
104100

105-
1. Verify that the server responds with the echoed message:
101+
1. Verify that the server responds with a random number:
102+
103+
```output
104+
Your random number is 42.
105+
```
106+
107+
## Add inputs and configuration options
108+
109+
In this example, you enhance the MCP server to use a configuration value set in an environment variable. This could be configuration needed for the functioning of your MCP server, such as an API key, an endpoint to connect to, or a local directory path.
110+
111+
1. Add another tool method after the `GetRandomNumber` method in `Tools/RandomNumberTools.cs`. Update the tool code to use an environment variable.
112+
113+
:::code language="csharp" source="snippets/mcp-server/Tools/RandomNumberTools.cs" range="19-36":::
114+
115+
1. Update the `.vscode/mcp.json` to set the `WEATHER_CHOICES` environment variable for testing.
116+
117+
```json
118+
{
119+
"servers": {
120+
"SampleMcpServer": {
121+
"type": "stdio",
122+
"command": "dotnet",
123+
"args": [
124+
"run",
125+
"--project",
126+
"<RELATIVE PATH TO PROJECT DIRECTORY>"
127+
],
128+
"env": {
129+
"WEATHER_CHOICES": "sunny,humid,freezing"
130+
}
131+
}
132+
}
133+
}
134+
```
135+
136+
1. Try another prompt with Copilot in VS Code, such as:
137+
138+
```console
139+
What is the weather in Redmond, Washington?
140+
```
141+
142+
VS Code should return a random weather description.
143+
144+
1. Update the `.mcp/server.json` to declare your environment variable input. The `server.json` file schema is defined by the [MCP Registry project](https://github.com/modelcontextprotocol/registry/blob/main/docs/server-json/README.md) and is used by NuGet.org to generate VS Code MCP configuration.
145+
146+
* Use the `environment_variables` property to declare environment variables used by your app that will be set by the client using the MCP server (for example, VS Code).
147+
148+
* Use the `package_arguments` property to define CLI arguments that will be passed to your app. For more examples, see the [MCP Registry project](https://github.com/modelcontextprotocol/registry/blob/main/docs/server-json/examples.md).
149+
150+
:::code language="json" source="snippets/mcp-server/.mcp/server.json":::
151+
152+
The only information used by NuGet.org in the `server.json` is the first `packages` array item with the `registry_name` value matching `nuget`. The other top-level properties aside from the `packages` property are currently unused and are intended for the upcoming central MCP Registry. You can leave the placeholder values until the MCP Registry is live and ready to accept MCP server entries.
153+
154+
You can [test your MCP server again](#test-the-mcp-server) before moving forward.
155+
156+
## Pack and publish to NuGet
157+
158+
1. Pack the project:
159+
160+
```bash
161+
dotnet pack -c Release
162+
```
163+
164+
1. Publish the package to NuGet:
165+
166+
```bash
167+
dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json
168+
```
169+
170+
If you want to test the publishing flow before publishing to NuGet.org, you can register an account on the NuGet Gallery integration environment: [https://int.nugettest.org](https://int.nugettest.org). The `push` command would be modified to:
171+
172+
```bash
173+
dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://apiint.nugettest.org/v3/index.json
174+
```
175+
176+
For more information, see [Publish a package](/nuget/nuget-org/publish-a-package).
177+
178+
## Discover MCP servers on NuGet.org
179+
180+
1. Search for your MCP server package on [NuGet.org](https://www.nuget.org/packages?packagetype=mcpserver) (or [int.nugettest.org](https://int.nugettest.org/packages?packagetype=mcpserver) if you published to the integration environment) and select it from the list.
181+
182+
:::image type="content" source="../media/mcp/nuget-mcp-search.png" alt-text="A screenshot showing a search for MCP servers on NuGet.org.":::
183+
184+
1. View the package details and copy the JSON from the "MCP Server" tab.
185+
186+
:::image type="content" source="../media/mcp/nuget-mcp-display.png" alt-text="A screenshot showing a specific MCP server displayed on NuGet.org.":::
187+
188+
1. In your `mcp.json` file in the `.vscode` folder, add the copied JSON, which looks like this:
189+
190+
```json
191+
{
192+
"inputs": [
193+
{
194+
"type": "promptString",
195+
"id": "weather_choices",
196+
"description": "Comma separated list of weather descriptions to randomly select.",
197+
"password": false
198+
}
199+
],
200+
"servers": {
201+
"Contoso.SampleMcpServer": {
202+
"type": "stdio",
203+
"command": "dnx",
204+
"args": ["[email protected]", "--yes"],
205+
"env": {
206+
"WEATHER_CHOICES": "${input:weather_choices}"
207+
}
208+
}
209+
}
210+
}
211+
```
212+
213+
If you published to the NuGet Gallery integration environment, you need to add `"--add-source", "https://apiint.nugettest.org/v3/index.json"` at the end of the `"args"` array.
214+
215+
1. Save the file.
216+
217+
1. In GitHub Copilot, select the **Select tools** icon to verify your **SampleMcpServer** is available with the tools listed.
218+
219+
1. Enter a prompt to run the new **get_city_weather** tool:
220+
221+
```console
222+
What is the weather in Redmond?
223+
```
224+
225+
1. If you added inputs to your MCP server (for example, `WEATHER_CHOICES`), you will be prompted to provide values.
226+
227+
1. Verify that the server responds with the random weather:
106228

107229
```output
108-
!revres PCM laminim ,olleH
230+
The weather in Redmond is balmy.
109231
```
110232

233+
## Common issues
234+
235+
### The command "dnx" needed to run SampleMcpServer was not found.
236+
237+
If VS Code shows this error when starting the MCP server, you need to install a compatible version of the .NET SDK.
238+
239+
:::image type="content" source="../media/mcp/missing-dnx.png" alt-text="A screenshot showing the missing dnx command in VS Code.":::
240+
241+
The `dnx` command is shipped as part of the .NET SDK, starting with version 10 preview 6. [Install the .NET 10 SDK](https://dotnet.microsoft.com/download/dotnet) to resolve this issue.
242+
243+
### GitHub Copilot does not use your tool (an answer is provided without invoking your tool).
244+
245+
Generally speaking, an AI agent like GitHub Copilot is informed that it has some tools available by the client application, such as VS Code. Some tools, such as the sample random number tool, might not be leveraged by the AI agent because it has similar functionality built in.
246+
247+
If your tool is not being used, check the following:
248+
249+
1. Verify that your tool appears in the list of tools that VS Code has enabled. See the screenshot in [Test the MCP server](#test-the-mcp-server) for how to check this.
250+
1. Explicitly reference the name of the tool in your prompt. In VS Code, you can reference your tool by name. For example, `Using #get_random_weather, what is the weather in Redmond?`.
251+
1. Verify your MCP server is able to start. You can check this by clicking the "Start" button visible above your MCP server configuration in the VS Code user or workspace settings.
252+
253+
:::image type="content" source="../media/mcp/started-mcp-server.png" alt-text="A screenshot showing an MCP server in VS Code configuration that is started.":::
254+
111255
## Related content
112256

113-
[Build a minimal MCP client](build-mcp-client.md)
114-
[Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md)
257+
- [Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md)
258+
- [Model Context Protocol .NET samples](https://github.com/microsoft/mcp-dotnet-samples)
259+
- [Build a minimal MCP client](build-mcp-client.md)
260+
- [Publish a package](/nuget/nuget-org/publish-a-package)
261+
- [Find and evaluate NuGet packages for your project](/nuget/consume-packages/finding-and-choosing-packages)
262+
- [What's new in .NET 10](../../core/whats-new/dotnet-10/overview.md)

0 commit comments

Comments
 (0)