> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Fastify 轉接器 `@mastra/fastify` 套件提供了一個 Server 轉接器,用於透過 [Fastify](https://fastify.dev) 執行 Mastra。有關通用轉接器概念(建構函式選項、初始化流程等),請參閱 [Server Adapters](https://mastra.zisheng.pro/zh-TW/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 app 執行個體 **mastra** (`Mastra`): Mastra 執行個體 **prefix** (`string`): 路由路徑前綴(例如,/api/v2) (Default: `''`) **openapiPath** (`string`): 提供 OpenAPI 規格的路徑(例如,/openapi.json) (Default: `''`) **bodyLimitOptions** (`BodyLimitOptions`): 請求請求主體大小限制 **streamOptions** (`StreamOptions`): 串流遮罩設定。為 true(預設值)時,會在將串流 chunk 傳送給使用者端之前,從中刪除敏感資料(系統 prompt、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/zh-TW/reference/server/register-api-route)。對於直接掛載在 app 上的原始 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 } }, ) ``` ## 手動初始化 如需自訂 middleware 順序,請分別呼叫每個方法,而不是呼叫 `init()`。詳見[手動初始化](https://mastra.zisheng.pro/zh-TW/docs/server/server-adapters)。 ## 範例 - [Fastify Adapter](https://github.com/mastra-ai/mastra/tree/main/examples/server-fastify-adapter):基本 Fastify Server 設定 ## 相關內容 - [Server Adapters](https://mastra.zisheng.pro/zh-TW/docs/server/server-adapters):共享的轉接器概念 - [MastraServer Reference](https://mastra.zisheng.pro/zh-TW/reference/server/mastra-server):完整 API 參考 - [createRoute() Reference](https://mastra.zisheng.pro/zh-TW/reference/server/create-route):建立類型安全的自訂路由