NestJS Adapter
@mastra/nestjs パッケージは、Express ベースの NestJS プラットフォームで Mastra を実行するための NestJS モジュールを提供します。
v1 では意図的に Express のみをサポートしています。別の HTTP アダプターで Nest をブートストラップした場合、部分的な統合を試みる代わりに、MastraModule が起動時にエラーをスローします。アダプター全般の概念については、Server Adapters を参照してください。
インストールインストールへの直接リンク
NestJS アダプターをインストールし、アプリが Express プラットフォームを使用していることを確認します。
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/nestjs@latest
pnpm add @mastra/nestjs@latest
yarn add @mastra/nestjs@latest
bun add @mastra/nestjs@latest
使用例使用例への直接リンク
src/app.module.ts
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)配下にマウントしてください。
src/main.ts
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`
ルートパスのプレフィックス(例:
/api/v2)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<string, boolean>
ルート単位の認証オーバーライド。キーは
METHOD:PATH です。tools?:
Record<string, Tool>
サーバーに登録された Tool
taskStore?:
InMemoryTaskStore
A2A(Agent-to-Agent)操作用のタスクストア
mcpOptions?:
{ serverless?: boolean; sessionIdGenerator?: () => string }
MCP トランスポートのオプション
auth?:
{ enabled?: boolean; allowQueryApiKey?: boolean }
= `{ enabled: false }`
Mastra のトークン認証を有効にします。デフォルトでは無効です。ほとんどの NestJS アプリは独自の認証ガードを使用します。後方互換性のため、クエリ文字列の
apiKey 認証はオプトインです。非同期登録非同期登録への直接リンク
src/app.module.ts
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 へのアクセスへの直接リンク
サービス内で MASTRA トークンまたは MastraService を使用します。
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 ルートへの直接リンク
MCP エンドポイントは API プレフィックス配下で公開されます。
POST /api/mcp/:serverId/mcpGET /api/mcp/:serverId/ssePOST /api/mcp/:serverId/messages
ヘルスチェックルートヘルスチェックルートへの直接リンク
インフラストラクチャとの互換性を保つため、運用エンドポイントには意図的にプレフィックスが付きません。
GET /healthGET /readyGET /info