MikroTik MCP (Model Context Protocol) Server

repository·master·Indexed 42 days ago

https://github.com/jeff-nasseri/mikrotik-mcp

An MCP server that enables AI assistants like Claude to manage MikroTik RouterOS devices using natural language. It provides a bridge for executing network configuration and management tasks, including VLAN operations, DHCP setup, firewall rules, and wireless configuration. It features a multi-device inventory system for fleet management, allowing for tagged rollouts, configuration comparisons, and fleet-wide updates via an LLM.

Tokens
48.3K
Snippets
220
Records
260
Agent score
54%

What's inside MikroTik MCP

  1. Overview of MikroTik MCP

    master

    MikroTik MCP acts as a bridge between AI assistants (like Claude Desktop) and MikroTik RouterOS devices. It enables LLMs to interact with your network infrastructure using natural language. Through this interface, an AI assistant can perform tasks such as:

    • Managing VLANs
    • Configuring firewall rules
    • Handling DNS settings
    • General RouterOS command execution via natural language requests.
  2. Explore MikroTik MCP tool categories

    master

    The MikroTik MCP server provides a wide range of tools categorized by their functional area on a MikroTik device. You can use these tools to manage everything from basic device inventory to advanced networking configurations like WireGuard, VLANs, and Firewall rules.

    Available tool categories include:

    • Inventory: Managing multiple MikroTik devices.
    • Interfaces: Ethernet, bridge, PPPoE, SFP, LTE, etc.
    • PoE: Power over Ethernet monitoring.
    • VLAN: VLAN Interface Management.
    • IP Address: IPv4 Address Management.
    • IPv6 Address: IPv6 Address Management.
    • DHCP: DHCP Server Management.
    • NAT: NAT Rules Management.
    • IP Pool: IP Pool Management.
    • Backup: Backup and Export Management.
    • Logs: Log Management.
    • Firewall: Firewall Filter Rules Management.
    • IPv6 Firewall: IPv6 Firewall Filter Rules Management.
    • Routes: Route Management.
    • DNS: DNS Management.
    • Users: User Management.
    • WireGuard: WireGuard VPN Management.
    • Queue: Queue Types, Trees, and Simple Queues.
    • Safe Mode: Safe Mode Session Management.
  3. What is MikroTik MCP?

    master
    MikroTik MCP (Model Context Protocol) is an AI-powered interface designed for managing and configuring MikroTik RouterOS devices using natural language. It acts as a bridge between an AI assistant and your network hardware, allowing you to describe desired network states (e.g., "Create a guest network") instead of manually typing complex RouterOS CLI commands. The AI assistant translates your intent into specific MCP tool calls that execute the necessary commands on the device.
  4. Manage Multi-Device Fleets via Inventory

    master

    When an inventory is configured, the MCP server manages multiple devices. Every tool accepts a device argument corresponding to the inventory title.

    Key Concepts:

    • Discovery: Use the list_devices tool to find available device titles, hosts, tags, and regions.
    • Targeting: Refer to devices by their exact title (e.g., office-core), by tags (e.g., tag: office), or by region.
    • Error Correction: If a device name is mistyped, the server provides a list of valid titles to allow the AI to self-correct.
  5. How device connections and safe mode work

    master

    Connection Lifecycle

    Every command opens its own SSH connection to the target device and closes it immediately upon completion. Connections are not pooled or shared. This ensures that multiple client sessions talking to the same server process do not interfere with each other (no 'shared fate'), though it does incur the cost of one SSH handshake per command.

    Safe Mode Scope

    Safe mode is managed on a per-device basis. Enabling safe mode on one device does not affect any other device in your inventory. Each device maintains its own persistent safe mode session.

  6. How the LLM interacts with a MikroTik fleet

    master

    The MCP server provides specific capabilities to allow an LLM to manage multiple devices seamlessly:

    1. Discovery: The list_devices tool allows the LLM to see all managed devices, including their title, host, tags, and region. Credentials are never exposed.
    2. Targeting: Every tool (e.g., interfaces, firewall, vlan) accepts an optional device parameter. The LLM uses the title from the inventory to specify which router to act upon.
    3. Error Handling: If the LLM omits the device parameter or provides an incorrect title, the server returns an error listing all valid device titles. This allows the LLM to self-correct.
    4. Isolation: Each command opens and closes its own SSH connection. Safe mode is tracked per device, meaning enabling safe mode on one router does not affect others.
  7. Select a specific device for a tool call

    master

    When managing multiple devices, every device-scoped tool accepts an optional device argument.

    • If only one device is configured in the inventory, you can omit the device argument.
    • If multiple devices are configured, you must pass the title of the target device.

    Omitting the device argument when multiple devices exist will result in an error listing the available device titles.

    list_devices()                                  # discover the fleet
    list_interfaces(device="TitleA")                # target one device
    create_vlan_interface(device="TitleB", name="vlan100", vlan_id=100, interface="ether1")
  8. How the multi-device inventory architecture works

    master

    The MikroTik MCP uses a multi-device inventory architecture to manage a fleet of routers. The workflow is as follows:

    1. The LLM calls specific MCP tools.
    2. Each tool passes a device title to the connector.
    3. The Inventory component resolves that title to the corresponding device's SSH client.
    4. The command is then executed on the selected router.

    To discover the available fleet, the LLM uses the list_devices tool.

  9. How MikroTik Safe Mode works in MCP

    master

    MikroTik Safe Mode protects against misconfigurations by holding all changes in memory only. In the context of this MCP server, Safe Mode works by opening a persistent SSH shell and sending a Ctrl+X command to the router.

    Key behaviors:

    • Persistence: While active, all subsequent tool calls are automatically routed through the same persistent interactive shell session.
    • Automatic Rollback: If the connection drops (due to a network outage, MCP server crash, or router reboot), the router automatically reverts all pending changes.
    • Manual Control: You must explicitly call commit_safe_mode to write changes to permanent storage (flash/NVRAM) or rollback_safe_mode to discard them.
    1. **Enable** — opens a persistent SSH shell and sends Ctrl+X to the router
    2. **Make changes** — every subsequent tool call is routed through the same session
    3. **Commit or Rollback** — send a second Ctrl+X to persist, or close the session to revert
  10. Manage multiple MikroTik devices with Inventory

    master
    Since version 0.14.0, MikroTik MCP supports managing multiple MikroTik devices organized within a fleet structure. This allows an LLM to interact with an entire inventory of devices rather than being limited to a single connection, enabling fleet-wide management and querying.
  11. Manage multiple devices using an Inventory file

    master

    Instead of configuring a single device via environment variables, you can provide a YAML inventory file. This allows the MCP server to manage a fleet of MikroTik devices.

    Important Notes:

    • The inventory file must be mounted as a volume into the container at the path specified by MIKROTIK_INVENTORY_FILE (the image uses /config as the mount point).
    • The container runs as uid 1000 (mcpuser), so ensure the mounted file is readable (e.g., chmod 644 inventory.yaml).
    • If an inventory is set, the single-device variables (MIKROTIK_HOST, etc.) are ignored.

    Example inventory.yaml structure:

    - title: TitleA
      host: 192.168.88.1
      port: 22
      username: admin
      password: change-me
      region: NL
      tags: [branch]
    
    - title: TitleB
      host: 192.168.89.1
      port: 22
      username: admin
      key_filename: /config/keys/id_ed25519

    Running with Docker CLI:

    docker run --rm -i \
      -v "$PWD/inventory.yaml:/config/inventory.yaml:ro" \
      -e MIKROTIK_INVENTORY_FILE=/config/inventory.yaml \
      ghcr.io/jeff-nasseri/mikrotik-mcp:latest
  12. Write and run integration tests

    master

    Integration tests should use testcontainers to spin up a real MikroTik RouterOS container.

    Guidelines:

    • Use the @pytest.mark.integration decorator.
    • Follow existing test structure and naming conventions.
    • Test complete workflows (CRUD operations).
    • Ensure proper cleanup for test isolation.

    To run your integration tests, use: pytest tests/integration/test_my_feature_integration.py -v

    """Integration tests for MikroTik my feature using testcontainers."""
    
    import pytest
    from mcp_mikrotik.scope.my_feature import (
        mikrotik_create_my_resource,
        mikrotik_list_my_resources
    )
    
    @pytest.mark.integration
    class TestMikroTikMyFeatureIntegration:
        def test_01_create_resource(self, mikrotik_container):
            result = mikrotik_create_my_resource(
                name="test_resource",
                required_param="test_value"
            )
            assert "failed" not in result.lower()
            assert "test_resource" in result
    
        def test_02_list_resources(self, mikrotik_container):
            result = mikrotik_list_my_resources()
            assert "test_resource" in result