Building Your First MCP Server with .NET 10 and Publishing to NuGet
Written by Jon Douglas, Joel Verhagen, and Jon Galloway, this article demonstrates how to build, customize, and publish your first Model Context Protocol (MCP) server using .NET 10, enabling discoverable AI integrations via NuGet.
Building Your First MCP Server with .NET and Publishing to NuGet
Authors: Jon Douglas, Joel Verhagen, Jon Galloway
Want to extend AI assistants with custom capabilities? In this post, you’ll learn how to build a Model Context Protocol (MCP) server using .NET 10 and publish it to NuGet, making your AI tools discoverable and reusable by the entire .NET community. Additionally, the article covers new .NET 10 and NuGet features supporting MCP server development and introduces a new project template for easier setup.
What is the Model Context Protocol (MCP)?
The Model Context Protocol (MCP) is an open standard enabling AI assistants to securely connect to external data sources and tools. MCP serves as a bridge between AI models and real-world systems—allowing assistants to access databases, APIs, file systems, and custom business logic. .NET 10’s new MCP templates empower developers to create and publish these servers to NuGet.
NuGet and MCP
NuGet.org now supports hosting and consuming MCP servers built with the ModelContextProtocol C# SDK. Key benefits:
- Discoverability: MCP servers are searchable via NuGet
- Versioning: Semantic versioning and dependency management
- Simple Installation: Easy configuration for VS Code and Visual Studio
- Community Growth: Expand the ecosystem of .NET AI tools
Search for MCP servers: NuGet MCP Server Packages
Creating an MCP Server: Step by Step
Prerequisites
Step 1: Install MCP Server Project Template
dotnet new install Microsoft.Extensions.AI.Templates
Step 2: Create the Project
dotnet new mcpserver -n SampleMcpServer
cd SampleMcpServer
dotnet build
Enhancing the MCP Server
The default template gives you a working MCP server with a sample get_random_number
tool. To add a custom weather tool:
Create WeatherTools.cs
in the Tools
directory:
[McpServerTool]
[Description("Describes random weather in the provided city.")]
public string GetCityWeather(
[Description("Name of the city to return weather for")] string city)
{
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]}.";
}
Update Program.cs
to include your tool:
.WithTools<WeatherTools>()
Testing with GitHub Copilot
Configure GitHub Copilot by creating .vscode/mcp.json
:
{
"servers": {
"SampleMcpServer": {
"type": "stdio",
"command": "dotnet",
"args": [ "run", "--project", "." ],
"env": { "WEATHER_CHOICES": "sunny,humid,freezing,perfect" }
}
}
}
Test with prompts like:
- “What’s the weather in Seattle?”
- “Give me a random number between 1 and 100”
VS Code will reflect available MCP server tools in Copilot.
Preparing for NuGet Publication
Update .mcp/server.json
:
{
"description": "A sample MCP server with weather and random number tools",
"name": "io.github.yourusername/SampleMcpServer",
"packages": [
{
"registry_name": "nuget",
"name": "YourUsername.SampleMcpServer",
"version": "1.0.0",
"package_arguments": [],
"environment_variables": [
{
"name": "WEATHER_CHOICES",
"description": "Comma separated list of weather descriptions",
"is_required": true,
"is_secret": false
}
]
}
],
"repository": {
"url": "https://github.com/yourusername/SampleMcpServer",
"source": "github"
},
"version_detail": {
"version": "1.0.0"
}
}
Update your .csproj
:
<PackageId>YourUsername.SampleMcpServer</PackageId>
Publishing Your Project to NuGet
-
Pack Your Project:
dotnet pack -c Release
-
Publish to NuGet:
dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json
Tip: For testing, use int.nugettest.org before publishing live.
Discoverability and Usage
Once published, your MCP server will show up under NuGet MCP Server Packages.
- Explore Package: View docs and copy configuration
- Install and Configure: Add settings to
.vscode/mcp.json
Example configuration for VS Code:
{
"inputs": [
{
"type": "promptString",
"id": "weather-choices",
"description": "Comma separated list of weather descriptions",
"password": false
}
],
"servers": {
"YourUsername.SampleMcpServer": {
"type": "stdio",
"command": "dnx",
"args": [ "YourUsername.SampleMcpServer", "--version", "1.0.0", "--yes" ],
"env": { "WEATHER_CHOICES": "${input:weather-choices}" }
}
}
}
VS Code will prompt for these environment settings the first time a user interacts.
Future Directions
The .NET + MCP + NuGet integration paves the way for extensible AI systems. Potential use cases include:
- Enterprise database gateways (e.g., SQL Server)
- Cloud API orchestrators (Azure, AWS)
- Document intelligence with OCR
- DevOps pipelines and workflow tools
- Data analytics and reporting engines
Learn More and Resources
- Get started with .NET AI and the Model Context Protocol
- Model Context Protocol .NET samples
- NuGet.org MCP Server Search
- MCP Registry Documentation
- What’s new in .NET 10
.NET + MCP + NuGet = The future of extensible AI
Happy building, and welcome to the growing community of MCP server creators!
This post appeared first on “Microsoft .NET Blog”. Read the entire article here