mastra-server
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Configure and extend the Mastra HTTP server: server options on new Mastra({ server }), built-in /api routes, custom routes with registerApiRoute or createRoute, Hono middleware, request context populated from headers, server adapters for Express, Hono, Fastify, Koa, Elysia, NestJS, Next.js, and TanStack Start, custom adapters, and the pub/sub event bus. Use this skill whenever the user wants to add an endpoint or webhook to a Mastra server, write middleware, set CORS, body size, timeouts, or an API prefix, mount Mastra inside an existing app, or run Mastra across several processes or hosts. Triggers on registerApiRoute, createRoute, @mastra/core/server, @mastra/server/server-adapter, MastraServer, @mastra/express, @mastra/hono, @mastra/fastify, @mastra/koa, @mastra/elysia, @mastra/nestjs, @mastra/next, @mastra/tanstack-start, server.middleware, server.cors, apiRoutes, startWorkers, RedisStreamsPubSub, UnixSocketPubSub, swagger-ui. Also triggers on Mastra endpoint 404, CORS, or 413 errors.
- Reads credentials
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 2 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 48
Ask before writing code if the serving mode is unclear.
- Very specific: line 105
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and node_modules/@mastra/server/dist/docs/ plus …
- Very specific: line 108
Add routes: Use registerApiRoute() for Hono-style handlers.
- Very specific: line 109
Add middleware: Put cross-cutting logic in server.middleware.
- Very specific: line 110
Populate request context: Read headers or the authenticated user in middleware and set() values.
- Very specific: line 112
With an adapter: call await server.init(), then await mastra.startWorkers() after listen, and await mastra.shutdown() on SIGTERM.
- Distinctive: line 113
Choose pub/sub: if you run more than one instance (see deep dive).
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Server
Configure the HTTP server that exposes agents, workflows, tools, and memory, add your own routes and middleware, and run Mastra inside another framework when needed.
When to use
- Adding a custom endpoint, webhook, or health check to a Mastra server
- Writing server middleware (logging, header checks, CORS, request context injection)
- Setting
serveroptions: port, host, CORS, body size, timeout, Swagger UI, error hooks - Mounting Mastra inside an existing Express, Hono, Fastify, Koa, Elysia, or NestJS app
- Exposing Mastra from a Next.js or TanStack Start catch-all route
- Choosing a pub/sub backend for multi-process or multi-host deployments
- Building an adapter for an unsupported HTTP framework
When NOT to use
- Calling the server from a browser or another service (use
mastra-client-sdk) - Step-by-step setup in a specific framework such as Next.js or SvelteKit (use
mastra-framework-integration) - Chat routes for
useChat(),chatRoute(),handleChatStream()(usemastra-agentic-ui) - Authentication providers, JWT, RBAC, FGA (use
mastra-auth) - Deploying to Vercel, Cloudflare, or containers (use
mastra-deployment) - MCP server routes and MCP auth (use
mastra-mcp) - Per-request agent behavior such as dynamic instructions (use
mastra-agents)
Required inputs
- How the server runs: generated server (
mastra dev/mastra build) or a server adapter in an existing app - The framework and version if an adapter is involved
- The routes to add: path, method, body shape, and whether they need auth
- Values to derive per request (tenant, locale, user tier) and the header or token they come from
- Deployment shape: single process, several processes on one host, or several hosts
Ask before writing code if the serving mode is unclear. Middleware behaves differently on non-Hono adapters.
The framework: one server, three extension points
Mastra serves a Hono app. mastra build generates it in .mastra. Server adapters mount the same routes on your framework instead. Built-in routes live under /api (agents, workflows, tools, memory, vectors, logs, MCP, and more). With mastra dev, browse them at http://localhost:4111/swagger-ui or /api/openapi.json.
You extend the server in three places:
| Extension | Where | Runs on |
| Custom routes | server.apiRoutes with registerApiRoute() or createRoute() | All serving paths |
| Middleware | server.middleware (Hono signature (c, next)) | Hono-based paths only |
| Request context | c.get('requestContext').set(...) inside middleware or routes | All paths |
// src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { registerApiRoute } from '@mastra/core/server'
export const mastra = new Mastra({
server: {
port: 3000, // Defaults to PORT env var or 4111
middleware: [
async (c, next) => {
const country = c.req.header('CF-IPCountry')
c.get('requestContext').set('temperature-unit', country === 'US' ? 'fahrenheit' : 'celsius')
await next()
},
],
apiRoutes: [
registerApiRoute('/health-check', {
method: 'GET',
handler: async c => c.json({ status: 'ok' }),
}),
],
},
})Custom routes are mounted at the server root (/health-check), not under /api. Paths that start with the API prefix throw at startup.
Generated server vs adapter
| Choose the generated server when | Choose an adapter when |
| Mastra is its own service | Mastra endpoints belong inside an existing app |
You want server.cors, server.port, apiRoutes handled for you | You need control over middleware order or the HTTP framework |
| You use file-based agents | You register every agent in code |
Adapters ignore port, host, cors, timeout, and apiRoutes from server config. Configure those with the framework. They still read auth, bodySizeLimit, onError, and middleware (Hono-based only).
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/andnode_modules/@mastra/server/dist/docs/plus the installed types. Otherwise fetchhttps://mastra.ai/docs/server/overview.mdandhttps://mastra.ai/reference/server/register-api-route.md. Trust these over memory. - Pick the serving mode. Generated server for a standalone service, or install the adapter package (
@mastra/express,@mastra/hono, and so on). - Set server options. Port, host, CORS,
bodySizeLimit(default 4.5 MB),timeout(default 3 minutes),build.openAPIDocsandbuild.swaggerUIfor production docs. - Add routes. Use
registerApiRoute()for Hono-style handlers. UsecreateRoute()from@mastra/server/server-adapterwhen you want Zod validation and typed handler params. - Add middleware. Put cross-cutting logic in
server.middleware. Use{ path, handler }to scope it. On Express, Fastify, Koa, Elysia, or NestJS, register middleware with the framework instead. - Populate request context. Read headers or the authenticated user in middleware and
set()values. Match them withrequestContextSchemaon agents and tools. - Decide auth per route. With
server.authconfigured, custom routes require auth. SetrequiresAuth: falsefor public webhooks (seemastra-auth). - With an adapter: call
await server.init(), thenawait mastra.startWorkers()afterlisten, andawait mastra.shutdown()onSIGTERM. - Choose pub/sub if you run more than one instance (see deep dive).
- Test with
curlagainst the route and check/api/openapi.json.
Deep dive: server adapters
import express from 'express'
import { MastraServer } from '@mastra/express'
import { mastra } from './mastra'
const app = express()
app.use(express.json()) // Required for body parsing
const server = new MastraServer({ app, mastra })
await server.init()
const httpServer = app.listen(4111, async () => {
try {
await mastra.startWorkers()
} catch (error) {
console.error('Failed to start background workers', error)
httpServer.close(() => process.exit(1))
}
})
process.on('SIGTERM', async () => {
await mastra.shutdown()
process.exit(0)
})init() runs registerContextMiddleware(), registerAuthMiddleware(), registerUserMiddleware(), then registerRoutes(). Routes added before init() have no Mastra context. Middleware added after init() never runs on Mastra routes. Call the steps by hand when you need custom ordering. Raw framework routes that need Mastra auth use the adapter's createAuthMiddleware({ mastra }).
Next.js and TanStack Start mount a catch-all route with createNextRouteHandler({ mastra }) or createStartRouteHandler({ mastra }). The prefix must match the mount path. See references/adapters.md.
Deep dive: pub/sub
Mastra uses one event bus for workflow execution, scheduled workflows, background tasks, agent signals, and resumable streams. The default EventEmitterPubSub is in process only.
| Deployment | Backend | Package |
| One process | EventEmitterPubSub (default) | @mastra/core/events |
| Several processes, one host | UnixSocketPubSub | @mastra/core/events |
| Several hosts | RedisStreamsPubSub, ValkeyStreamsPubSub, GoogleCloudPubSub | @mastra/redis-streams, @mastra/valkey-streams, @mastra/google-cloud-pubsub |
import { Mastra } from '@mastra/core'
import { RedisStreamsPubSub } from '@mastra/redis-streams'
export const mastra = new Mastra({
pubsub: new RedisStreamsPubSub({ url: 'redis://localhost:6379' }),
})Wrap the in-process default in CachingPubSub to support stream replay on reconnect.
Failure patterns
- Custom route under
/api. Startup throws. Use/webhooks/github, not/api/webhooks/github, or changeserver.apiPrefixand update auth paths. server.middlewareon Express, Fastify, Koa, Elysia, or NestJS. It is skipped with a warning. Register framework middleware, or use manual initialization.- Scheduled workflows never fire with an adapter.
init()does not start workers. Callmastra.startWorkers()afterlisten. - Express route returns an empty body.
express.json()is missing or added afternew MastraServer(). - Setting
server.corsorserver.portwith an adapter. Ignored. Configure CORS and the port in the framework. - Middleware that blocks a public route. User middleware never runs on
requiresAuth: falseroutes. Authorize inside the handler. - Middleware path without the prefix. With
apiPrefix: '/api/v2', block/api/v2/memory/*, not/api/memory/*. - Signals or workflow events lost across instances. The default pub/sub is in process. Use Redis Streams, Valkey Streams, or Google Cloud Pub/Sub.
- NestJS routes return 404.
MastraModuleregisters a catch-all controller. Import it last or mount it under a dedicated prefix. - Streaming route keeps running after the client leaves, or stops too early. Pass
c.req.raw.signalasabortSignalto cancel on disconnect. Omit it and callconsumeStream()to keep generating. - Large uploads fail with 413. Raise
server.bodySizeLimitor setmaxBodySizeon acreateRoute()route.
Output format
A finished server change contains:
src/mastra/index.tswith theserverblock (routes, middleware, options) or route modules imported into it- For adapters: the app entry file with
MastraServer,init(),startWorkers(), and shutdown handling - Env vars (
PORT,MASTRA_HOST, pub/sub URLs) listed in.env.example - A
curlcommand or test proving each new route responds with the expected status - Notes on which routes are public and why
Reference files
references/api.md- Server config options,registerApiRoute(),createRoute(),MastraServeroptions, built-in route groups, pub/sub classes.references/adapters.md- Install, mount, context access, and quirks for each official adapter, plus custom adapters.references/examples.md- Validated routes, tenant middleware, Hono adapter with manual init, persisted background stream.references/checklist.md- Pre-production server checklist.
Live docs
- https://mastra.ai/docs/server/overview
- https://mastra.ai/docs/server/custom-api-routes
- https://mastra.ai/docs/server/middleware
- https://mastra.ai/docs/server/request-context
- https://mastra.ai/docs/server/server-adapters
- https://mastra.ai/docs/server/custom-adapters
- https://mastra.ai/docs/server/pubsub
- https://mastra.ai/reference/server/routes
- https://mastra.ai/reference/configuration
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

