Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build Mastra AI agents and workflows with guidance on current API lookup, tools, memory, and RAG patterns.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/create-mastra.md
1# Create Mastra Reference23Complete guide for creating new Mastra projects. Includes both quickstart CLI method and detailed manual installation.45**Official documentation: [mastra.ai/docs](https://mastra.ai/docs)**67## Get started89Ask: **"How would you like to create your Mastra project?"**10111. **Quick Setup**: Copy and run: `npm create mastra@latest`122. **Guided Setup**: I walk you through each step, you approve commands133. **Automatic Setup**: I create everything, just give me your API key1415> **For AI agents:** The CLI is interactive. Use **Automatic Setup** to create files using the steps in "Automatic Setup / Manual Installation" below.1617## Prerequisites1819- An API key from a supported model provider (OpenAI, Anthropic, Google, etc.)2021## Quick Setup (user runs CLI)2223Create a new Mastra project with one command:2425```bash26npm create mastra@latest27```2829**Other package managers:**3031```bash32pnpm create mastra@latest33yarn create mastra@latest34bun create mastra@latest35```3637## CLI flags3839**Skip the example agent:**4041```bash42npm create mastra@latest --no-example43```4445**Use a specific template:**4647```bash48npm create mastra@latest --template <template-name>49```5051## Automatic setup / manual installation5253**Use this for automatic setup** (AI creates all files) or when you prefer manual control.5455Follow these steps to create a complete Mastra project:5657### Step 1: Create project directory5859```bash60mkdir my-first-agent && cd my-first-agent61npm init -y62```6364### Step 2: Install dependencies6566```bash67npm install -D typescript @types/node mastra@latest68npm install @mastra/core@latest zod@^469```7071### Step 3: Configure package scripts7273Add to `package.json`:7475```json76{77"scripts": {78"dev": "mastra dev",79"build": "mastra build"80}81}82```8384### Step 4: Configure TypeScript8586Create `tsconfig.json`:8788```json89{90"compilerOptions": {91"target": "ES2022",92"module": "ES2022",93"moduleResolution": "bundler",94"esModuleInterop": true,95"forceConsistentCasingInFileNames": true,96"strict": true,97"skipLibCheck": true,98"noEmit": true,99"outDir": "dist"100},101"include": ["src/**/*"]102}103```104105**Important:** Mastra requires `"module": "ES2022"` and `"moduleResolution": "bundler"`. CommonJS will cause errors.106107### Step 5: Create environment file108109Create `.env` with your API key:110111```env112GOOGLE_GENERATIVE_AI_API_KEY=<your-api-key>113```114115Or use `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, etc.116117### Step 6: Create weather tool118119Create `src/mastra/tools/weather-tool.ts`:120121```typescript122import { createTool } from "@mastra/core/tools";123import { z } from "zod";124125export const weatherTool = createTool({126id: "get-weather",127description: "Get current weather for a location",128inputSchema: z.object({129location: z.string().describe("City name"),130}),131outputSchema: z.object({132output: z.string(),133}),134execute: async () => {135return { output: "The weather is sunny" };136},137});138```139140### Step 7: Create weather agent141142Create `src/mastra/agents/weather-agent.ts`:143144```typescript145import { Agent } from "@mastra/core/agent";146import { weatherTool } from "../tools/weather-tool";147148export const weatherAgent = new Agent({149id: "weather-agent",150name: "Weather Agent",151instructions: `152You are a helpful weather assistant that provides accurate weather information.153154Your primary function is to help users get weather details for specific locations. When responding:155- Always ask for a location if none is provided156- If the location name isn't in English, please translate it157- If giving a location with multiple parts (e.g. "New York, NY"), use the most relevant part (e.g. "New York")158- Include relevant details like humidity, wind conditions, and precipitation159- Keep responses concise but informative160161Use the weatherTool to fetch current weather data.162`,163model: "google/gemini-2.5-pro",164tools: { weatherTool },165});166```167168**Note:** Model format is `"provider/model-name"`. Examples:169170- `"google/gemini-2.5-pro"`171- `"openai/gpt-5.4"`172- `"anthropic/claude-sonnet-4-5"`173174### Step 8: Create mastra entry point175176Create `src/mastra/index.ts`:177178```typescript179import { Mastra } from "@mastra/core";180import { weatherAgent } from "./agents/weather-agent";181182export const mastra = new Mastra({183agents: { weatherAgent },184});185```186187### Step 9: Launch Mastra Studio188189Launch the development server:190191```bash192npm run dev193```194195Access Studio at `http://localhost:4111` to test your agent.196197## Next steps198199After creating your project with `create mastra`:200201- **Customize the example agent** in `src/mastra/agents/weather-agent.ts`202- **Add new agents** - see [Agents documentation](https://mastra.ai/docs/agents/overview)203- **Create workflows** - see [Workflows documentation](https://mastra.ai/docs/workflows/overview)204- **Add more tools** to extend agent capabilities205- **Integrate into your app** - see framework guides at [mastra.ai/docs](https://mastra.ai/docs)206207## Troubleshooting208209| Issue | Solution |210| ------------------ | ------------------------------------------------------------------------------------ |211| API key not found | Make sure your `.env` file has the correct key |212| Studio won't start | Check that port 4111 is available |213| CommonJS errors | Ensure `tsconfig.json` uses `"module": "ES2022"` and `"moduleResolution": "bundler"` |214| Command not found | Ensure you're using Node.js 20+ |215216## Resources217218- [Docs](https://mastra.ai/docs)219- [Installation](https://mastra.ai/docs/getting-started/installation)220- [Agents](https://mastra.ai/docs/agents/overview)221- [Workflows](https://mastra.ai/docs/workflows/overview)222- [GitHub](https://github.com/mastra-ai/mastra)223