Mastra Agents
Build stateful AI agents with memory, tools and MCP, using any LLM
Mastra agents use any LLM to reason, decide and act on open-ended tasks. Every agent comes with memory, tool calling, MCP, logging, tracing and eval primitives built in. Build and test agents in a local playground, then deploy to production with the same configuration.
Built-in agent primitives
When you need agents that retain context, call tools and evaluate their own output, Mastra gives you every primitive built in. Tool calling connects agents to external APIs and services. Memory retains conversation history, user data and relevant context across interactions. Logging, tracing and evals surface what happened and why.
Orchestrate agent networks
Single agents handle open-ended tasks well. Mastra lets you compose agents with workflows for increased reliability, or assemble agent networks where agents collaborate on more complex problems. Specify execution paths for increased control or delegate to a team of agents for increased power.
Context and guardrails
Mastra gives you precise control over what agents know and what they can do. Dynamically inject context based on the request, configure access from user identity and enforce guardrails against prompt injection and data leaks. Suspend and resume agent execution for human review.
Frequently asked questions
What LLMs does Mastra support for agents?
Mastra supports more than 600 models through its built-in model router. Set your provider API key as an environment variable and Mastra auto-detects it. Mastra agents work with any LLM that supports the model router, including OpenAI, Anthropic and other major providers.
How do Mastra supervisor agents work?
Mastra agents support a supervisor pattern where a parent agent registers subagents and delegates tasks to them. The supervisor can control delegations with hooks, filter context and monitor progress. Registering subagents in a parent agent makes that agent a supervisor automatically.
How do Mastra agents use tools and MCP?
Mastra agents use tools to go beyond language generation, enabling structured interactions with external APIs and services. Tools allow agents to access data and perform clearly defined operations in a reliable, repeatable way. Mastra also supports MCPClient to connect agents to any MCP-compatible tool server.
When should I use agent networks instead of a single agent?
Mastra agent networks assemble a team of agents for increased power on complex problems. Use a single Mastra agent for open-ended tasks where one model can reason and act effectively. Use agent networks when tasks require parallel execution or capabilities that benefit from coordinated agents.
How do Mastra agents handle security and prompt injection?
Mastra agents support guardrails to guard against prompt injection and prevent data leaks. Processors let you sanitize inputs and outputs at the agent level. Runtime context and auth let you configure agent access dynamically from user identity.