You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Create and connect to a minimal MCP server using .NET
11
+
# Create a minimal MCP server using C# and publish to NuGet
12
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).
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.
14
17
15
18
## Prerequisites
16
19
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)
18
21
-[Visual Studio Code](https://code.visualstudio.com/)
19
22
-[GitHub Copilot extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) for Visual Studio Code
- 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.
- 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`.
58
52
59
53
## Configure the MCP server in Visual Studio Code
60
54
@@ -66,15 +60,14 @@ Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:
66
60
67
61
```json
68
62
{
69
-
"inputs": [],
70
63
"servers": {
71
-
"MinimalMcpServer": {
64
+
"SampleMcpServer": {
72
65
"type": "stdio",
73
66
"command": "dotnet",
74
67
"args": [
75
68
"run",
76
69
"--project",
77
-
"${workspaceFolder}/MinimalMcpServer.csproj"
70
+
"<RELATIVE PATH TO PROJECT DIRECTORY>"
78
71
]
79
72
}
80
73
}
@@ -85,30 +78,185 @@ Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:
85
78
86
79
## Test the MCP server
87
80
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.
90
86
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.":::
92
88
93
-
1. Enter a prompt to run the **ReverseEcho** tool:
89
+
1. Enter a prompt to run the **get_random_number** tool:
94
90
95
91
```console
96
-
Reverse the following: "Hello, minimal MCP server!"
92
+
Give me a random number between 1 and 100.
97
93
```
98
94
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:
100
96
101
97
- **Current session** always runs the operation in the current GitHub Copilot Agent Mode session.
102
98
- **Current workspace** always runs the command for the current Visual Studio Code workspace.
103
99
- **Always allow** sets the operation to always run for any GitHub Copilot Agent Mode session or any Visual Studio Code workspace.
104
100
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.
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).
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.
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:
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.",
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:
106
228
107
229
```output
108
-
!revres PCM laminim ,olleH
230
+
The weather in Redmond is balmy.
109
231
```
110
232
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
+
111
255
## Related content
112
256
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)
0 commit comments