Back to Mastra

@mastra/express

server-adapters/express/README.md

2025-12-181.8 KB
Original Source

@mastra/express

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

Installation

bash
npm install @mastra/express express

Usage

typescript
import express from 'express';
import { MastraServer } from '@mastra/express';
import { mastra } from './mastra';

const app = express();
const server = new MastraServer({ app, mastra });

await server.init();

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Adding Custom Routes

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

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

Configuration Options

typescript
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 res.locals:

KeyDescription
mastraMastra instance
requestContextRequest context map
abortSignalRequest cancellation signal
toolsAvailable tools