Skip to content
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0898b44
Initial draft
JonDouglas Jun 30, 2025
0929dc4
Fix image path
JonDouglas Jun 30, 2025
cfd6d95
Add to ToC
JonDouglas Jun 30, 2025
2e218fc
Fix NuGet link
JonDouglas Jun 30, 2025
881fdb4
Fix download link
JonDouglas Jun 30, 2025
67feebd
Add VS docs
JonDouglas Jun 30, 2025
15e1148
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
JonDouglas Jul 1, 2025
3b81b04
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
JonDouglas Jul 1, 2025
7da8068
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
JonDouglas Jul 1, 2025
73dcd40
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
JonDouglas Jul 1, 2025
36301eb
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
JonDouglas Jul 1, 2025
d57afb3
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
JonDouglas Jul 1, 2025
2eebf55
Add to list
JonDouglas Jul 1, 2025
9b44d02
Update pkg args
JonDouglas Jul 1, 2025
3b6816e
Fix indention, use auto-number, fix tool name based on latest templat…
joelverhagen Jul 1, 2025
9238f77
Undo
joelverhagen Jul 1, 2025
4a6351d
Update docs/ai/quickstarts/build-mcp-server-publish-nuget.md
joelverhagen Jul 1, 2025
f2e4f02
Update image with new tool name
joelverhagen Jul 1, 2025
c04f9e7
Merge into existing quickstart
joelverhagen Jul 2, 2025
a525a33
Improve
joelverhagen Jul 2, 2025
eb6d4e4
Fix lint issues
joelverhagen Jul 2, 2025
790d356
Fix up
joelverhagen Jul 2, 2025
a9e8b7d
Improve sample, suggest int environment as an option
joelverhagen Jul 2, 2025
c402d78
Fix
joelverhagen Jul 2, 2025
b145877
Fix method name
joelverhagen Jul 2, 2025
12f65e1
Word
joelverhagen Jul 2, 2025
5f63725
Fix lint
joelverhagen Jul 3, 2025
7950495
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
72323e6
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
2903727
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
5aaf9ca
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
62b7731
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
444dda7
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
6aa8a64
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
98b668b
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
af7bb13
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
e7eef78
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
384ca48
Indent and polish
joelverhagen Jul 3, 2025
5ece1ef
Move to snippet
joelverhagen Jul 3, 2025
3720db2
Use full snippet
joelverhagen Jul 3, 2025
1c7b5ee
Lint
joelverhagen Jul 3, 2025
6e6d072
Fix rename references
joelverhagen Jul 3, 2025
13d8f52
Update docs/ai/quickstarts/build-mcp-server.md
joelverhagen Jul 3, 2025
d6ece12
Fix URLs
joelverhagen Jul 3, 2025
cff7ef1
Add NuGet.org screenshots
joelverhagen Jul 3, 2025
5d71c33
Bigger screenshots
joelverhagen Jul 3, 2025
a2e9b7d
Use real template version
joelverhagen Jul 8, 2025
295ea8d
Add a trouble shooting section for tool not being called
joelverhagen Jul 8, 2025
e8ead19
Update embedded sample
joelverhagen Jul 9, 2025
b4c2ca8
Update screenshot
joelverhagen Jul 9, 2025
140b995
Fix up inputs
joelverhagen Jul 9, 2025
e74233c
Polish
joelverhagen Jul 9, 2025
2d991f1
Update based on blog and latest template
joelverhagen Jul 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/ai/media/mcp/available-tools-nuget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ai/media/mcp/missing-dnx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
260 changes: 221 additions & 39 deletions docs/ai/quickstarts/build-mcp-server.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,54 @@
---
title: Quickstart - Create a minimal MCP Server using .NET
description: Learn to create and connect to a minimal MCP server using .NET
ms.date: 05/21/2025
title: Quickstart - Create a minimal MCP server using C# and publish to NuGet
description: Learn to create and connect to a minimal MCP server using C# and publish it to NuGet.
ms.date: 07/02/2025
ms.topic: quickstart
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
author: alexwolfmsft
ms.author: alexwolf
---

# Create and connect to a minimal MCP server using .NET
# Create a minimal MCP server using C# and publish to NuGet

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).
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).

> [!NOTE]
> This experience is currently in preview. The referenced 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.

## Prerequisites

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

## Create the project

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:
1. In a terminal window, install the MCP Server template:

```bash
dotnet new console -n MinimalMcpServer
dotnet new install Microsoft.Extensions.AI.Templates::9.6.0-TBD
```

1. Navigate to the `MinimalMcpServer` directory:
1. Create a new MCP server app with the `dotnet new mcpserver` command:

```bash
cd MinimalMcpServer
dotnet new mcpserver -n SampleMcpServer
```

1. Add the following NuGet packages to your app:
1. Navigate to the `SampleMcpServer` directory:

```bash
dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting
cd SampleMcpServer
```

- The [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol) package is the official C# SDK for working with the Model Context Protocol.
- 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.

## Add the app code

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

:::code language="csharp" source="snippets/mcp-server/program.cs" :::

The preceding code:
```bash
dotnet build
```

- Creates a generic host builder for dependency injection, logging, and configuration.
- Configures logging for better integration with MCP clients.
- Registers the MCP server, configures it to use stdio transport, and scans the assembly for tool definitions.
- Builds and runs the host, which starts the MCP server.
- Defines a static class to hold two MCP tools that echo values back to the client.
1. Update the `<PackageId>` in the `.csproj` file to be unique on NuGet.org, for example `<NuGet.org username>.SampleMcpServer`.

## Configure the MCP server in Visual Studio Code

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

```json
{
"inputs": [],
"servers": {
"MinimalMcpServer": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"${workspaceFolder}/MinimalMcpServer.csproj"
"<RELATIVE PATH TO PROJECT DIRECTORY>"
]
}
}
Expand All @@ -85,30 +78,219 @@ Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:

## Test the MCP server

1. Open GitHub Copilot in Visual Studio Code and switch to agent mode.
1. Select the **Select tools** icon to verify your **MinimalMcpServer** is available with both tools listed.
1. Open GitHub Copilot in Visual Studio Code and switch to chat mode.

:::image type="content" source="../media/mcp/available-tools.png" alt-text="A screenshot showing the available MCP tools.":::
1. Select the **Select tools** icon to verify your **SampleMcpServer** is available with the sample tool listed.

1. Enter a prompt to run the **ReverseEcho** tool:
:::image type="content" source="../media/mcp/available-tools-nuget.png" alt-text="A screenshot showing the available MCP tools.":::

1. Enter a prompt to run the **get_random_number** tool:

```console
Reverse the following: "Hello, minimal MCP server!"
Give me a random number between 1 and 100.
```

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:
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:

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

1. Verify that the server responds with the echoed message:
1. Verify that the server responds with a random number:

```output
!revres PCM laminim ,olleH
Your random number is 42.
```

## Adding inputs and configuration options

In this example, 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 depending on your needs.

1. Update the tool code to use an environment variable. Add another tool method after the `GetRandomNumber` method in `Tools/RandomNumberTools.cs`:

```csharp
[McpServerTool(Name = "get_city_weather")]
[Description("Describes random weather in the provided city.")]
public string GetCityWeather(
[Description("Name of the city to return weather for")] string city)
{
// Read the environment variable during tool execution.
// Alternatively, this could be read during startup and passed via IOptions dependency injection
var weather = Environment.GetEnvironmentVariable("WEATHER_CHOICES");
if (string.IsNullOrWhitespace(weather))
{
weather = "balmy,rainy,stormy";
}

var weatherChoices = weather.Split(",");
var selectedWeatherIndex = Random.Shared.Next(0, weatherChoices.Length);

return $"The weather in {city} is {weatherChoices[selectedWeatherIndex]}.";
}
```

1. Update the `.vscode/mcp.json` to set the `WEATHER_CHOICES` environment variable for testing.

```json
{
"servers": {
"MinimalMcpServer": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"<RELATIVE PATH TO PROJECT DIRECTORY>"
],
"env": {
"WEATHER_CHOICES": "sunny,humid,freezing"
}
}
}
}
```

1. Try another prompt with Copilot in VS Code, such as:

```console
What is the weather in Redmond, Washington?
```

VS Code should return a random weather description.

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.

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).

You can use `package_arguments` to define CLI arguments that will be passed to your app. See the MCP Registry project for [more examples](https://github.com/modelcontextprotocol/registry/blob/main/docs/server-json/examples.md).

```json
{
"description": "<your description here>",
"name": "io.github.<your GitHub username here>/<your repo name>",
"packages": [
{
"registry_name": "nuget",
"name": "<your package ID here, defined by PackageId in the .csproj>",
"version": "<your package version here, defined by PackageVersion in the .csproj>",
"package_arguments": [],
"environment_variables": [
{
"name": "WEATHER_CHOICES",
"description": "Comma separated list of weather descriptions to randomly select.",
"is_required": true,
"is_secret": false
}
]
}
],
"repository": {
"url": "https://github.com/<your GitHub username here>/<your repo name>",
"source": "github"
},
"version_detail": {
"version": "0.1.0-beta"
}
}
```

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.

You can [test your MCP server again](#test-the-mcp-server) before moving forward.

## Pack and publish to NuGet

1. Pack the project:

```bash
dotnet pack -c Release
```

1. Publish the package to NuGet:

```bash
dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json
```

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:

```bash
dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://apiint.nuget.org/v3/index.json
```

For more information, see [Publish a package](/nuget/nuget-org/publish-a-package).

## Discover MCP servers on NuGet.org

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.

1. View the package details and copy the JSON from the "MCP Server" tab.

1. In your `mcp.json` file in the `.vscode` folder, add the copied JSON, which will look like:

```json
{
"inputs": [
{
"type": "promptString",
"id": "weather-choices",
"description": "Comma separated list of weather descriptions to randomly select.",
"password": false
}
],
"servers": {
"Contoso.SampleMcpServer": {
"type": "stdio",
"command": "dnx",
"args": [
"Contoso.SampleMcpServer",
"--version",
"0.0.1-beta",
"--yes"
],
"env": {
"WEATHER_CHOICES": "${input:weather-choices}"
}
}
}
}
```

If you published to the NuGet Gallery integration environment, you need to add `"--add-source", "https://apiint.nuget.org/v3/index.json"` at the end of the `"args"` array above.

1. Save the file.

1. In GitHub Copilot, select the **Select tools** icon to verify your **SampleMcpServer** is available with the tools listed.

1. Enter a prompt to run the new **get_city_weather** tool:

```console
What is the weather in Redmond?
```

1. If you added inputs to your MCP server (for example, `WEATHER_CHOICES`), you will be prompted to provide values.

1. Verify that the server responds with the random weather:

```output
The weather in Redmond is balmy.
```

## Common issues

### The command "dnx" needed to run SampleMcpServer was not found.

If VS Code shows this error when starting the MCP server, you need to install a compatible version of the .NET SDK.

:::image type="content" source="../media/mcp/missing-dnx.png" alt-text="A screenshot showing the missing dnx command in VS Code.":::

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.

## Related content

[Build a minimal MCP client](build-mcp-client.md)
[Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md)
- [Get started with .NET AI and the Model Context Protocol](../get-started-mcp.md)
- [Model Context Protocol .NET Samples](https://github.com/microsoft/mcp-dotnet-samples)
- [Build a minimal MCP client](build-mcp-client.md)
- [Publish a package](/nuget/nuget-org/publish-a-package)
- [Find and evaluate NuGet packages for your project](/nuget/consume-packages/finding-and-choosing-packages)
- [What's new in .NET 10](../../core/whats-new/dotnet-10/overview.md)
2 changes: 1 addition & 1 deletion docs/ai/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ items:
href: quickstarts/create-assistant.md
- name: Get started using the AI app templates
href: quickstarts/ai-templates.md
- name: Build a minimal MCP server
- name: Build a minimal MCP server and publish to NuGet
href: quickstarts/build-mcp-server.md
- name: Build a minimal MCP client
href: quickstarts/build-mcp-client.md
Expand Down