CLI Reference
Relevant source files
The following files were used as context for generating this wiki page:The Mastra Command Line Interface (CLI) is a tool for creating, running, and managing Mastra projects. It provides a streamlined developer experience, from scaffolding a new application to running development servers and managing project components. The CLI is designed to support both interactive setup for new users and argument-based execution for automated environments.
The CLI is the recommended entry point for starting with Mastra. It handles project creation, dependency installation, and boilerplate generation for core Mastra concepts like agents, tools, and workflows.
Sources: packages/cli/package.json:5, README.md:46-48, DEVELOPMENT.md:10-14
Getting Started
The recommended way to start a new Mastra project is by using the create-mastra package, which is a wrapper around the CLI's create command.
npm create mastra@latest
This command will initiate an interactive prompt to guide you through setting up a new project.
For developers contributing to the Mastra monorepo, the environment is set up using pnpm:
# 1. Clone the repository
git clone https://github.com/mastra-ai/mastra.git
cd mastra
# 2. Enable corepack
corepack enable
# 3. Install dependencies and build the CLI
pnpm run setup
Sources: README.md:46-48, DEVELOPMENT.md:25-35, packages/cli/src/commands/create/create.ts:16-29
Commands
The Mastra CLI is exposed as the mastra binary. While many operations are wrapped in pnpm scripts for convenience within the monorepo, the core commands provide the main functionality.
Sources: packages/cli/package.json:9, DEVELOPMENT.md
mastra create
This is the primary command for scaffolding a new Mastra project. It can be run interactively or with command-line arguments.
The command has two main operational modes:
- Interactive Setup: If run without sufficient arguments, it launches an interactive wizard to configure the new project, including selecting an LLM provider and example components.
- Template-based Setup: If a template is specified via the
--templateflag, it clones and intelligently merges the template into the new project.
Sources: packages/cli/src/commands/create/create.ts:31-41, 195-238
The following diagram illustrates the high-level logic of the create command.
Arguments
| Argument | Description | Type | Default |
|---|---|---|---|
projectName | The name of the project directory to create. | string | Interactive prompt |
--components | A list of Mastra components to initialize. | Component[] | [] |
--llmProvider | The default LLM provider to configure. | LLMProvider | openai |
--addExample | Whether to add example code. | boolean | false |
--template | The name of a template or a GitHub URL to a Mastra project. | string | undefined |
--llmApiKey | The API key for the selected LLM provider. | string | undefined |
--mcpServer | Configure the editor with the MCP docs server. | Editor | undefined |
Sources: packages/cli/src/commands/create/create.ts:31-41
Creating from a Template
When using the --template flag, the CLI executes a sophisticated workflow to merge the template into the target project. This process is orchestrated by the agentBuilderTemplateWorkflow.
The workflow involves several automated steps, including using an AI agent (AgentBuilder) to perform intelligent code merging and validation.
This diagram shows the sequence of steps in the template creation workflow.
Workflow Steps:
cloneTemplateStep: Clones the template repository to a temporary directory.analyzePackageStep: Reads the template'spackage.jsonto extract dependencies and scripts.discoverUnitsStep: Uses an AI agent to scan the template and identify all Mastra units (agents, workflows, tools).orderUnitsStep: Sorts the discovered units to ensure correct installation order.packageMergeStep: Merges dependencies and scripts from the template'spackage.jsoninto the target project'spackage.json.installStep: Runs the package manager to install the merged dependencies.programmaticFileCopyStep: Copies files from the template to the target project, handling naming conventions and simple conflicts.intelligentMergeStep: UsesAgentBuilderto resolve complex conflicts and register new components in the project's Mastra index file.validationAndFixStep: Runs a validation agent to check for TypeScript errors, linting issues, and other problems, attempting to fix them automatically.
Sources: packages/agent-builder/src/workflows/template-builder/template-builder.ts
GitHub URL Validation
When a GitHub URL is provided as a template, the CLI performs a validation check to ensure it's a valid Mastra project before proceeding.
This sequence diagram shows the validation process.
mastra dev
The mastra dev command starts the local development server. This is essential for running and testing agents and workflows. In the monorepo, this is often run via pnpm run dev.
The command is responsible for:
- Starting the Mastra server (typically on port 4200).
- Loading the Mastra configuration from
src/mastra/index.ts. - Making agents and workflows available via API endpoints.
Sources: DEVELOPMENT.md:78, templates/template-ad-copy-from-content/README.md:31, packages/agent-builder/src/defaults.ts:886-894
Project Structure
The CLI generates and manages a standardized project structure to organize Mastra components. This convention is crucial for the framework's modularity.
| Directory | Purpose |
|---|---|
src/mastra/agents | Contains definitions for AI agents. |
src/mastra/workflows | Contains definitions for multi-step workflows. |
src/mastra/tools | Contains definitions for tools that agents can use. |
src/mastra/mcp | Contains definitions for MCP (Model Context Protocol) servers. |
src/mastra/networks | Contains definitions for agent networks. |
src/mastra/index.ts | The main entry point where the Mastra instance is configured and exported. |
Sources: packages/agent-builder/src/defaults.ts:251-257, packages/agent-builder/src/workflows/workflow-builder/prompts.ts:114-123, examples/dane/README.md:27-35
Development and Build Scripts
The package.json for the @mastra/cli and the root DEVELOPMENT.md define several scripts for building, testing, and maintaining the project. These are executed using pnpm.
| Command | Description |
|---|---|
pnpm run setup | Installs all dependencies and builds the CLI package. |
pnpm build | Builds all packages in the monorepo. |
pnpm build:cli | Builds only the CLI and playground packages. |
pnpm test | Runs all tests across the monorepo. |
pnpm test:cli | Runs tests specifically for the CLI package. |
pnpm typecheck | Runs TypeScript checks across all packages. |
pnpm format | Formats code with Prettier and runs linting. |
Sources: DEVELOPMENT.md:33-84, CLAUDE.md:7-30, packages/cli/package.json:20-26