The Mastra Class
The Mastra class is the core entry point for your application. It manages agents, workflows, and server endpoints.
Constructor Options
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 createLogger()
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.
serverMiddleware?:
Array<{ handler: (c: any, next: () => Promise<void>) => Promise<Response | void>; path?: string; }>
= []
Server middleware functions to be applied to API routes. Each middleware can specify a path pattern (defaults to '/api/*').
Initialization
The Mastra class is typically initialized in your src/mastra/index.ts
file:
import { Mastra } from "@mastra/core";
import { createLogger } from "@mastra/core/logger";
// Basic initialization
export const mastra = new Mastra({});
// Full initialization with all options
export const mastra = new Mastra({
agents: {},
workflows: [],
integrations: [],
logger: createLogger({
name: "My Project",
level: "info",
}),
storage: {},
tools: {},
vectors: {},
});
You can think of the Mastra
class as a top-level registry. When you register tools with Mastra, your registered agents and workflows can use them. When you register integrations with Mastra, agents, workflows, and tools can use them.
Methods
getAgent(name):
Agent
Returns an agent instance by id. Throws if agent not found.
getAgents():
Record<string, Agent>
Returns all registered agents as a key-value object.
getWorkflow(id, { serialized }):
Workflow
Returns a workflow instance by id. The serialized option (default: false) returns a simplified representation with just the name.
getWorkflows({ serialized }):
Record<string, Workflow>
Returns all registered workflows. The serialized option (default: false) returns simplified representations.
getVector(name):
MastraVector
Returns a vector store instance by name. Throws if not found.
getVectors():
Record<string, MastraVector>
Returns all registered vector stores as a key-value object.
getDeployer():
MastraDeployer | undefined
Returns the configured deployer instance, if any.
getStorage():
MastraStorage | undefined
Returns the configured storage instance.
getMemory():
MastraMemory | undefined
Returns the configured memory instance. Note: This is deprecated, memory should be added to agents directly.
getServerMiddleware():
Array<{ handler: Function; path: string; }>
Returns the configured server middleware functions.
setStorage(storage):
void
Sets the storage instance for the Mastra instance.
setLogger({ logger }):
void
Sets the logger for all components (agents, workflows, etc.).
setTelemetry(telemetry):
void
Sets the telemetry configuration for all components.
getLogger():
Logger
Gets the configured logger instance.
getTelemetry():
Telemetry | undefined
Gets the configured telemetry instance.
getLogsByRunId({ runId, transportId }):
Promise<any>
Retrieves logs for a specific run ID and transport ID.
getLogs(transportId):
Promise<any>
Retrieves all logs for a specific transport ID.
Error Handling
The Mastra class methods throw typed errors that can be caught:
try {
const tool = mastra.getTool("nonexistentTool");
} catch (error) {
if (error instanceof Error) {
console.log(error.message); // "Tool with name nonexistentTool not found"
}
}