Mastra Code

3 min read Original article ↗

Mastra Code is a terminal-based AI coding agent built on Mastra's Harness, Agent, and Memory primitives. It runs in your terminal, connects to thousands of AI models, and provides tools for reading, searching, editing, and executing code.

Mastra Code organizes its capabilities around these areas:

  • Modes: Switch between Build, Plan, and Fast modes to match your workflow.
  • Tools: Built-in tools for file viewing, editing, searching, shell commands, and web search.
  • Configuration: Project-scoped threads, MCP servers, hooks, custom commands, skills, and database settings.
  • Customization: Extend Mastra Code programmatically with custom modes, tools, subagents, and storage.

When to use Mastra Code

  • Day-to-day coding: Ask questions about your codebase, make edits, run tests, and manage Git.
  • Code exploration: Use Plan mode to analyze architecture and create implementation plans before writing code.
  • Quick lookups: Switch to Fast mode for brief answers and small edits with minimal latency.
  • Multi-model workflows: Compare responses across different AI providers by switching models mid-conversation.

Prerequisites

Mastra Code requires Node.js 22.13.0 or later.

Get started

  1. Install Mastra Code globally:

    • npm
    • pnpm
    • Yarn
    • Bun
    npm install -g mastracode

    Or run it with npx:

    • npm
    • pnpm
    • Yarn
    • Bun
  2. Navigate to your project directory and start Mastra Code:

    cd your-project
    mastracode
  3. On your first startup, Mastra Code starts an onboarding wizard to help you connect and choose your models. You can always rerun this with the /setup command.

    In the first step, log in to Anthropic or OpenAI through OAuth. Alternatively, skip this step to later authenticate with an API key. Next, choose models for each mode (you can later switch models with /models). When you select a model from the model selector that doesn't have an API key configured, Mastra Code prompts you to enter one.

    By default, Mastra Code uses google/gemini-2.5-flash for its Observational Memory models. In the next step, you can choose a different model or continue using Gemini Flash. You can later switch this setting with the /om command.

    In the last step, choose whether you want to run in "YOLO" mode or not. You're all set up with Mastra Code!

  4. Type a message and press Enter. If the agent is already working, Enter queues your next message and sends it automatically after the current run finishes.

Slash commands

Mastra Code provides built-in slash commands for managing sessions and settings:

CommandDescription
/newStart a new conversation thread
/cloneClone the current thread (with optional rename)
/threadsList all threads for this project
/threadShow current thread info
/nameRename the current thread
/modelsSwitch model pack
/modeSwitch or list modes
/permissionsConfigure tool approval permissions
/settingsOpen the settings panel
/omConfigure Observational Memory
/skillsList available skills
/costShow token usage and costs
/diffShow modified files or git diff
/sandboxManage sandbox allowed paths
/reviewReview a GitHub pull request
/report-issueOpen or browse mastracode issues
/loginAuthenticate with OAuth provider
/logoutLog out from an OAuth provider
/setupRun the setup wizard
/themeSwitch color theme (auto/dark/light)
/browserConfigure browser automation
/updateCheck for and install updates
/hooksShow/reload configured hooks
/mcpShow/reload MCP connections
/helpShow available commands
/exitExit the TUI

You can also define custom slash commands as markdown files. See Configuration for details.

Keyboard shortcuts

ShortcutAction
Ctrl+CInterrupt current operation or clear input
Ctrl+C ×2Exit (double-tap)
Ctrl+DExit (when editor is empty)
Ctrl+ZSuspend process (fg to resume)
Alt+ZUndo last clear
Ctrl+TToggle thinking blocks visibility
Ctrl+EExpand/collapse all tool outputs
EnterSend a message, or queue a follow-up while the agent is running
Ctrl+YToggle YOLO mode

Architecture

Mastra Code is built on four layers:

  1. TUI: Terminal interface (pi-tui components)
  2. Harness: Mode management, thread persistence, event system, state management
  3. Mastra Agent: Dynamic model selection, tool execution, memory integration, subagents
  4. LibSQL Storage: Thread persistence, message history, token usage tracking, observational memory

Demo Video

In this demo, you'll see Mastra Code in action and learn how it came to be.

Next steps