Reverse WebSocket bridge for remote terminal and desktop operations.
Embed directly in a FastAPI backend, or run as an authenticated Streamable HTTP / stdio MCP server.
English · 简体中文
Caution
Enabling --allow-shell, --allow-write, or --allow-input grants the connected model or caller the corresponding privileges of the current OS account.
- Clients start in read-only mode (
read, plusscreenon--kind desktop). Do not enable--allow-shellfor an untrusted model or user. - Use a dedicated low-privilege OS account and a long, unique token per device.
- Terminate TLS (
wss://) at a trusted reverse proxy on public networks; do not expose barews://. /mcprequires a dedicatedBACKWIRE_MCP_TOKENseparate from device tokens and rejects requests when unset.
backwire connects machines behind a LAN, NAT, or container (without public IPs, inbound ports, or VPN/TUN interfaces) to a hub over an outbound WebSocket. An embedded FastAPI application or an MCP client routes explicitly authorized operations back to each connected device.
flowchart LR
subgraph Devices["Devices (Outbound Only)"]
M["macOS / Windows\n(desktop: screen + input)"]
L["Linux / Container\n(headless: single .py script)"]
end
H["backwire Hub\n/ws/terminal (Per-device Bearer Auth)\nCapability Check · Audit Log · Rate Limit"]
subgraph Callers["Callers (Provider-Neutral)"]
A["FastAPI Backend\nhub.invoke(...)"]
C["Remote / Local MCP Clients\n/mcp (Dedicated MCP Token)"]
end
M -- "Outbound wss://" --> H
L -- "Outbound wss://" --> H
A <--> H
C <--> H
- Two integration modes: mount
create_ws_routerinside an existing FastAPI backend (without installingmcp), or runbackwire-server(Streamable HTTP/mcp) orbackwire-mcp(local stdio). - Standalone device script: copy
client/backwire_client.pyto any Python 3.9+ machine; headless mode depends only onwebsockets. - Access controls & logging: per-device tokens (wildcard
*disabled), capability checks on both hub and client,--workdirpath restriction for file APIs, and redacted JSONL audit logging.
Note
Support for remote Streamable HTTP MCP clients depends on each client's current version and account capabilities. Verify client support before deployment.
Install from source:
git clone https://github.com/chuli1122/backwire.git
cd backwire
python3 -m venv .venv
. .venv/bin/activate
# Core package and WebSocket hub (Python 3.10+; no MCP dependency)
pip install -e .
# Core package + optional Streamable HTTP / stdio MCP transport
pip install -e '.[mcp]'
# Optional desktop automation dependencies
pip install -e '.[desktop]'- Without
.[mcp](or withbackwire-server --no-mcp),/mcpis not mounted and returns404. - Tested with MCP SDK
1.27.0and2.x(MCPServer); if another SDK version changes transport APIs and fails to initialize, check startup logs and pin a tested version. - Device client:
client/backwire_client.pyis standalone and supports Python3.9+(websocketsfor headless; pluspyautogui,pillow, andpyperclipfor desktop mode).
from fastapi import FastAPI
from backwire import BackwireConfig, BackwireHub, create_ws_router
config = BackwireConfig.with_tokens({
"laptop": "REPLACE_WITH_LONG_RANDOM_DEVICE_TOKEN",
})
hub = BackwireHub(config)
app = FastAPI()
app.include_router(create_ws_router(hub, config))
# Call directly from your application code:
# devices = await hub.list_devices()
# result = await hub.invoke("read_file", {"path": "notes.txt"})
# result = await hub.invoke("run_bash", {"command": "uname -a"}, timeout=65)See examples/embed_fastapi.py for a complete example with application-layer Bearer authentication and explicit target device selection. Application routes calling hub.invoke require their own authentication and must not be exposed unauthenticated.
Device tokens and MCP tokens are configured separately (plaintext or sha256:<64 hex>):
export BACKWIRE_TOKENS_JSON='{"laptop":"REPLACE_WITH_LONG_RANDOM_DEVICE_TOKEN"}'
export BACKWIRE_MCP_TOKEN='REPLACE_WITH_LONG_RANDOM_MCP_TOKEN'
backwire-server --host 127.0.0.1 --port 8000| Endpoint | Purpose |
|---|---|
GET /healthz |
Minimal health check ({"ok": true}) |
WSS /ws/terminal |
Device reverse-WebSocket endpoint (Authorization: Bearer <device-token>, X-Device-Name) |
POST/GET /mcp |
Canonical Streamable HTTP MCP URL (Authorization: Bearer <mcp-token>) |
- MCP tools:
list_devices,run_command(maps to corerun_bash),read_file,write_file,screenshot,mouse_click,keyboard_type,hotkey,scroll. - Default device routing: when
deviceis omitted, the hub checksBACKWIRE_DEFAULT_DEVICE, then prefers adesktopdevice ifBACKWIRE_PREFER_DESKTOP_DEFAULT=true, then picks the first connected device. Specifydeviceexplicitly in production. - Local stdio MCP:
backwire-mcp --ws-host 127.0.0.1 --ws-port 8765runs a stdio MCP server alongside a WebSocket listener (it does not serve HTTP/mcpor/healthz). Binding--ws-hostto a non-local interface exposes plaintext WebSocket.
Create the working directory first (--workdir validates path boundaries and does not create directories):
mkdir -p "$HOME/backwire-work"
python3 client/backwire_client.py \
--url wss://your-host.example/ws/terminal \
--token 'REPLACE_WITH_LONG_RANDOM_DEVICE_TOKEN' \
--name laptop \
--kind headless \
--workdir "$HOME/backwire-work"- Default capabilities & transport: read-only (
read, plusscreenfor--kind desktop). Non-local plaintextws://is rejected by default unless--allow-insecure-wsis passed (which prints a warning). Higher capabilities require--allow-write,--allow-shell, and--allow-input. - File & screenshot limits:
--workdirblocks path traversal and symlink escapes forread_fileandwrite_file; if omitted, file APIs can access any path readable by the current OS account.read_filerejects non-regular files, caps each call at 200,000 characters / 512 KiB, and supports byte-offset pagination viaoffset(returningoffset,next_offset,truncated,size_bytes).screenshotreturns in-memory PNG base64 and does not write to disk. - Shell & reconnection:
--workdiris not an OS sandbox for shell commands.run_bashusessubprocess.run(..., shell=True)on Windows and prefers/bin/bash(falling back to the default shell) on POSIX, with hard caps on timeout (300 s) and output (200 KiB) plus process-group / process-tree termination. Disconnected clients retry with 1–30 s exponential backoff.
backwire |
Tailscale + SSH | |
|---|---|---|
| Connection direction | Device initiates outbound WebSocket (no inbound ports or TUN) | Operator reaches device over private network |
| Model / tool integration | Embedded FastAPI hub and structured MCP tools | Requires separate SSH wrapper / integration |
| Desktop screen & input | Optional explicit capabilities (desktop mode) |
Not included in SSH |
| Security boundary | Per-device token, capability checks, --workdir restriction, audit log |
Network identity, SSH keys, OS account policy |
| Best fit | A controlled application or agent needing a small tool surface | Human administration and general-purpose shell |
Nginx Reverse Proxy (WSS + Streamable HTTP MCP)
Match both /mcp and /mcp/*, forward Host, and disable response buffering:
location /ws/terminal {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
location ~ ^/mcp(?:/|$) {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}- Set
BACKWIRE_MCP_ALLOWED_HOSTS(e.g.mcp.example.com,mcp.example.com:443) on public deployments to enable DNS rebinding protection matching the proxy'sHostheader. When unset, DNS rebinding protection is disabled for reverse-proxy compatibility. - Set
BACKWIRE_TRUSTED_PROXIESto the fixed proxy IPs or CIDRs (for a local Nginx, e.g.127.0.0.1,::1) to useX-Real-IPorX-Forwarded-Forfor auth-failure rate limiting. When unset, rate limiting usesclient.host; behind a reverse proxy, all requests then share the proxy address counter. Forwarded headers are ignored unless the direct peer is trusted.
Environment Variables & Defaults
| Variable | Default / Notes |
|---|---|
BACKWIRE_HOST / BACKWIRE_PORT |
127.0.0.1 / 8000 (overridden by --host / --port) |
BACKWIRE_TOKENS_JSON / BACKWIRE_TOKENS_FILE |
Per-device tokens (JSON takes priority; plaintext or sha256:<hex>; * wildcard rejected) |
BACKWIRE_MCP_TOKEN / BACKWIRE_MCP_TOKENS_JSON |
Dedicated MCP tokens (BACKWIRE_MCP_TOKEN takes priority; required for /mcp) |
BACKWIRE_MCP_ALLOWED_HOSTS |
Empty (DNS rebinding check off); comma-separated host allowlist |
BACKWIRE_TRUSTED_PROXIES |
Empty (forwarded headers ignored); comma-separated proxy IPs or CIDRs, e.g. 127.0.0.1,::1 |
BACKWIRE_ALLOW_QUERY_TOKEN |
false (keep disabled on public deployments to avoid log leaks) |
BACKWIRE_MAX_MESSAGE_BYTES |
8388608 (8 MiB) |
BACKWIRE_COMMAND_TIMEOUT_SECONDS |
65 seconds (hard ceiling 300 s) |
BACKWIRE_MAX_AUTH_FAILURES_PER_MINUTE |
12 per source address |
BACKWIRE_AUDIT_LOG_PATH |
Empty (off); writes redacted JSONL entries (rejects symlink targets) |
BACKWIRE_DEFAULT_DEVICE |
Empty; explicit default device name |
BACKWIRE_PREFER_DESKTOP_DEFAULT |
true |
BACKWIRE_URL, BACKWIRE_TOKEN, BACKWIRE_NAME, BACKWIRE_KIND, BACKWIRE_WORKDIR |
Device-client env equivalents (headless default kind) |
Device Client Autostart Templates (deploy/)
deploy/ contains device-client startup templates only (no server unit or TLS configuration):
- systemd (Linux): system-level template; install
deploy/systemd/backwire-client@.serviceto/etc/systemd/system/, create the user's~/.config/backwire/client.env(chmod 600) withBACKWIRE_URL,BACKWIRE_TOKEN,BACKWIRE_NAME,BACKWIRE_WORKDIR, copyclient/backwire_client.pyto~/bin/, then runsudo systemctl daemon-reload && sudo systemctl enable --now backwire-client@<user>.service. Do not use arootinstance orsystemctl --user. - launchd (macOS): copy
deploy/launchd/com.backwire.client.plistto~/Library/LaunchAgents/anddeploy/launchd/run_backwire_client.sh(chmod +x) to~/bin/, store credentials in~/.config/backwire/client.env(chmod 600, parsed as key-value pairs rather than sourced), update the wrapper path, and runlaunchctl load. - Windows: copy
client/backwire_client.py, set user environment variablesBACKWIRE_URL,BACKWIRE_TOKEN,BACKWIRE_NAME, andBACKWIRE_WORKDIR(never pass tokens in CLI args), then launch viadeploy/windows/run_hidden.vbs. - Container: place
client/backwire_client.pyat/opt/backwire/backwire_client.py, inject theBACKWIRE_*environment variables, and rundeploy/container/profile-autostart.sh.