Skip to main content
Glama
README.md
# openwrt-agent-mcp

A [Model Context Protocol](https://modelcontextprotocol.io) server for OpenWrt,
built for people who do **not** want an AI to brick their router.

- **Read-only by default.** 12 read tools; one write tool that is disabled unless
  you enable it and confirm every call.
- **Zero runtime dependencies.** Python 3.12+, standard library only. No Node, no
  Docker, no packages installed on the router.
- **Nothing runs on the router.** It connects over SSH from your computer, so
  `apk upgrade` and sysupgrade stay clean.
- **PassWall 2 aware.** Status, node list and active node; an optional node
  switch that snapshots, probes and rolls back by itself.

> Persian documentation: [README.fa.md](README.fa.md)

---

## Choose your path

| You are… | Start at |
|---|---|
| New to OpenWrt | [Start here](#start-here-no-openwrt-experience) |
| A developer | [Install](#install-developers) |
| Building an AI agent | [Tool contract](docs/TOOLS.md) and [AGENTS.md](AGENTS.md) |

---

## Start here (no OpenWrt experience)

You need three things: the router's address, SSH access, and a key.

**1. Find the router address.** Open LuCI in a browser. The address is usually
`http://192.168.1.1` (some routers use `192.168.8.1` or `192.168.0.1`). That is
`OPENWRT_MCP_HOST`.

**2. Turn on SSH.** In LuCI: **System → Administration → SSH Access**. Make sure
the SSH server is enabled and the root password is set. On stock OpenWrt SSH is
usually already on, port 22.

**3. Create a key on your computer** (not on the router):

```bash
ssh-keygen -t ed25519 -f ~/.ssh/openwrt-agent-mcp -C openwrt-agent-mcp -N ''
ssh-copy-id -i ~/.ssh/openwrt-agent-mcp.pub root@192.168.1.1
```

Test it. It must print `OK` without asking for a password:

```bash
ssh -i ~/.ssh/openwrt-agent-mcp -o BatchMode=yes root@192.168.1.1 'echo OK'
```

**4. Run the server's self-test:**

```bash
./bin/openwrt-mcp --check
```

**5. Add it to your AI client.** Example for an MCP client that reads
`opencode.json`-style configuration:

```json
{
  "mcp": {
    "openwrt": {
      "type": "local",
      "command": ["/absolute/path/to/openwrt-agent-mcp/bin/openwrt-mcp"],
      "environment": {
        "OPENWRT_MCP_HOST": "192.168.1.1",
        "OPENWRT_MCP_KEY": "/Users/you/.ssh/openwrt-agent-mcp"
      }
    }
  }
}
```

Then ask in plain language:

- "How much RAM and disk does the router have left?"
- "Which devices are on Wi-Fi, and how strong is their signal?"
- "Show me the last 50 log lines and summarise the errors."
- "Which PassWall 2 node is active?"

Nothing above changes the router.

---

## Install (developers)

```bash
git clone https://github.com/mortezabahmani/openwrt-agent-mcp.git
cd openwrt-agent-mcp
python3 -m unittest discover -s tests     # 28 offline tests
./bin/openwrt-mcp --check --simulate      # no device needed
./bin/openwrt-mcp                         # stdio MCP server
```

There is no `pip install` step: the package is standard-library only. If you
prefer an installed entry point:

```bash
python3 -m venv .venv && . .venv/bin/activate
pip install -e .
openwrt-agent-mcp --check
```

### MCP client configuration

Most clients accept a command plus environment:

```json
{
  "mcpServers": {
    "openwrt": {
      "command": "/absolute/path/to/openwrt-agent-mcp/bin/openwrt-mcp",
      "args": [],
      "env": {
        "OPENWRT_MCP_HOST": "192.168.1.1",
        "OPENWRT_MCP_USER": "root",
        "OPENWRT_MCP_KEY": "/Users/you/.ssh/openwrt-agent-mcp"
      }
    }
  }
}
```

### Configuration

| Variable | Default | Meaning |
|---|---|---|
| `OPENWRT_MCP_HOST` | `192.168.1.1` | Router address |
| `OPENWRT_MCP_USER` | `root` | SSH user |
| `OPENWRT_MCP_KEY` | `~/.ssh/openwrt-agent-mcp` | Private key path |
| `OPENWRT_MCP_CONNECT_TIMEOUT` | `8` | SSH connect timeout (s) |
| `OPENWRT_MCP_COMMAND_TIMEOUT` | `20` | Per-command timeout (s) |
| `OPENWRT_MCP_AUDIT` | `1` | `0` disables the JSONL audit log |
| `PASSWALL2_ALLOW_WRITE` | `0` | `1` enables `passwall2_switch_node` |

Audit log: `~/Library/Logs/openwrt-agent-mcp/` on macOS,
`$XDG_STATE_HOME/openwrt-agent-mcp/logs/` on Linux (mode `0600`).

---

## Tools

Full schemas and examples: [docs/TOOLS.md](docs/TOOLS.md).

| Tool | Type | Returns |
|---|---|---|
| `system_info` | read | board, release, kernel, uptime, load, memory, overlay |
| `network_status` | read | interfaces, WAN status, routes |
| `wifi_status` | read | radios, channels, tx power, associated stations |
| `clients` | read | DHCP leases + neighbours (identifiers masked by default) |
| `logs` | read | last 1–200 lines of `logread` |
| `ubus_list` | read | objects, or methods of one allowlisted object |
| `ubus_call` | read | read methods on allowlisted objects |
| `uci_get` | read | allowlisted UCI keys; secrets redacted |
| `service_status` | read | init scripts and key daemons |
| `passwall2_status` | read | service instance + running proxy process |
| `passwall2_nodes` | read | node remarks/type only |
| `passwall2_active` | read | active `tcp_node` / `udp_node` |
| `passwall2_switch_node` | **write** | gated node switch with rollback |

### PassWall 2

Read tools are always available. The switch tool is deliberately awkward:

```text
PASSWALL2_ALLOW_WRITE=1  +  confirm=true  +  a valid node index
```

It then:

1. snapshots the previous `tcp_node` / `udp_node`;
2. sets both to the requested node and commits;
3. reloads PassWall 2;
4. probes DNS (`nslookup example.com`);
5. on failure, restores the snapshot, commits and reloads again.

A reload briefly interrupts proxied traffic. That is why it is off by default
and why an agent must never call it on its own.

---

## Safety model

- **Allowlists, not blocklists.** Only named ubus objects and UCI configs are
  reachable. `file` is intentionally excluded: its `read`/`list` methods would
  turn a diagnostics server into an arbitrary file reader.
- **No destructive tools.** There is no reboot, sysupgrade, factory reset,
  package install or firewall flush tool.
- **Output hygiene.** MAC addresses, long identifiers and `key`/`password`
  patterns are masked; output is capped at 12,000 characters.
- **Audit trail.** Every call appends one JSON line with tool, result size and
  error class (never the payload).
- **Bounded transport.** `BatchMode=yes`, no password fallback, 8 s connect and
  20 s command timeouts.

If you need a capability outside this model, open an issue before opening a PR.

---

## Test without a router

```bash
python3 -m unittest discover -s tests     # fixtures + fake runner
./bin/openwrt-mcp --check --simulate      # end-to-end stdio, no device
```

The simulator (`src/openwrt_mcp/sim.py`) is a synthetic router: fake ubus/UCI
output, no real addresses, MACs, keys or serials. The local check script uses it, so tests
never touch hardware.

Or run everything at once (lint is skipped if `ruff` is not installed):

```bash
./scripts/check.sh
```

### Continuous integration (optional)

Hosted CI is not required. `docs/ci-github-actions.yml` is a ready workflow;
copy it to `.github/workflows/ci.yml` if your GitHub account can run Actions.
The same steps run locally in `scripts/check.sh`.

---

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `Permission denied (publickey)` | key not authorised | run `ssh-copy-id` again |
| `Host key verification failed` | changed host key | remove the old entry from `known_hosts` |
| `ubus object 'file' is not read-allowlisted` | by design | use a dedicated tool |
| `PassWall 2 writes are disabled` | gate is closed | set `PASSWALL2_ALLOW_WRITE=1` and `confirm=true` |
| `(no PassWall 2 nodes configured)` | PassWall 2 has no nodes | add nodes in LuCI first |
| `probe failed` after a switch | node is unreachable | the previous node was restored automatically |

---

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md). Security reports: [SECURITY.md](SECURITY.md).

## License

MIT — see [LICENSE](LICENSE).

TDQS

A3.5/5.0

Scored across 13 tools

Disambiguation4/5

Most tools cover clearly distinct resources: system, network, wifi, clients, logs, ubus, uci, services, and PassWall. The PassWall trio (status, nodes, active) and generic service_status could be momentarily confused, but their descriptions make the boundaries clear.

Naming Consistency4/5

All names use snake_case, with a readable resource_status or namespace_action pattern such as network_status, ubus_list, uci_get, and passwall2_switch_node. Bare names like clients and logs are minor deviations but still clear.

Tool Count5/5

13 tools is a well-scoped count for router diagnostics plus focused PassWall management. Each tool covers a distinct observability area or a clearly gated write operation.

Completeness4/5

The read-only surface is broad and covers the main diagnostic areas of an OpenWrt router, including system, network, wifi, clients, logs, ubus, uci, services, and PassWall. It intentionally lacks broader write operations such as uci_set, service restart, firewall inspection, or package management, which are minor gaps given the safe read-only design.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues