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, metrics, 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 URL14- Hosted Mastra Platform Observability: `https://observability.mastra.ai` (auto-targeted by `trace`, `log`, `score`, and `metric` commands)1516For local servers, `mastra api` defaults to `http://localhost:4111`:1718```bash19npx mastra api agent list20```2122For 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:2324```bash25npx mastra api --url $MASTRA_URL agent list26```2728Verify the server once with a cheap check before resource calls:2930```bash31MASTRA_URL="${MASTRA_URL:-http://localhost:4111}"32curl -fsS "$MASTRA_URL/api/system/api-schema" >/dev/null33```3435If `$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.3637For authenticated servers, pass repeatable headers:3839```bash40npx mastra api --url "$MASTRA_URL" --header "Authorization: Bearer $TOKEN" agent list41```4243### Target resolution4445Runtime commands (`agent`, `workflow`, `tool`, `mcp`, `thread`, `memory`, `dataset`, `experiment`) resolve the target in this order:46471. `--url <url>` for an explicit remote or self-hosted server.482. `http://localhost:4111` for a local `mastra dev` server.493. `.mastra-project.json` for a Mastra platform project.5051Observability commands (`trace`, `log`, `score`, `metric`) target `https://observability.mastra.ai` by default instead of a project deployment URL. The CLI resolves credentials in this order:52531. Explicit `Authorization` and `X-Mastra-Project-Id` headers passed with `--header`.542. `MASTRA_PLATFORM_ACCESS_TOKEN` and `MASTRA_PROJECT_ID` from the environment.553. Project metadata from `.mastra-project.json` for the project ID.564. The Mastra CLI login token as an auth fallback.5758For observability calls, no `--url` or `--header` is required if `MASTRA_PLATFORM_ACCESS_TOKEN` and `MASTRA_PROJECT_ID` are set, or if `.mastra-project.json` is present:5960```bash61npx mastra api trace list '{"page":0,"perPage":10}'62npx mastra api metric names63```6465Pass `--url` and `--header` only when overriding the hosted observability target or credentials.6667## Decision flow68691. Clear read-only request (`list X`, `latest X`, `get X`, `summarize recent X`): infer the resource and use the fast path first.702. Mutating request (`create`, `update`, `delete`, `run`, `resume`, `execute`), unclear resource/action, failed fast path, or exact syntax requested: use narrow CLI discovery.713. JSON input uncertain: use command-specific `--schema`.724. Route behavior confusing: inspect `/api/system/api-schema`.7374Start with these command groups when present; verify with `mastra api --help` if the group fails.7576```text77agent workflow tool mcp thread memory trace log metric score dataset experiment78```7980## Fast path for read-only requests8182Use conventional `list`/`get` commands first. Keep pages small and pipe through `jq` immediately.8384Latest item:8586```bash87npx mastra api <resource> list '{"page":0,"perPage":1}' \88| jq '.data[0]'89```9091Recent items:9293```bash94npx mastra api <resource> list '{"page":0,"perPage":10}' \95| jq '.data[]'96```9798When the shape is known, project only the fields needed for the task:99100```bash101npx mastra api <resource> list '{"page":0,"perPage":10}' \102| jq '.data[] | {id, name, createdAt, status}'103```104105Get details:106107```bash108npx mastra api <resource> get <id> \109| jq '.data'110```111112When the shape is known, project only the fields needed for the task:113114```bash115npx mastra api <resource> get <id> \116| jq '.data | {id, name, createdAt, status}'117```118119If a resource does not support the conventional shape, fall back to narrow `--help` for that resource/action.120121## Output control122123- Do not use unfiltered `--pretty` during exploration.124- Always project list/get output with `jq` before reading details.125- Use `perPage:1` for latest and `perPage:10` or less for recent lists.126- If output is truncated or noisy, rerun with a narrower `jq` projection. Do not increase terminal output just to see more raw JSON.127- Fetch full JSON only when the user asks for raw output or compact projections are insufficient.128129## Fallback discovery130131Use the narrowest discovery command that can answer the question. Example for traces:132133```bash134npx mastra api trace --help135npx mastra api trace list --help136npx mastra api trace list --schema137```138139Use top-level help only when the resource is unknown:140141```bash142npx mastra api --help143```144145Read `--schema` output as the contract:146147- `command`: usage string148- `examples`: known-good examples149- `positionals`: required path/identity arguments150- `input.required`: whether JSON input is required151- `input.schema`: accepted CLI JSON input, including query/body fields152- `schemas`: raw server route schemas for deeper debugging153154## JSON and output contract155156`mastra api` accepts at most one inline JSON object as input. Do not use stdin or files unless the user explicitly asks.157158For non-GET routes, the CLI splits the one JSON object into query parameters and request body according to the server route schema.159160Output envelopes:161162```json163{ "data": {} }164{ "data": [], "page": { "total": 0, "page": 0, "perPage": 0, "hasMore": false } }165{ "error": { "code": "...", "message": "...", "details": {} } }166```167168## Error handling169170- `INVALID_JSON`: fix shell quoting; input must be one JSON object.171- `MISSING_INPUT`: run the same command with `--schema` and supply required JSON.172- `MISSING_ARGUMENT`: provide the positional shown by `--help` / `--schema`.173- `HTTP_ERROR`: inspect `error.details`, then compare against `--schema` or route schema.174- `REQUEST_TIMEOUT`: retry with larger `--timeout`, especially for workflow execution.175- `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.176177## Route-level debugging178179If CLI behavior seems wrong, inspect the route-derived schema manifest instead of guessing.180181Find routes by path:182183```bash184curl -fsS "$MASTRA_URL/api/system/api-schema" \185| jq '.routes[] | select(.path | contains("/memory"))'186```187188Inspect one route:189190```bash191curl -fsS "$MASTRA_URL/api/system/api-schema" \192| jq '.routes[] | select(.method == "POST" and .path == "/tools/:toolId/execute") | {pathParamSchema, queryParamSchema, bodySchema, responseShape}'193```194195## Known notes196197- Tool and MCP tool execution accept raw tool input; explicit `{ "data": ... }` also works.198- Workflow resume only works for suspended workflow runs.199- Working memory update requires the agent's memory to have working memory enabled.200- Empty lists may simply mean the server has no matching stored data yet.201- `trace list` and `trace get` return lightweight payloads by default (no span input, output, attributes, or metadata). Pass `--verbose` to fetch full span records, or use `trace span <traceId> <spanId>` to fetch one specific span in full.202