Server
Mastra configures its HTTP server from a server.ts file directly under src/mastra/. The server exposes agents, workflows, and other registered primitives as REST endpoints, and the file default-exports the same ServerConfig shape you'd pass to new Mastra().
Use this page for the file-based convention. For server features, middleware, custom routes, generated API docs, and deployment behavior, see Server overview.
QuickstartDirect link to Quickstart
Create src/mastra/server.ts and default-export a ServerConfig object:
import type { ServerConfig } from '@mastra/core/server'
export default {
port: 4111,
host: '0.0.0.0',
} satisfies ServerConfig
Common configurationsDirect link to Common configurations
server.ts can configure the same server options as code registration:
| Need | Use |
|---|---|
| Bind to a deployment platform address | host and port |
| Restrict browser access | cors |
| Add webhooks or health checks | apiRoutes |
| Enable generated API docs | build.openAPIDocs and build.swaggerUI |
See Mastra server, middleware, and custom API routes for examples.
Precedence with codeDirect link to Precedence with code
Code-registered server config wins over server.ts. Routes added during startup, such as channel webhooks, are preserved when file-based server config is applied.