createMastraCode()
createMastraCode() is the main factory function that bootstraps the Mastra Code coding agent. It wires together project detection, storage, authentication, MCP servers, hooks, modes, subagents, and the Harness that drives the TUI or any other frontend.
Usage exampleDirect link to Usage example
import { createMastraCode } from 'mastracode'
const { harness, mcpManager, hookManager, authStorage } = createMastraCode()
Pass a config object to customize behavior:
import { createMastraCode } from 'mastracode'
const { harness } = createMastraCode({
cwd: '/path/to/project',
disableMcp: true,
initialState: { yolo: false },
})
await harness.init()
await harness.selectOrCreateThread()
await harness.sendMessage({ content: 'Explain the auth flow' })
ParametersDirect link to Parameters
createMastraCode() accepts an optional MastraCodeConfig object:
cwd?:
modes?:
subagents?:
extraTools?:
storage?:
initialState?:
heartbeatHandlers?:
disableMcp?:
disableHooks?:
Return valueDirect link to Return value
createMastraCode() returns an object with four properties:
harness:
mcpManager:
hookManager:
authStorage:
Default modesDirect link to Default modes
When no modes are specified, Mastra Code creates three modes:
| Mode | Default model | Description |
|---|---|---|
| Build (default) | anthropic/claude-opus-4-6 | Full tool access. Read, write, edit files, and execute commands. |
| Plan | openai/gpt-5.2-codex | Read-only mode. Explore the codebase and produce implementation plans. |
| Fast | cerebras/zai-glm-4.7 | Quick answers and small edits with minimal overhead. |
Default subagentsDirect link to Default subagents
When no subagents are specified, Mastra Code creates three subagent types:
| Subagent | Tools | Description |
|---|---|---|
| Explore | view, search_content, find_files | Read-only codebase exploration for answering questions about code structure. |
| Plan | view, search_content, find_files | Read-only analysis and planning with structured plan output. |
| Execute | All read tools + string_replace_lsp, write_file, execute_command, task_write, task_check | Focused task execution with full write capabilities. |
Project detectionDirect link to Project detection
Mastra Code automatically detects project identity from the working directory:
- Git remote URL — if available, normalizes SSH and HTTPS URLs to create a stable identifier shared across clones and worktrees.
- Absolute path — falls back to the filesystem path when Git isn't available.
The resulting resourceId scopes threads to the project, so conversations are shared across clones, worktrees, and SSH/HTTPS URLs of the same repository.
Storage resolutionDirect link to Storage resolution
Storage configuration is resolved in priority order:
storageparameter passed tocreateMastraCode()MASTRA_DB_URLandMASTRA_DB_AUTH_TOKENenvironment variables- Project config at
.mastracode/database.json - Global config at
~/.mastracode/database.json - Local SQLite database at the platform-specific app data directory
The returned harness object is a full Harness instance with methods for sending messages, switching modes and models, managing threads, and subscribing to events.