Exposing ASP.NET Core APIs as MCP Servers with HTTP Transport

Introduction:

In my earlier post, I explained how to build your first MCP server using .NET and connect it to VS Code. Now, let’s take it a step further by converting an ASP.NET Core Web API into an MCP server that communicates via HTTP transport. This approach is especially useful when you want to expose existing APIs to AI-powered developer tools, such as GitHub Copilot Chat in VS Code. By the end of this article, you’ll have an ASP.NET Core API running as an MCP server that Copilot Chat can call directly

Prerequisites

  • .NET 8 SDK
  • Visual Studio Code
  • GitHub Copilot Chat extension
  • Basic knowledge of ASP.NET Core Web API

Step 1 – Start with an Existing ASP.NET Core API

Let’s assume you already have a Web API project. I have taken a typical Weather forecast API project.

Run the API and check that it works

Step 2 – Add MCP Server Support

Install the MCP NuGet package:

dotnet add package ModelContextProtocol.AspNetCore 

Next, update your Program.cs to register MCP services with HTTP transport:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMcpServer()
    .WithHttpTransport()
    .WithToolsFromAssembly();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapGet("/", () => "MCP server is running!");
app.MapMcp();
app.MapControllers();
app.Run();
  • AddMcpServer() initializes MCP support.
  • WithHttpServerTransport() exposes the MCP server over an HTTP endpoint (/mcp).
  • WithToolsFromAssembly() automatically registers your MCP tools.
  • app.MapMcp() –
    • It registers the MCP server endpoint into your ASP.NET Core app.
    • By default, it listens on the path you configured in .WithHttpServerTransport()
    • This ensures MCP clients (like GitHub Copilot Chat or Claude for Windows) can send JSON-RPC requests to your API.

Step 3 – Create an MCP Tool from Your API

Let’s expose an existing API method as a tool. For example, create a tool that wraps the

WeatherForecast API:

[ApiController]
[Route("[controller]")]
[McpServerToolType]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild",
        "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private static readonly List<WeatherForecast> Forecasts = new();

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;

        // Pre-populate data if empty
        if (!Forecasts.Any())
        {
            Forecasts.AddRange(Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            }));
        }
    }

    [HttpGet(Name = "GetWeatherForecastAPI")]
    [McpServerTool(Name = "GetWeatherForecastAPI"), Description("Returns the next few days weather forcast.")]
    public ActionResult<IEnumerable<WeatherForecast>> Get()
    {
      
        return Forecasts.ToArray();
    }
}

Decorate the class with [McpServerToolType] and the action with [McpServerTool] to expose the action as a MCP tool

Step 4 – Configure VS Code with GitHub Copilot Chat

  1. Open your mcp.json config file.
    HTTP Transport
  2. Add the MCP server configuration:
{
  "servers": {
    "mcp-api-server": {
      "url": "http://localhost:5154",
      "type": "http"
    }
  }
}

Make sure you use only http

Step 5 – Test with GitHub Copilot Chat

Now open Copilot Chat in VS Code and type:

Get the weather forecast

VS Code Copilot chat prompt

Copilot will invoke your MCP server, which calls your ASP.NET Core API and returns the weather forecast directly into the chat window.

Summary

In this article, we have seen how to convert an ASP.NET Core Web API into an MCP server using HTTP transport, making it accessible to GitHub Copilot Chat in VS Code.

This approach allows you to:

  • Modernize existing APIs without rewriting them.
  • Make your APIs directly usable by AI-powered developer assistants.
  • Lay the foundation for exposing more complex business logic as MCP tools.

In my next article of this series, we’ll explore the security of MCP servers and the handling of authentication/authorization for API-backed tools in more detail.

Gowtham K

Gowtham K has been awarded as MVP(Most Valuable Professional) for 9 times by Microsoft for his exceptional contribution in Microsoft technologies under the category “Developer Technologies & Security” . He has more than 12 years of experience on Microsoft technologies such as C#, ASP.NET MVC, ASP.NET WEB API, ASP.NET Core, MS SQL Server, Azure, Microsoft Entra ID, Azure AD B2C and other technologies such as JavaScript, jQuery, HTML and CSS .He is also a blogger and author of articles on various technologies. He is also a speaker and delivered talk on various technologies like ASP.NET MVC, Azure and Azure DevOps in the public events.

Leave a Reply

Your email address will not be published. Required fields are marked *