CLI agent: forward localhost with npm
The Mercur CLI is a device agent you install from npm. HTTP works without an account:
npx @mercur_dev/cli http 3000That forwards public HTTPS to localhost:3000 and prints a share URL for logs. Guest
tunnels idle out 24 hours after you stop.
WebSocket and TCP, and a stable workspace URL, need a workspace-scoped CLI token. Create
the endpoint in the console first — authenticated http / ws / tcp reuse a stopped
endpoint; they do not auto-create. start <id> --port is the advanced, explicit path.
Open the console
When to use the CLI vs the macOS app
- macOS app — tray agent on a Mac, console-driven start/stop, log-only mode, request inspector in the product.
- npm CLI (
@mercur_dev/cli) — any OS with Node.js 20+, CI, and scripts. HTTPhttp/startforwards and writes a request log by default.
One device holds an endpoint at a time. The CLI and the desktop app cannot both claim the same endpoint.
Install
Requires Node.js 20 or later from nodejs.org.
npm install -g @mercur_dev/cliConfirm the binary:
mercur --helpnpx works without a global install:
npx @mercur_dev/cli http 3000npx @mercur_dev/cli --helpGuest HTTP
No token. A guest id is stored in config.json (mode 0600) and reused until the 24h
idle lease expires.
mercur http 3000--no-log turns off request capture. The share link still lets anyone send traffic to
your tunnel.
Workspace token (advanced)
- Sign in at the console.
- Open Settings → Account → CLI tokens.
- Pick the workspace (auto-selected if you have one), name the token, create it.
- Copy the secret immediately. It is shown once and starts with
mcrcli_.
Open CLI tokens
Do not paste a workspace API key (mcr_…) or the Agent Auth Token. Those are different
credentials. Tokens minted before workspace scoping no longer work — create a new one.
Authenticate
mercur authPaste the mcrcli_… secret when prompted, or pass it in one shot:
mercur auth mcrcli_…The token is written to a mode 0600 file: ~/.config/mercur/config.json on macOS and
Linux, or %APPDATA%\mercur\config.json on Windows.
One-command tunnels
With a token, the CLI searches that one workspace for the oldest stopped, unoccupied endpoint of the protocol, binds the port, and starts proxy:
mercur http 3000mercur ws 4000mercur tcp 5432If none is free, the CLI prints why (log-only inspector, failed, already running on this machine, occupied elsewhere, or plan) and a console link. It does not create an endpoint.
List endpoints
mercur listYou see endpoints in this token’s workspace: id, protocol (tcp for raw TCP), local port,
state, public URL, and workspace name. Copy the endpoint id for start and stop.
If the table is empty, create an endpoint in the console first.
Start a specific endpoint
Run something on the local port you will forward to, then:
mercur start ep-abc123 --port 3000The process stays in the foreground and prints the public URL mapped to localhost. Leave
that terminal open. HTTP tunnels write a request log by default; inspect it in the
console. Turn capture off without stopping the tunnel:
mercur start ep-abc123 --port 3000 --no-log--no-log applies only to HTTP. WebSocket traffic is inspected on the
Connections tab, not as HTTP request rows. TCP has no
session inspector.
One CLI process handles one endpoint. Open a second terminal to run a second tunnel.
Plan limits apply exactly as they do in the console. If the protocol is not on your plan, the CLI prints the API error and a link to upgrade.
Stop a tunnel
Stop with Ctrl+C in the tunnel terminal, or from another terminal on the same
machine:
mercur stop ep-abc123stop only releases a claim this machine started. If the desktop app or another host
holds the endpoint, stop it there. Without a token, stop uses the saved guest session.
CI and environment variables
Keep the tunnel command in the foreground. The tunnel lives only while that process is running.
| Variable | Meaning |
| --- | --- |
| MERCUR_TOKEN | CLI token. Overrides the file. Prefer this in CI so the secret never hits disk. |
| MERCUR_API_URL | Console origin. Default https://console.mercur.sh. |
export MERCUR_TOKEN=mcrcli_…
mercur http 3000 --no-logCreate a token named for the workflow and revoke it when the job is retired. CI examples
use --no-log so webhook payloads are not stored in request history.
What the CLI does not do
- Auto-create endpoints for authenticated users — use the console.
- HTTP log-only mode (capture without forwarding) — use the console or the macOS app.
- Steal an endpoint another device already holds.
Revoking a CLI token stops tunnels started with that token. The desktop agent is unaffected.
Troubleshooting
No CLI token found — HTTP still works: npx @mercur_dev/cli http 3000. For a
workspace, create a token in Settings and run mercur auth, or set
MERCUR_TOKEN.
That does not look like a CLI token — CLI tokens start with mcrcli_.
Unauthorized / workspace-scoped token — mint a new token in Settings (pick a
workspace). A token from a local console will not work against production. Set
MERCUR_API_URL if you are not using production.
Already running / occupied — another mercur http on this machine, the
desktop app, or a different host holds the endpoint.
Nothing reaches localhost — confirm the process printed Forwarding … -> localhost
and that something is listening on the port you passed.
Relay errors such as 503 No agent for client are the same as for the desktop app. See
troubleshooting.
Related
- Inspect a WebSocket session — Connections tab, share, MCP
- Expose a local service — shortest HTTP path
- For coding agents — npx
--jsonplaybook for IDE agents - How to test webhooks
- Documentation