Fastify アダプター
@mastra/fastify パッケージは、Fastify で Mastra を実行するための Server アダプターを提供します。一般的なアダプターの概念(コンストラクターオプション、初期化フローなど)については、Server アダプターを参照してください。
インストールインストールへの直接リンク
Fastify アダプターと Fastify フレームワークをインストールします。
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/fastify@latest fastify
pnpm add @mastra/fastify@latest fastify
yarn add @mastra/fastify@latest fastify
bun add @mastra/fastify@latest fastify
使用例使用例への直接リンク
server.ts
import Fastify from 'fastify'
import { MastraServer } from '@mastra/fastify'
import { mastra } from './mastra'
const app = Fastify({ logger: true })
const server = new MastraServer({ app, mastra })
await server.init()
app.listen({ port: 3000 }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server running on ${address}`)
})
コンストラクターパラメーターコンストラクターパラメーターへの直接リンク
app:
FastifyInstance
Fastify アプリのインスタンス
mastra:
Mastra
Mastra インスタンス
prefix?:
string
= ''
ルートパスのプレフィックス(例:
/api/v2)openapiPath?:
string
= ''
OpenAPI 仕様を配信するパス(例:
/openapi.json)bodyLimitOptions?:
BodyLimitOptions
リクエストボディのサイズ上限
streamOptions?:
StreamOptions
= { redact: true }
Stream の機密情報をマスキングする設定。true(デフォルト)の場合、クライアントに送信する前に Stream チャンクから機密データ(システムプロンプト、Tool 定義、API キー)をマスキングします。
customRouteAuthConfig?:
Map<string, boolean>
ルートごとの認証設定の上書き。キーは
METHOD:PATH(例: GET:/api/health)です。値が false の場合はルートを公開し、true の場合は認証を必須にします。tools?:
ToolsInput
Server で使用できる Tool
taskStore?:
InMemoryTaskStore
A2A(Agent-to-Agent)操作用のタスクストア
mcpOptions?:
MCPOptions
MCP トランスポートオプション。Cloudflare Workers や Vercel Edge などのステートレス環境では
serverless: true を設定します。素のルートの保護素のルートの保護への直接リンク
Mastra が管理する認証と requiresAuth などのルートメタデータを使用する場合は、registerApiRoute() を推奨します。アプリに直接マウントする素の Fastify ルートでは、createAuthMiddleware() を使用します。
server.ts
import Fastify from 'fastify'
import { createAuthMiddleware, MastraServer } from '@mastra/fastify'
import { mastra } from './mastra'
const app = Fastify()
const server = new MastraServer({ app, mastra })
await server.init()
app.get('/custom/protected', { preHandler: createAuthMiddleware({ mastra }) }, async request => {
const user = request.requestContext.get('user')
return { user }
})
app.get(
'/custom/public',
{ preHandler: createAuthMiddleware({ mastra, requiresAuth: false }) },
async () => {
return { ok: true }
},
)
手動初期化手動初期化への直接リンク
ミドルウェアの順序をカスタマイズするには、init() の代わりに各メソッドを個別に呼び出します。詳細については、手動初期化を参照してください。
例例への直接リンク
- Fastify アダプター: Fastify Server の基本的なセットアップ
関連項目関連項目への直接リンク
- Server アダプター: アダプター共通の概念
- MastraServer リファレンス: 完全な API リファレンス
- createRoute() リファレンス: 型安全なカスタムルートの作成