Steve SpenceKK4HFJ · Advanced Research Systems

Blog

Letting Claude manage my OpenWrt router, without giving it the keys

Steve Spence, KK4HFJ · 5 December 2026

In my last post I turned an old TP-Link access point into an OpenWrt router for my ESP32 bench. Once it was running, I wanted Claude to be able to look after it: list the DHCP leases, see who's on the Wi-Fi, add a static lease for a new board, open a port for a test. I didn't want to click through LuCI every time, and I didn't want to hand an AI a root shell on my router either.

So I built openwrt-ubus-mcp, an MCP server that gives an AI assistant a small, fenced-off set of router tools. It's open source, and it's on GitHub and PyPI. This post covers how it works, what it found along the way, and the one OpenWrt feature that makes it safe.

Why not just SSH?

Most ways of scripting a router start with SSH. The trouble is that an SSH key is all or nothing: whoever holds it is root.

OpenWrt already has something finer. LuCI, its web interface, doesn't use SSH at all. It talks to a daemon called rpcd, which exposes the router's internal message bus (ubus) over HTTP at /ubus. Every rpcd login has an ACL, a list of exactly which ubus objects, methods and config files it may touch. The router enforces that list, not my code. If the server has a bug, it still can't widen its own access.

So openwrt-ubus-mcp logs in as its own user, mcp, with an ACL that allows:

  • Reading: board info, interfaces, DHCP leases, Wi-Fi radios and clients, the firewall, the system log, and the network, dhcp, firewall, system and wireless configs.
  • Writing: only the dhcp and firewall configs. That's enough for static leases, port forwards and firewall rules.

Nothing else. No file access, no exec, no service control, and no writes to the network, Wi-Fi or system settings. I checked each one live:

file read:   {'code': -32002, 'message': 'Access denied'}
file exec:   {'code': -32002, 'message': 'Access denied'}
rc list:     {'code': -32002, 'message': 'Access denied'}
uci add on network, wireless, system:  refused, "permission denied"

Nothing gets installed on the router except that ACL file and the login, and a small script does both. There's no extra daemon or package on the router.

Lesson one: the Wi-Fi key comes back in plain text

The read ACL has to include wireless, or the assistant can't see SSIDs, channels or encryption. But uci get wireless returns the Wi-Fi key in plain text. So the server redacts secrets in two layers:

  1. By name. Any option called key, psk, password and so on becomes ***.
  2. By value. Right after logging in, the server learns the actual secret values, then scrubs them from every tool result and every log line. That catches a key that turns up somewhere unexpected, such as a daemon logging its passphrase.

To prove both layers earn their keep, I switched each one off in turn and ran the tests. Value scrubbing off: 8 tests failed. Name masking off: 11 failed. Neither layer is dead weight.

Another small surprise: rpcd answers "Access denied" (-32002) both when a session has expired and when the ACL forbids the call. A client that logs in again on every -32002 will loop forever on a forbidden call. Mine logs in again once, and a second refusal is reported as access denied.

The feature that makes writes safe: rollback

Reading is easy to make safe. Writing is where you can lock yourself out of your own router.

OpenWrt has the answer built in. uci apply takes a rollback flag and a timeout. The router commits the change, reloads the services, and waits. If nobody confirms within the timeout, the router puts the old config back by itself. It's the same thing LuCI does when you see "Configuration changes have been rolled back!" A change that cuts the network off can't be confirmed, so it undoes itself.

Every write tool in openwrt-ubus-mcp goes through the same steps:

  1. Check the input: a real MAC, an IP inside the LAN, valid ports and zone names.
  2. Preview. The first call only stages the change and shows exactly what would happen, then throws it away. Nothing is applied until the assistant calls again with confirm: true, which Claude asks me before doing.
  3. Apply with a 30-second rollback armed.
  4. Wait 5 seconds for the services to reload.
  5. Check that the router still answers, using a brand-new login, not the existing session.
  6. Confirm only if that check passed. If it failed, don't confirm, and let the router undo the change.

The server also only removes what it created. Everything it adds is named with an mcp_ prefix, and the remove tools refuse anything else unless you force them.

Lesson two: the router was right, and my server was wrong

To prove the rollback works, I wrote a test that adds a harmless, disabled firewall rule but deliberately fails the check, so the change is never confirmed. A second, separate login watched the firewall config every 2 seconds.

The first run looked like this:

+  0s  rule absent
+  2s  rule PRESENT
+ 30s  rule absent        <- the router's own timer
result: unknown   ("gave up waiting" after 61 s)

The router removed the rule exactly on time, but my server never noticed. It turned out that when rpcd rolls back a change, it puts the reverted changes back into the session that applied them, as staged changes. From that session, the rule still looked present, though the router's real config no longer had it. Nothing in the documentation hints at this. It only showed up because the test ran on a real router.

The fix: after an unconfirmed apply, check the config through a fresh login, then clear the changes rpcd handed back. After that, every run reported rolled_back correctly.

Here's the rollback in LuCI. The server added the rule and never confirmed it, and 30 seconds later it was gone, with nobody touching the router:

LuCI Traffic Rules before the test
The disabled test rule mcp:rollback-test at the bottom of the list
The same list after the router's rollback

The "before" and "gone" screenshots are byte-for-byte identical files. Taking these screenshots was its own small project: two Claude sessions with no real-time link between them ran one fixed timetable on separate clocks. The Pi made each change at an exact second, and the browser session captured LuCI at the matching seconds.

Lesson three: a router isn't a full Linux box

The installer is a shell script that runs on the router. It hides the password as you type it, using stty -echo. On the router it stopped at once with stty: not found: OpenWrt builds BusyBox without stty. My tests had missed it because they fed the password from a pipe, and the Pi's own BusyBox does have stty. BusyBox's shell has read -s built in, which does the same job, and two new tests now type the password on a real terminal.

Two more that would catch anyone:

  • scp to OpenWrt fails with "subsystem request failed". The router's SSH server (dropbear) has no SFTP, and modern scp uses SFTP by default. scp -O forces the old protocol.
  • Running cat >> /etc/config/rpcd twice gives you two identical logins. Use uci commands, and check with uci show rpcd afterwards.

I also tested OpenWrt 24.10.8 in a virtual machine on the Pi 5 and saw the same rpcd behavior. A firmware upgrade keeps the login but not the ACL file, so the installer now adds the ACL to /etc/sysupgrade.conf. I proved that with real upgrades in the VM: with the line, the server still works after the upgrade; without it, the login survives but every call is refused.

Trying it

On the router, as root (the password you choose is hashed on the router, never put on a command line):

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

Leave off --write for a read-only login. Then add it to Claude Code:

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

It also runs in Docker over HTTP with a bearer token, for a server that several clients share. The README has the details.

Then just ask: "Who's on the lab Wi-Fi?", "Give the board with MAC aa:bb:cc:dd:ee:01 a fixed address at .100", or "Forward port 18080 to the test server." Claude shows you the change first, and it only applies when you say yes.

What's next

The engineering notebook in the repo, How we built and tested openwrt-ubus-mcp, has every command and every result, including the mistakes. It's where this post came from.

The next step is a fleet console: one web page that lists every OpenWrt router on a network, shows their health, and applies a change to a group of them, each with the same rollback protection. The roadmap describes it. It's planned, not built yet.

OpenWrtMCPClaudeNetworkingPython

Reply to this post

A question, a correction, or your own results? Send it here. Replies come to me by email and aren't published. Your address is only used to answer you.

Ask AI answers only from these posts, with numbered links to where it found it. It's a small model: check the post it links.

← All posts