Skip to main content

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.

Quickstart
Direct link to Quickstart

Create src/mastra/server.ts and default-export a ServerConfig object:

src/mastra/server.ts
import type { ServerConfig } from '@mastra/core/server'

export default {
port: 4111,
host: '0.0.0.0',
} satisfies ServerConfig

Common configurations
Direct link to Common configurations

server.ts can configure the same server options as code registration:

NeedUse
Bind to a deployment platform addresshost and port
Restrict browser accesscors
Add webhooks or health checksapiRoutes
Enable generated API docsbuild.openAPIDocs and build.swaggerUI

See Mastra server, middleware, and custom API routes for examples.

Precedence with code
Direct 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.

On this page