UniFi MCP
# UniFi MCP
A local stdio MCP server for a UniFi gateway's Network application. It covers every operation in the documented local Network API (11.0.104): curated tools for common reads, one tool for any documented read, and a preview, approve, apply workflow for every documented write. It also covers a named set of reads and writes on the Network application's internal API. See [docs/coverage.md](docs/coverage.md) for each operation's status and evidence.
The point of this server is its boundary: the agent never holds the API key, never sees credentials, and cannot change your network without a one-time code that only you can read.
> Not affiliated with or endorsed by Ubiquiti. The internal API is unofficial and can change with any UniFi update.
## Requirements
- **macOS**, with FileVault on. The key broker is a LaunchDaemon; see [docs/persistent-broker.md](docs/persistent-broker.md).
- **Agents that cannot `sudo`.** Approvals rely on it: `sudo -n true` from an agent's shell should ask for a password.
- **`/usr/bin/python3`** from Apple's Command Line Tools or Xcode (`xcode-select --install`).
- **Node.js 22+** and **pnpm**.
- **A UniFi gateway with one site**, running UniFi Network 11 (tested on 11.0.104), reachable on your local network, and a local API key created in its Network application (Settings, Control Plane, Integrations).
## Boundary
- **The MCP never holds the API key.** The broker runs as a LaunchDaemon under the hidden `_unifibroker` user, from root-owned code, with the key in a file only that user can read. It answers over a Unix socket only the owner's `unifibroker` group can reach. The MCP refuses a socket that the broker's user does not own, or whose directory other users can enter.
- **The gateway is pinned.** The broker talks to the address you set, and only if the gateway presents the TLS public key you confirmed at setup.
- **Documented fields only, never credentials.** `broker/unifi_api.py` derives each read's allowlist from the Network API spec in `spec/`, minus the credential fields in `EXCLUDED_FIELDS` (Wi-Fi passphrases, preshared keys, hotspot voucher codes). Undocumented fields are dropped. A test fails if the spec gains a credential-like field nobody has classified. The six curated reads keep their own hand-written allowlists (`LIST_OPERATIONS`), as does every internal read.
- **Every write needs your approval code.** `broker/unifi_changes.py` validates a write against the spec, reads the target, and gives the owner the exact target, diff, impact, and rollback with a one-time code through `sudo "$UNIFI_BROKER" pending`. The code never crosses the agents' socket. No operation has standing authorization.
- **Credentials cannot be written.** Previews refuse changes to passphrases and preshared keys. Updates resend the current values, which the broker reads and never returns.
See [SECURITY.md](SECURITY.md) for what this does and does not protect against.
## Setup
```bash
git clone https://github.com/edmprojects/unifi-mcp.git
cd unifi-mcp
pnpm install
pnpm build
```
### Broker
```bash
UNIFI_BROKER="/Library/Application Support/UniFiBroker/bin/unifi-broker"
```
1. Review the checkout (`git status`, `git log`, `broker/`), then install from it once: `sudo /usr/bin/python3 -I broker/unifi_service.py install`. It lists each file it will install and asks you to type `yes`.
2. Pin your gateway, on your own network: `sudo "$UNIFI_BROKER" set-gateway 192.168.1.1` (use your gateway's address). It shows the gateway's TLS key pin and asks you to type `yes`.
3. Create a new UniFi API key for the broker and store it: `sudo "$UNIFI_BROKER" set-key`.
4. Restart your MCP client so it picks up your new `unifibroker` group membership.
5. To approve a write, run `sudo "$UNIFI_BROKER" pending` and give the agent the code it shows.
6. To update, pull or commit in the checkout, then run `sudo "$UNIFI_BROKER" update --repo <checkout>`. The installed copy shows its own diff of every changed file before asking.
`sudo "$UNIFI_BROKER" uninstall` removes the daemon, files, gateway pin, user, and group, and asks before deleting the key. The broker logs to `/Library/Logs/UniFiBroker/broker.log`.
### MCP client
```json
{
"mcpServers": {
"unifi": {
"command": "node",
"args": ["/path/to/unifi-mcp/dist/server.js"]
}
}
}
```
## Writes
1. `unifi_preview_change` takes a documented write `operation`, its `params` (path IDs and query parameters; the site is supplied), and `changes`. For an update, `changes` holds only the fields to change; the broker merges them into the current record and sends the full body PUT requires, so every field you did not name is resent as read. For a create or action, `changes` is the request body.
2. The preview returns the target, current state, exact body (credentials redacted), diff, impact (`routine`, `disruptive`, or `destructive`, with a network's references where relevant), and rollback. The owner sees the same plan and a one-time approval code with `sudo "$UNIFI_BROKER" pending`.
3. If you approve, give the agent the code. `unifi_apply_change` sends only the change ID and the code; the body is the one the broker stored at preview.
4. The broker refuses the write if there is no pending preview, the change ID or code differs, after three mismatched attempts, after 10 minutes, if the code was already used, or if the target changed since the preview. Every approval is single use, including one refused as stale.
5. After writing, the broker reads the target back. Updates report every changed field and any unexpected or missing change; deletes confirm the target is gone; creates report the new ID; actions report that the gateway accepted them.
The read-back is the confirmation. Some changes take effect on the network a little later: after a live DNS record create and delete, the gateway's DNS server answered the new record a few seconds after the API did, and kept answering the deleted one for about 30 seconds.
To roll back, preview the returned `rollback` and approve it the same way.
### Batches
`unifi_preview_batch` plans up to 25 routine changes, such as client renames or DNS record edits, for one approval code. Each change is planned exactly as it would be alone, and `sudo "$UNIFI_BROKER" pending` lists every one with its diff. A batch refuses disruptive and destructive changes and more than one change per target; those need their own approval. `unifi_apply_change` with the batch's code applies the changes in order, re-checks each for staleness, reads each back, and stops at the first that does not apply: `status: "partial"` lists what applied, what failed, and what was not attempted. Deletes recreate with a new ID, and voucher and device deletes are not reversible.
## Tools
| Tool | Returns |
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `unifi_list_clients` | Connected clients with type, IP and MAC address, connection time, and uplink device. Filters: `type`, `search` (name, IP, or MAC substring), `limit` (1–500, default 200). |
| `unifi_list_devices` | Adopted devices with model, state, addresses, firmware version, and update availability. |
| `unifi_list_networks` | Networks with VLAN ID, enabled state, management type, and firewall zone. |
| `unifi_list_wifi_broadcasts` | SSIDs with enabled state, security type, and assigned network name. No passphrases. |
| `unifi_list_firewall_policies` | Policies in gateway order with action, source and destination zone names, network selectors resolved to network names (with `matchOpposite` for "all except"), IP version, and logging. `userDefinedOnly` (default false). An empty description is reported as `null`; `unifi_read` with `getFirewallPolicy` returns the stored text, which can be `""`. |
| `unifi_read` | Any documented read by operation name (for example `getDnsPolicyPage`, `getAclRule`, `getWans`), with path IDs, `offset`, `limit`, and `filter` in `params`. Documented fields only, no credentials. |
| `unifi_internal_read` | A named read on the Network application's internal, unofficial API: active clients with live stats, known clients, health (including the latest speed test), devices with radios, ports, and firmware, neighboring APs, port forwards, static routes, port profiles, and the system log (`params`: `hours` up to 168, `limit` up to 100). Hand-written field allowlists; no credentials. |
| `unifi_inspect_device` | One device's details and latest statistics together. |
| `unifi_preview_change` | Plans any documented write: target, current state, exact body, diff, impact, rollback. Changes nothing; the owner gets an approval code from `sudo … pending`. |
Internal writes go through `unifi_preview_change` and `unifi_apply_change` too, with the same approval and read-back: `renameClient`, `setClientNote`, `setClientFixedIp`, `renameDevice`, `setDeviceLed`, and `setDeviceRadio` edit a record by `params.id`; `locateDevice`, `blockClient`, `unblockClient`, `reconnectClient`, `forgetClient`, and `upgradeDevice` act on a client or device by `params.mac`. Block, reconnect, radio, and firmware changes are disruptive and forget is destructive, so none of them can be batched. See [docs/gaps.md](docs/gaps.md#the-internal-network-api).
| `unifi_preview_batch` | Plans up to 25 routine changes for one approval code; refuses disruptive or destructive changes and repeated targets. |
| `unifi_apply_change` | Applies a previewed change with your code, then reads the target back and reports what changed. |
Field names and operations come from Ubiquiti's UniFi Network API spec, 11.0.104 (`spec/`), as a gateway serves it from its API docs page; see [NOTICE](NOTICE). Its Filtering section documents the `filter` syntax that `unifi_read` passes through, such as `name.eq('Guests')` and `and(enabled.eq(true), name.like('IoT*'))`. Other Network versions are untested.
## Checks
```bash
pnpm format:check
pnpm lint
pnpm typecheck
pnpm test # vitest (including the coverage matrix), then the broker's Python tests
pnpm coverage # regenerate docs/coverage.md after changing coverage/status.json
pnpm build
pnpm smoke # live reads through a running broker; prints counts only
```
See [CONTRIBUTING.md](CONTRIBUTING.md) for the rules every change keeps.
## License
MIT, except the UniFi Network API spec in `spec/`; see [NOTICE](NOTICE).
TDQS
Scored across 11 tools
The set has a clear split between specific list tools, generic read escapes (unifi_read / unifi_internal_read), an inspection tool, and a preview/apply write flow, so most selections are unambiguous. There is mild overlap: unifi_list_devices vs unifi_inspect_device vs unifi_internal_read(devices), and unifi_list_clients vs unifi_internal_read(activeClients/knownClients) all touch similar data, and the two generic read tools differ mainly on documented-vs-internal API. Descriptions do explain the distinction, keeping it at a solid 4.
All 11 names share a unifi_ prefix and snake_case, and most follow verb_noun (list_clients, preview_change, inspect_device, apply_change). The exceptions are unifi_read and unifi_internal_read, which are bare verbs without an object, a minor deviation from the pattern. Predictable overall with only that small inconsistency.
11 tools is well within the sweet spot, and each earns its place: five resource-specific listers, two generic read escapes, one inspection shortcut, and a three-tool preview/preview_batch/apply write pipeline. Nothing feels redundant or bolted on.
Read coverage is broad (clients, devices, networks, SSIDs, firewall policies, plus generic documented and internal reads) and writes are covered generically through the preview/apply broker, including batch and rollback. Gaps are minor: approval management is punted to `sudo … pending` outside the toolset (no list/cancel pending approvals), and some resources like DNS records, routes, and port forwards are only reachable indirectly via the generic read tools.