Skip to content

Host API and CLI

This guide covers the boundary where applications meet a deployment: an HTTP API under /api/v1, and a mash CLI whose every command is a client of that same API. Together they are the concrete form of the seam the architecture overview argues for.

Three API conventions

The API is REST plus SSE, composed in src/mash/api. Three conventions apply across all of it.

Envelopes. Success responses arrive as {"data": ...}. Errors arrive as {"error": {"code": ..., "message": ..., "details": ...}} with stable codes (AGENT_NOT_FOUND, EVENT_NOT_FOUND, REQUEST_NOT_FOUND), so client code matches on the code rather than the message.

Auth. With no API key configured, routes are open, which suits local development, and the host binds only a loopback address: mash host serve --host 0.0.0.0 without a key refuses to start. With MASH_API_KEY set, every /api/v1/* route requires Authorization: Bearer <key> or X-API-Key: <key>. The admin dashboard asks for the key once per browser session and keeps it in a mash_api_key cookie that only the API routes see.

Discovery. GET / returns service discovery info, /api/v1/openapi.json is the generated schema, and /api/v1/docs serves Swagger UI. Both need the API key, like the rest of /api/v1. GET /api/v1/health reports the deployment shape, the agent list and the host's members, and doubles as the liveness and readiness probe in deployment.

The endpoint groups

Each group of endpoints projects one subsystem from the architecture series.

Agents. GET /agent lists every agent on the host with its metadata. GET /agent/{agent_id} returns one agent's detail.

Policies. PUT /policies/{policy_id} creates or replaces a policy: whether agents get the sandbox, which browser sites they may open, and how long an interaction may wait. GET reads it and DELETE removes it with its sandbox and browser. Every event names one.

Events. Every input to a host is an event in a session, from the events post:

Method Path (under /api/v1) Purpose
POST /sessions/{session_id}/events post a new event (event_id, app_id, policy_id, message, and optional context, metadata and structured_output); returns request_id. With interaction_id and response, it answers the event's open interaction instead
GET /sessions/{session_id}/events/{event_id}/stream SSE stream of the event's request, a replay of persisted events; ?after= resumes from a frame id
GET /sessions/{session_id}/events/{event_id} the event's status, open interactions, and its response or error
GET /sessions/{session_id}/events the session's ledger in arrival order
GET /events the same across every session
POST /sessions/{session_id}/events/{event_id}/cancel cancel a queued event, or a running one's request and the request it routed to

Events in a session run one at a time, in arrival order, each as one request on the host's built-in companion, which routes it among the host's agents and workflows. Posting an event_id the session already has returns the existing row, so a retried POST runs once.

Requests. Requests start only from a session's queue, and their routes read: GET /agent/{agent_id}/request/{request_id}/events streams any request, the one the companion routed included, .../status reports its recorded status, and GET /agent/{agent_id}/sessions/{session_id}/requests lists a session's requests.

Host. GET /host returns the host's id and the agents, with their metadata, and workflows the companion routes to. GET /host/snapshot adds each agent's spec state, which is what an eval experiment records when it starts. The deploy defines the host, so no route changes it.

Sessions. The board, read over HTTP: GET .../sessions and .../sessions/{session_id} for listings, .../history for turns, .../signals for per-turn signal payloads with their definitions, and POST .../compact to trigger compaction manually. A reasoning endpoint (GET .../session/{session_id}/trace/{trace_id}/reasoning) returns the compact step-by-step trace for one turn.

Workflows. GET /workflow lists definition summaries, GET /workflow/{workflow_id} returns a complete definition, POST /workflow/{workflow_id}/run starts a run under an app_id and a policy_id, with an optional dedup_key and input, GET .../runs pages through run summaries, GET .../runs/{run_id} returns one run with its result and step snapshots, and GET .../runs/{run_id}/events streams step lifecycle events over SSE.

Feedback. Two routes for capturing user notes. POST /feedback records a free-form note with its session context, and GET /feedback lists notes for an agent, narrowed by a required after timestamp and an optional full-text q over the message. The runtime store keeps them in a runtime_feedback table beside the event log.

Telemetry.

Path (under /api/v1/telemetry) Purpose
GET /events recent runtime events, paginated by cursor
GET /traces recent trace summaries
GET /trace/analysis span tree and timing breakdown for one trace
GET /usage token and request usage aggregated by time bucket
GET /sessions session listing scoped to the runtime store
GET /memory/search keyword search over conversation turns
POST /command-events ingest CLI command lifecycle events from the REPL
GET /command-events list ingested command events

One client underneath

MashHostClient in mash.cli wraps the API one method per endpoint: health(), post_event(...), event_events(...), answer_event(...), cancel_event(...), run_workflow(...), stream_workflow_run(...), get_trace_analysis(...), and so on. The streaming methods parse SSE internally and yield frames as dicts, so consumers iterate events without touching the wire format.

The mash CLI is built entirely on this client. Nothing in it has a private path into the runtime; mash status calls GET /api/v1/health, and every frame the REPL renders arrived on the same SSE stream your application would read. Driving the CLI against a host exercises exactly the protocol your integration will use.

The mash command

mash connect validates a connection against GET /health and persists it: base URL, API key, the app events act for (--app-id) and the policy they run under (--policy-id), Later commands resolve their connection from flags first, then environment (MASH_API_BASE_URL, MASH_API_KEY), then that saved config. mash policy put|get|rm <id> manages policies. With a connection in place, one-shot commands cover quick checks: mash status, mash browse, mash agents, mash workflows, mash sessions, and mash history --session-id .... mash run <message> posts one event and prints its answer.

mash browse shows the agents and workflows the companion routes to. mash agents lists every agent on the host, built-in ones included, and mash workflows every workflow.

mash host serve is the other side of the boundary: it loads build_host() from --host-app (or MASH_HOST_APP) and runs the API server, with flags for bind host, port, API key, CORS origins, and disabling observability.

The REPL

mash repl opens the interactive shell on the host. Plain input becomes an event in the REPL's session, posted and then streamed; lines starting with / are commands that run locally:

Command What it does
/new, /resume start a new session, or pick an earlier one to go on in
/agents, /tools the host's agents, and their tools and skills
/retry send the last message again as a new event
/trace [N] trace analysis for recent traces
/feedback <message> record a note or bug report for the session
/workflow list|run|status workflow operations
/help, /clear, /exit shell basics

The input stays open while a request runs. A message sent then is posted at once and waits in the session's queue on the host; Up on an empty input takes the last queued one back and cancels its event. Ctrl-C cancels the running event.

Two parts of the shell do more than print frames.

Interactions render as prompts. When a request.interaction.create frame arrives, the shell prompts for an approval decision, a choice, or free-form input, and posts the answer back to the event, so the human-in-the-loop flow works end to end at the terminal.

Streaming renders as formatted markdown. The chain renderer buffers chat.delta chunks and flushes each completed markdown block as it finishes, so answers stream in with headings and highlighted code instead of raw text, and unterminated code fences stay buffered until they close. The shell then suppresses the end-of-turn re-render when tokens already streamed, so the response appears exactly once.

Building on it

The same pieces are public for your own interface: MashHostClient for any Python application, and Repl, Command, and CLIContext for a CLI with your own slash commands. Building an Agent CLI walks through that end to end, and the deploy guide covers putting the API on the network.