Mastra Class
The Mastra
class is the central orchestrator in any Mastra application, managing agents, workflows, storage, logging, telemetry, and more. Typically, you create a single instance of Mastra
to coordinate your application.
Think of Mastra
as a top-level registry:
- Registering integrations makes them accessible to agents, workflows, and tools alike.
- tools aren’t registered on
Mastra
directly but are associated with agents and discovered automatically.
Usage example
src/mastra/index.ts
import { Mastra } from '@mastra/core/mastra';
import { PinoLogger } from '@mastra/loggers';
import { LibSQLStore } from '@mastra/libsql';
import { weatherWorkflow } from './workflows/weather-workflow';
import { weatherAgent } from './agents/weather-agent';
export const mastra = new Mastra({
workflows: { weatherWorkflow },
agents: { weatherAgent },
storage: new LibSQLStore({
url: ":memory:",
}),
logger: new PinoLogger({
name: 'Mastra',
level: 'info',
}),
});
Constructor parameters
agents?:
Agent[]
= []
Array of Agent instances to register
tools?:
Record<string, ToolApi>
= {}
Custom tools to register. Structured as a key-value pair, with keys being the tool name and values being the tool function.
storage?:
MastraStorage
Storage engine instance for persisting data
vectors?:
Record<string, MastraVector>
Vector store instance, used for semantic search and vector-based tools (eg Pinecone, PgVector or Qdrant)
logger?:
Logger
= Console logger with INFO level
Logger instance created with new PinoLogger()
idGenerator?:
() => string
Custom ID generator function. Used by agents, workflows, memory, and other components to generate unique identifiers.
workflows?:
Record<string, Workflow>
= {}
Workflows to register. Structured as a key-value pair, with keys being the workflow name and values being the workflow instance.
tts?:
Record<string, MastraTTS>
An object for registering Text-To-Speech services.
telemetry?:
OtelConfig
Configuration for OpenTelemetry integration.
deployer?:
MastraDeployer
An instance of a MastraDeployer for managing deployments.
server?:
ServerConfig
= { port: 4111, host: localhost, cors: { origin: '*', allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], allowHeaders: ['Content-Type', 'Authorization', 'x-mastra-client-type'], exposeHeaders: ['Content-Length', 'X-Requested-With'], credentials: false } }
Server configuration including port, host, timeout, API routes, middleware, CORS settings, and build options for Swagger UI, API request logging, and OpenAPI docs.
mcpServers?:
Record<string, MCPServerBase>
An object where keys are unique server identifiers and values are instances of MCPServer or classes extending MCPServerBase. This allows Mastra to be aware of and potentially manage these MCP servers.
bundler?:
BundlerConfig
= { externals: [], sourcemap: false, transpilePackages: [] }
Configuration for the asset bundler with options for externals, sourcemap, and transpilePackages.