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.
integrations?:
Integration[]
Array of Mastra integrations to register. Will be used by agents, workflows, and tools.
engine?:
MastraEngine
Database engine instance
vectors?:
Record<string, MastraVector>
Vector store instance, used for semantic search and vector-based tools (eg Pinecone, PgVector or Qdrant)
logger?:
Logger
Logger instance created with createLogger()
workflows?:
Workflow[]
Array of Workflow instances to register
syncs?:
Record<string, SyncConfig>
Sync configurations (requires engine)
Initialization
The Mastra class is typically initialized in your src/mastra/index.ts
file:
import { Mastra, createLogger } from "@mastra/core";
// Basic initialization
export const mastra = new Mastra({});
// Full initialization with all options
export const mastra = new Mastra({
agents: {},
workflows: [],
integrations: [],
logger: createLogger({
type: "CONSOLE",
level: "INFO",
}),
engine: {},
syncs: {},
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.
sync<K>(key, params):
Promise<void>
Executes a sync operation. Requires engine to be configured. Throws if sync not found.
setLogger({ key, logger }):
void
Sets a logger for a specific component (AGENT | WORKFLOW). Advanced use case.
getLogger(key):
Logger | undefined
Gets the logger for a specific component. Advanced use case.
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"
}
}