Watch Aspire live streamsドキュメント試す

リソース MCP サーバー

Aspire リソースは、独自の MCP (Model Context Protocol) サーバーを公開でき、AI コーディング エージェントがデータベース、API、その他のサービスと直接対話できるようにします。例えば、PostgreSQL リソースは SQL クエリ ツールを公開でき、エージェントが会話を終了することなくクエリを実行できます。

AppHost で WithMcpServer() で注釈が付けられたリソースの場合、Aspire は MCP エンドポイントを検出し、そのツールを 2 つの方法で利用可能にします。

  • Aspire MCP サーバーを通じて — リソース ツールが、組み込みの Aspire MCP サーバー ツールと共に自動的にプロキシされます。AI エージェントは追加の構成なしでツール リストにそれらを表示します。
  • CLI を通じてaspire mcp toolsaspire mcp call を使用してリソース ツールをターミナルから直接検出して呼び出します。

リソースに MCP サーバーを追加する

Section titled “リソースに MCP サーバーを追加する”

WithMcpServer() 拡張メソッドを使用して、リソースが MCP サーバーをホストしていることを宣言します。

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
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 resource to the application model. A container is used for local development.

addPostgres
("db")
.
PostgresServerResource.addDatabase(name: string, options?: {
databaseName?: string;
} | undefined): PostgresDatabaseResource (+1 overload)

Adds a PostgreSQL database to the application model.

addDatabase
("appdata");
// PostgreSQL MCP ツール (SQL クエリ、スキーマ検査など) を公開します
await
const db: PostgresDatabaseResource
db
.
PostgresDatabaseResource.withPostgresMcp(options?: {
configureContainer?: ((obj: PostgresMcpContainerResource) => Promise<void>) | undefined;
containerName?: string;
} | undefined): PostgresDatabaseResource (+1 overload)

Adds a Postgres MCP server container and configures it to connect to the database represented by builder.

withPostgresMcp
();
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

aspire mcp tools を使用して、実行中のリソースによって公開されているすべての MCP ツールをリスト表示します。

Aspire CLI
aspire mcp tools

入力スキーマを含むマシン読み取り可能な出力の場合:

Aspire CLI — JSON 出力
aspire mcp tools --format Json

aspire mcp call を使用して、リソース上の特定のツールを呼び出します。

Aspire CLI
aspire mcp call <resource> <tool> --input '{"key": "value"}'

例えば、PostgreSQL リソースで SQL クエリを実行するには:

Aspire CLI — クエリの例
aspire mcp call appdata query --input '{"sql": "SELECT * FROM users LIMIT 5"}'

カスタム MCP サーバー リソース

Section titled “カスタム MCP サーバー リソース”

MCP プロトコルを実装するコンテナーを MCP 対応リソースとして追加できます。WithMcpServer() を使用して、Aspire に MCP エンドポイントの場所を指示します。

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// カスタム MCP サーバー コンテナーを追加します
const
const myMcpServer: ContainerResource
myMcpServer
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource

Adds a container resource to the application.

addContainer
("my-mcp", {
AddContainerOptions.image?: string | undefined
image
: "myregistry/my-mcp-server",
AddContainerOptions.tag?: string | undefined
tag
: "latest" })
.
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)

Marks the resource as hosting a Model Context Protocol (MCP) server on the specified endpoint.

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
();

WithMcpServer() メソッドはオプションのパスとエンドポイント名を受け入れます。

  • WithMcpServer() — ルート パスでデフォルトの HTTP エンドポイントを使用します
  • WithMcpServer("/mcp")/mcp でデフォルトの HTTP エンドポイントを使用します
  • WithMcpServer("/sse", endpointName: "https")/sse で名前付きエンドポイントを使用します