Core Concept
Mastra follows a dependency injection pattern where components register with the central Mastra instance to access shared resources:- Centralized Configuration: All components configured in one place
- Dependency Injection: Components automatically receive access to storage, logging, observability
- Plugin Architecture: Swap implementations without changing application code
- Type Safety: Full TypeScript support with generic type parameters
Basic Example
Configuration Interface
TheConfig interface defines all optional components that can be registered:
Dependency Injection Flow
When components are registered with Mastra, they automatically gain access to shared resources:Component Registry Methods
Agents
Agents are autonomous systems that can make decisions and take actions:Workflows
Workflows provide type-safe, composable task execution:Storage
Storage persists conversation history, workflow state, and application data:Vector Stores
Vector stores enable semantic search and RAG:Tools
Tools extend agent capabilities with reusable functions:Memory
Memory instances manage conversation persistence:Logging
Mastra supports pluggable logging implementations:- Development:
INFOlevel - Production:
WARNlevel
Observability
Track LLM interactions and application traces:Custom ID Generation
Override default UUID generation:Event System
Mastra includes a pub/sub system for event-driven communication:Working with Stored Agents
Mastra automatically manages stored agents from the database:Best Practices
Initialize storage early
Initialize storage early
Configure storage in the Mastra constructor so all components can access it:
Use dependency injection
Use dependency injection
Let Mastra inject dependencies instead of passing them manually:
Register components in constructor
Register components in constructor
Register all components in the Mastra constructor for proper initialization order: