> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # NestJS Adapter `@mastra/nestjs` パッケージは、Express ベースの NestJS プラットフォームで Mastra を実行するための NestJS モジュールを提供します。 v1 では意図的に Express のみをサポートしています。別の HTTP アダプターで Nest をブートストラップした場合、部分的な統合を試みる代わりに、`MastraModule` が起動時にエラーをスローします。アダプター全般の概念については、[Server Adapters](https://mastra.zisheng.pro/ja/docs/server/server-adapters) を参照してください。 ## インストール NestJS アダプターをインストールし、アプリが Express プラットフォームを使用していることを確認します。 **npm**: ```bash npm install @mastra/nestjs@latest ``` **pnpm**: ```bash pnpm add @mastra/nestjs@latest ``` **Yarn**: ```bash yarn add @mastra/nestjs@latest ``` **Bun**: ```bash bun add @mastra/nestjs@latest ``` ## 使用例 ```typescript import { Module } from '@nestjs/common' import { MastraModule } from '@mastra/nestjs' import { mastra } from './mastra' @Module({ imports: [ MastraModule.register({ mastra, }), ], }) export class AppModule {} ``` > **注記:** `MastraModule` はキャッチオールコントローラー(`@All('*')`)を登録します。アプリのモジュールより先にインポートすると、関係のないルートをインターセプトして 404 を返す可能性があります。競合を避けるには、`MastraModule` を最後にインポートするか、専用のプレフィックス(例:`/api/v1/mastra`)配下にマウントしてください。 ```typescript import { NestFactory } from '@nestjs/core' import { AppModule } from './app.module' async function bootstrap() { const app = await NestFactory.create(AppModule) await app.listen(3000) } bootstrap() ``` デフォルトでは、Mastra のルートは `/api` 配下にマウントされます。変更するには `prefix` を使用します。 ## モジュールオプション **mastra** (`Mastra`): Mastra インスタンス **prefix** (`string`): ルートパスのプレフィックス(例:/api/v2) (Default: `` `/api` ``) **rateLimitOptions** (`{ enabled?: boolean; defaultLimit?: number; windowMs?: number; generateLimit?: number }`): レート制限の設定(デフォルトで有効) **shutdownOptions** (`{ timeoutMs?: number; notifyClients?: boolean }`): グレースフルシャットダウンの設定 **bodyLimitOptions** (`{ maxSize?: number; maxFileSize?: number; tempDir?: string; allowedMimeTypes?: string[] }`): リクエストボディのサイズ制限 **streamOptions** (`{ redact?: boolean; heartbeatMs?: number }`): ストリーミングの設定 **tracingOptions** (`{ enabled?: boolean; serviceName?: string }`): OpenTelemetry トレーシングの設定 **contextOptions** (`{ strict?: boolean; logWarnings?: boolean }`): リクエストコンテキスト解析の設定 **customRouteAuthConfig** (`Map`): ルート単位の認証オーバーライド。キーは METHOD:PATH です。 **tools** (`Record`): サーバーに登録された Tool **taskStore** (`InMemoryTaskStore`): A2A(Agent-to-Agent)操作用のタスクストア **mcpOptions** (`{ serverless?: boolean; sessionIdGenerator?: () => string }`): MCP トランスポートのオプション **auth** (`{ enabled?: boolean; allowQueryApiKey?: boolean }`): Mastra のトークン認証を有効にします。デフォルトでは無効です。ほとんどの NestJS アプリは独自の認証ガードを使用します。後方互換性のため、クエリ文字列の apiKey 認証はオプトインです。 (Default: `` `{ enabled: false }` ``) ## 非同期登録 ```typescript import { Module } from '@nestjs/common' import { ConfigModule, ConfigService } from '@nestjs/config' import { MastraModule } from '@mastra/nestjs' import { Mastra } from '@mastra/core/mastra' @Module({ imports: [ ConfigModule.forRoot(), MastraModule.registerAsync({ imports: [ConfigModule], useFactory: (config: ConfigService) => ({ mastra: new Mastra({ agents: { greeter: { name: 'greeter', description: 'Greets the user', model: config.get('MASTRA_MODEL', 'openai/gpt-5-mini'), }, }, }), prefix: config.get('MASTRA_PREFIX', '/api'), }), inject: [ConfigService], }), ], }) export class AppModule {} ``` ## Mastra へのアクセス サービス内で `MASTRA` トークンまたは `MastraService` を使用します。 ```typescript import { Injectable, Inject } from '@nestjs/common' import { MASTRA, MastraService } from '@mastra/nestjs' import type { Mastra } from '@mastra/core/mastra' @Injectable() export class AgentService { constructor(@Inject(MASTRA) private readonly mastra: Mastra) {} } @Injectable() export class WorkflowService { constructor(private readonly mastraService: MastraService) {} } ``` ## MCP ルート MCP エンドポイントは API プレフィックス配下で公開されます。 - `POST /api/mcp/:serverId/mcp` - `GET /api/mcp/:serverId/sse` - `POST /api/mcp/:serverId/messages` ## ヘルスチェックルート インフラストラクチャとの互換性を保つため、運用エンドポイントには意図的にプレフィックスが付きません。 - `GET /health` - `GET /ready` - `GET /info` ## 関連項目 - [Server Adapters](https://mastra.zisheng.pro/ja/docs/server/server-adapters) - [MastraServer リファレンス](https://mastra.zisheng.pro/ja/reference/server/mastra-server)