🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@mastra/fastify

Package Overview
Dependencies
Maintainers
9
Versions
491
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
This package has malicious versions linked to the ongoing "Mastra AI framework compromise" supply chain attack.

Affected versions:

1.3.31
View campaign page

@mastra/fastify

Mastra Fastify adapter for the server

Source
npmnpm
Version
1.3.30
Version published
Weekly downloads
13K
0.94%
Maintainers
9
Weekly downloads
 
Created
Source

@mastra/fastify

Fastify server adapter for Mastra, enabling you to run Mastra with the Fastify framework.

Installation

npm install @mastra/fastify fastify

Usage

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

Adding Custom Routes

Add routes directly to the Fastify app with access to Mastra context:

// Routes added after init() have access to Mastra context
app.get('/health', async request => {
  const mastraInstance = request.mastra;
  const agents = Object.keys(mastraInstance.listAgents());
  return { status: 'ok', agents };
});

Configuration Options

const server = new MastraServer({
  app,
  mastra,
  prefix: '/api/v2', // Route prefix
  openapiPath: '/openapi.json', // OpenAPI spec endpoint
  bodyLimitOptions: {
    maxSize: 10 * 1024 * 1024, // 10MB
    onError: err => ({ error: 'Payload too large' }),
  },
  streamOptions: { redact: true }, // Redact sensitive data from streams
});

Context Variables

Access these in route handlers via request:

KeyDescription
mastraMastra instance
requestContextRequest context map
abortSignalRequest cancellation signal
toolsAvailable tools

Multipart File Uploads

For multipart file uploads, register @fastify/multipart:

import multipart from '@fastify/multipart';

await app.register(multipart);

FAQs

Package last updated on 12 Jun 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts