mastra-file-based-agents
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Define Mastra agents by file convention instead of code: an agent directory under src/mastra/agents with config.ts (agentConfig), instructions.md or instructions.ts, and tools/, skills/, subagents/, processors/, scorers/, schedules/, memory.ts, workspace.ts, plus project files like storage.ts, logger.ts, observability.ts, server.ts, studio.ts, and src/mastra/workflows. Use this skill whenever the user wants to create an agent as a folder, add a tool or skill by dropping in a file, declare subagents or cron schedules as files, debug why a file-based agent is not discovered, or resolve precedence between config.ts and sibling files. Triggers on file-based agents, agentConfig, agentInstructions, defineSchedule, config.ts, instructions.md, src/mastra/agents folder, subagents directory, file routing, mastra dev discovery, mastra build discovery. Also triggers when a project has agent folders but no new Agent() calls.
- Reads credentials
- Sends data out
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-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-project-setupSkill
- 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
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 99
If config.ts exports new Agent({...}), Mastra uses it as-is and ignores subagents/, processors/, and schedules/ with a warning.
- Very specific: line 105
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the types for agentConfig, …
- Distinctive: line 111
Add memory, workspace, processors, scorers, schedules: only when needed. Schedules need storage and a long-running host.
≈ “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 File-Based Agents
Define an agent as a directory. Mastra discovers the files under src/mastra/ during mastra dev and mastra build, assembles a normal Agent, and registers it.
When to use
- Creating an agent by convention (
src/mastra/agents/<name>/config.tsplusinstructions.md) - Adding tools, skills, processors, scorers, or schedules by adding files
- Declaring specialist subagents as nested directories
- Setting project-wide storage, logger, observability, server, or Studio config from single files
- Debugging a file-based agent that does not show up, or a setting that does not take effect
- Converting a code-defined agent to the file convention, or the reverse
When NOT to use
- Constructing agents with
new Agent({...})in code (usemastra-agents) - Tool schemas and execution options (use
mastra-tools) - Delegation hooks, message filtering, and rubric loops for subagents (use
mastra-multi-agent) - The
SKILL.mdpackage format and workspace skill discovery (usemastra-agent-skills) - Runtime schedules created with
mastra.schedules.create()(usemastra-schedules) - Memory, storage, or workspace provider details (use
mastra-memory,mastra-storage,mastra-workspace-sandbox) - Apps that import the
mastrainstance through a framework or server adapter (usemastra-framework-integrationormastra-serverand register in code)
Required inputs
- The agent directory name (it becomes the default
idandname) - The model (a
provider/modelstring, verified withmastra-models) - The always-on prompt, and whether it needs code (
instructions.ts) or not (instructions.md) - Tools, skills, subagents, processors, scorers, and schedules the agent needs
- How the app runs: through
mastra dev/mastra build(discovery works) or as a library (it does not)
The framework: a directory is an agent
src/mastra/
storage.ts # project store (optional)
agents/
weather/
config.ts # required: model and runtime options
instructions.md # or instructions.ts: always-on prompt
tools/get_weather.ts # tool key: get_weather
skills/units.md # skill name: units
memory.ts # optional Memory instance
workspace.ts # optional; a default workspace exists
workspace/ # seed files copied at build time
processors/input/ # input processors
processors/output/ # output processors
scorers/relevance.ts # scorer key: relevance
schedules/daily.ts # schedule id: daily (root agents only)
subagents/
researcher/ # its own config.ts, instructions.md, tools/ ...
workflows/daily-report.ts # project-level workflow (default export)// src/mastra/agents/weather/config.ts
import { agentConfig } from '@mastra/core/agent'
export default agentConfig({
model: 'openai/gpt-6.1-sol',
})<!-- src/mastra/agents/weather/instructions.md -->
You are a helpful weather assistant. Answer questions about current conditions and forecasts.model is the only required agentConfig() field. agentConfig() accepts Agent constructor options. A missing model or missing instructions fails the build.
Precedence summary
| Domain | Winner |
| Instructions | Function config.instructions, then instructions.ts, then instructions.md, then static config.instructions |
| Tools, skills, scorers | Merge. config.* wins on key collisions. A function-valued config.* ignores discovered files. |
| Processors | config processors run first, then discovered files. A function-valued config.*Processors ignores discovered files. |
| Memory, workspace | config.memory over memory.ts. config.workspace over workspace.ts. |
| Subagents | config.agents entry wins over a same-id directory. A function-valued config.agents ignores discovered subagents. |
| Project files | Code-registered config in new Mastra({...}) wins over storage.ts, logger.ts, observability.ts, server.ts, studio.ts, and file workflows. |
If config.ts exports new Agent({...}), Mastra uses it as-is and ignores subagents/, processors/, and schedules/ with a warning.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the types foragentConfig,agentInstructions, anddefineSchedulefrom@mastra/core/agent. Otherwise fetchhttps://mastra.ai/reference/file-based-agents/config.mdand the sibling pages underhttps://mastra.ai/reference/file-based-agents/. Trust these over memory. - Confirm discovery runs. The app must start with
mastra devormastra build. Server adapters and direct imports ofmastraskip discovery. - Create the directory
src/mastra/agents/<name>/withconfig.tsandinstructions.md. - Add tools as one file per tool in
tools/, each default-exportingcreateTool(...). Filename equals tool key. No nested folders. - Add skills as flat
.mdfiles,SKILL.mddirectories, or modules exportingcreateSkill(...). Each needs adescription. - Add subagents under
subagents/<id>/. Eachconfig.tsneeds a non-emptydescription. Nest at most three levels. - Add memory, workspace, processors, scorers, schedules only when needed. Schedules need storage and a long-running host.
- Run
npx mastra dev, open Studio, and confirm the agent, its tools, skills, and subagents appear. Read build warnings: they name precedence conflicts.
Deep dive: schedules as files
// src/mastra/agents/support/schedules/heartbeat.ts -> id: heartbeat
import { defineSchedule } from '@mastra/core/agent'
export default defineSchedule({
cron: '*/5 * * * *',
prompt: 'Check system health and report any failures.',
})---
cron: '0 9 * * 1'
timezone: 'America/New_York'
---
Summarize the past week's tickets and post the digest to the team channel.- Set exactly one of
promptorhandler. A handler returns overrides (for example{ prompt }) ornullto skip the fire. - Quote the cron in Markdown frontmatter. A leading
*is a YAML alias. - The id is the path under
schedules/without the extension. Renaming a file deletes one schedule and creates another. - Root agents only. A
schedules/directory undersubagents/is a build error. - Fire one on demand:
curl -X POST http://localhost:4111/api/schedules/<scheduleId>/run(copy the encoded id fromGET /api/schedules).
Failure patterns
- Agent missing from Studio. The app imports
mastradirectly or runs through a server adapter. Start withmastra dev/mastra build, or register the agent in code. - Build fails naming the agent directory. No
instructions.md,instructions.ts, orconfig.instructions. Add one. - Build fails on a model.
agentConfig()has nomodel. Add it. - Tool not found. The file is in a subfolder of
tools/, lacks a default export, or is a*.test.ts. Use flat files named after the action. - Discovered tools or skills ignored.
config.toolsorconfig.skillsis a function. Use a static record or move everything into the function. - Subagent build error. Missing
description, an id that collides with a parent tool key, or a duplicate id. Fix the subagentconfig.tsor rename. - Subagent cannot use the parent's tools or memory. Subagents are isolated by design. Share dependencies in code and assign them in each
config.ts. - Agent has no memory. Neither
memory.tsnorconfig.memoryexists. Addmemory.tsexportingnew Memory(). - Unwanted shell access. File-based agents get a default workspace with
LocalFilesystemandLocalSandbox. Setworkspace: undefinedinconfig.tsto disable it. - Schedules never fire. In-memory storage, a serverless host, or a schedule under a subagent. Add
storage.ts, use a long-running process, and put schedules on the root agent. - Edits not live in production.
instructions.mdis inlined at build time. Rebuild and redeploy.
Output format
A finished file-based agent delivery contains:
src/mastra/agents/<name>/config.tsandinstructions.md(orinstructions.ts)- One file per tool in
tools/, and skills, subagents, or schedules as needed src/mastra/storage.tswhen memory or schedules must persist- Required env vars in
.env.example - Evidence:
npx mastra devstarts with no precedence warnings you did not intend, and the agent answers in Studio
Reference files
references/api.md- Every file convention: exports, naming, precedence, and build-time behavior.references/examples.md- Minimal agent, agent with tools, skills, and memory, research supervisor with subagents, scheduled support agent.references/checklist.md- Checklist for file-based agents.
Live docs
- https://mastra.ai/docs/develop
- https://mastra.ai/reference/file-based-agents/config
- https://mastra.ai/reference/file-based-agents/instructions
- https://mastra.ai/reference/file-based-agents/tools
- https://mastra.ai/reference/file-based-agents/skills
- https://mastra.ai/reference/file-based-agents/subagents
- https://mastra.ai/reference/file-based-agents/schedules
- https://mastra.ai/reference/file-based-agents/workspace
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.

