Unleashing the Power of MCP with .NET and Azure - A Fun and Practical Guide

Share on:

If you're a .NET dev who's ever built or consumed a REST API, buckle up—because you're in for a sit-down with a new, powerful protocol that's going to shake your cloud integration universe. Introducing MCP: Model Context Protocol. It's Microsoft's latest innovation for real-time, strongly typed service-to-service communication, and it's arriving to make your development life easier, quicker, and an awful lot of fun.

Wait, What is MCP?

MCP = Model Context Protocol, a new alternative to REST and gRPC. Contrary to stateless operations over HTTP upon which REST APIs are based and gRPC's reliance on Protobufs for light-weight remote procedure calls, MCP takes a different approach. It is about models (your data structures) and contexts (state and operations that they belong to), which stay in sync with one another across client and server.

Think of MCP as being a smart, reactive layer that is familiar with your app's domain models and has them in sync. Unlike REST, where you would have to do marshaling and unmarshaling of data yourself, MCP lets you code against models as if they were in-memory objects. And yes, it includes rich features like subscriptions and auto updates!

Why Should You Care?

Because it's awesome. But also:

  • Real-time updates with no polling.
  • Strongly typed C# models.
  • Fewer HTTP headaches.
  • Easy integration with .NET and Azure.

Alright, let’s get to the fun part—how to actually use this thing.


Step 1: Set Up Your MCP Server in .NET

First things first: let's spin up an MCP server in your .NET project.

  1. Create a new ASP.NET Core project:

    dotnet new webapi -n MyMcpServer
    cd MyMcpServer
    
  2. Add the Azure.Mcp.Server NuGet package:

    dotnet add package Azure.Mcp.Server --prerelease
    
  3. Define your model (e.g., a simple WeatherForecast):

    public class WeatherForecast
    {
       public DateTime Date { get; set; }
       public int TemperatureC { get; set; }
       public string? Summary { get; set; }
    }
    
  4. Create a context class:

    public class WeatherContext : ModelContext<WeatherForecast>
    {
       public override IQueryable<WeatherForecast> Query()
       {
          return new List<WeatherForecast>
          {
                new WeatherForecast { Date = DateTime.Now, TemperatureC = 25, Summary = "Sunny" },
                new WeatherForecast { Date = DateTime.Now.AddDays(1), TemperatureC = 18, Summary = "Cloudy" }
          }.AsQueryable();
       }
    }
    
  5. Register the context in Startup.cs (or Program.cs in minimal API):

    builder.Services.AddMcpServer(options =>
    {
       options.RegisterContext<WeatherContext>();
    });
    
  6. Add MCP middleware:

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
       endpoints.MapMcp();
    });
    

Boom! You now have an MCP server running.


Step 2: Hook It Up to Azure

MCP is built for Azure. To make it production-ready, use Azure API Management (APIM) as your secure gateway.

Why use APIM?

  • Authentication and authorization out of the box.
  • Centralized management of all your APIs (including MCP).
  • Developer portal, analytics, and rate-limiting.

Here's how to do it:

  1. Deploy your MCP server to Azure App Service or Azure Container Apps.

  2. Create an API in APIM:

    • In Azure Portal, go to your APIM instance.
    • Add a new API: HTTP, WebSocket, or Custom.
    • Set the backend URL to your MCP server.
  3. Secure it with Azure AD:

    • Protect your MCP API with OAuth2.
    • Configure client credentials or use user delegation flows.
  4. Test the connection using an MCP client or simple WebSocket tool.

Check out the Azure API Management as Auth Gateway for MCP blog for detailed instructions.


Final Thoughts

MCP is like having an always-up-to-date, super-smart object model on both client and server. It cuts through the boilerplate and lets you focus on your app's logic. If you're working with .NET and Azure, this is a protocol worth diving into.

Go ahead and play with it—build a real-time dashboard, sync app settings across devices, or whatever your app dreams up. MCP + .NET + Azure = serious magic.

Links to Explore Further: