Skip to content

Development services

A Service is a long-running process you declare on a session: the dev server, a database, Storybook. It runs inside the session workspace, next to the agent and the worktree, and like every other session process it keeps running when you disconnect. Mend never scans the workspace for listeners and never exposes anything you did not name; a Service exists because you declared it.

A workspace is a container with no published ports. When Vite listens on port 3000 in there, that port exists only inside the container, and your machine cannot see it. So you declare it:

Terminal window
mend service run --port 3000 --http -- pnpm dev

Mend supervises the command, waits for port 3000 to answer, and opens a port on your machine (your laptop, not the Mend host), loopback only: http://127.0.0.1:43127. Open it. That is your app, hot reload and all, because Mend pipes raw bytes and rewrites nothing. Pass --no-connect to start the Service without opening that port here.

Mend waits up to a minute for the port to answer. A script that starts a slow Service (a build, an install, a cold boot) passes --wait: Mend keeps waiting while the Service’s process runs and its port has not answered, up to --timeout (10 minutes by default), and exits 0 once it answers, 2 when the process ended first, and 124 when it is still starting at the timeout. A waited start opens no port here; mend service connect does that afterwards. The exit codes are in the CLI reference.

One exception: when the CLI’s server URL is localhost, mend service run treats the server’s own forward as your machine and prints it instead of tunnelling. On the Docker deployment that forward lives inside the application container, so run mend service connect <name> there to bring the port to your loopback.

Behind that port, Mend bridges each TCP connection through to the dev server:

browser ──TCP──▶ Mend's listener on your machine (:43127)
──WS───▶ Sealant API
──pipe─▶ sealantd, inside the container
──TCP──▶ Vite on 127.0.0.1:3000 (an ordinary local connect)

The dev server needs no configuration and no rebinding to 0.0.0.0. To it, every connection looks like a client on its own loopback.

That is the only port in the picture, and it lives on your machine, bound to 127.0.0.1. It is not opened on the Mend host and not on any network interface. When the server is remote, the CLI stays running to hold the port locally and bridges the bytes over the authenticated connection it already has to the server; Ctrl-C closes the bridge, not the Service. There is nothing to expose, nothing to firewall, and nothing ever touches the public internet.

A Service the agent or the web started needs no command of yours. While mend attach (or mend codex, mend rejoin, the dashboard) is attached to a session on a remote server, every live Service of that session declared --http or --https is tunneled the same way, on its own port when that port is free here: web → http://localhost:5173. The tunnel closes when the Service stops or when you detach. --no-tunnel turns this off. The web app, looking at a remote server, shows mend service connect <name> where an Open link to the server’s loopback would not load.

Where the server serves previews, the web app’s Open goes to the worktree’s preview of that Service instead. The link is /preview-auth, which signs you in to the preview’s own address, so it never depends on where the Service’s port is. An https Service, or one Mend adopted without a command, keeps its own address, because the preview gateway does not serve it. The preview’s status line shows under the Service’s row (preview · web · answered HTTP 200 · observed 4 s ago), and a preview that another session of the same worktree runs gets its own row.

The phone app’s session screen has a Preview action for the same preview, and the same status lines under its header. It shows only where the server serves previews on the tailnet or a domain: a phone cannot reach a preview at *.localhost. See Phone.

You can declare a Service from three places, and the result is the same:

  • A recipe in .mend/mend.toml. The repository’s own declaration: every session can start web by name, and the recipe travels with the code.
  • The CLI, wrapping the command you already run in mend service run.
  • The agent itself. Each workspace has a scoped-down mend on its PATH that can run, adopt, list, stop, and restart Services for its own session. When the agent starts a dev server, it can declare it properly instead of leaving a listener nobody can reach. It cannot open ports or change exposure; that authority stays on the server.

A Service can also adopt a port that something else already listens on inside the workspace. Adoption makes it reachable without supervision: there is no Mend-owned process to restart and no log beyond what started it.

Recipes live in .mend/mend.toml, one table per Service. Root mend.toml is still read for existing repositories. When both exist, .mend/mend.toml wins; Mend does not combine them. .mend/ holds Mend’s repository files, including files a future setup session may write.

The file location and resource behavior below follow the design agreed on November 1, 2026. They replace the October 31 build’s resource behavior; this documentation change does not implement them. See Resources for definitions, explicit starts and env.

For example:

[setup]
run = ["pnpm install --frozen-lockfile"]
inputs = ["pnpm-lock.yaml"]
[service.web]
command = "pnpm dev --port 3000 --host 127.0.0.1"
port = 3000
browserScheme = "http"
cwd = "apps/web"
env = { NODE_ENV = "development" }
ready = { http = "/", timeout = "120s" }
depends = ["db"]
[service.db]
command = "docker compose up db"
port = 5432
address = "docker"
[service.cache]
# no command: adopt a listener something else starts (a compose sidecar, a daemon)
port = 6379
[service.game]
command = "node server.js"
port = 9000
protocol = "udp"
[preview]
service = "web"

The fields, and what Mend refuses in each:

  • port is the only required field: where the process listens inside the workspace, 1–65535.
  • command is the shell command Mend supervises. Leave it out for an adopt-only recipe: Mend binds the listener but supervises nothing.
  • protocol is "tcp" unless you say "udp".
  • browserScheme ("http" or "https") is what gives a Service its Open action. TCP alone never implies HTTP, and a UDP recipe cannot declare one.
  • cwd is the directory the command runs in, relative to the repository root. An absolute path, a path with .., and one with a NUL byte are refused.
  • env sets the recipe’s own environment variables, strings only. Values may reference MEND_* resource variables, for example env = { DATABASE_URL = "${MEND_DB_URL}" }. Mend resolves them when it starts the Service. Setting a MEND_* name or PORT is still refused.
  • ready says when the Service is up. { http = "/health" } waits for any HTTP answer for that path (a 403 counts), over TLS for an https Service, whose own certificate Mend takes without checking it. { timeout = "2m" } changes how long to wait, from 1 second to 10 minutes, for an adopted port too. Without it, Mend waits up to a minute for the port to accept. An HTTP check is refused on UDP and on a recipe with no command. The same rule holds after the start: a port that only accepts connections never reads as up for an HTTP check.
  • host is the Host your app sees on a preview’s requests: "rewrite" (the default, localhost:<port>) or "preserve" (the preview’s own host, for an app that builds its links from it, such as WordPress). It needs browserScheme = "http".
  • address = "docker" marks a port Docker publishes, as a compose service’s. Mend dials it by port, never claiming its process, and its preview says port-scoped.
  • depends names recipes of the same session to start first, in order. One already running is not started again, but Mend waits for it to answer its own ready first. The first that fails or never answers stops the start and says which, and the ones already started keep running. A name no recipe has, the recipe itself, and a cycle are refused.
  • depends = ["@db"] names the resource this worktree uses, at project or worktree level. Resources require an explicit first start; a dependency or session launch never performs it. The Service gets MEND_DB_URL, MEND_DB_HOST and MEND_DB_PORT; Postgres adds _USER, _PASSWORD and _DATABASE. Mend sets no DATABASE_URL, PG*, REDIS_URL or other conventional resource env. Map the variable your app expects in recipe env, as above. An existing explicit mapping such as depends = [{ shared = "db", env = { TEST_DATABASE_URL = "url" } }] also opts in; resource is a synonym for shared. Duplicate mapped names and recipe env that shadows a binding are refused by name. A missing resource, or one the worktree has off, refuses only that Service. Mend waits up to 30 seconds for readiness, then starts the Service anyway and says @db not ready. A preview wake restarts only a resource stopped for idleness. A person’s or agent’s stop holds until an explicit Start. Stopping a preview leaves its resources running.
  • [preview] names the Service the worktree’s preview shows. It must be a recipe with browserScheme = "http".
  • [setup] is what a start runs before anything else: run is a list of shell commands, run in order at the repository root as the person the Service runs as, and inputs lists files whose contents decide when to run them again. An empty run, a step that is not a string, and an input path that is absolute, has .. or names the repository itself are refused.

OAuth providers often require an exact callback URL registered in advance. To keep one preview address while changing worktrees, opt in on the project’s default branch:

[preview]
service = "web"
fixed = true

Name a runnable Service with browserScheme = "http". The project reads this declaration from its shared Local default-branch worktree if one exists, otherwise from the stored default branch. The selected target supplies the Service recipe. Its workspace port remains the recipe’s fixed port.

Run these from an adopted checkout, or add --project <project>:

Terminal window
mend preview start
mend preview project --json
mend preview use <worktree-id-or-name>
mend preview use --default

Start uses the shared Local worktree on the actual default branch unless you selected another target. It creates that checkout only on explicit Start, reuses it afterwards and preserves its edits. Choosing a target starts nothing. Start the chosen target explicitly when needed. Mend remembers your choice across restarts; starting another session never switches it. Removing a selected worktree leaves the address without a target until you choose another.

The project’s Worktrees tab shows the fixed URL, target and observed status, with Use target and Start controls for project managers. The phone shows these facts read-only. Open the web preview in a new tab and complete Mend sign-in before beginning the app’s own sign-in.

The fixed preview proxies directly to the target. On a preview domain its random hostname stays the same. On the ts.net default, one external port is permanently reserved for the project, including when no target is running. Unlike ordinary worktree preview ports, inactivity never reassigns it. A full port pool refuses a new allocation. If the operator removes a reserved port from that pool, Mend asks them to restore it rather than change a registered address. Project removal releases the reservation. Changing the operator’s domain or ts.net hostname still needs new provider registrations.

The URL and target survive Service restarts, workspace replacements and new worktrees. Target switches close existing preview connections and recheck access and workspace lending. Browser app cookies remain at the same origin, so a switch can retain the previous target’s app sign-in. Normal worktree previews remain available, each at its own address.

On its next start, the selected Service gets this fixed origin as MEND_PREVIEW_URL. Selecting it does not restart a running Service or change its existing environment. Map that variable to the app’s actual public/base-URL setting, then explicitly restart the Service when needed. Never set a MEND_* key in recipe env.

See setup and provider registration for exact callback rows and the localhost tunnel route. A fixed address does not change the gateway’s cross-origin POST restriction.

When a start runs any command, Mend runs [setup] first, then the dependencies, then the Service. It runs setup again only when something it covers changed: a step, the contents of an input (an input that appears or goes counts), or the workspace image. Otherwise the start says setup · unchanged since it last ran here and goes on. mend service <name> --setup runs it anyway, for example after git clean -fdx removed what it installed.

  • Where it is remembered. Per worktree when Mend runs beside your files, which every session in that worktree shares, and per workspace in a captured workspace. Removing a worktree forgets it. One start at a time runs setup in one place: a second start waits for the first, then reads its inputs again and uses the first one’s result if they still match.
  • What an input can be. A file in the repository. One that links outside it, a directory, or anything else that is not a regular file refuses the start and says which. In a captured workspace Mend reads each input there as you, and keeps only its hash, never its contents.
  • How long a step may run. Thirty minutes. A step that runs longer is stopped, and the start says so; the next start runs setup again.
  • When a step fails. The start stops, and says the step, its exit code and the end of its output. Your next start runs setup again. A start Mend makes on its own, such as restarting Services after it replaces a workspace, does not: it says setup failed and asks for your start.
  • When it is refused. A bad [setup] refuses no recipe: they all still list, your start goes on without setup and says so, and a start Mend makes on its own is refused.
  • Stopping it. Stop the Service (mend service stop <name> works before the Service has started), or all of the session’s Services, while setup runs: the running step and everything it started end, and the next start runs setup from the beginning.
  • A restored workspace. A captured workspace that comes back as a new workspace runs setup at its first start, even if the capture already holds what setup made. Mend does not share setup’s output across workspaces until restoring has been measured against installing.

Setup holds up no other Service’s start or stop, however long it takes, and keeps its workspace running while it does. mend service <name> --wait shows each step as it runs.

A start with dependencies can take a while, so Mend runs it to the end on its own: closing the terminal, pressing Ctrl-C at the spinner, or an edge cutting the request does not stop it. A Stop of one of its Services does. mend service <name> --wait follows it and names each dependency as it starts and answers, for as long as --timeout allows.

Each started Service also gets PORT (its port) and, when its preview has an address, MEND_PREVIEW_URL. For each recipe it depends on it gets MEND_SERVICE_<NAME>_HOST and MEND_SERVICE_<NAME>_PORT, where that dependency answers inside the workspace, with the name upper-cased and anything outside A-Z0-9 turned into _ (db-main gives MEND_SERVICE_DB_MAIN_HOST). A Docker-published dependency gets the host Mend reaches it on. Two dependencies that would give the same names refuse the recipe. A restart runs what the Service ran before, with the same directory, environment and dependencies, even after .mend/mend.toml changed; a new start reads the file again. After a resource level switch, Mend restarts the worktree’s Services with current resource values and tells the agent which variables changed. Other running processes, including the agent’s shell, keep their old values. mend resource env prints current exports; it does not update a running shell. Nothing in the file starts on its own.

A problem with one recipe refuses that recipe alone, naming the key, and the others still run. mend service <name> tells you why a refused one does not. A key Mend does not know is ignored and listed, never refused. These keys are read from Mend 0.37 on: an older server ignores them, runs the command at the repository root, and does not say so.

Use a fixed workspace port when the app’s authentication assumes one. The command must listen on that same port, either by reading Mend’s PORT or by taking an explicit flag. Make the dev server fail if it cannot bind that port. For Vite, for example, pnpm dev --port 3000 --strictPort matches port = 3000. A dev server that silently moves to 3001 breaks both the declared Service and any OAuth callback registered for 3000.

There are three different addresses to consider:

Access Address to configure in the app and provider
Browser inside the workspace The fixed Service port, such as http://localhost:3000, plus the app’s callback path
Mend preview The actual preview origin, including its external port when present, plus the app’s callback path
CLI tunnel on your machine http://localhost:<port>, plus the app’s callback path; pin it with mend service connect <service> --port <port>

A workspace’s localhost is not your laptop’s. For OAuth on your machine, register http://localhost:<port>/<callback path> with the provider and run mend service connect <service> --port <port> using that exact local port. Open http://localhost:<port> there and keep the tunnel running during sign-in. Configure the app’s public/base URL for that origin, separately from its preview URL, and restart the Service if needed. Without an explicit --port, a tunnel can choose another local port when the preferred one is unavailable. A Service’s workspace port does not pin the tunnel or preview’s external port.

Mend supplies MEND_PREVIEW_URL to a Service once its preview has an address. Your launch command can pass that value to the app’s public/base-URL variable. For example, an app that reads AUTH_URL can prefix its command with AUTH_URL="$MEND_PREVIEW_URL" when the value is set. Document a missing address rather than passing an empty base URL. The agent’s shell does not automatically receive this Service env. Do not set PORT or MEND_* in recipe env; Mend owns those names. host = "rewrite" sends Host: localhost:<port> with X-Forwarded-Host and X-Forwarded-Proto, while "preserve" sends the preview host. Configure the app’s base URL and trusted proxy/origin settings for its auth library.

Ordinary preview addresses belong to a worktree and Service. Without fixed = true, each new worktree gets a new preview address and needs its own OAuth provider registration. On the ts.net default, an ordinary preview’s external port is allocated separately and can be reassigned after inactivity. A fixed project preview reserves its hostname or external port across worktrees. Operator domain or hostname changes still require registration again. Copy the actual app origin after opening the preview in a new tab and completing Mend sign-in; do not register /preview-auth or /__mend/auth as your app’s callback.

Setup uses the configured fixed address reported by Mend in the preview registration row. If an opted-in fixed preview has not been allocated yet, it records unknown with explicit Start and address-discovery steps. Otherwise it records the actual per-worktree address when known. The person’s pinned localhost registration row remains an alternative.

The gateway refuses cross-origin POST callbacks, including OIDC form_post and SAML POST responses. Registering the URL alone does not make those flows usable. Record the app/provider limit or a GET callback alternative it supports. The setup-session guide explains what to register and how to record an unknown preview address for later completion.

The table name is the lookup key (mend service web): lowercase letters and digits plus ., _, and -, up to 64 characters.

Mend reads the file from the session’s worktree, not from the project’s main branch. In a captured workspace (the default store) the worktree lives only in the workspace, so Mend reads /workspace/repo/.mend/mend.toml from the live workspace. A session with no live workspace lists only the project’s recipes, and mend service <name> asks you to resume it first. Two things follow. An agent can add a recipe as part of its change, and the addition reviews like any other edit. And two sessions on different branches can carry different recipes. A file that is not valid TOML is a named error, never a guess; a missing file means no declared recipes. Recipes declared on the project in the web app join the same set, and on a name collision the file wins, because it travels with the code.

mend service init scaffolds the file from the package and Compose files it finds, and shows you the result before writing it. A workspace package gets its own cwd, a known dev server gets browserScheme = "http", a Compose service gets address = "docker" (a UDP one stays on loopback), and when exactly one Service opens in a browser it becomes the [preview]. A folder or Compose service whose name is not a recipe name gets one (My_App becomes my_app). With a lockfile at the root, it adds a [setup] that installs from it without changing it (pnpm install --frozen-lockfile, yarn install --frozen-lockfile or, on Yarn 2 and later, yarn install --immutable, bun install --frozen-lockfile or npm ci), with the lockfile as its input.

Every start is an attempt with a recorded log you can replay and then follow live. mend service list prints each Service’s id and its current attempt’s process id, which mend logs --process and mend wait --process take; mend logs --service <name> reads the current attempt’s log. Restarting adds another attempt under the same Service identity and endpoint, so history accumulates instead of being replaced. Status words are observations: “reachable” means the declared target answered when Mend checked, not a guarantee about the next request.

A live Service also keeps the session workspace retained after the agent settles, the same way a detached shell does. Stopping the agent does not stop its Services. The session then reads, for example, agent stopped · 3 services keep the workspace up, in the web app, the dashboard, and the output of mend stop. Stop them when you are done, or let them hold the workspace deliberately:

  • Stop services on the session page in the web app;
  • mend stop --services [session] from the terminal;
  • ⇧K in the dashboard, on a row whose agent has stopped while its Services keep the workspace up.

Once nothing is live, the workspace closes.

Every command is listed in the CLI reference: mend service run, add (adopt a port something in the workspace already listens on), list, logs, restart, stop, and connect, recipe scaffolding with mend service init, mend stop --services for all of a session’s Services at once, and the in-workspace helper’s smaller set.