Skip to content

How to Deploy a Mash Host

This guide covers deploying a Mash host and its agents, on your laptop or on a server. The steps apply to any Mash application; the examples use my_agent.spec:build_host.

Architecture

A Mash host is the unit of deploy: one container or one local process. It runs the API and the workflows, and starts one child process per agent. Each child serves its agent's runtime behind an H2A HTTP server on a localhost port. The code an agent's model writes runs in a sandbox that belongs to the request's policy, and people open what it made at links the host serves.

 user app ──HTTP──► Mash host (API, companion routing, workflows)
                     │  one child per agent, on 127.0.0.1:
                     │    mash agent serve --agent research
                     │  /h2a relay (companion calls), /sandbox relay (run_code,
                     │  sandbox_put, sandbox_get), /s/ links
                     │
                     ▼ Docker socket
   ┌──────────────────────────────────────┐
   │ mash-sandbox-<key>   (policy acme-ws)│
   │  mash sandbox serve :9000            │
   │  /home/agent/workspaces/research  ◄── volume mash-sandbox-<key>
   └──────────────────────────────────────┘

   Postgres: policies, sessions and events, request event logs,
             turns, workflow runs (host and agents only)
  • The host imports build_host() and keeps each agent's spec and metadata. Each agent process imports the same build_host(), under the serving host's id (MASH_HOST_ID), and serves one agent from it. Agent processes are the app's own code and inherit the host's environment.
  • A child that exits is restarted on the same port, with backoff from 1 to 30 seconds. While it is down, host routes for that agent return 503 AGENT_UNAVAILABLE. Children live and die with the host.
  • An app posts each message as an event to a session. The host runs a session's events one at a time on the companion, which runs the host's agents and workflows through the host's /h2a relay; the relay checks each call against the host's members.
  • Every event names an app and a policy. A run_code, sandbox_put or sandbox_get call goes from the agent process to the host's /sandbox relay with only the request id; the host looks up the request's policy and acts in that policy's sandbox, in the agent's folder /home/agent/workspaces/<agent_id>.
  • A sandbox holds one policy's workspaces and nothing else. It gets no environment from the host and has no database credentials. Each agent's code runs as a uid of its own with a clean environment, in a folder only that uid can use, so agents cannot read each other's files or variables. The sandbox's daemon accepts only that policy's sandbox token, which only the host can derive.
  • Files in a workspace and servers the code started open at /s/<link token>/files/<agent_id>/<path> and /s/<link token>/ports/<n>/ on the host. The link token names the policy, so links need no API key.
  • Sandboxes start on first use and stop after MASH_SANDBOX_IDLE_SECONDS (default 900) without a call or a link request. At most MASH_SANDBOX_MAX_RUNNING (default 20) run at once.
  • A host that cannot reach a Docker daemon runs without sandboxes, and its agents get no sandbox tools. This is how a host runs on a platform with no Docker socket (Render, Cloud Run, Fargate). The host logs which mode it chose.
  • All durable state (events, request event logs, sessions, workflow runs) lives in Postgres. Workspace files live on the sandbox volumes.

A deployment runs one host replica.

Prerequisites

  • PostgreSQL 14 or later, reachable from the host
  • An API key for your LLM provider (ANTHROPIC_API_KEY, OPENAI_API_KEY, or GEMINI_API_KEY)
  • A host secret in MASH_HOST_SECRET. The host derives each agent's and each sandbox's token from it, so keep it stable across restarts:
export MASH_HOST_SECRET=$(openssl rand -hex 32)
  • For sandboxes: Docker Engine on the machine that runs the host.

The images

The host runs from an image that contains your app:

FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 MASH_DATA_DIR=/var/lib/mash \
    MASH_API_HOST=0.0.0.0 MASH_API_PORT=8000

RUN useradd --create-home --shell /bin/bash mash \
    && mkdir -p /var/lib/mash && chown mash:mash /var/lib/mash

WORKDIR /opt/app
COPY . .
# Your app, with mashpy as a dependency.
RUN pip install .

USER mash
CMD ["sh", "-c", "mash host serve --host-app \"$MASH_HOST_APP\" --host \"$MASH_API_HOST\" --port \"$MASH_API_PORT\""]

The repository's own Dockerfile has the same layout.

Sandboxes run Mash's sandbox image, ghcr.io/imsid/mash-sandbox:<mashpy version>, built from Dockerfile.sandbox. It holds Python 3.12 with pip and uv, Node 22 with npm, SQLite, git, curl, jq and rg, and the two users a sandbox needs: mash runs the sandbox daemon, and agent runs the model's code. The host pulls it on first start. Data libraries are not preinstalled; the model installs what it needs with pip install --user, which lands in the agent's folder and persists. To preinstall more, build on it and name your image in MASH_SANDBOX_IMAGE:

FROM ghcr.io/imsid/mash-sandbox:<version>
USER root
RUN pip install pandas matplotlib
USER mash

Browsers run Mash's browser image, ghcr.io/imsid/mash-browser:<mashpy version>, built from Dockerfile.browser. It holds Chromium, Playwright MCP, Xvfb, x11vnc and noVNC, run by a browser user. The host pulls it only when an agent's spec lists browser_sites(), and starts one browser per policy on the first browser call, on its own network, mash-browsers, with a volume mash-browser-<key> for the policy's profile. Sandboxes are not on that network. A sign-in link (/s/<view token>/browser/) uses MASH_PUBLIC_URL, and the live view is a websocket, so a proxy in front of the host must pass websockets through.

Run the host in Docker

This is the production layout, and it works the same on a laptop. Each running sandbox has a network of its own, mash-sandbox-<key>, named like its container, so one policy's code cannot reach another policy's sandbox. The host container joins each running sandbox's network and reaches the sandbox by container name. Stopping a sandbox removes its network.

# docker-compose.yml
services:
  postgres:
    image: postgres:17-alpine
    environment:
      POSTGRES_DB: mash
      POSTGRES_USER: mash
      POSTGRES_PASSWORD: mash
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks: [mash]
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "mash"]
      interval: 3s
      retries: 5

  host:
    build: .
    environment:
      MASH_HOST_APP: my_agent.spec:build_host
      MASH_DATABASE_URL: postgresql://mash:mash@postgres:5432/mash
      MASH_HOST_SECRET: ${MASH_HOST_SECRET}
      MASH_API_KEY: ${MASH_API_KEY:?set MASH_API_KEY}
      # Where users' browsers reach the host, for sandbox links.
      MASH_PUBLIC_URL: http://127.0.0.1:8000
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    # The image runs as `mash`; the socket belongs to root's group (0) on
    # Docker Desktop and usually to the `docker` group on Linux.
    group_add: ["0"]
    ports:
      - "127.0.0.1:8000:8000"
    networks: [mash]
    depends_on:
      postgres:
        condition: service_healthy

networks:
  mash:
    name: mash

volumes:
  pgdata:
docker compose up -d
echo '{"sandbox": true}' | mash policy put dev --file - \
  --api-base-url http://127.0.0.1:8000 --api-key dev-key
mash connect --api-base-url http://127.0.0.1:8000 --api-key dev-key \
  --app-id my-app --policy-id dev
mash repl

On Linux, find the socket's group with stat -c %g /var/run/docker.sock and use that number in group_add. Without the socket, the host runs without sandboxes.

Agent processes inherit the host's environment, so provider keys, MCP header tokens and anything else a spec reads reach them. MASH_HOST_SECRET and MASH_API_KEY are the exceptions: they stay with the host, and each agent authenticates with a token of its own. MCP URLs resolve inside the host container. Sandboxes get none of the host's environment.

To give every sandbox files from the Docker host, list bind mounts in MASH_SANDBOX_MOUNTS, for example MASH_SANDBOX_MOUNTS=/srv/docs:/home/agent/docs:ro. The source is a path on the Docker host, even when the host runs in a container, because the daemon resolves it. A target inside /home/agent/workspaces sits on top of the policy's volume without being copied into it. A target inside one agent's folder, /home/agent/workspaces/<agent_id>/..., can be reached by that agent alone, because the folder is its uid's. Bound files keep their owner on the Docker host, so add :ro unless every agent's uid may write them.

Run the host on your laptop

For a faster loop you can run mash host serve directly. Its agents run as its children on your machine, so localhost addresses work as they are in MASH_DATABASE_URL and MCP server URLs.

Sandboxes still run in Docker, from the same sandbox image; each sandbox's daemon port is published on 127.0.0.1. Links use the bind address, so they open in your browser as they are. Without Docker, the host runs and its agents get no sandbox tools.

export MASH_DATABASE_URL=postgresql://mash:mash@localhost:5432/mash
mash host serve --host-app my_agent.spec:build_host --port 8000

Deploy to a server

Run the same compose layout on a VM with Docker (EC2, Compute Engine, a Hetzner box) and point MASH_DATABASE_URL at managed Postgres if you like. On a platform without Docker, run the host container alone; it serves every agent without sandbox tools.

  1. Provision Postgres 14+ and note the connection string. Only the host's container connects to it.
  2. Build the image on the VM, or build it elsewhere and pull it.
  3. Set MASH_HOST_APP, MASH_DATABASE_URL, MASH_HOST_SECRET, MASH_API_KEY, MASH_PUBLIC_URL (the HTTPS address users reach the host at) and your provider keys in the host's environment.
  4. Start the host with the socket mounted and group_add set, as in the compose file above.
  5. Put TLS in front of port 8000. For SSE, disable response buffering and set the idle timeout to at least 3600s.

CPU, memory and disk limits for sandboxes are set on the Docker daemon. Mash only decides which sandboxes exist.

Each sandbox network gets a /28 from MASH_SANDBOX_SUBNETS (default 10.213.0.0/16, room for 4096), not from Docker's own address pools, which hold about 30 networks. Pick another range if that one is used on your network or VPN.

Sandboxes can reach the internet, and so the cloud metadata address 169.254.169.254, where a VM's instance credentials are served. Block it on the Docker host with a rule in the DOCKER-USER chain, which Docker leaves to you and applies to traffic from every container:

sudo iptables -I DOCKER-USER -d 169.254.169.254 -j DROP

Make the rule persistent with your distribution's tool (iptables-persistent or netfilter-persistent on Debian and Ubuntu). It also blocks the host's container, so give the host's credentials another way, such as environment variables.

Operating agents

The host writes each agent process's output to its own log, one line at a time, prefixed with [<agent_id>].

GET /api/v1/agent reports each agent's status: running, starting (after a restart, until its health check passes) or unavailable. The set of agents is the deploy's; adding or removing one takes a deploy.

Operating sandboxes

These commands run on the Docker host and find sandboxes by their mash.sandbox.policy label. For a namespaced deployment, pass --namespace or set MASH_SANDBOX_NAMESPACE.

mash sandbox ls                                  # policy, status, uptime, container
mash sandbox shell acme-ws --agent research      # a shell as research's uid, in its folder
mash sandbox shell acme-ws --root
mash sandbox stop acme-ws                        # stop now and remove its network; the volume is kept
  • shell starts a stopped sandbox on its network. Commands run from it are not in the event log; their history is appended to /home/agent/workspaces/.admin_history.
  • Anything outside /home/agent/workspaces is on the container layer and is lost when the container is replaced. pip install --user and npm install install into the agent's folder, so packages persist.
  • Background processes the model left running stop with the sandbox, so a port link fails until the code starts its server again. File links keep working.
  • A sandbox keeps the image it was created from. To pick up a new image, remove the containers with docker rm -f $(docker ps -aq --filter label=mash.sandbox.policy); each policy's next call creates a new one on the same volume.

Durability

  • A request records each step's output in its event log. After a crash or a restart, an agent process rebuilds its own agent's running requests from their events and continues them, running only the work with no recorded end. A request continued 10 times without finishing fails.
  • Each agent process works on at most MASH_AGENT_MAX_RUNNING requests at once (default 32). The others stay running and wait for a slot, recovered requests included, so a restart doesn't start every request at once. A request waiting on an answer gives its slot back. A waiting request's stream shows nothing until it starts.
  • Events wait in the events table. On start the host follows every event that was running and starts the oldest queued event of every idle session, so a restart loses no message. An event whose request never landed is submitted again under the same request id.
  • The host continues the workflow runs under its runner id that are queued or running, from their completed steps.
  • The companion's routed requests and workflow agent steps submit with fixed request ids, so a step that runs again reattaches to the request it already started.
  • A run_code call is a tool step. If the process dies during it, the step runs again after recovery, and the code runs again in the same sandbox.
  • A host restart restarts every agent process, and each continues its running requests. Running sandboxes count as active from the restart.

Retention

The host keeps every request, event and workflow run until you set MASH_RETENTION_DAYS. With it set, the host deletes, when it starts and every hour after:

  • requests that finished (completed, failed or cancelled) more than that many days ago, with their event logs, unless the session event they ran is still running;
  • event log rows that belong to no request, older than the window;
  • workflow runs that finished before the window, with their steps and step events, unless an eval experiment points at the run.

A request that is still running is kept however old it is. A deleted request's trace is gone from the admin UI and the telemetry API; its session event keeps the response or error. The sessions and events ledger, conversation turns (memory_turns), Cortex, feedback and evals are never deleted, so the board, search_conversations and cortex-build keep working. Deletes run in batches of 100 requests or runs, so no statement holds locks for long.

      MASH_RETENTION_DAYS: "30"

Idle cost

An idle host and its agent processes make no queries to Postgres and keep no connections open. Each process has one connection pool, shared by its stores, that holds no connections at rest and closes one unused for 60 seconds. A process opens connections when it has work: a request, a workflow run, an API read. At startup each process runs the migrations and one recovery query. With MASH_RETENTION_DAYS set, the host also runs one retention pass an hour.

Measured on a host with six agent processes, idle, on Postgres 17: no transactions in 30 seconds, and no open connections two minutes after startup. A process busy with work holds up to MASH_DB_POOL_MAX_SIZE connections (default 10), so a host with six agent processes can open 70. At startup the host compares that budget with Postgres's max_connections and logs a warning when it is above it.

Exporting traces

The host can send every request's trace to an OpenTelemetry backend over OTLP. Install mashpy with the otel extra in the host image and point it at a collector:

uv add 'mashpy[otel]'
      OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4318
      OTEL_SERVICE_NAME: assistant

The exporter runs in the host and is off until an endpoint is set. It exports a request when it ends: its spans (invoke_agent, chat, execute_tool, mash.*), its log records (exception, mash.request.resumed, mash.request.mode_changed), and the two GenAI client metrics. A workflow run exports as one trace when it finishes, with each agent step's request under its step. The exporter keeps its position in otel_export_cursor, so a restart picks up where it stopped. The first time it runs it starts at the end of the log, so requests and runs that finished before you set the endpoint are not exported.

Headers, protocol and timeouts come from the standard OTEL_EXPORTER_OTLP_* variables. Prompts, completions, tool arguments and results, and error messages stay out of the export unless MASH_OTEL_CAPTURE_CONTENT=1 is set. The admin UI shows the same spans whether or not export is on.

Accessing the host from applications

The Mash host exposes a REST + SSE API under /api/v1. Any HTTP client can interact with it.

Authentication

Without MASH_API_KEY the host refuses to bind anything but a loopback address, so a container, which binds 0.0.0.0, needs one. Include the key in requests:

Authorization: Bearer <api-key>
X-API-Key: <api-key>          # alternative header

The /h2a and /sandbox relays are for the host's own agent processes and authenticate with agent tokens, not the API key. They share the host's port; a proxy in front of the host can block both paths from outside. The /s/ links are for people and must stay reachable; the token in each link is its only credential.

Core API endpoints

Method Path Purpose
GET /api/v1/health Health check and agent info
GET /api/v1/agent List registered agents with their status
PUT /api/v1/policies/{policy_id} Create or replace a policy
POST /api/v1/sessions/{session_id}/events Post a message as an event; or, with interaction_id, answer an interaction
GET /api/v1/sessions/{session_id}/events/{event_id}/stream Stream the event's response (SSE)
GET /api/v1/sessions/{session_id}/events/{event_id} The event's status, open interactions and response
POST /api/v1/sessions/{session_id}/events/{event_id}/cancel Cancel the event

Example: post an event and stream the response

# 1. A policy, once
curl -s -X PUT http://localhost:8000/api/v1/policies/dev \
  -H "Authorization: Bearer dev-key" \
  -H "Content-Type: application/json" \
  -d '{"sandbox": true}'

# 2. Post the message as an event
curl -s -X POST http://localhost:8000/api/v1/sessions/my-session/events \
  -H "Authorization: Bearer dev-key" \
  -H "Content-Type: application/json" \
  -d '{"event_id": "e-1", "app_id": "my-app", "policy_id": "dev",
       "message": "summarize the open issues"}'

# 3. Stream the response via SSE
curl -N http://localhost:8000/api/v1/sessions/my-session/events/e-1/stream \
  -H "Authorization: Bearer dev-key"

Posting the same event_id again returns the existing event and runs nothing, so a retry is safe.

Example: Python client

import httpx

BASE = "http://localhost:8000/api/v1"
HEADERS = {"Authorization": "Bearer dev-key"}

# Post the message as an event
httpx.post(
    f"{BASE}/sessions/s1/events",
    headers=HEADERS,
    json={
        "event_id": "e-1",
        "app_id": "my-app",
        "policy_id": "dev",
        "message": "summarize the open issues",
    },
)

# Stream SSE
with httpx.stream(
    "GET",
    f"{BASE}/sessions/s1/events/e-1/stream",
    headers=HEADERS,
    timeout=None,
) as stream:
    for line in stream.iter_lines():
        if line.startswith("data:"):
            print(line)

Example: connect with the Mash CLI

mash connect \
  --api-base-url http://your-host:8000 \
  --api-key your-api-key \
  --app-id my-app \
  --policy-id dev
mash repl

CORS configuration

By default, the host allows requests from localhost:3000 and localhost:5173. For production, set allowed origins via the --cors-origin CLI flag (repeatable) or configure cors_allow_origins in MashHostConfig.

Tearing down and restarting

  • Restart the host: the new process calls build_host(), which takes a new host id, reconnects to Postgres, starts every agent process again, and adopts the running sandboxes.
  • Stop everything: stop the host, then docker rm -f $(docker ps -aq --filter label=mash.sandbox.policy). Postgres and the sandbox volumes keep all state; each policy's next call recreates its sandbox on the same volume.
  • Remove a policy's workspaces and browser profile: mash policy rm <policy_id> (or DELETE /api/v1/policies/{policy_id}) removes the sandbox, the browser and the policy, once no session on it has a queued or running event.
  • Database migrations: Mash runs pending migrations automatically on startup. See Schema migrations below.

Schema migrations

Mash uses an ordered-file migration runner rather than a third-party tool. All Mash tables (the runtime event store, the memory store, and the evals store) share one schema baseline in src/mash/storage/migrations/, and every store runs the same runner when it opens. On each run the runner:

  1. Takes a Postgres advisory lock, so stores opening concurrently apply each migration exactly once.
  2. Creates a _mash_migrations tracking table if it does not exist.
  3. Reads all .sql files from src/mash/storage/migrations/ in filename order.
  4. Applies any file not yet recorded in _mash_migrations and records the filename and timestamp so it is not applied again. The whole run is one transaction.

The baseline (001_baseline.sql) is idempotent: all CREATE TABLE and CREATE INDEX statements use IF NOT EXISTS, so applying it against an existing database is a no-op.

Adding a migration: Create a new file named NNN_description.sql (e.g. 002_add_model_column.sql) in src/mash/storage/migrations/. The next startup applies it automatically. Write it to be rollback-safe.

Upgrading from an earlier deployment: Releases up to 0.15 tracked runtime and memory migrations separately, in _mash_migrations and _mash_memory_migrations. The combined baseline applies cleanly on top of a database created by those runners: every statement is guarded, so existing tables and data are untouched, and the old _mash_memory_migrations table is simply left behind. MASH_DATABASE_URL is required; the agent raises RuntimeError at startup if it is unset.

Environment variables

Variable Set on Default Purpose
MASH_HOST_APP host none module:attribute for build_host(); agent processes import the same one
MASH_DATABASE_URL host none PostgreSQL connection string; agent processes inherit it. Each deployment needs a database of its own
MASH_DB_POOL_MAX_SIZE host 10 Most Postgres connections each process holds: the host and every agent process
MASH_AGENT_MAX_RUNNING host 32 Most requests each agent process works on at once; agent processes inherit it
MASH_RETENTION_DAYS host none Delete finished requests, their events and finished workflow runs older than this many days; unset keeps everything
MASH_HOST_SECRET host none Derives each agent's, each sandbox's and each browser's token, and signs links; required for a host with agents
MASH_API_KEY host none API key for /api/v1; required unless MASH_API_HOST is a loopback address
MASH_API_HOST host 127.0.0.1 Bind host (0.0.0.0 in containers)
MASH_API_PORT host 8000 Bind port
MASH_PUBLIC_URL host the bind address Where people reach the host; the base of sandbox links and sign-in links
MASH_SANDBOX_IMAGE host ghcr.io/imsid/mash-sandbox:<mashpy version> Sandbox image, pulled when missing
MASH_SANDBOX_MOUNTS host none Comma-separated source:target[:ro] bind mounts added to every sandbox; sources are Docker-host paths
MASH_SANDBOX_IDLE_SECONDS host 900 Idle time before a sandbox stops
MASH_SANDBOX_MAX_RUNNING host 20 Most sandboxes running at once
MASH_SANDBOX_NAMESPACE host, CLI mash Prefix for sandbox and browser containers, volumes and networks (<ns>-sandbox-<key>, <ns>-browser-<key>, <ns>-browsers), so deployments sharing a Docker daemon keep them separate
MASH_SANDBOX_SUBNETS host, CLI 10.213.0.0/16 IPv4 range each sandbox's network takes a /28 from; /28 or wider
MASH_BROWSER_IMAGE host ghcr.io/imsid/mash-browser:<mashpy version> Browser image, pulled when an agent lists sites
MASH_BROWSER_IDLE_SECONDS host 900 Idle time before a browser stops; its profile is kept
MASH_BROWSER_MAX_RUNNING host 10 Most browsers running at once
MASH_BROWSER_HANDOFF_SECONDS host 900 How long a minted sign-in link works; how long a request waits is the policy's wait_max_seconds
MASH_DATA_DIR host /var/lib/mash Persistent data directory
MASH_RUNNER_ID host default Workflow runner identity for this deployment
MASH_AGENT_TOKEN agent process set by the host The agent's token
MASH_HOST_API_URL agent process set by the host The host address for the companion's calls and the sandbox tools
MASH_SANDBOX_ENABLED agent process set by the host 1 when the host has sandboxes, so the agent gets run_code, and sandbox_put and sandbox_get when its spec declares file roots
MASH_BROWSER_ENABLED agent process set by the host 1 when the host has browsers, so an agent whose spec lists sites gets the browser tools
MASH_PARENT_PID agent process set by the host The agent process stops when this process exits
MASH_SANDBOX_TOKEN sandbox set by the host The policy's sandbox token; the sandbox's only variable
MASH_BROWSER_TOKEN browser set by the host The policy's browser token; the browser's only variable
ANTHROPIC_API_KEY host none Anthropic API key; agent processes inherit it
OPENAI_API_KEY host none OpenAI API key; agent processes inherit it
GEMINI_API_KEY host none Google Gemini API key; agent processes inherit it
CORTEX_AGENT_<PROVIDER>_MODEL host the provider's default Model for cortex-agent on a host with Cortex enabled (ANTHROPIC, OPENAI, GEMINI)
OTEL_EXPORTER_OTLP_ENDPOINT host none OTLP/HTTP collector address; setting it (or a per-signal OTEL_EXPORTER_OTLP_{TRACES,LOGS,METRICS}_ENDPOINT) turns the exporter on. Needs mashpy[otel]
OTEL_EXPORTER_OTLP_HEADERS host none Headers for the collector, such as an API key
OTEL_SERVICE_NAME host mash service.name on every exported span, log record and metric
MASH_OTEL_CAPTURE_CONTENT host off 1 adds prompts, completions, tool arguments and results, and error messages to the export