> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Fastify アダプター `@mastra/fastify` パッケージは、[Fastify](https://fastify.dev) で Mastra を実行するための Server アダプターを提供します。一般的なアダプターの概念(コンストラクターオプション、初期化フローなど)については、[Server アダプター](https://mastra.zisheng.pro/ja/docs/server/server-adapters)を参照してください。 ## インストール Fastify アダプターと Fastify フレームワークをインストールします。 **npm**: ```bash npm install @mastra/fastify@latest fastify ``` **pnpm**: ```bash pnpm add @mastra/fastify@latest fastify ``` **Yarn**: ```bash yarn add @mastra/fastify@latest fastify ``` **Bun**: ```bash bun add @mastra/fastify@latest fastify ``` ## 使用例 ```typescript 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) (Default: `''`) **openapiPath** (`string`): OpenAPI 仕様を配信するパス(例: /openapi.json) (Default: `''`) **bodyLimitOptions** (`BodyLimitOptions`): リクエストボディのサイズ上限 **streamOptions** (`StreamOptions`): Stream の機密情報をマスキングする設定。true(デフォルト)の場合、クライアントに送信する前に Stream チャンクから機密データ(システムプロンプト、Tool 定義、API キー)をマスキングします。 (Default: `{ redact: true }`) **customRouteAuthConfig** (`Map`): ルートごとの認証設定の上書き。キーは 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()`](https://mastra.zisheng.pro/ja/reference/server/register-api-route) を推奨します。アプリに直接マウントする素の Fastify ルートでは、`createAuthMiddleware()` を使用します。 ```typescript 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()` の代わりに各メソッドを個別に呼び出します。詳細については、[手動初期化](https://mastra.zisheng.pro/ja/docs/server/server-adapters)を参照してください。 ## 例 - [Fastify アダプター](https://github.com/mastra-ai/mastra/tree/main/examples/server-fastify-adapter): Fastify Server の基本的なセットアップ ## 関連項目 - [Server アダプター](https://mastra.zisheng.pro/ja/docs/server/server-adapters): アダプター共通の概念 - [MastraServer リファレンス](https://mastra.zisheng.pro/ja/reference/server/mastra-server): 完全な API リファレンス - [createRoute() リファレンス](https://mastra.zisheng.pro/ja/reference/server/create-route): 型安全なカスタムルートの作成