Metadata-Version: 2.5
Name: multivendor-netconf-mcp
Version: 0.3.2
Summary: An MCP server for routers over NETCONF (Junos, IOS XE, IOS XR): router-checked dry runs, changes as model paths, confirmed commits
License-Expression: MIT
License-File: LICENSE
Requires-Python: >=3.11
Requires-Dist: anyio>=4
Requires-Dist: mcp<2,>=1.12
Requires-Dist: ncclient>=0.6.15
Requires-Dist: netmiko<5,>=4.3
Requires-Dist: pyang<3,>=2.6
Description-Content-Type: text/markdown

# multivendor-netconf-mcp

An [MCP](https://modelcontextprotocol.io) server that lets an AI agent read and change
routers over NETCONF, with the same tools on every platform. A change is checked by
the router before it is committed, the agent is shown what would change as a list of
configuration paths, and a commit is one the router undoes by itself unless it is
confirmed.

| Platform | Tried on | Notes |
|---|---|---|
| Juniper Junos | vSRX, Junos 20.4R3 | [docs/junos.md](docs/junos.md) |
| Cisco IOS XE | Catalyst 8000V, IOS XE 17.13.1 | [docs/cisco.md](docs/cisco.md) |
| Cisco IOS XR | IOS XRv 9000, IOS XR 7.11.1 | [docs/cisco.md](docs/cisco.md) |

> **Status: early.** Each platform was tried on one release, on virtual routers, and on
> nothing else. It is not a server for any NETCONF device: every vendor's NETCONF differs
> enough to need code of its own, and a fourth platform means writing that code. The
> server applies no rules of its own: it commits whatever it is given. Whoever puts it in
> front of real routers is responsible for validating it there first, with their own
> tests. Not affiliated with Juniper or Cisco.

## Why this exists

The MCP servers that exist for routers are mostly one per vendor, and return what the
command line prints. Juniper publishes
[junos-mcp-server](https://github.com/Juniper/junos-mcp-server), and for a person steering
an agent it does the job: it runs commands, loads configuration, has a
dry run and a confirmed commit. This project started from building a change firewall,
[Holddown](https://github.com/Jitter00/holddown), in front of it. A program that has to
judge an agent's change, and not only a person reading along, ran into four limits there.
They were seen at commit `2fb721a` of junos-mcp-server, against vSRX on Junos 20.4.

| | junos-mcp-server | multivendor-netconf-mcp |
|---|---|---|
| What a dry run returns | The text of `show \| compare`. A program has to parse indentation, braces and quoted names to know what changed. | One line per changed leaf, as its path with list keys: `+ interfaces interface[ge-0/0/0] description = to vsrx1`. The same lines whether the change was written as XML or as set commands. |
| Pipes in a show command | Passed to the router, which honours `display` and `compare` and drops `match`, `except`, `count`, `last` and the rest without saying so. The agent gets the whole output. (PyEZ's `cli()` documents this.) | Refused, with the reason. The server filters the output itself: `include`, `exclude`, `head`, `tail` and `count` parameters, the same on every platform. |
| A confirmed commit that is still pending | Told by `<commit-confirmed>` in the commit history, which Junos 20.4 does not have. On that release a pending commit is not seen without a patch. | Told the same way, with a documented fallback for releases without the element (`"legacy_pending": true`). |
| Vendors | Junos. | Junos, Cisco IOS XE and Cisco IOS XR, with the same tools and the same change lines. |

Where this server differs by design (on Junos; the Cisco platforms are in
[docs/cisco.md](docs/cisco.md)):

- **The change list is worked out from the configuration, not from text.** The server
  loads the change into the candidate, has the router check it (`commit check`), then
  compares the candidate with the committed configuration as XML.
- **List keys come from the router's own YANG modules**, read over NETCONF, so an entry is
  named by all of its keys: `security policies policy[trust,untrust]`.
- **A statement that is switched off shows** (`deactivate`): a line ending in `inactive`.
- **Somebody else's work is left alone.** The candidate on Junos is shared. If it already
  holds uncommitted changes, the server loads nothing and says so.
- **A commit while a confirmed commit is pending is refused.** On Junos any later commit
  makes the pending one permanent.
- **One login.** Show commands go over the NETCONF session too.

What junos-mcp-server has and this does not: Jinja2 templates, PFE commands, a tool that
adds a router while it runs, pooled connections, and Juniper behind it. Templates and PFE
commands are left out on purpose: a template is a second way to write a change, and a PFE
command runs in the forwarding plane's shell. Here a router is added by editing the device
list, which the agent cannot do.

## Install and run

Needs Python 3.11 or later and [uv](https://docs.astral.sh/uv/). NETCONF has to be on:
`set system services netconf ssh` on Junos, and see [docs/cisco.md](docs/cisco.md) for
IOS XE and IOS XR.

Write a `devices.json` that lists your routers (below), then:

    uvx multivendor-netconf-mcp -f devices.json --models
    uvx multivendor-netconf-mcp -f devices.json

`uvx` fetches the server from [PyPI](https://pypi.org/project/multivendor-netconf-mcp/)
and runs it; `pip install multivendor-netconf-mcp` installs the same command.

The first command reads the routers' YANG modules once, for the keys of their lists. It
takes about a minute per software release and is kept under
`~/.local/state/multivendor-netconf-mcp` (or `$NETCONF_MCP_STATE`). Run it again after a
software upgrade. The second command is the MCP server, on stdio. For an MCP client:

<!-- x-release-please-start-version -->

    {"mcpServers": {"netconf": {
      "command": "uvx",
      "args": ["multivendor-netconf-mcp==0.3.2", "-f", "/path/to/devices.json"]}}}

<!-- x-release-please-end -->

Name a version there, as above: the server logs in to your routers, and without one `uvx`
runs whatever release is newest.

`devices.json`:

    {"vsrx1": {"host": "192.0.2.11", "platform": "junos", "username": "netconf"},
     "xe1":   {"host": "192.0.2.21", "platform": "iosxe", "username": "netconf",
               "password_env": "XE1_PASSWORD"}}

| Field | Meaning |
|---|---|
| `host`, `username` | where and as whom to log in |
| `platform` | `junos`, `iosxe` or `iosxr` |
| `password_env` | the name of an environment variable that holds the password, so that it is not in the file |
| `password` | the password itself. Prefer `password_env`, or keys |
| `key_file` | a private key to log in with. With neither a password nor this, the login uses your SSH agent and your own keys |
| `ssh_config` | an OpenSSH configuration file to read for this router (`HostName`, `User`, `IdentityFile`, `ProxyCommand`) |
| `known_hosts` | the file the router's host key is looked up in, instead of the `known_hosts` file in your `.ssh` folder |
| `host_key_check` | `false` turns host key checking off. For labs only |
| `port`, `ssh_port` | NETCONF port (830) and, on the Cisco platforms, the SSH port for show commands (22) |
| `legacy_pending` | `true` for Junos releases whose commit history has no `<commit-confirmed>` element (20.4). Without it the server finds nothing pending there and refuses to confirm |

On the Cisco platforms the server logs in twice, to NETCONF and to the SSH command line
for show commands, with the same login. A key works for both on IOS XE; on IOS XR only a
password was tried.

### Host keys

The server connects only to a router whose host key is in the `known_hosts` file, and
refuses one that shows a different key. A router you have never logged in to has to be
added first: `ssh-keyscan -p 830 192.0.2.11` prints its key; check it against the key
the router itself shows on its console, then append it to the file. A key stored for the plain address, as `ssh` does for port 22, is accepted for the
NETCONF port too.

A router can have keys of several types, and shows one of them. If the file holds only
another type, the server says so and connects once that key is added: `ssh-keyscan`
prints every type, so add all its lines. IOS XE has an RSA key only.

### Read-only

    uvx multivendor-netconf-mcp -f devices.json --read-only

offers no tool that changes a router and runs only commands that start with `show`. Use it
where an agent should look and not touch. It is a convenience, not a guarantee: the
routers' own login class is what limits a login.

### The device list is read again

When `devices.json` changes, the server reads it again at the next call: a router can be
added, changed or taken out without a restart. If the file is then not valid, the server
says so on its standard error and keeps the list it had.

### Over HTTP

    export NETCONF_MCP_TOKEN=$(openssl rand -hex 32)
    uvx multivendor-netconf-mcp -f devices.json --transport http

serves MCP at `http://127.0.0.1:8000/mcp` (streamable HTTP) instead of standard input and
output, so that one server can be shared. Every request has to carry the token, as
`Authorization: Bearer <token>`; the server does not start without one of at least 32
characters. For an MCP client:

    {"mcpServers": {"netconf": {"type": "http", "url": "http://127.0.0.1:8000/mcp",
                                "headers": {"Authorization": "Bearer <token>"}}}}

`--host` and `--port` change where it listens. The server speaks plain HTTP: to reach it
from another machine, keep it on 127.0.0.1 and put a TLS proxy in front, or the token and
the configurations cross the network unencrypted. There is one token: everyone who has it
can do everything the server can, and the routers' logs show the server's login, not who
asked.

## Tools

| Tool | What it does |
|---|---|
| `get_router_list` | the routers in `devices.json` |
| `get_config` | the configuration as XML, whole or limited by a subtree filter. On Junos also as set commands or in braces (`format`), or as the diff against a rollback (`compare_rollback`) |
| `execute_command` | one exec command, for example `show bgp summary`, with the output filtered by the server. On Junos also as XML or JSON (`format`) |
| `execute_command_batch` | the same command on several routers at once, each router's output under its name |
| `get_facts` | what a router is: hostname, model, software version, serial number, uptime |
| `get_state` | operational state as XML over NETCONF, on IOS XE and IOS XR |
| `load_and_commit_config` | a dry run, a commit, or a commit the router undoes unless confirmed |
| `confirm_commit` | makes a pending confirmed commit permanent |

On Junos a change is `<configuration>` XML as `get_config` returns it, the configuration
in braces (which is merged: it adds and changes, it does not remove), or lines of set
commands. On the Cisco platforms it is XML in the router's own model.

    set interfaces ge-0/0/0 description "to vsrx1"
    delete protocols bgp group EBGP neighbor 10.0.1.1 shutdown
    deactivate interfaces lo0 unit 0 family inet address 10.255.0.2/32

A command may not hold a pipe. In its place:

    execute_command(router_name="vsrx1", command="show bgp summary", include="establ")
    execute_command(router_name="vsrx1", command="show interfaces terse", exclude="down", count=true)
    execute_command(router_name="vsrx1", command="show log messages", tail=20)

`include` and `exclude` are regular expressions and ignore case; then `head` or `tail`
keep that many lines, or `count` gives their number. The router sends the whole output
and the server cuts it down, so a filter saves the agent's reading, not the router's work.

For a change:

- `dry_run=true` loads it, has the router check it, returns what would change and discards
  it.
- `confirm_timeout_secs` makes it a confirmed commit. Junos counts in minutes, so give
  whole minutes (120); anything else is rounded up.
- `comment` goes into the router's commit log.

`get_config`, `get_state` and the two command tools take a `timeout`, the seconds a router
is given to answer: 60 unless the call says otherwise, up to 600. A router that does not
answer in time gives `Error on <router>: no answer within N seconds`; the request may
still be running there.

## Limits

- **No rules.** Any change the login may make is committed, and any exec command without
  a pipe is passed to the router as it comes, `request system reboot` included. Give the login a Junos class
  that fits, and put a policy in front: [Holddown](https://github.com/Jitter00/holddown)
  has adapters for this server (`junos_netconf`, `iosxe`, `iosxr`).
- **One session per call.** Every call logs in again. That took under a second on the lab
  routers; what takes long on IOS XR (about ten seconds for a dry run) is the router
  working on the candidate configuration.
- **One release per platform, on virtual routers.** Nothing else was tried.
- **Groups are compared as written.** What a group changes where it is applied does not
  show under the places it applies to.
- **Secrets are returned.** `get_config` gives the configuration as the router holds it,
  password hashes included, and a dry run prints the hash of a secret it sets.
- **Element names are printed without their module.**

## Development

    git clone https://github.com/Jitter00/multivendor-netconf-mcp
    cd multivendor-netconf-mcp
    uv sync
    uv run ruff check
    uv run pytest

The tests need no router. Branches and pull requests only.

## Licence

MIT. See [LICENSE](LICENSE).
