Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build Mastra AI agents and workflows with guidance on current API lookup, tools, memory, and RAG patterns.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/mastra-api.md
1# Mastra API CLI Reference23How to use the `mastra api` CLI to interact with Mastra servers. Prefer fast, focused commands and compact JSON projections. Treat the installed CLI and server schema as the source of truth when discovery is needed.45Use this reference when the user asks to inspect or call agents, workflows, tools, MCP servers, memory threads, traces, logs, scores, datasets, experiments, or to debug/test `mastra api` commands.67## Setup89The CLI can interact with any reachable Mastra server:1011- Local dev server: `http://localhost:4111` from `npm run dev`12- Mastra platform deployment: Use the deployment URL13- Remote/self-hosted server: Use the server URL1415For local servers, `mastra api` defaults to `http://localhost:4111`:1617```bash18npx mastra api agent list19```2021For Mastra platform or remote servers, pass `--url`. For the sake of brevity in examples, `$MASTRA_URL` is used as a placeholder for the actual server URL which you need to set yourself:2223```bash24npx mastra api --url $MASTRA_URL agent list25```2627Verify the server once with a cheap check before resource calls:2829```bash30MASTRA_URL="${MASTRA_URL:-http://localhost:4111}"31curl -fsS "$MASTRA_URL/api/system/api-schema" >/dev/null32```3334If `$MASTRA_URL` is not reachable, the user may be using a Mastra platform deployment or remote URL. Ask for the correct server URL and set `--url` accordingly. If authentication is required, ask the user for the necessary token or credentials and set them in the environment for subsequent commands.3536For authenticated servers, pass repeatable headers:3738```bash39npx mastra api --url "$MASTRA_URL" --header "Authorization: Bearer $TOKEN" agent list40```4142## Decision flow43441. Clear read-only request (`list X`, `latest X`, `get X`, `summarize recent X`): infer the resource and use the fast path first.452. Mutating request (`create`, `update`, `delete`, `run`, `resume`, `execute`), unclear resource/action, failed fast path, or exact syntax requested: use narrow CLI discovery.463. JSON input uncertain: use command-specific `--schema`.474. Route behavior confusing: inspect `/api/system/api-schema`.4849Start with these command groups when present; verify with `mastra api --help` if the group fails.5051```text52agent workflow tool mcp thread memory trace log score dataset experiment53```5455## Fast path for read-only requests5657Use conventional `list`/`get` commands first. Keep pages small and pipe through `jq` immediately.5859Latest item:6061```bash62npx mastra api <resource> list '{"page":0,"perPage":1}' \63| jq '.data[0]'64```6566Recent items:6768```bash69npx mastra api <resource> list '{"page":0,"perPage":10}' \70| jq '.data[]'71```7273When the shape is known, project only the fields needed for the task:7475```bash76npx mastra api <resource> list '{"page":0,"perPage":10}' \77| jq '.data[] | {id, name, createdAt, status}'78```7980Get details:8182```bash83npx mastra api <resource> get <id> \84| jq '.data'85```8687When the shape is known, project only the fields needed for the task:8889```bash90npx mastra api <resource> get <id> \91| jq '.data | {id, name, createdAt, status}'92```9394If a resource does not support the conventional shape, fall back to narrow `--help` for that resource/action.9596## Output control9798- Do not use unfiltered `--pretty` during exploration.99- Always project list/get output with `jq` before reading details.100- Use `perPage:1` for latest and `perPage:10` or less for recent lists.101- If output is truncated or noisy, rerun with a narrower `jq` projection. Do not increase terminal output just to see more raw JSON.102- Fetch full JSON only when the user asks for raw output or compact projections are insufficient.103104## Fallback discovery105106Use the narrowest discovery command that can answer the question. Example for traces:107108```bash109npx mastra api trace --help110npx mastra api trace list --help111npx mastra api trace list --schema112```113114Use top-level help only when the resource is unknown:115116```bash117npx mastra api --help118```119120Read `--schema` output as the contract:121122- `command`: usage string123- `examples`: known-good examples124- `positionals`: required path/identity arguments125- `input.required`: whether JSON input is required126- `input.schema`: accepted CLI JSON input, including query/body fields127- `schemas`: raw server route schemas for deeper debugging128129## JSON and output contract130131`mastra api` accepts at most one inline JSON object as input. Do not use stdin or files unless the user explicitly asks.132133For non-GET routes, the CLI splits the one JSON object into query parameters and request body according to the server route schema.134135Output envelopes:136137```json138{ "data": {} }139{ "data": [], "page": { "total": 0, "page": 0, "perPage": 0, "hasMore": false } }140{ "error": { "code": "...", "message": "...", "details": {} } }141```142143## Error handling144145- `INVALID_JSON`: fix shell quoting; input must be one JSON object.146- `MISSING_INPUT`: run the same command with `--schema` and supply required JSON.147- `MISSING_ARGUMENT`: provide the positional shown by `--help` / `--schema`.148- `HTTP_ERROR`: inspect `error.details`, then compare against `--schema` or route schema.149- `REQUEST_TIMEOUT`: retry with larger `--timeout`, especially for workflow execution.150- `SERVER_UNREACHABLE`: verify the URL and the server check. If localhost is not running, ask whether the user wants to use a Mastra platform deployment or another remote server URL.151152## Route-level debugging153154If CLI behavior seems wrong, inspect the route-derived schema manifest instead of guessing.155156Find routes by path:157158```bash159curl -fsS "$MASTRA_URL/api/system/api-schema" \160| jq '.routes[] | select(.path | contains("/memory"))'161```162163Inspect one route:164165```bash166curl -fsS "$MASTRA_URL/api/system/api-schema" \167| jq '.routes[] | select(.method == "POST" and .path == "/tools/:toolId/execute") | {pathParamSchema, queryParamSchema, bodySchema, responseShape}'168```169170## Known notes171172- Tool and MCP tool execution accept raw tool input; explicit `{ "data": ... }` also works.173- Workflow resume only works for suspended workflow runs.174- Working memory update requires the agent's memory to have working memory enabled.175- Empty lists may simply mean the server has no matching stored data yet.176