What you need
Node.js 18+ and npm installed.
A Next.js or Node.js project (or start fresh).
An API key for your model provider.
Step 1: Install Mastra
Run `npm install mastra` in your project directory.
For a new project: `npx create-mastra` to scaffold a Mastra project with examples.
Set your API key as an environment variable: `OPENAI_API_KEY=sk-your-key`.
Step 2: Create your first agent
Define an agent with TypeScript types for structured outputs.
Example: `const researchAgent = new Agent({ name: 'researcher', model: openai('gpt-4'), instructions: 'You are a research specialist...' })`.
The typed output ensures your application gets predictable, validated results.
Step 3: Add MCP tools
Mastra has first-class MCP support. Import MCP tools and assign them to agents.
Tools can be web search, database queries, API calls, or any MCP-compatible service.
Each tool has typed input/output schemas for type safety.
Step 4: Add memory and context
Mastra supports persistent memory using the Mastra Memory API.
Configure memory storage: in-memory for development, Postgres or Redis for production.
Memory enables agents to maintain context across conversations and sessions.
Step 5: Deploy your agent
Mastra agents can be deployed as Next.js API routes, serverless functions, or standalone Node services.
Example Next.js route: `export async function POST(req) { const result = await agent.run(await req.json()); return Response.json(result); }`.
Monitor agent usage and performance with Mastra's built-in observability.
FAQ
Is Mastra production-ready?
Yes. Mastra v1.0 shipped January 2026. It is used by Replit, SoftBank, and PayPal in production.
Can Mastra use local LLMs?
Yes. Mastra supports any model provider with an OpenAI-compatible API, including Ollama and LocalAI.
Build Mastra Agents
- Install Mastra: npm install mastra
- Create a typed agent with instructions
- Add MCP tools with typed schemas
- Configure memory persistence
- Deploy as API route or serverless function
- Monitor with built-in observability
Ready to see the full catalog?
Browse all 42 agent-ready business kits. Each includes pre-configured agent roles, prompts, workflows, install steps, and approval gates.
Shop all business kits