DEV Community

Cover image for Build a .NET MCP Server in 10 Minutes – Hands‑On Guide
Vikrant Bagal
Vikrant Bagal

Posted on

Build a .NET MCP Server in 10 Minutes – Hands‑On Guide

Build a .NET MCP Server in 10 Minutes – Hands‑On Guide

Your .NET API can now talk to AI agents — here's how to expose it in under 10 minutes.

Create the Project and Choose Transport

dotnet new mcpserver -n MyMcpServer
cd MyMcpServer
Enter fullscreen mode Exit fullscreen mode

The dotnet new mcpserver template (from Microsoft.McpServer.ProjectTemplates) scaffolds a complete MCP server project. You get two transport options out of the box:

  • ** stdio ** – ideal for local tools, single‑user scenarios like GitHub Copilot.
  • ** ASP.NET Core HTTP ** – suited for multi‑user, production deployments.

Pick the one that matches your deployment topology. For a quick demo, run the server with dotnet run --project MyMcpServer.csproj. The template automatically configures the appropriate transport based on the MCP_TRANSPORT environment variable; set it to Stdio or Http to switch.

Standalone takeaway: The template removes boilerplate, letting you focus on business logic while the SDK handles protocol details.

Define Tools with Attributes

using Microsoft.Mcp.Server;
using Microsoft.Mcp.Server.Abstractions;

public class WeatherTools
{
    [McpServerTool]
    public async Task<WeatherResponse> GetWeatherAsync(string city)
    {
        // Simulate a call to a weather API
        var response = new WeatherResponse
        {
            City = city,
            TemperatureC = 22,
            Summary = "Sunny"
        };
        return response;
    }
}
Enter fullscreen mode Exit fullscreen mode

Attributes like [McpServerTool], [McpServerResource], and [McpServerFunction] turn ordinary C# methods into discoverable MCP tools. The SDK scans the assembly at runtime, registers the method, and handles serialization, parameter validation, and error mapping. You only need to annotate the method; the runtime wires it to the protocol automatically.

Standalone takeaway: By decorating methods with attributes, you expose them as AI‑actionable endpoints without writing any transport‑specific code.

Configure Transport and Publish

dotnet publish -c Release -o ./publish
Enter fullscreen mode Exit fullscreen mode

After building, publish the binary. For stdio, the published executable can be launched directly by an AI agent via a .mcp.json configuration file in VS Code. For HTTP, add a simple ASP.NET Core endpoint:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMcpServer<WeatherTools>();
var app = builder.Build();
app.MapMcpEndpoint(); // exposes /mcp endpoint
app.Run();
Enter fullscreen mode Exit fullscreen mode

Publish the HTTP server to any cloud host, then register the endpoint URL in the agent’s configuration. Publishing to NuGet.org makes the server consumable by any .NET‑based AI tooling, and the MCP Registry automatically indexes it for discovery.

Standalone takeaway: Choose stdio for local dev, HTTP for production, and publish to NuGet to make your server reusable across AI agents.

Conclusion

Start by scaffolding a stdio MCP server from your existing API today — you’ll have a functional AI‑exposable service in under ten minutes.

Top comments (0)