Metadata-Version: 2.4
Name: openwrt-ubus-mcp
Version: 0.1.1
Summary: MCP server for OpenWrt over ubus: no SSH, router-enforced ACL, and changes that roll back on their own if they break access.
Author: Steve Spence (KK4HFJ)
License-Expression: MIT
Project-URL: Homepage, https://github.com/KK4HFJ/openwrt-ubus-mcp
Project-URL: Issues, https://github.com/KK4HFJ/openwrt-ubus-mcp/issues
Project-URL: Changelog, https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/CHANGELOG.md
Project-URL: Notebook, https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/docs/NOTEBOOK.md
Keywords: openwrt,ubus,rpcd,uci,luci,mcp,model-context-protocol,router,firewall,claude
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: System :: Networking
Classifier: Topic :: System :: Networking :: Firewalls
Classifier: Topic :: System :: Systems Administration
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mcp<3,>=2.3
Requires-Dist: httpx>=0.27
Requires-Dist: uvicorn>=0.30
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Requires-Dist: pytest-asyncio>=0.23; extra == "test"
Requires-Dist: asgi-lifespan>=2; extra == "test"
Dynamic: license-file

# openwrt-ubus-mcp

<!-- mcp-name: io.github.KK4HFJ/openwrt-ubus-mcp -->

openwrt-ubus-mcp lets an AI assistant manage an OpenWrt router **without SSH, root, or any
software installed on the router**. It talks only to the router's own ubus JSON-RPC API, the
one LuCI uses, as a dedicated login, so **the router itself** enforces what the AI may touch,
through a stock rpcd ACL. Every change is previewed, applied under OpenWrt's rollback timer, and
confirmed only after a fresh login proves the router still answers, so a bad firewall rule
reverts itself instead of locking you out. Wi-Fi keys and passwords are redacted before
anything leaves the server.

Status: **v0.1.0**, December 2026. MIT licensed. The engineering notebook
([docs/NOTEBOOK.md](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/docs/NOTEBOOK.md)) records how it was built and live-tested, including the
rpcd behaviour the live tests uncovered.

## What it is, and what it isn't

**It is** a small Python MCP server that runs on any machine that can reach the router (your
laptop, a home server, a container). It offers read tools for the router's state and a few
narrow write tools: DHCP static leases, port forwards and firewall traffic rules.

**It isn't** a remote shell, a firmware or package manager, or a general UCI editor. It can't
touch interfaces, the WAN or wireless settings, and it can't reboot the router. That is on
purpose (see [Limits](#limits)). It is not affiliated with the OpenWrt project.

## The safety model

1. **ubus only.** The server speaks rpcd's JSON-RPC over HTTP(S) at `/ubus`. There is no SSH
   session, no shell, and no `file` or `exec` call anywhere in the code.
2. **The router enforces the limits.** The server logs in as its own rpcd user. The ACL
   ([rpcd/acl.d/openwrt-ubus-mcp.json](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/rpcd/acl.d/openwrt-ubus-mcp.json)) allows a short list
   of read calls, plus UCI writes to the `dhcp` and `firewall` configs only. A bug in this
   server, or a confused AI, can't widen that: rpcd refuses anything else. Without the write
   group the login is read-only.
3. **Preview, then confirm.** Every write tool first returns the exact UCI changes it would
   make and leaves nothing staged. Only a second call with `confirm: true` applies them.
   Clients that show tool calls put that preview in front of you.
4. **Changes undo themselves.** A confirmed change is applied with `uci apply` and the
   rollback timer armed (30 s). The server waits for services to settle (5 s), checks that a
   **fresh login** still works, and only then sends `uci confirm`. If the router stops
   answering, nothing confirms the change and the router restores the old config on its own.
   The server watches until it has, and reports `rolled_back`.
5. **Only its own entries.** Sections the server creates are named `mcp_*` in UCI, and its
   firewall entries show in LuCI as `mcp:<name>`. The remove tools refuse anything else
   unless you pass `force: true`.
6. **Secrets stay on the router.** Wi-Fi keys, PSKs, passwords and private keys are shown as
   `***` in tool output and in the server's own logs, matched both by option name and by
   value. Tests enforce it, and the live check compares every output against the router's
   real secrets.

| Before | Applied, not confirmed (+10 s) | Rolled back by the router (+45 s) |
|---|---|---|
| ![LuCI Traffic Rules before the test](https://raw.githubusercontent.com/KK4HFJ/openwrt-ubus-mcp/main/docs/img/rollback-1-before.png) | ![The disabled test rule mcp:rollback-test at the bottom of the list](https://raw.githubusercontent.com/KK4HFJ/openwrt-ubus-mcp/main/docs/img/rollback-2-present.png) | ![The same list after the router's rollback](https://raw.githubusercontent.com/KK4HFJ/openwrt-ubus-mcp/main/docs/img/rollback-3-gone.png) |

*The rollback test in LuCI's Traffic Rules. The server added a disabled rule, `mcp:rollback-test`,
so it changed nothing, and deliberately didn't confirm it. The router removed it by itself 30 s
after apply. The "before" and "after" images are byte-identical files.
[More screenshots](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/docs/NOTEBOOK.md#2026-12-05-0734z--screenshot-run-all-three-sets-live).*

## Tools

| Read | |
|---|---|
| `router_info` | model, firmware, kernel, uptime, load, memory |
| `interfaces` | lan/wan/...: up, protocol, device, addresses, gateway, DNS |
| `dhcp_leases` | active DHCPv4 and DHCPv6 leases |
| `wifi_status` | each radio: SSID, channel, TX power, signal, encryption |
| `wifi_clients` | associated stations with signal and rates, named from the DHCP leases |
| `firewall_config` | zones, forwardings, rules and port forwards |
| `uci_show` | one of `network`, `dhcp`, `firewall`, `system`, `wireless` |
| `system_log` | the last lines of the log, optionally filtered |
| `pending_changes` | anything staged in the server's session (normally nothing) |

| Write (preview, then `confirm: true`) | |
|---|---|
| `static_lease_add` / `static_lease_remove` | DHCP static leases (MAC, IPv4 address in the LAN subnet, hostname) |
| `port_forward_add` / `port_forward_remove` | port forwards (firewall `redirect`, DNAT) |
| `firewall_rule_add` / `firewall_rule_remove` | traffic rules (ACCEPT, DROP or REJECT; IPv4, IPv6 or both) |

Inputs are checked before anything is staged: MAC addresses, hostnames, port ranges and zones.
A lease's IP must be inside the LAN subnet (not the network, broadcast or router address), and
its MAC, IP and name must not already be in use.

## 1. Set up the router (once)

You need rpcd and `uhttpd-mod-ubus`, which come with LuCI on stock OpenWrt images. Copy the
`rpcd` folder to the router and run the installer there:

```
scp -O -r rpcd root@192.168.1.1:/tmp/
ssh -t root@192.168.1.1 sh /tmp/rpcd/install.sh --write
```

It asks for a password (12 characters or more) and stores only its hash, made with
`uhttpd -m`. It installs the ACL and creates **one** rpcd login named `mcp`. Leave out
`--write` for a read-only login. Running it again updates the same login, removing any
duplicates, instead of adding another.

It also lists the ACL file in `/etc/sysupgrade.conf`. A firmware upgrade keeps `/etc/config/rpcd`
(the login) but not files added under `/usr/share`, so without that line the login would survive
an upgrade with no permissions at all. Tested with real upgrades: with the line the ACL is kept;
without it, it is lost.

Things that trip people up:
- `scp -O` is needed because OpenWrt's dropbear has no SFTP server.
- Copy to `/tmp/` as shown, not to a named folder: when the target folder already exists, a
  second `scp -r` puts the files one level deeper and you'd run the old copy.
- The installer restarts rpcd, which logs everyone out of LuCI. Just log in again.

To undo it, run `ssh root@192.168.1.1 sh /tmp/rpcd/uninstall.sh`. It removes the
login, the ACL and its `sysupgrade.conf` line, and lists any `mcp_*` entries still in `dhcp` or `firewall` so you can
remove them too. If you'd rather set it up by hand, [rpcd/login.example](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/rpcd/login.example)
shows the login section.

## 2. Run the server

Settings come from environment variables (see [example.env](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/example.env)):

| Variable | Default | |
|---|---|---|
| `OPENWRT_URL` | `http://192.168.1.1/ubus` | the router's ubus endpoint; `https://` works too |
| `OPENWRT_USER` | `mcp` | |
| `OPENWRT_PASSWORD` | | required |
| `OPENWRT_VERIFY_TLS` | `true` | `false`, or a CA file, for a self-signed router certificate |
| `OPENWRT_ROLLBACK_TIMEOUT` | `30` | seconds before an unconfirmed change is rolled back |
| `OPENWRT_SETTLE_SECONDS` | `5` | wait after apply before the check (0 to 20) |
| `MCP_TRANSPORT` | `stdio` | or `http` (streamable HTTP) |
| `MCP_HTTP_BIND` / `MCP_HTTP_PORT` | `127.0.0.1` / `8765` | HTTP only |
| `MCP_BEARER_TOKEN` | | required for HTTP |
| `MCP_HTTP_ALLOWED_HOSTS` | | extra Host names accepted over HTTP, e.g. `example-host` |

### stdio, started by your MCP client

With [uv](https://docs.astral.sh/uv/) there is nothing to install first:

```
claude mcp add openwrt -e OPENWRT_PASSWORD=your-password -- uvx openwrt-ubus-mcp
```

Or with pip:

```
pip install openwrt-ubus-mcp
claude mcp add openwrt -e OPENWRT_PASSWORD=your-password -- openwrt-ubus-mcp
```

Add `-e OPENWRT_URL=https://192.168.1.1/ubus` if your router isn't at the default address.
Other clients use the same command and environment in their own config format.

### HTTP, as a service (Docker)

Run it on one machine and point several clients at it, protected by a bearer token:

```
docker run -d --name openwrt-ubus-mcp --network host --restart unless-stopped \
  -e OPENWRT_PASSWORD=your-password -e MCP_BEARER_TOKEN=your-long-random-token \
  -e MCP_HTTP_BIND=192.168.1.10 -e MCP_HTTP_ALLOWED_HOSTS=example-host \
  ghcr.io/kk4hfj/openwrt-ubus-mcp

claude mcp add --transport http openwrt http://example-host:8765/mcp \
  --header "Authorization: Bearer your-long-random-token"
```

[compose.yaml](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/compose.yaml) does the same from a `.env` file, with a read-only filesystem and
no Linux capabilities. Bind to a private address (a VPN interface is ideal), never to `0.0.0.0`
on an untrusted network. Make a token with `openssl rand -hex 32`.

## How it compares

OpenWrt MCP servers broadly come in three kinds:

- **SSH-based** servers log in from your machine and run commands. They can do anything the
  login can, and their limits live in the server's own code.
- **On-router** servers install their own daemon on the router, with its own policy engine and
  audit log, and usually reach the client over an SSH tunnel. They can cover far more of the
  router (packages, upgrades, any ubus call) than this one does.
- **API-based** servers use a REST API that has to be installed on the router first.

This one sits in a narrow spot: **nothing to install on the router** (one JSON file and one
login), **no SSH at all**, and **limits that rpcd enforces**, the same mechanism that already
guards LuCI. The price is scope: it only does what the ACL and its few typed tools allow. If
you want an agent that can explore and change anything on the router, one of the others will
suit you better.

## Limits

- **No interfaces, WAN, wireless writes, VLANs or bridges.** A wrong change there can cut off
  the machine the server runs on, and recovery would rest entirely on the rollback timer.
  Wireless status is read-only.
- **No reboot, packages, upgrades or shell.** The ACL doesn't allow them.
- **IPv4 for leases and port forwards.** DHCPv6 leases are shown but not managed.
- **One router per server.** Run one instance per router.
- **Rollback restores the router's config, not what happened meanwhile.** It can't undo what a
  client did during the 30 s window.

## Tested versions

| OpenWrt | Result |
|---|---|
| 25.12.6 | Tested live on a real router: `install.sh`, every read tool, adding and removing a lease, a port forward and a rule, and the forced rollback (4 runs). |
| 24.10.8 | Tested in a KVM virtual machine (the official armsr-armv8 image, no Wi-Fi): `install.sh` (twice, with one login left), every read tool, adding and removing a lease, and the forced rollback. rpcd behaves the same as on 25.12. |

Reports for other versions are welcome as issues.

## Roadmap

openwrt-ubus-mcp is meant to become the control layer of a small fleet console: many routers, staged
rollouts that stop at the first failure, health monitoring, and token enrollment. That console is
**planned, not built**. See the [executive summary](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/docs/roadmap/EXECUTIVE-SUMMARY.md) and the
[whitepaper](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/docs/roadmap/WHITEPAPER.md).

## Development

```
python -m venv .venv && .venv/bin/pip install -e '.[test]'
.venv/bin/pytest                                  # fake ubus and BusyBox ash, no router needed
OPENWRT_LIVE=1 python scripts/live_check.py       # read tools against a real router
OPENWRT_LIVE=1 python scripts/rollback_proof.py   # the rollback proof (adds a disabled rule)
```

The fake ubus models rpcd's sessions, ACL refusals, per-session staging, and `apply` with a
rollback timer, including rpcd putting rolled-back changes back into the applying session. It
runs on a test clock. Each safety rule was mutation-checked: break it and a test fails. The
router scripts are tested under BusyBox ash with a stand-in `uci`.

## License

MIT. See [LICENSE](https://github.com/KK4HFJ/openwrt-ubus-mcp/blob/main/LICENSE).
