跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

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
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
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
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