{"id":2250,"date":"2025-07-15T20:19:15","date_gmt":"2025-07-15T20:19:15","guid":{"rendered":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/07\/15\/building-your-first-mcp-server-with-net-and-publishing-to-nuget\/"},"modified":"2025-07-15T20:19:15","modified_gmt":"2025-07-15T20:19:15","slug":"building-your-first-mcp-server-with-net-and-publishing-to-nuget","status":"publish","type":"post","link":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/07\/15\/building-your-first-mcp-server-with-net-and-publishing-to-nuget\/","title":{"rendered":"Building Your First MCP Server with .NET and Publishing to NuGet"},"content":{"rendered":"<p>Want to extend AI assistants with custom capabilities? In this post, we\u2019ll show you how to build a Model Context Protocol (MCP) server using .NET 10 and publish it to NuGet \u2014 making your AI tools discoverable and reusable by the entire .NET community. We\u2019ll also show you some new features we\u2019ve added to .NET 10 and NuGet to support this, and a new MCP Server project template that makes it easier to get started!<\/p>\n<h2>Building MCP Servers with .NET 10<\/h2>\n<h3> Intro: What\u2019s the Model Context Protocol?<\/h3>\n<p>The <strong>Model Context Protocol (MCP)<\/strong> is an open standard that enables AI assistants to securely connect to external data sources and tools. Think of it as a bridge between AI models and the real world \u2014 letting assistants access databases, APIs, file systems, and custom business logic.<\/p>\n<p>With .NET 10 and the new MCP templates, you can create powerful servers that extend AI capabilities \u2014 and now publish them to <strong>NuGet<\/strong> for the entire .NET community to discover and use!<\/p>\n<h3> NuGet: .NET MCP Servers Available on NuGet<\/h3>\n<p>Here\u2019s the exciting part: <strong>NuGet.org now supports hosting and consuming MCP servers built with the <a href=\"https:\/\/www.nuget.org\/packages\/ModelContextProtocol\">ModelContextProtocol<\/a> C# SDK<\/strong>. This means:<\/p>\n<p><strong>Discoverability<\/strong>: Developers can find your MCP servers through NuGet search<br \/>\n<strong>Versioning<\/strong>: Proper semantic versioning and dependency management<br \/>\n<strong>Easy Installation<\/strong>: Copy VS Code and Visual Studio MCP configuration<br \/>\n<strong>Community<\/strong>: Join a growing ecosystem of .NET AI tools<\/p>\n<p>Search for MCP servers on NuGet.org using the <a href=\"https:\/\/www.nuget.org\/packages?packagetype=McpServer\">MCP Server package type filter<\/a>, and you\u2019ll see what the community is building!<\/p>\n<h2> Creating Your First MCP Server<\/h2>\n<p>Let\u2019s build a simple MCP server that provides weather information and random numbers. You\u2019ll see how easy it is to get started with the new .NET 10 MCP templates.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Before we start, make sure you have:<\/p>\n<p><a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet\/10.0\">.NET 10.0 SDK<\/a> (preview 6 or higher)<br \/>\n<a href=\"https:\/\/code.visualstudio.com\/\">Visual Studio Code<\/a><br \/>\n<a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=GitHub.copilot\">GitHub Copilot extension<\/a><br \/>\n<a href=\"https:\/\/www.nuget.org\/users\/account\/LogOn\">NuGet.org account<\/a><\/p>\n<h3>Step 1: Install the MCP Template<\/h3>\n<p>First, install the MCP Server template (version 9.7.0-preview.2.25356.2 or newer):<\/p>\n<p>dotnet new install Microsoft.Extensions.AI.Templates<\/p>\n<h3>Step 2: Create Your MCP Server Project<\/h3>\n<p>Create a new MCP server with the template:<\/p>\n<p>dotnet new mcpserver -n SampleMcpServer<br \/>\ncd SampleMcpServer<br \/>\ndotnet build<\/p>\n<p>The template gives you a working MCP server with a sample get_random_number tool. But let\u2019s make it more interesting!<\/p>\n<h2> Adding Custom Tools and Configuration<\/h2>\n<p>Let\u2019s enhance our MCP server with a weather tool that uses environment variables for configuration. Add a new WeatherTools.cs class to the Tools directory with the following method:<\/p>\n<p>[McpServerTool]<br \/>\n[Description(&#8220;Describes random weather in the provided city.&#8221;)]<br \/>\npublic string GetCityWeather(<br \/>\n    [Description(&#8220;Name of the city to return weather for&#8221;)] string city)<br \/>\n{<br \/>\n    \/\/ Read the environment variable during tool execution.<br \/>\n    \/\/ Alternatively, this could be read during startup and passed via IOptions dependency injection<br \/>\n    var weather = Environment.GetEnvironmentVariable(&#8220;WEATHER_CHOICES&#8221;);<br \/>\n    if (string.IsNullOrWhiteSpace(weather))<br \/>\n    {<br \/>\n        weather = &#8220;balmy,rainy,stormy&#8221;;<br \/>\n    }<\/p>\n<p>    var weatherChoices = weather.Split(&#8220;,&#8221;);<br \/>\n    var selectedWeatherIndex =  Random.Shared.Next(0, weatherChoices.Length);<\/p>\n<p>    return $&#8221;The weather in {city} is {weatherChoices[selectedWeatherIndex]}.&#8221;;<br \/>\n}<\/p>\n<p>Next, update your <em>Program.cs<\/em> to include .WithTools&lt;WeatherTools&gt;() after the previous WithTools call.<\/p>\n<p>This tool demonstrates how to:<\/p>\n<p>Accept parameters from AI assistants<br \/>\nUse environment variables for configuration<br \/>\nReturn meaningful responses<\/p>\n<h2> Testing Your MCP Server<\/h2>\n<p>Configure GitHub Copilot to use your MCP server by creating .vscode\/mcp.json:<\/p>\n<p>{<br \/>\n  &#8220;servers&#8221;: {<br \/>\n    &#8220;SampleMcpServer&#8221;: {<br \/>\n      &#8220;type&#8221;: &#8220;stdio&#8221;,<br \/>\n      &#8220;command&#8221;: &#8220;dotnet&#8221;,<br \/>\n      &#8220;args&#8221;: [<br \/>\n        &#8220;run&#8221;,<br \/>\n        &#8220;&#8211;project&#8221;,<br \/>\n        &#8220;.&#8221;<br \/>\n      ],<br \/>\n      &#8220;env&#8221;: {<br \/>\n        &#8220;WEATHER_CHOICES&#8221;: &#8220;sunny,humid,freezing,perfect&#8221;<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>Now test it in GitHub Copilot with prompts like:<\/p>\n<p>\u201cWhat\u2019s the weather in Seattle?\u201d<br \/>\n\u201cGive me a random number between 1 and 100\u201d<\/p>\n<h2> Configuring for NuGet Publication<\/h2>\n<p>Update your <a href=\"https:\/\/github.com\/modelcontextprotocol\/registry\/blob\/main\/docs\/server-json\/README.md\">.mcp\/server.json<\/a> file to declare inputs and metadata:<\/p>\n<p>{<br \/>\n  &#8220;description&#8221;: &#8220;A sample MCP server with weather and random number tools&#8221;,<br \/>\n  &#8220;name&#8221;: &#8220;io.github.yourusername\/SampleMcpServer&#8221;,<br \/>\n  &#8220;packages&#8221;: [<br \/>\n    {<br \/>\n      &#8220;registry_name&#8221;: &#8220;nuget&#8221;,<br \/>\n      &#8220;name&#8221;: &#8220;YourUsername.SampleMcpServer&#8221;,<br \/>\n      &#8220;version&#8221;: &#8220;1.0.0&#8221;,<br \/>\n      &#8220;package_arguments&#8221;: [],<br \/>\n      &#8220;environment_variables&#8221;: [<br \/>\n        {<br \/>\n          &#8220;name&#8221;: &#8220;WEATHER_CHOICES&#8221;,<br \/>\n          &#8220;description&#8221;: &#8220;Comma separated list of weather descriptions&#8221;,<br \/>\n          &#8220;is_required&#8221;: true,<br \/>\n          &#8220;is_secret&#8221;: false<br \/>\n        }<br \/>\n      ]<br \/>\n    }<br \/>\n  ],<br \/>\n  &#8220;repository&#8221;: {<br \/>\n    &#8220;url&#8221;: &#8220;https:\/\/github.com\/yourusername\/SampleMcpServer&#8221;,<br \/>\n    &#8220;source&#8221;: &#8220;github&#8221;<br \/>\n  },<br \/>\n  &#8220;version_detail&#8221;: {<br \/>\n    &#8220;version&#8221;: &#8220;1.0.0&#8221;<br \/>\n  }<br \/>\n}<\/p>\n<p>Also update your .csproj file with a unique &lt;PackageId&gt;:<\/p>\n<p>&lt;PackageId&gt;YourUsername.SampleMcpServer&lt;\/PackageId&gt;<\/p>\n<h2> Publishing to NuGet<\/h2>\n<p>Now for the exciting part \u2014 publishing to NuGet!<\/p>\n<h3>Step 1: Pack Your Project<\/h3>\n<p>dotnet pack -c Release<\/p>\n<h3>Step 2: Publish to NuGet<\/h3>\n<p>dotnet nuget push bin\/Release\/*.nupkg &#8211;api-key &lt;your-api-key&gt; &#8211;source https:\/\/api.nuget.org\/v3\/index.json<\/p>\n<p> <strong>Tip<\/strong>: Want to test first? Use the NuGet test environment at <a href=\"https:\/\/int.nugettest.org\/\">int.nugettest.org<\/a> before publishing to production.<\/p>\n<h2> Discovering and Using MCP Servers<\/h2>\n<p>Once published, your MCP server becomes discoverable on NuGet.org:<\/p>\n<p><strong>Search<\/strong>: Visit <a href=\"https:\/\/www.nuget.org\/packages?packagetype=mcpserver\">NuGet.org<\/a> and filter by mcpserver package type<br \/>\n<strong>Explore<\/strong>: View package details and copy the configuration from the \u201cMCP Server\u201d tab<br \/>\n<strong>Install<\/strong>: Add the configuration to your .vscode\/mcp.json file<\/p>\n<p>The generated configuration looks like this:<\/p>\n<p>{<br \/>\n  &#8220;inputs&#8221;: [<br \/>\n    {<br \/>\n      &#8220;type&#8221;: &#8220;promptString&#8221;,<br \/>\n      &#8220;id&#8221;: &#8220;weather-choices&#8221;,<br \/>\n      &#8220;description&#8221;: &#8220;Comma separated list of weather descriptions&#8221;,<br \/>\n      &#8220;password&#8221;: false<br \/>\n    }<br \/>\n  ],<br \/>\n  &#8220;servers&#8221;: {<br \/>\n    &#8220;YourUsername.SampleMcpServer&#8221;: {<br \/>\n      &#8220;type&#8221;: &#8220;stdio&#8221;,<br \/>\n      &#8220;command&#8221;: &#8220;dnx&#8221;,<br \/>\n      &#8220;args&#8221;: [<br \/>\n        &#8220;YourUsername.SampleMcpServer&#8221;,<br \/>\n        &#8220;&#8211;version&#8221;,<br \/>\n        &#8220;1.0.0&#8221;,<br \/>\n        &#8220;&#8211;yes&#8221;<br \/>\n      ],<br \/>\n      &#8220;env&#8221;: {<br \/>\n        &#8220;WEATHER_CHOICES&#8221;: &#8220;${input:weather-choices}&#8221;<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>VS Code will prompt for input values when you first use the MCP server, making configuration seamless for users.<\/p>\n<h2> What\u2019s Next?<\/h2>\n<p>With .NET 10 and NuGet as the official support for .NET MCP you\u2019re now part of a growing ecosystem that\u2019s transforming how AI assistants interact with the world. The combination of .NET\u2019s robust libraries and NuGet\u2019s package management creates endless possibilities for AI extensibility.<\/p>\n<p>This is our first release of the .NET MCP Server project template, and we\u2019ve started with a very simple scenario. We\u2019d love to hear what you\u2019re building, and what you\u2019d like to see in future releases of the template. Let us know at <a href=\"https:\/\/aka.ms\/dotnet-mcp-template-survey\">https:\/\/aka.ms\/dotnet-mcp-template-survey<\/a>.<\/p>\n<h3>Real-World MCP Server Ideas<\/h3>\n<p>Here are some powerful MCP servers you could build next:<\/p>\n<p><strong>Enterprise Database Gateway<\/strong>: Safely expose SQL Server, PostgreSQL, or MongoDB queries with role-based access<br \/>\n<strong>Cloud API Orchestrator<\/strong>: Wrap Azure, AWS, or Google Cloud services for AI-driven infrastructure management<br \/>\n<strong>Document Intelligence Hub<\/strong>: Process PDFs, Word docs, and spreadsheets with OCR and content extraction<br \/>\n<strong>DevOps Command Center<\/strong>: Automate Git operations, CI\/CD pipelines, and deployment workflows<br \/>\n<strong>Data Analytics Engine<\/strong>: Transform CSV files, generate reports, and create visualizations on demand<\/p>\n<p>Each of these represents a unique opportunity to bridge AI capabilities with real business needs \u2014 and share your solutions with the entire .NET community through NuGet.<\/p>\n<p>To go further:<\/p>\n<p> <strong>Learn More<\/strong>: Explore the <a href=\"https:\/\/github.com\/microsoft\/mcp-dotnet-samples\">Model Context Protocol .NET samples<\/a><br \/>\n <strong>Watch &amp; Code<\/strong>: Tune in to the <a href=\"https:\/\/dotnet.microsoft.com\/live\/community-standup\">AI &amp; .NET Community StandUp<\/a><br \/>\n <strong>Get Started<\/strong>: Check out the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/ai\/quickstarts\/build-mcp-server\">MCP documentation<\/a><\/p>\n<p>.NET + MCP + NuGet = The future of extensible AI <\/p>\n<p>Happy building, and welcome to the growing community of MCP server creators!<\/p>\n<h3> Resources<\/h3>\n<p><a href=\"https:\/\/learn.microsoft.com\/dotnet\/ai\/get-started-mcp\">Get started with .NET AI and the Model Context Protocol<\/a><br \/>\n<a href=\"https:\/\/github.com\/microsoft\/mcp-dotnet-samples\">Model Context Protocol .NET samples<\/a><br \/>\n<a href=\"https:\/\/www.nuget.org\/packages?packagetype=mcpserver\">NuGet.org MCP Server Search<\/a><br \/>\n<a href=\"https:\/\/github.com\/modelcontextprotocol\/registry\">MCP Registry Documentation<\/a><br \/>\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/whats-new\/dotnet-10\/overview\">What\u2019s new in .NET 10<\/a><\/p>\n<p>The post <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/mcp-server-dotnet-nuget-quickstart\/\">Building Your First MCP Server with .NET and Publishing to NuGet<\/a> appeared first on <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\">.NET Blog<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Want to extend AI assistants with custom capabilities? In this post, we\u2019ll show you how to build a Model Context [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[],"class_list":["post-2250","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/2250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/comments?post=2250"}],"version-history":[{"count":0,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/2250\/revisions"}],"wp:attachment":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/media?parent=2250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/categories?post=2250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/tags?post=2250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}