The Mastra Class
The Mastra
class is the central orchestrator in any Mastra application. It is responsible for managing agents, tools, workflows, storage, logging, telemetry, and more. You typically create a single instance of the Mastra
class for your application.
Importing
import { Mastra } from "@mastra/core";
Constructor
Creates a new Mastra
instance with the specified configuration.
constructor(config?: Config);
Configuration (Config
object)
The constructor accepts an optional Config
object to customize its behavior and integrate various Mastra components. All properties on the Config
object are optional.
agents?:
tools?:
storage?:
vectors?:
logger?:
workflows?:
tts?:
telemetry?:
deployer?:
server?:
mcpServers?:
bundler:
Initialization
The Mastra class is typically initialized in your src/mastra/index.ts
file:
import { Mastra } from "@mastra/core";
import { PinoLogger } from "@mastra/loggers";
// Basic initialization
export const mastra = new Mastra({});
// Full initialization with all options
export const mastra = new Mastra({
agents: {},
workflows: [],
integrations: [],
logger: new PinoLogger({
name: "My Project",
level: "info",
}),
storage: {},
tools: {},
vectors: {},
mcpServers: {},
});
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):
getAgents():
getWorkflow(id, { serialized }):
getWorkflows({ serialized }):
getVector(name):
getVectors():
getDeployer():
getStorage():
getMemory():
getServer():
setStorage(storage):
setLogger({ logger }):
setTelemetry(telemetry):
getLogger():
getTelemetry():
getLogsByRunId({ runId, transportId }):
getLogs(transportId):
getMCPServers():
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"
}
}