Resource MCP servers
Questi contenuti non sono ancora disponibili nella tua lingua.
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.
How it works
Section titled “How it works”When a resource is annotated with WithMcpServer() 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 toolsandaspire mcp callto discover and invoke resource tools directly from the terminal.
Add an MCP server to a resource
Section titled “Add an MCP server to a resource”Use the WithMcpServer() extension method to declare that a resource hosts an MCP server:
#pragma warning disable ASPIREMCP001
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddPostgres("db") .AddDatabase("appdata");
// Expose PostgreSQL MCP tools (SQL queries, schema inspection, etc.)db.WithPostgresMcp();
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const db: PostgresDatabaseResource
db = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("db") .PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("appdata");
// Expose PostgreSQL MCP tools (SQL queries, schema inspection, etc.)await const db: PostgresDatabaseResource
db.PostgresDatabaseResource.withPostgresMcp(options?: { configureContainer?: ((obj: PostgresMcpContainerResource) => Promise<void>) | undefined; containerName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds Postgres MCP server
withPostgresMcp();
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();Discover available tools
Section titled “Discover available tools”Use aspire mcp tools to list all MCP tools exposed by running resources:
aspire mcp toolsFor machine-readable output including input schemas:
aspire mcp tools --format JsonCall a tool
Section titled “Call a tool”Use aspire mcp call to invoke a specific tool on a resource:
aspire mcp call <resource> <tool> --input '{"key": "value"}'For example, to run a SQL query on a PostgreSQL resource:
aspire mcp call appdata query --input '{"sql": "SELECT * FROM users LIMIT 5"}'Custom MCP server resources
Section titled “Custom MCP server resources”You can add any container that implements the MCP protocol as an MCP-enabled resource. Use WithMcpServer() to tell Aspire where the MCP endpoint lives:
#pragma warning disable ASPIREMCP001
var builder = DistributedApplication.CreateBuilder(args);
// Add a custom MCP server containervar myMcpServer = builder.AddContainer("my-mcp", "myregistry/my-mcp-server") .WithHttpEndpoint(targetPort: 8080) .WithMcpServer("/mcp");
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
// Add a custom MCP server containerconst const myMcpServer: ContainerResource
myMcpServer = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addContainer(name: string, image: string): ContainerResource
Adds a container resource
addContainer("my-mcp", "myregistry/my-mcp-server") .ContainerResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ContainerResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ targetPort?: number | undefined
targetPort: 8080 }) .ContainerResource.withMcpServer(path?: string, endpointName?: string): ContainerResource (+1 overload)
Configures an MCP server endpoint on the resource
withMcpServer("/mcp");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();The WithMcpServer() method accepts an optional path and endpoint name:
WithMcpServer()— uses the default HTTP endpoint at the root pathWithMcpServer("/mcp")— uses the default HTTP endpoint at/mcpWithMcpServer("/sse", endpointName: "https")— uses a named endpoint at/sse
See also
Section titled “See also”- Use AI coding agents — set up your project for AI agents
- Aspire MCP server — the built-in MCP server tools
- ASPIREMCP001 diagnostic — experimental MCP server API warning
- ASPIREPOSTGRES001 diagnostic — experimental PostgreSQL MCP warning