Mastra TypeScript AI Agents Framework: Agents, Workflows, and Memory in One Place
Mastra is the TypeScript framework for production agents: agents, typed workflows, working memory, evals and RAG in one place. This guide covers each primitive.
By Galactic News Editorial · Published 2026-11-27T23:22:13+00:00 · Updated 2026-11-27T23:22:13+00:00
Mastra TypeScript AI Agents Framework: Agents, Workflows, and Memory in One Place
Mastra TypeScript AI agents framework is built for one audience: TypeScript teams that want production agents without assembling the plumbing themselves. It is open source under the Apache 2.0 licence. It sits on top of the Vercel AI SDK. The SDK handles the model call. Mastra adds the application layer: agents, tools, workflows, memory, evals and RAG. This guide explains each piece and how they fit together.
The problem Mastra solves is familiar. A single chat completion is easy. A dependable agent is not. Memory needs a database. Workflows need state machines. Evals need a harness. Observability needs tracing. Teams either build all of that or ship a chatbot that forgets everything between sessions. Mastra ships the pieces as first-class primitives. A TypeScript developer can go from scaffolding to a working agent with memory in hours.
Agents: the core building block
An agent wraps a language model with instructions, tools and optional memory. The API is small. You create an agent with a name, instructions, a model and a set of tools. The model decides when to call a tool and when to answer. You register agents on a central Mastra instance. That instance wires everything together and exposes a local dev studio.
Tools are functions the model can call. You define them with createTool. Each tool needs an id, a description, an input schema and an execute function. The schema is usually written with Zod. The description matters: it is what the model reads when deciding whether to call the tool. Write it like documentation, not a label.
Models come through the Vercel AI SDK. Mastra supports dozens of providers through that integration, including OpenAI, Anthropic and Google. You pick the provider and model in the agent definition. Switching providers means changing the model config, not rewriting the agent.
Getting started is quick. Run npx create-mastra@latest my-app to scaffold a project. The scaffold creates a src/mastra/ directory with an example agent, tool and workflow. Add your provider key to .env. Run mastra dev for a local server with a visual Studio UI, auto-generated Swagger docs and an OpenAPI spec.
Workflows: deterministic steps with typed data
Agents decide their own order. Workflows do the opposite. A workflow is a graph of steps that you decide, with typed data between one step and the next. The official docs give the rule in one line: the agent is for open-ended tasks whose steps you do not know, the workflow is for processes defined in advance with a precise order.
Each step declares an input schema and an output schema, usually with Zod. The engine checks that the data leaving one step is what the next step expects. Steps chain with .then() for sequence, .branch() for conditionals and .parallel() for fan-out. The workflow is finalised with .commit() and started with run.start().
Workflows can suspend mid-execution. A run can pause for a human approval step, an API webhook or rate-limit throttling. It resumes later from the exact snapshot that was saved. That snapshot lives in the workflows storage domain. Use a reliable persistent database for it, so a suspended run survives a server restart.
Here is the shape of a two-step workflow. It normalises a text, then counts the words. The pattern scales to real pipelines: extract, analyse, summarise.
import { createStep, createWorkflow } from '@mastra/core/workflows';
import { z } from 'zod';
const normalizeStep = createStep({
id: 'normalize',
inputSchema: z.object({ text: z.string() }),
outputSchema: z.object({ text: z.string() }),
execute: async ({ inputData }) => ({
text: inputData.text.trim().toLowerCase(),
}),
});
const countStep = createStep({
id: 'count',
inputSchema: z.object({ text: z.string() }),
outputSchema: z.object({ words: z.number() }),
execute: async ({ inputData }) => ({
words: inputData.text.split(/\s+/).length,
}),
});
export const countWords = createWorkflow({
id: 'count-words',
inputSchema: z.object({ text: z.string() }),
outputSchema: z.object({ words: z.number() }),
})
.then(normalizeStep)
.then(countStep)
.commit();Memory: threads, working memory and recall
Memory is where most agent frameworks leave you to build your own layer. Mastra ships it. Memory covers message history, threads, resources and working memory. Threads are conversation sessions. Resources are user identities. Working memory is a block of Markdown text the agent updates over time to store continuously relevant information, such as user preferences.
Working memory has two scopes. Resource-scoped memory is the default. It persists across all conversation threads for the same user. Thread-scoped memory is isolated per conversation. Switching scopes means the agent will not see memory from the other scope. Pick the scope when you configure the memory, not later.
To enable it, pass a Memory instance to the agent with working memory turned on. Then call generate with a thread id and a resource id. The agent remembers context across calls in the same thread. With resource scope, it remembers facts across threads for the same user.
Storage backs all of this. Mastra organises storage into domains: memory, workflows, observability, evals and more. Each domain can use a different adapter. A common setup uses libSQL for memory and PostgreSQL for workflows. Configure storage at the Mastra instance level to share it, or at the agent level to override it for one agent.
When to use the Mastra TypeScript AI agents framework
| Situation | Verdict | Reason |
|---|---|---|
| TypeScript or Next.js team building agents | Strong fit | TypeScript-first API, Vercel AI SDK integration |
| Deterministic multi-step process | Use workflows | Typed steps, branching, suspend and resume |
| Open-ended task with unknown steps | Use agents | Model decides tool calls and order |
| Python-only team | Weak fit | Framework is TypeScript-first |
| Single chat completion, no memory | Overkill | Use the Vercel AI SDK directly |
Watch the version drift. Mastra moves fast. One careful review in November 2026 found the package at version 1.83.0, requiring Node 22.13 or later. It also found tutorials using names that no longer exist. The current import is @mastra/core/agent, not mastra. The package @mastra/sdk does not exist on the registry. Before copying any example, check that the names resolve against the current docs. The official docs are the source of truth, not a tutorial from six months ago.
MCP support is built in on both sides. Agents can load tools from remote MCP servers. Your own agents and tools can be exposed as an MCP server for other clients. That matters as the MCP ecosystem grows, because it turns your agent into a tool other systems can call.
Bottom line
Mastra gives TypeScript teams five primitives in one framework: agents, tools, workflows, memory and evals. Agents handle open-ended work. Workflows handle defined processes with typed steps and suspend-and-resume. Memory keeps context across sessions. Start with npm create mastra@latest, build one agent with one tool, add working memory, then add a workflow for the deterministic part. Check every example against the current docs before you trust it. That is the whole path from scaffold to production.