rex-mcp
by yenson-lau
README.md
# rex-mcp
A pure-Python **FastMCP** server for controlling **Rex** terminal multiplexer sessions, windows, and split panes over **Tailscale**.
- **Local Desktop MCP (`stdio`):** Connect Gemini Desktop, Claude Desktop, or local IDEs directly via `scripts/stdio.sh` — no OAuth or Tailscale Funnel required.
- **Remote Web Connectors (`http` + Google OAuth 2.1):** Expose `rex-mcp` via `tailscale funnel` and connect ChatGPT, Claude.ai, or Gemini web connectors directly over HTTPS.
- **Fail-Closed Security:** Requires OAuth 2.1 (or static Bearer token) on HTTP transport, verifies every caller's Google email against `REX_MCP_ALLOWED_USERS`, and restricts `server` overrides to configured `REX_HOSTS` / your Tailscale tailnet (`REX_ALLOWED_TAILNET`).
- **Native Rex Wire Protocol:** Speaks `rex.control.v1` directly over `wss://<host>/api/control` with automatic `session.attach_all` — no local `rex` CLI binary needed.
```text
┌──────────────────────────────────────────────┐
│ Local Desktop App (Gemini / IDE) │
│ └─ stdio ──► scripts/stdio.sh │
│ │
│ Web Connector (ChatGPT / Gemini / Claude) │
│ └─ HTTPS + Google OAuth 2.1 │
│ └─► tailscale funnel (:443) │
│ └─► localhost:8765/mcp │
└──────────────────────┬───────────────────────┘
▼
┌──────────────────────────────────────────────┐
│ rex-mcp (FastMCP · Your Mac on Tailscale) │
│ • GoogleProvider + Email Allowlist (HTTP) │
│ • Tailnet Host Allowlist (REX_SERVER_URL) │
│ • rex.control.v1 WebSocket Client │
└──────────────────────┬───────────────────────┘
│ wss://.../api/control
│ (Tailscale Identity Auth)
▼
┌──────────────────────────────────────────────┐
│ Rex Server(s) on Tailnet │
│ • my-mac-rex.my-tailnet.ts.net │
│ • other hosts via REX_HOSTS (optional) │
└──────────────────────────────────────────────┘
```
---
## Install & Configure Target
```bash
uv sync --extra dev
cp .env.example .env
```
Set `REX_SERVER_URL` in `.env` to your Rex server's Tailscale address (e.g., `https://my-mac-rex.my-tailnet.ts.net`).
---
## Option A: Local Desktop MCP (`stdio`) — Fastest Setup
Best for **Gemini Desktop**, **Claude Desktop**, or local IDEs running on the same Mac. `stdio` automatically skips HTTP/OAuth checks while still reading `REX_SERVER_URL` / `REX_HOSTS` from `.env`.
### Gemini Desktop (`Local MCP`)
Open **Connect to an MCP server → Local MCP** and enter:
- **Name:** `Rex`
- **Description:** `Control Rex terminal sessions, windows, and panes over Tailscale`
- **Command:**
```text
/path/to/rex-mcp/scripts/stdio.sh
```
- **Allow full file access:** Checked *(required so Gemini's sandbox can execute the Python `.venv` under your checkout and Homebrew Python)*.
### Other `stdio` MCP Clients
Point the client's `command` directly to `/path/to/rex-mcp/scripts/stdio.sh` (no extra args or env vars needed).
---
## Option B: Remote / Web Connectors (Tailscale Funnel + Google OAuth 2.1)
Use this to connect web apps (**ChatGPT**, **Claude.ai**, **Gemini Web**) from anywhere over HTTPS.
### 1. Create a Google Cloud OAuth 2.0 Web Client
1. Open [Google Cloud Console](https://console.cloud.google.com/) and select or create a project (no billing required).
2. Open **Google Auth Platform → Branding / Audience** (formerly *OAuth consent screen*):
- Set **Audience** to **External** (so any personal Gmail can sign in).
- Under **Audience → Test users**, add the Google email(s) you plan to sign in with. *(Keeping the app in Testing mode avoids Google app verification.)*
3. Open **Google Auth Platform → Clients → + Create Client**:
- **Application type:** `Web application`
- **Name:** `rex-mcp`
- **Authorized redirect URIs:**
```text
https://my-mac.my-tailnet.ts.net/auth/callback
```
*(Replace with your machine's Tailscale MagicDNS hostname.)*
- **Use this client for an AI-powered agent:** Leave **unchecked** *(only needed when calling Google's own APIs/MCP tools; `rex-mcp` only uses Google for sign-in identity)*.
4. Click **Create** and copy the **Client ID** and **Client Secret**.
### 2. Configure `.env`
Fill in the OAuth fields in `.env`:
```dotenv
REX_SERVER_URL=https://my-mac-rex.my-tailnet.ts.net
REX_MCP_AUTH_MODE=google
REX_MCP_BASE_URL=https://my-mac.my-tailnet.ts.net
REX_MCP_GOOGLE_CLIENT_ID=<your-client-id>.apps.googleusercontent.com
REX_MCP_GOOGLE_CLIENT_SECRET=GOCSPX-<your-client-secret>
REX_MCP_ALLOWED_USERS=you@gmail.com
```
### 3. Start `rex-mcp` + Tailscale Funnel
Run the all-in-one startup script (validates `.env`, enables Tailscale Funnel on HTTPS `:443`, prints all relevant URLs, and runs `rex-mcp` in the foreground; turns Funnel off automatically on `Ctrl+C` unless `--keep-funnel` is passed):
```bash
./scripts/start.sh
```
Or run persistently on macOS with `launchd`:
```bash
cp deploy/com.example.rex-mcp.plist.example ~/Library/LaunchAgents/com.example.rex-mcp.plist
# Edit paths inside ~/Library/LaunchAgents/com.example.rex-mcp.plist
launchctl load -w ~/Library/LaunchAgents/com.example.rex-mcp.plist
tailscale funnel --bg --https=443 http://127.0.0.1:8765
```
Logs are written to `~/Library/Logs/rex-mcp.stdout.log` and `~/Library/Logs/rex-mcp.stderr.log`.
### 4. Connect from Web Apps
Add a Custom MCP Connector:
- **Server URL:** `https://my-mac.my-tailnet.ts.net/mcp`
- **Authentication:** `OAuth`
Sign in with a Google account listed in `REX_MCP_ALLOWED_USERS`, then test with `rex_server_status` and `rex_list_sessions`.
---
## Multi-Host & Optional Modes
### Targeting Multiple Rex Servers on Your Tailnet
Set `REX_HOSTS` in `.env` to map short aliases to Rex servers:
```dotenv
REX_HOSTS={"mac":"https://my-mac-rex.my-tailnet.ts.net","linux":"https://my-linux-rex.my-tailnet.ts.net"}
```
Every tool accepts an optional `server` argument (e.g. `server="linux"` or any `*.<REX_ALLOWED_TAILNET>` hostname). External hosts outside `REX_HOSTS` and `REX_ALLOWED_TAILNET` are blocked to prevent SSRF.
### Static Bearer Token / Prefect Horizon Gateway
To authenticate with a static Bearer token (e.g. behind `https://horizon.prefect.io/` or a service client):
```dotenv
REX_MCP_AUTH_MODE=bearer
REX_MCP_BEARER_TOKEN=<long-random-secret>
```
---
## Configuration Reference
| Variable | Default | Purpose |
| :--- | :--- | :--- |
| `REX_SERVER_URL` | *(required)* | Default Rex server URL or Tailscale hostname |
| `REX_HOSTS` | `{}` | JSON map of short aliases to Rex server URLs |
| `REX_ALLOWED_TAILNET` | *(auto-inferred)* | Allowed `*.ts.net` suffix for ad-hoc `server` overrides |
| `REX_ALLOW_ANY_HOST` | `false` | Disable host allowlist (not recommended) |
| `REX_REQUEST_TIMEOUT` | `15.0` | Per-RPC timeout in seconds |
| `REX_PROXY_URL` | *(unset)* | Optional SOCKS5/HTTP proxy URL |
| `REX_MCP_AUTH_MODE` | `google` | HTTP auth mode: `google`, `bearer`, or `none` (ignored on `stdio`) |
| `REX_MCP_BASE_URL` | *(required in `google` mode)* | Public HTTPS base URL for OAuth metadata & callbacks |
| `REX_MCP_GOOGLE_CLIENT_ID` | *(unset)* | Google OAuth 2.0 Web Client ID |
| `REX_MCP_GOOGLE_CLIENT_SECRET` | *(unset)* | Google OAuth 2.0 Web Client Secret |
| `REX_MCP_ALLOWED_USERS` | *(empty)* | Comma-separated or JSON list of allowed Google emails (required in `google` mode) |
| `REX_MCP_BEARER_TOKEN` | *(unset)* | Static Bearer token (required in `bearer` mode) |
| `REX_MCP_TRANSPORT` | `http` | Default CLI transport (`http` or `stdio`) |
| `REX_MCP_HOST` / `REX_MCP_PORT` | `127.0.0.1` / `8765` | HTTP bind host and port |
| `REX_MCP_ALLOW_INSECURE_LOCAL` | `false` | Must be `true` to run unauthenticated HTTP (`REX_MCP_AUTH_MODE=none`) |
---
## Available Tools
| Tool | Safety | Purpose |
| :--- | :---: | :--- |
| `rex_server_status` | Read-only | Server status, Rex version, Tailscale identity |
| `rex_list_sessions` | Read-only | List all Rex sessions |
| `rex_inspect_session` | Read-only | View windows, layouts, active window, and blocks |
| `rex_capture_terminal` | Read-only | Capture screen and scrollback (`text`, `html`, `vt`) |
| `rex_terminal_info` | Read-only | Get cwd, title, running processes, exit status, listening ports |
| `rex_list_directory` | Read-only | List directory entries relative to terminal cwd |
| `rex_list_api_methods` | Read-only | Discover Rex server API methods (`help.all`) |
| `rex_create_session` | Mutating | Create a new session with an initial shell block |
| `rex_new_window` | Mutating | Open a new window/tab in a session |
| `rex_split_pane` | Mutating | Split a pane (`right`, `left`, `below`, `above`) |
| `rex_send_text` | Mutating | Write text to a terminal (optional Enter) |
| `rex_send_keys` | Mutating | Send keystrokes (`enter`, `ctrl+c`, `esc`, arrows, `f1`–`f12`) |
| `rex_run_command` | Mutating | Run a command in a block or new window and return output |
| `rex_reset_terminal` | Idempotent | Clear screen & scrollback without killing the process |
| `rex_close_block` | Destructive | Close a pane/block and stop its process |
| `rex_close_window` | Destructive | Close a window/tab and all its blocks |
| `rex_destroy_session` | Destructive | Destroy an entire session |
| `rex_api_call` | Destructive | Raw escape hatch for any Rex control API method |
---
## Development & Testing
```bash
uv run pytest -v
uv run ruff check src tests
REX_MCP_TRANSPORT=stdio uv run fastmcp inspect src/rex_mcp/server.py:mcp
```
---
## License
[MIT](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues