mastra-project-setup
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Create and configure a Mastra project: scaffold with create-mastra (default starter, templates, empty scaffold, Mastra Gateway), install manually with @mastra/core, mastra, zod, and TypeScript, set up package.json scripts and an ES2022 bundler tsconfig, lay out src/mastra, write the central new Mastra({...}) instance with agents, workflows, tools, storage, logger, server, and bundler options, load env files, and run mastra dev with Studio on localhost:4111. Use this skill whenever the user starts a new Mastra project, asks how a Mastra project is structured, wires something into src/mastra/index.ts, hits module resolution or CommonJS errors, cannot start mastra dev or Studio, or wants to use a template. Triggers on create-mastra, npm create mastra, mastra init, mastra dev, mastra build, mastra start, Studio, src/mastra/index.ts, new Mastra, @mastra/core, Mastra class, tsconfig moduleResolution bundler, .env, templates, project structure. Also triggers on set up Mastra or start a Mastra app from scratch.
- Reads credentials
- Installs packages
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 92
Add "scripts": { "dev": "mastra dev", "build": "mastra build" } and "type": "module" to package.json.
- Very specific: line 112
Create one Mastra instance and export it as mastra.
- Very specific: line 134
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed Mastra types.
- Very specific: line 137
Set env vars: Put provider keys in .env and the names in .env.example.
- Very specific: line 178
Committing
.envormastra.db: Add both to .gitignore; commit .env.example.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Project Setup
Get a working Mastra project on disk, configured correctly, and running in Studio. Covers the CLI scaffold, a manual install, the folder layout, the central Mastra instance, and the local dev loop.
When to use
- Starting a new standalone Mastra project (CLI or manual)
- Choosing between the default starter, a template, and an empty scaffold
- Writing or fixing
package.jsonscripts andtsconfig.json - Organizing
src/mastra/and registering primitives insrc/mastra/index.ts - Configuring top-level
new Mastra({...})options: storage, logger, server, bundler, gateways - Setting up
.envfiles and model provider keys - Running
mastra dev, opening Studio, and reaching the local API - Diagnosing "module not found", CommonJS, or "Studio won't start" problems
When NOT to use
- Adding Mastra to an existing Next.js, Vite, Express, Hono, SvelteKit, or Astro app (use
mastra-framework-integration) - Writing agents (use
mastra-agents), tools (usemastra-tools), or workflows (usemastra-workflows) - File-based agents under
src/mastra/agents/<name>/(usemastra-file-based-agents) - Picking a model ID or provider env var (use
mastra-models) - Choosing a storage adapter in depth (use
mastra-storage) - Server routes, middleware, and request context (use
mastra-server) mastra buildfor a host, deployers, andmastra deploy(usemastra-deploymentandmastra-cli-api)- Studio features beyond opening it (use
mastra-studio-editor) - Upgrading an old project (use
mastra-migrations) - Deciding what to build first (use
mastra-guideormastra-build-playbook)
Required inputs
- New project or existing app (existing apps go to
mastra-framework-integration) - Package manager (npm, pnpm, Yarn, Bun) and Node.js version
- Model provider and API key, or the Mastra Gateway
- Whether the user wants a full starter, a specific template, or an empty scaffold
- Whether data must persist (storage) from day one
The framework: scaffold, configure, run
Option A: CLI scaffold
npx create-mastra@latest my-mastra-project --llm anthropic # default starter, no prompts
npx create-mastra@latest my-mastra-project --template agent-harness
npx create-mastra@latest my-empty-project --empty # no agents, no providerThe default starter is an agent harness with workspace tools, memory, task tracking, web access, schedules, storage, and observability. --llm accepts openai, anthropic, google, xai, or mastra. The CLI also installs Mastra skills for detected coding assistants and makes an initial Git commit (--no-skills, --no-git to skip).
Option B: manual install
mkdir my-first-agent && cd my-first-agent && npm init -y
npm install -D typescript @types/node mastra@latest
npm install @mastra/core@latest zod@^4{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "bundler",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"types": ["node"],
"noEmit": true,
"outDir": "dist"
},
"include": ["src/**/*"]
}Add "scripts": { "dev": "mastra dev", "build": "mastra build" } and "type": "module" to package.json.
The central instance
// src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { LibSQLStore } from '@mastra/libsql'
import { PinoLogger } from '@mastra/loggers'
import { weatherAgent } from './agents/weather-agent'
import { weatherWorkflow } from './workflows/weather-workflow'
export const mastra = new Mastra({
agents: { weatherAgent },
workflows: { weatherWorkflow },
storage: new LibSQLStore({ id: 'mastra-storage', url: 'file:./mastra.db' }),
logger: new PinoLogger({ name: 'Mastra', level: 'info' }),
})Create one Mastra instance and export it as mastra. Registration gives each primitive shared storage, logging, tracing, Studio visibility, and HTTP endpoints.
Layout
src/mastra/
agents/ weather-agent.ts
tools/ weather-tool.ts
workflows/ weather-workflow.ts
scorers/ (optional)
mcp/ (optional custom MCP servers)
public/ (copied into .mastra/output on build)
index.ts the Mastra instance
.env.example
package.json
tsconfig.jsonWorkflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installedMastratypes. Otherwise fetchhttps://mastra.ai/reference/cli/create-mastra.md,https://mastra.ai/reference/manual-install.md, andhttps://mastra.ai/reference/configuration.md. Trust these over memory. - Pick the path. Human at a terminal:
create-mastra. Coding agent without an interactive TTY: pass flags (--llm,--template,--empty) or do the manual install. Existing app: stop and usemastra-framework-integration(ormastra initinside the app). - Configure the project. ESM (
"type": "module"), the tsconfig above, anddev/buildscripts. - Set env vars. Put provider keys in
.envand the names in.env.example. Look up names withmastra-models. - Add one primitive. A tool and an agent are enough to prove the setup (see examples).
- Register it in
src/mastra/index.ts. Addstoragenow if memory, workflows that suspend, or traces must persist. - Run.
npm run dev, then openhttp://localhost:4111(Studio) andhttp://localhost:4111/api(REST). For scripts and CI, callmastra.getAgentById(...).generate(...)directly instead of starting Studio. - Validate.
npx tsc --noEmitandnpx mastra lint. Thennpm run build.
Deep dive: Mastra class options
| Option | Purpose |
agents, workflows, tools | Registries keyed by name. Fetch with getAgentById, getWorkflow, getTool. |
storage | One MastraCompositeStore for memory, workflow snapshots, traces. |
vectors | Vector stores for RAG and semantic recall. |
memory | Named memory instances for stored agents. |
logger | PinoLogger from @mastra/loggers, or false. Default console logger (INFO in dev, WARN in prod). |
observability | Tracing config (see mastra-observability). |
server | port (4111), host (localhost), timeout (180000), cors, apiRoutes, middleware, auth, studioBase. |
bundler | externals (default true), sourcemap, minify, transpilePackages. |
gateways | Custom or Azure model gateways (see mastra-models). |
scorers, classifiers, processors, mcpServers, deployer, pubsub, workspace | Feature-specific registries. See the owning skill. |
recovery | { workflows: 'auto' | 'off', durableAgents, workflowConcurrency }. |
idGenerator, environment | Custom IDs; environment tag for observability. |
PORT and MASTRA_HOST env vars override server.port and server.host. Full list in references/api.md.
Failure patterns
- CommonJS settings (
"module": "commonjs","moduleResolution": "node"). Imports fail to resolve. UseES2022plusbundler. - Running
create-mastrainteractively from an agent. Prompts hang. Pass the project name and--llm,--template, or--empty, or install manually. - Combining
--emptywith--template, or--llmwith a template. The CLI rejects it.--llmand--llm-api-keyapply to the default starter only. - Project name with capitals or an existing directory. Rejected before files are written. Use a lowercase, new directory name.
- Several
new Mastra()instances. Registries and storage split. Export onemastrafromsrc/mastra/index.ts. - Primitive defined but not registered. It is missing from Studio and the API. Add it to the right registry.
- Plain object instead of
createTool(). Tools silently do nothing. Always usecreateTool. - Installing
@ai-sdk/*packages for the model. Not needed. Use router strings like'openai/gpt-6.1-sol'. .envnot loaded in a custom script.mastra devloads.env.development,.env.local, and.env. Plain Node scripts need your own loader or--env-file.- Port 4111 taken. Studio fails to start. Free the port or set
PORT. - Committing
.envormastra.db. Add both to.gitignore; commit.env.example. - File-based agents missing when the app imports
mastradirectly. Discovery only runs undermastra devandmastra build.
Output format
A finished setup contains:
package.jsonwith"type": "module",devandbuildscripts, and@mastra/core,mastra,zodinstalledtsconfig.jsonwith ES2022 andbundlerresolutionsrc/mastra/index.tsexporting onemastrainstance with every primitive registered- At least one working agent or workflow
.env.examplelisting every required variable, and.gitignorecovering.env,.mastra, and local DB files- Evidence:
mastra devrunning with Studio onhttp://localhost:4111, or a script call that returns a response, plus a cleantsc --noEmit
Reference files
references/api.md-create-mastraflags,mastraCLI dev commands and env vars, everyMastraconstructor option with defaults, instance accessors, server and bundler options.references/examples.md- Manual install end to end, production-leaningindex.ts, template setup, empty scaffold plus first agent.references/checklist.md- New project checklist.
Live docs
- https://mastra.ai/docs
- https://mastra.ai/docs/develop
- https://mastra.ai/reference/cli/create-mastra
- https://mastra.ai/reference/manual-install
- https://mastra.ai/reference/project-structure
- https://mastra.ai/reference/configuration
- https://mastra.ai/reference/core/mastra-class
- https://mastra.ai/reference/templates/overview
- https://mastra.ai/reference/cli/mastra
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

