Aspire resources can expose their own MCP (Model Context Protocol) servers, enabling AI coding agents to interact directly with databases, APIs, and other services. For example, a PostgreSQL resource can expose SQL query tools, allowing an agent to run queries without leaving the conversation.
When a resource is annotated with WithMcpServerwithMcpServerWithMcpServer in the AppHost, Aspire discovers the MCP endpoint and makes its tools available in two ways:
Through the Aspire MCP server — resource tools are automatically proxied alongside the built-in Aspire MCP server tools. AI agents see them in their tool list without any extra configuration.
Through the CLI — use aspire mcp tools and aspire mcp call to discover and invoke resource tools directly from the terminal.
var builder =DistributedApplication.CreateBuilder(args);
// Add a custom MCP server container
var myMcpServer =builder.AddContainer("my-mcp","myregistry/my-mcp-server")
.WithHttpEndpoint(targetPort:8080)
.WithMcpServer("/mcp");
builder.Build().Run();
WithMcpServer declares an MCP server that already exists on the resource. It adds metadata that Aspire tooling uses for discovery and proxying; it doesn’t create an endpoint, start a server, or configure transport or authentication. The server must implement MCP at the declared URL.
The method accepts an optional path and endpoint name:
With no arguments, it uses the https endpoint if one exists, otherwise the http endpoint, at the /mcp path. Endpoint names are matched case-insensitively, regardless of declaration order.
To use any other endpoint, pass its name with endpointName, even when it uses HTTPS.
A path without a leading slash gets one added. A null or empty path uses the endpoint’s base URL, and trailing slashes are kept.
A missing endpoint selection fails when the resolver consumes the declaration, not when you call WithMcpServer.