Automatically create an MCP (Model Context Protocol) Server from your ASP.NET Core APIs.
AutoMCP bridges the gap between your ASP.NET Core APIs and AI-powered tools by automatically exposing your API endpoints as MCP tools. With just a few lines of code, your existing API controllers become accessible to AI assistants and other MCP-compatible clients.
Install the main package:
dotnet add package AutoMcpFor OData support, also install:
dotnet add package AutoMcp.ODataAdd AutoMCP to your ASP.NET Core application in Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Add MCP server with automatic API endpoint discovery
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithOData() // Optional: Add OData query support. Must be called before WithAutoMcp()
.WithAutoMcp(); // Automatically expose API endpoints as MCP tools
var app = builder.Build();
app.UseAuthorization();
app.MapControllers();
app.MapMcp("mcp"); // Map MCP endpoint at /mcp
app.Run();The repository includes a complete working example with .NET Aspire support. To run it:
# Clone the repository
git clone https://github.com/wertzui/AutoMCP
cd AutoMCP/src/AutoMcp
# Run the Aspire app host
dotnet run --project AutoMcp.AppHostThe Aspire dashboard will open, showing the running example API with AutoMCP configured. And the MCP insprector will be pointed to the correct endpoint.
The WithAutoMcp() method is the heart of AutoMCP. It automatically discovers all API endpoints in your ASP.NET Core application and generates corresponding MCP tools.
- Automatic Discovery: Scans all controller actions and creates MCP tools for each endpoint
- Schema Generation: Automatically generates JSON schemas for request parameters and response types
- Metadata Extraction: Uses endpoint metadata like
[Description]or[ProducesResponseType]attributes to provide context to AI clients - Action Results: Properly unwraps ASP.NET Core
ActionResult<T>types
builder.Services.AddMcpServer()
.WithAutoMcp(
serializerOptions: customJsonOptions, // Optional: Custom JSON serialization options
apiDescriptionGroupCollectionProvider: provider // Optional: Custom API description provider
);[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet(Name = "GetWeatherForecast")]
[Description("Get the weather forecast for the given date.")]
public ActionResult<WeatherForecast> Get(DateOnly date)
{
var forecast = new WeatherForecast
{
Date = date,
TemperatureC = Random.Shared.Next(-20, 55),
Summary = "Sunny"
};
return Ok(forecast);
}
}This controller action automatically becomes an MCP tool named WeatherForecast_Get with:
- The description from the
[Description]attribute - Proper parameter schema for the
dateparameter - Response schema based on the
WeatherForecasttype
The WithOData() method adds support for OData query options, enabling powerful filtering, sorting, and pagination capabilities in your MCP tools.
- Query Support: Enables
$filter,$orderby,$top,$skip,$select, and other OData query options - JSON Serialization: Adds custom JSON converters to properly serialize and deserialize
ODataQueryOptions<T> - Schema Integration: Integrates OData query schemas into MCP tool definitions
- Type Preservation: Maintains type information across the MCP boundary
builder.Services.AddMcpServer()
.WithAutoMcp()
.WithOData(); // Enable OData query support[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly IQueryable<WeatherForecast> _repository =
GetWeatherForecasts().AsQueryable();
[HttpGet("multiple", Name = "GetMultipleWeatherForecasts")]
[Description("Get multiple weather forecasts with filtering and sorting options.")]
public ActionResult<IEnumerable<WeatherForecast>> GetMultiple(
ODataQueryOptions<WeatherForecast> options)
{
return Ok(options.ApplyTo(_repository));
}
}With WithOData(), AI clients can now query your API with powerful filters:
$filter=temperatureC gt 20- Get forecasts warmer than 20Β°C$orderby=date desc- Sort by date descending$top=10- Get only the first 10 results$skip=5&$top=10- Pagination support
- β Discovers all API endpoints automatically
- β Generates proper JSON schemas for parameters and responses
- β Handles complex types and nested objects
- β Supports async and sync controller actions
- β Full OData query syntax support
- β Filtering, sorting, pagination
- β Complex query expressions
- β Works with standard ASP.NET Core controllers
- β Respects routing and HTTP method constraints
- β Integrates with ASP.NET Core dependency injection
- β Maintains HttpContext throughout the request pipeline
- β Minimal configuration required
- β Uses existing API metadata and attributes
- β Comprehensive error handling and logging
- Discovery Phase:
WithAutoMcp()scans your application's API description to find all endpoints - Schema Generation: For each endpoint, it generates:
- Input parameter schemas using reflection and JSON schema generation
- Output schemas based on return types and
[ProducesResponseType]attributes
- Tool Registration: Each endpoint becomes an MCP tool with a name like
{ControllerName}_{ActionName} - Invocation: When an AI client calls the tool:
- Parameters are deserialized using the configured JSON options
- A simulated HTTP request is created with proper routing
- The controller action is invoked through ASP.NET Core's pipeline
- Results are serialized and returned to the client
var customJsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
builder.Services.AddMcpServer()
.WithAutoMcp(serializerOptions: customJsonOptions);Query large datasets with OData filtering and pagination.
Retrieve and filter content with complex query expressions.
Enable AI assistants to query business data with natural language converted to OData queries.
- .NET 9.0 or later
- ASP.NET Core
This project is released into the public domain under The Unlicense. See LICENSE for details.
Contributions are welcome! Please feel free to submit issues and pull requests.
- GitHub Issues: Report bugs or request features
- Discussions: Ask questions and share ideas