herdr-mcp
by Pakheria
README.md
# herdr-mcp
MCP server that puts the `herdr` CLI (`hc`) behind an authenticated HTTP endpoint, so any MCP client on any machine can list terminals, read their screens, type into them, run detached programs, and wait for exit codes.
Two files, zero dependencies: `server.mjs` (protocol + tools) and `oauth.mjs` (authorization server). Runs on Node.js 20 or newer.
## What it implements
- **Transport:** MCP Streamable HTTP, JSON responses only (no SSE stream). Single endpoint `POST /mcp`.
- **Protocol version:** `2026-07-28`, advertised on `initialize` and `server/discover`.
- **Extension:** `io.modelcontextprotocol/tasks`. `herdr_run_task` starts a detached program and returns a `CreateTaskResult` (`resultType: "task"`) instead of a `CallToolResult`. `tasks/get`, `tasks/cancel`, and `tasks/update` are supported. Task TTL is 1 hour, poll interval 3 seconds.
- **Capability gate:** a client that calls `herdr_run_task` without declaring the tasks extension in `params._meta["io.modelcontextprotocol/clientCapabilities"].extensions` still gets a normal `CallToolResult` (never a `CreateTaskResult`), and `tasks/*` calls fail with JSON-RPC `-32021`.
## Tools
| Tool | Required args | Description |
| --- | --- | --- |
| `herdr_machines` | none | List herdr machines and their state |
| `herdr_ls` | none | List terminals on all machines (`all: true` includes finished) |
| `herdr_read` | `terminal` | Read a terminal screen and history (`lines`) |
| `herdr_send` | `terminal`, `text` | Paste text into a terminal and press Enter |
| `herdr_keys` | `terminal`, `keys[]` | Press named keys (`enter`, `esc`, `up`, `ctrl+c`) |
| `herdr_interrupt` | `terminal` | Press the stop key in a terminal |
| `herdr_exec` | `argv[]` | Run one program without a terminal, return output and exit code |
| `herdr_run` | `argv[]` | Start a program in a new persistent background terminal |
| `herdr_wait` | `terminal` | Block until a terminal finishes, return exit code and output |
| `herdr_run_task` | `argv[]` | Start a program as a background MCP task |
| `herdr_file_read` | `path` | Read a text file locally or on another machine |
A `terminal` is an address (`machine/t7`), a terminal id (`t7`), or a name. Optional args: `on` (machine), `cwd`, `timeout`, `name`, `lines`. Arguments are passed to `hc` as an argv array via `execFile` — no shell, no string interpolation.
## Setup
```sh
git clone https://github.com/Pakheria/herdr-mcp
cd herdr-mcp
cp .env.example .env
# edit .env: set OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, APPROVE_PASSWORD (16+ chars each)
set -a; . ./.env; set +a
node server.mjs
```
Requires `hc` on `PATH`. The server binds `127.0.0.1:8791` — put a reverse proxy or tunnel in front of it to expose TLS; the OAuth issuer and resource URLs are derived from the `Host` header with the scheme fixed to `https`, so the metadata is only correct behind a TLS-terminating proxy.
Client configuration (any MCP client that supports remote HTTP servers with OAuth):
```json
{
"mcpServers": {
"herdr": {
"type": "http",
"url": "https://your-host.example/mcp"
}
}
}
```
## OAuth surface
| Endpoint | Purpose |
| --- | --- |
| `GET /.well-known/oauth-protected-resource` | Resource metadata (`resource`, `authorization_servers`) |
| `GET /.well-known/oauth-authorization-server` | Authorization server metadata |
| `POST /register` | Dynamic client registration (redirect URIs restricted to the allowlist) |
| `GET/POST /authorize` | Consent page, gated by `APPROVE_PASSWORD` |
| `POST /token` | `authorization_code` (PKCE S256) and `refresh_token` grants |
| `POST /mcp` | MCP endpoint, requires `Authorization: Bearer` |
Flow: unauthenticated `POST /mcp` returns `401` with `WWW-Authenticate: Bearer resource_metadata=".../.well-known/oauth-protected-resource"`. The client registers, opens `/authorize`, the owner types the password, the client exchanges the code with its PKCE verifier, and gets a 24h access token plus a 30d sliding refresh token.
## Security model
- **Owner-gated consent.** No user login system. One password (`APPROVE_PASSWORD`) decides whether a client gets linked. Rotate it to revoke future linkings.
- **PKCE S256 mandatory.** Plain and missing challenges are rejected.
- **Redirect allowlist.** Only `https://oauth-redirect[-sandbox|-test].googleusercontent.com/[ra]/...` is accepted, at registration and at authorize time. This is a fixed constant in `oauth.mjs`, not a config value; widening it turns the consent page into an open redirect.
- **No plaintext credentials at rest.** `state.json` stores only SHA-256 hashes of client secrets and tokens, written atomically with mode `0600`.
- **Timing-safe comparisons** for the owner password and client secrets.
- **Loopback bind.** `127.0.0.1` only. Body reads are capped at 1 MiB.
- **DCR cap.** At most 100 dynamically registered clients.
- **No shell.** Every `hc` invocation is an argv array through `execFile`, with a 660s hard timeout and a 16 MiB output cap.
### What this server does not protect you from
- **`herdr_exec` and `herdr_run` are remote code execution by design.** Any holder of a valid bearer token can run arbitrary programs as the server's user, on this machine or any machine herdr can reach. Treat an access token as a shell.
- Token hashes in `state.json` cannot be reversed, but a stolen `state.json` still lets an attacker race live tokens; back it up like a credential, or delete it to revoke everything.
- Tool arguments are logged to stdout, including `herdr_send` text and `herdr_exec` argv. Do not send secrets through a terminal you also log.
- There is no per-client scope or per-tool authorization. Every authenticated client gets every tool.
Report vulnerabilities per [SECURITY.md](SECURITY.md).
## License
MIT. See [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues