Skip to main content

Fastify 适配器

🌐 Fastify Adapter

@mastra/fastify 包提供了一个用于在 Fastify 上运行 Mastra 的服务器适配器。

🌐 The @mastra/fastify package provides a server adapter for running Mastra with Fastify.

info

有关一般适配器概念(构造函数选项、初始化流程等),请参见 服务器适配器

🌐 For general adapter concepts (constructor options, initialization flow, etc.), see Server Adapters.

安装
Direct link to 安装

🌐 Installation

安装 Fastify 适配器和 Fastify 框架:

🌐 Install the Fastify adapter and Fastify framework:

npm install @mastra/fastify@latest fastify

使用示例
Direct link to 使用示例

🌐 Usage example

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}`);
});

构造函数参数
Direct link to 构造函数参数

🌐 Constructor parameters

app:

FastifyInstance
Fastify app instance

mastra:

Mastra
Mastra instance

prefix?:

string
= ''
Route path prefix (e.g., `/api/v2`)

openapiPath?:

string
= ''
Path to serve OpenAPI spec (e.g., `/openapi.json`)

bodyLimitOptions?:

BodyLimitOptions
Request body size limits

streamOptions?:

StreamOptions
= { redact: true }
Stream redaction config. When true (default), redacts sensitive data (system prompts, tool definitions, API keys) from stream chunks before sending to clients.

customRouteAuthConfig?:

Map<string, boolean>
Per-route auth overrides. Keys are `METHOD:PATH` (e.g., `GET:/api/health`). Value `false` makes route public, `true` requires auth.

tools?:

ToolsInput
Available tools for the server

taskStore?:

InMemoryTaskStore
Task store for A2A (Agent-to-Agent) operations

mcpOptions?:

MCPOptions
MCP transport options. Set `serverless: true` for stateless environments like Cloudflare Workers or Vercel Edge.

手动初始化
Direct link to 手动初始化

🌐 Manual initialization

对于自定义中间件顺序,请分别调用每个方法,而不是使用 init()。详情请参阅手动初始化

🌐 For custom middleware ordering, call each method separately instead of init(). See manual initialization for details.

示例
Direct link to 示例

🌐 Examples

🌐 Related