Server Configuration
When deploying your Mastra application to production, it runs as an HTTP server that exposes your agents, workflows, and other functionality as API endpoints. This page covers how to configure and customize the server for a production environment.
This page covers the server configuration options passed to the Mastra constructor. For running Mastra with your own HTTP server (Hono, Express, etc.), see Server Adapters.
Server architectureDirect link to Server architecture
Mastra uses Hono as its underlying HTTP server framework. When you build a Mastra application using mastra build, it generates a Hono-based HTTP server in the .mastra directory.
The server provides:
- API endpoints for all registered agents
- API endpoints for all registered workflows
- Custom API route support
- Custom middleware support
- Configuration of timeout
- Configuration of port
- Configuration of body limit
See the Middleware and Custom API Routes pages for details on adding additional server behaviour.
Server configurationDirect link to Server configuration
You can configure server port, host, studioBase, and timeout in the Mastra instance.
import { Mastra } from "@mastra/core";
export const mastra = new Mastra({
server: {
port: 3000, // Defaults to 4111
host: "0.0.0.0", // Defaults to 'localhost'
studioBase: "/my-mastra-studio", // Sub-path for the studio (optional)
timeout: 10000, // Defaults to 3 * 60 * 1000 (3 minutes)
},
});
The studioBase option allows you to host Mastra Studio on a sub-path of your existing application. For example, with studioBase: "/my-mastra-studio", the studio will be available at http://your-host:port/my-mastra-studio instead of the root.
TypeScript configurationDirect link to TypeScript configuration
Mastra requires module and moduleResolution values that support modern Node.js versions. Older settings like CommonJS or node are incompatible with Mastra’s packages and will cause resolution errors.
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "bundler",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"noEmit": true,
"outDir": "dist"
},
"include": ["src/**/*"]
}
This TypeScript configuration is optimized for Mastra projects, using modern module resolution and strict type checking.
CORS configurationDirect link to CORS configuration
Mastra allows you to configure CORS (Cross-Origin Resource Sharing) settings for your server.
import { Mastra } from "@mastra/core";
export const mastra = new Mastra({
server: {
cors: {
origin: ["https://example.com"], // Allow specific origins or '*' for all
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["Content-Type", "Authorization"],
credentials: false,
},
},
});
Stream data redactionDirect link to Stream data redaction
When streaming agent responses, the HTTP streaming layer redacts system prompts, tool definitions, API keys, and similar data from each chunk before sending it to clients. This is enabled by default.
If you're using server adapters directly, you can configure redaction behavior. See Stream data redaction in the Server Adapters docs.