Welcome to Mastra
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- DEVELOPMENT.md
- CLAUDE.md
- packages/agent-builder/src/defaults.ts
- packages/agent-builder/src/workflows/template-builder/template-builder.ts
- packages/agent-builder/src/workflows/workflow-builder/prompts.ts
- templates/template-docs-chatbot/README.md
- templates/template-ad-copy-from-content/README.md
- templates/template-csv-to-questions/README.md
- CONTRIBUTING.md
- packages/mcp-docs-server/README.md
Mastra is a framework for building AI-powered applications and agents using a modern TypeScript stack. It provides a comprehensive suite of tools to develop, prototype, and deploy production-ready AI applications. The framework is designed to integrate with various frontend and backend frameworks like React, Next.js, and Node.js, or it can be deployed as a standalone server.
The core philosophy of Mastra is to provide a modular, extensible architecture built around established AI patterns. It enables developers to build everything from simple chatbots to complex, autonomous agents that can reason, use tools, and perform multi-step tasks. With features like model routing, context management, and production-focused tools for evaluation and observability, Mastra aims to be the easiest way to build, tune, and scale reliable AI products.
Sources: README.md:11-16
Core Features
Mastra offers a range of features designed for building robust AI applications.
| Feature | Description |
|---|---|
| Model Routing | Connect to over 40 language model providers (OpenAI, Anthropic, Gemini, etc.) through a single, standardized interface. |
| Agents | Build autonomous agents that use LLMs and tools to solve open-ended tasks. Agents can reason, decide which tools to use, and iterate until a goal is met. |
| Workflows | Orchestrate complex, multi-step processes using a graph-based workflow engine with intuitive syntax for control flow (.then(), .branch(), .parallel()). |
| Human-in-the-loop | Suspend agents or workflows to await user input or approval before resuming. Execution state is stored, allowing for indefinite pauses. |
| Context Management | Provide agents with relevant context through conversation history, data retrieval (RAG), working memory, and semantic memory. |
| Integrations | Bundle agents and workflows into existing applications (React, Next.js, Node.js) or deploy them as standalone endpoints. |
| MCP Servers | Author Model Context Protocol (MCP) servers to expose agents and tools, making them accessible to any system that supports the protocol. |
| Production Essentials | Built-in evaluation and observability tools to observe, measure, and refine agent performance continuously. |
Sources: README.md:18-45
Architecture Overview
Mastra is a modular AI framework organized as a monorepo. Its architecture is centered around a core orchestration layer with pluggable components for storage, deployment, and other functionalities.
Sources: CLAUDE.md:31, DEVELOPMENT.md:10
Repository Structure
The diagram below illustrates the high-level structure of the Mastra monorepo.
Sources: DEVELOPMENT.md:12-32, CLAUDE.md:38-46
Key Architectural Patterns
Mastra's design is based on several key patterns that promote modularity and extensibility.
| Pattern | Description |
|---|---|
| Dependency Injection | Components register with a central Mastra instance, which manages dependencies. |
| Plugin Architecture | Storage, vector stores, memory systems, and deployers are all pluggable, allowing for flexible configurations. |
| Runtime Context | Request-scoped context is propagated to enable dynamic configuration and dependency injection during execution. |
| Message List Abstraction | A unified message handling system supports various formats across different model providers. |
Sources: CLAUDE.md:48-55
Key Components
Agents
Agents are the primary abstraction for AI interaction. They are configured with instructions, a language model, a set of tools, and a memory system. They can reason about goals, select appropriate tools, and iterate on tasks.
The AgentBuilder provides a specialized "Mastra Expert Agent" with default instructions to build and modify Mastra applications. This agent follows a structured MASTRA method: Understand, Plan, Build, Validate.
Sources: README.md:22-24, packages/agent-builder/src/defaults.ts:9-25
Workflows
Workflows provide explicit control over multi-step processes. They are defined as a graph of steps with typed inputs and outputs. Mastra's workflow engine supports sequential, parallel, and conditional execution, as well as loops.
A key feature is the ability to suspend a workflow to wait for external input (e.g., human approval) and then resume it. This is essential for building human-in-the-loop systems.
The following diagram shows the flow of the agentBuilderTemplateWorkflow, which automates the process of merging a template into a project.
This diagram illustrates the sequential and parallel steps involved in the template merging workflow.
Sources: README.md:26-28, packages/agent-builder/src/workflows/template-builder/template-builder.ts:1100-1132, [packages/agent-builder/src/workflows/workflow-builder/prompts.ts:12-23]
Tools
Tools are functions that agents and workflows can execute. The framework includes a system for dynamic tool composition from multiple sources. The AgentBuilder comes with a rich set of default tools for file operations, project management, and code validation.
A selection of default tools available to the AgentBuilder is listed below:
| Tool ID | Description |
|---|---|
read-file | Read contents of a file with optional line range selection. |
write-file | Write content to a file, with options for creating directories. |
list-directory | List contents of a directory with filtering and metadata options. |
execute-command | Execute shell commands with error handling and output capture. |
task-manager | Create and manage structured task lists for coding sessions. |
multi-edit | Perform multiple search-replace operations on files atomically. |
replace-lines | Replace specific line ranges in files with new content. |
validate-code | Validates code using a hybrid approach (syntax, types, lint). |
manage-project | Handles project creation and dependency management. |
manage-server | Manages the Mastra development server (start, stop, status). |
http-request | Makes HTTP requests to test APIs. |
web-search | Searches the web for information. |
Sources: CLAUDE.md:57-59, packages/agent-builder/src/defaults.ts:318-724
Memory
The memory system allows agents to persist and recall information across sessions. It supports:
- Conversation History: Storing and retrieving recent messages.
- Vector Storage: For semantic recall of relevant past interactions.
- Working Memory: Maintains context within a single conversation thread.
Sources: DEVELOPMENT.md:24-25, README.md:33-36
Model Context Protocol (MCP)
MCP is a standardized protocol for communication with AI models, enabling tool usage and structured responses across different providers. Mastra includes an MCP server that can expose documentation, code examples, and changelogs as tools for an AI assistant. This allows developers to build documentation-aware agents.
Sources: README.md:40-42, packages/mcp-docs-server/README.md:3-9
Getting Started
The recommended way to start a new Mastra project is by using the CLI:
npm create mastra@latest
This command will guide you through setting up a new project.
For developers wanting to contribute to the framework itself, the setup involves cloning the repository, enabling corepack to use the correct pnpm version, and running pnpm run setup to install dependencies and build initial packages.
Sources: README.md:49-53, DEVELOPMENT.md:37-50
Templates and Examples
Mastra provides a variety of templates and examples to demonstrate its capabilities and help developers get started quickly.
- Documentation Chatbot: A monorepo template with a separate MCP server for documentation tools and an agent that consumes them.
- Ad Copy Generation: Generates ad copy and images from text, PDFs, or URLs, demonstrating content extraction and image generation.
- CSV to Questions: Shows how to handle large datasets by first summarizing a CSV with a large-context model before generating questions, thus avoiding token limits.
Sources: templates/template-docs-chatbot/README.md:3-7, templates/template-ad-copy-from-content/README.md:3-9, templates/template-csv-to-questions/README.md:3-8
Contributing and Support
Contributions to Mastra are welcome. The process typically involves opening a GitHub issue to discuss a bug or feature, followed by a pull request. The DEVELOPMENT.md file contains detailed instructions for setting up a local development environment.
For support and community discussion, you can join the Mastra Discord.
Sources: CONTRIBUTING.md:3-5, README.md:68-71