An ASP.NET extension that generates a Model Context Protocol (MCP) server, enabling seamless conversion of existing REST APIs as MCP tools, now with enhanced authorization support and improved schema descriptions.
Install the MCPfiedSwagger NuGet package from your preferred NuGet source:
dotnet add package MCPfiedSwaggerOr via the NuGet Package Manager:
PM> Install-Package MCPfiedSwagger
- Add the MCPfiedSwagger service in your ASP.NET Core project. In your
Program.cs, register the service after adding controllers and Swagger:
builder.Services.AddMCPfiedSwagger();- Enable the middleware in your HTTP pipeline, after mapping controllers:
app.UseMCPfiedSwagger();- Configure Swagger (optional):
By default, the Swagger/OpenAPI document is named
"v1". To specify a different document name, pass it as an argument:
app.UseMCPfiedSwagger("v1");- Specify the MCP endpoint (optional):
The default MCP endpoint is
/mcp. To change it, provide the desired path:
app.UseMCPfiedSwagger("v1", "/custom-mcp-endpoint");Authorization support is now integrated. You can enable stateless authorization easily:
builder.Services.AddMCPfiedSwagger()
.AddAuthorization();The authorization process validates controller actions based on Authorize attributes and handles unauthorized requests gracefully.
A minimal Program.cs setup:
using MCPfiedSwagger.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMCPfiedSwagger();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
// Enable MCP endpoint at /mcp for the "v1" Swagger document
app.UseMCPfiedSwagger();
app.Run();- Restore dependencies and build:
dotnet restore
dotnet build- Run the authentication example project:
dotnet run --project MCPfiedSwagger.Authenticated-
Access the Swagger UI: Open 🌐
http://localhost:<port>/swaggerin your browser. -
Test Authorization:
- Authenticate via
POST /api/Auth/loginwith usernametestand passwordpassword. - Use the received JWT token as your authorization header when connecting to your MCP endpoint.
- Authenticate via
Run the original example project:
dotnet run --project MCPfiedSwagger.Example- Swagger UI: 🌐
http://localhost:<port>/swagger - MCP Endpoint: 🔗
http://localhost:<port>/mcp
- The NuGet package depends on
ModelContextProtocol.AspNetCoreandSwashbuckle.AspNetCore. - Ensure your project targets
.NET 9.0or compatible. - For more details, see the project repository.
- Streamable HTTP MCP does not work on
httpsfor some reason. Still trying to investigate the issue I encountered.