Mastra Framework Setup
This skill helps you set up and configure Mastra, a comprehensive TypeScript framework for building AI-powered applications and agents.
What This Skill Does
Sets up the Mastra framework (`@mastra/core`) in your project, including:
Package installation and configurationCore component initialization (Mastra class, agents, workflows, tools)Memory and storage setupOptional observability, MCP, vector, and voice capabilitiesIntegration examples and best practicesInstructions
When the user requests Mastra setup, follow these steps:
1. Assess Project Context
First, understand the user's project:
Check if this is a new or existing projectIdentify the framework (Next.js, Node.js, React, standalone, etc.)Ask about specific Mastra components needed using AskUserQuestion if unclear2. Install Dependencies
Install the core package and recommended dependencies:
```bash
npm install @mastra/core
```
Optionally install additional packages based on requirements:
`@mastra/observability` - For advanced tracing and monitoringVector store packages (e.g., `@mastra/pinecone`, `@mastra/qdrant`)Database adapters (e.g., `@mastra/postgresql`, `@mastra/libsql`)3. Create Mastra Configuration
Create a central Mastra configuration file (e.g., `src/mastra/index.ts` or `mastra.config.ts`):
```typescript
import { Mastra } from '@mastra/core';
export const mastra = new Mastra({
// Configure components as needed
});
```
4. Set Up Core Components
Based on user requirements, configure:
**Agents**: If the user needs AI agents that can use tools and maintain conversations
Create agent definitions with instructions, tools, and model configurationReference: https://mastra.ai/docs/agents/overview**Workflows**: If the user needs to chain or orchestrate multiple AI operations
Define workflow graphs with steps, branching, and error handlingReference: https://mastra.ai/docs/workflows/overview**Tools**: If agents need to interact with external systems or APIs
Create tool definitions with schemas and execution functionsReference: https://mastra.ai/docs/tools-mcp/overview**Memory**: If agents need conversation history or semantic recall
Configure thread-based memory storageReference: https://mastra.ai/docs/memory/overview**Storage**: If persisting data beyond in-memory storage
Set up database adapter (PostgreSQL, LibSQL, MongoDB, etc.)Reference: https://mastra.ai/docs/server-db/storage5. Add Environment Variables
Create or update `.env.local` with necessary API keys:
```env
LLM Provider (OpenAI, Anthropic, etc.)
OPENAI_API_KEY=your_key_here
or
ANTHROPIC_API_KEY=your_key_here
Vector Store (if using RAG)
PINECONE_API_KEY=your_key_here
Database (if using persistence)
DATABASE_URL=your_connection_string
```
6. Create Example Implementation
Provide a working example based on the user's use case:
Simple agent with toolsMulti-step workflowRAG-enabled agent with vector searchFull-stack integration with Next.js API routes7. Documentation Links
Point the user to relevant documentation:
Getting Started: https://mastra.ai/docs/getting-started/installationAPI Reference: https://mastra.ai/referenceExamples: https://mastra.ai/docs/examplesImportant Notes
Mastra is designed to work with modern TypeScript projectsThe `@mastra/core` package includes agents, workflows, tools, and memoryAdditional features like advanced observability require separate packagesMastra can be deployed as a standalone server or integrated into existing frameworksAlways configure environment variables for API keys and credentialsUse the official documentation for detailed component configurationExample Usage Scenarios
1. "Set up Mastra with a simple AI agent that can search the web"
2. "Initialize Mastra for a Next.js project with RAG capabilities"
3. "Create a Mastra workflow that processes documents in parallel"
4. "Configure Mastra with PostgreSQL storage and conversation memory"
5. "Set up Mastra agents with MCP tool integration"
Constraints
Requires Node.js and npm/yarn/pnpmTypeScript recommended but not strictly requiredLLM provider API keys needed for agent functionalityDatabase required for persistent storage featuresVector store needed for semantic search/RAG features