使用MCP服务器的网络AI自动化
通过与Ansible和Arista路由器集成的模型上下文协议(MCP)服务器,使用Containerlab、Claude AI自动化网络配置和管理。
🚀 概述
该项目利用Containerlab Claude AI和模型上下文协议(MCP)提供智能网络自动化功能。它将人工智能驱动的决策能力与行业标准的Ansible剧本相结合,有效地管理Arista网络设备。
主要特点
- 人工智能驱动的自动化:使用Claude AI智能管理网络配置
- MCP服务器集成:通过MCP与Claude无缝集成,实现动态网络运营
- 可靠的集成:可靠网络自动化的行业标准剧本
- Arista路由器支持:直接支持Arista EOS设备
- OSPF配置:自动化OSPF路由协议管理
- 可扩展设计:使用基于意图的配置组织您的网络基础设施
📋 先决条件
在开始之前,请确保已安装以下内容:
- \[x\] Containerlab和Docker
- \[x\] Python 3.8或更高版本
- \[x\] 答案2.9+
- \[x\] 使用MCP服务器功能访问Claude API
- \[x\] 将部署在containerlab中的Arista路由器映像
- \[x\] 为您的网络设备配置SSH访问
您将在下面找到:
- \[x\] 如何 设置这个实验室 从零开始
- \[x\] 电流 拓扑图 便携式网络图形
- \[x\] The Containerlab YAML 实验室文件
- \[x\] The 网络.json 库存文件
- \[x\] The 意图。JSON 库存文件
- \[x\] Ansible剧本文件
- \[x\] 重要注意事项和指导
⚒️ 项目技术栈
用于构建项目的主要工具和技术:
- \[x\] 克劳德AI(克劳德代码)
- \[x\] MCP服务器(FastMCP)
- \[x\] 集装箱实验室
- \[x\] 可靠
- \[x\] Python
- \[x\] Scrapli
- \[x\] EOS eAPI(Arista)
- \[x\] Ubuntu
- \[x\] VS代码
- \[x\] VirtualBox/VMware
🛠️ 环境设置
在进入网络拓扑部分之前,您将在下面找到构建实验室的指导。如果你有更多的空间,你可以添加
本实验室的虚拟机资源:
- \[x\] VirtualBox或VMware
- \[x\] Ubuntu 24.04.4虚拟机
- \[x\] 4个处理器核心
- \[x\] 4 GB RAM内存
- \[x\] 50GB硬盘
总结检查表
🔧 构建拓扑和配置路由器的步骤
- \[x\] 所有要求的初始安装和升级都包括fastmcp。
- git clone https://github.com/chiwiks/Network-AI-Automation-using-MCP-Server.git
- cd Network-AI-Automation-using-MCP-Server
- python3 -m venv mcp
- source mcp/bin/activate
- pip install --upgrade pip
- pip install fastmcp==3.0.0b1 scrapli asyncssh python-dotenv- \[x\] 安装Docker和Containerlab
- \[x\] 下载官方Arista路由器镜像并将其导入Docker:
- [Aristat cEOs] (https://www.arista.com/en/login) image
- Download lates cE0 image with .tar.xz
- sudo docker import ~/cEOS64-lab-4.35.0F.tar.xz ceos:4.35.0F
- docker images
- \[x\] 使用Containerlab和\*\*\*aris.yml\*\*\*文件构建拓扑
name: aris-lab
topology:
nodes:
r1:
kind: arista_ceos
image: ceos:4.35.0F
mgmt-ipv4: 172.20.20.11
r2:
kind: arista_ceos
image: ceos:4.35.0F
mgmt-ipv4: 172.20.20.22
r3:
kind: arista_ceos
image: ceos:4.35.0F
mgmt-ipv4: 172.20.20.33
links:
- endpoints: ["r1:eth1", "r2:eth1"]
- endpoints: ["r2:eth2", "r3:eth1"]
- endpoints: ["r1:eth2", "r3:eth2"]
# The topology consists of three Arista router connected to each other
- \[x\] 运行Containerlab命令以构建topolgy
- containerlab deploy -t aris.yml (build topology)
- containerlab save -t aris.yml ( save topology and configurations)
- containerlab inspect -t aris.yml (check the condition of the topology)
- containerlab redeploy -t aris.yml (redeploy the deploy the topolgy)
- docker exec -it Cli (connect to different routers after they are deployed default is enable)
- containerlab graph -t aris.yml (view topology. The png is found topology folder)
- containerlab destroy -t aris.yml (Destroy the topology)
- Name of the three routers are clab-mcp-lab-r1 clab-mcp-lab-r2 clab-mcp-lab-r1- \[x\] 当前拓扑:
- \[x\] 使用Ansible脚本文件在三个Arista路由器上配置接口ip和OSPF。
- When you build your topology using containerlab, it automatically creates an ansible inventory file of the topolgy. Run the playbook_ospf.yml to configure the arista routers.
- name: configure ospf on the three routers
hosts: arista_ceos
gather_facts: no
tasks:
- name: Configure R1 interfaces
arista.eos.eos_interfaces:
config:
- name: Ethernet1
enabled: true
mode: layer3
- name: Ethernet2
enabled: true
mode: layer3
state: merged
when: inventory_hostname == "clab-aris-lab-r1"
- name: Configure R2 interfaces
arista.eos.eos_interfaces:
config:
- name: Ethernet1
enabled: true
mode: layer3
- name: Ethernet2
enabled: true
mode: layer3
state: merged
when: inventory_hostname == "clab-aris-lab-r2"
- name: Configure R3 interfaces
arista.eos.eos_interfaces:
config:
- name: Ethernet1
enabled: true
mode: layer3
- name: Ethernet2
enabled: true
mode: layer3
state: merged
when: inventory_hostname == "clab-aris-lab-r3"
- name: Configure R1 layer interfaces
arista.eos.eos_l3_interfaces:
config:
- name: Ethernet1
ipv4:
- address: 192.168.12.1/24
- name: Ethernet2
ipv4:
- address: 192.168.13.1/24
state: merged
when: inventory_hostname == "clab-aris-lab-r1"
- name: Configure R2 layer interfaces
arista.eos.eos_l3_interfaces:
config:
- name: Ethernet1
ipv4:
- address: 192.168.12.2/24
- name: Ethernet2
ipv4:
- address: 192.168.23.2/24
state: merged
when: inventory_hostname == "clab-aris-lab-r2"
- name: Configure R3 layer interfaces
arista.eos.eos_l3_interfaces:
config:
- name: Ethernet1
ipv4:
- address: 192.168.23.3/24
- name: Ethernet2
ipv4:
- address: 192.168.13.3/24
state: merged
when: inventory_hostname == "clab-aris-lab-r3"
- name: Configure OSPF on the three routers
arista.eos.eos_ospfv2:
config:
hostname: clab-aris-lab-r1
- process_id: 1
router_id: "1.1.1.1"
areas:
id: "0.0.0.0"
type: normal
networks:
- area: "0.0.0.0"
prefix: 192.168.12.0/24
- area: "0.0.0.0"
prefix: 192.168.13.0/24
state: merged
hostname: clab-aris-lab-r2
- process_id: 1
router_id: "2.2.2.2"
areas:
id: "0.0.0.0"
type: normal
networks:
- area: "0.0.0.0"
prefix: 192.168.12.0/24
- area: "0.0.0.0"
prefix: 192.168.23.0/24
state: merged
hostname: clab-aris-lab-r3
- process_id: 1
router_id: "3.3.3.3"
areas:
id: "0.0.0.0"
type: normal
networks:
- area: "0.0.0.0"
prefix: 192.168.23.0/24
- area: "0.0.0.0"
prefix: 192.168.13.0/24
state: merged
- \[x\] 创建包含三个路由器及其类型和用于登录它们的连接类型的newtwork-json清单文件。
{
"R1": { "host": "172.20.20.11", "platform": "arista_eos", "transport": "asyncssh"},
"R2": { "host": "172.20.20.22", "platform": "arista_eos", "transport": "asyncssh"},
"R3": { "host": "172.20.20.33", "platform": "arista_eos", "transport": "asyncssh"}
}🔧 构建将连接到拓扑的python MCP服务器
- \[x\] Mcpserver.py代码摘要
- \[x\] Showcommand输入模型,处理MCP在设备上运行的客户端输入的显示和显示命令等输入(取设备和命令名称)
class ShowCommand(BaseModel):
"""Run show command against network device"""
device: str = Field(..., description="Device name from inventory (e.g. R1, R2 , R3)")
command: str = Field(..., description="Show command to execute on the device")- \[x\] 处理设备推送配置的配置输入模型(接受设备和命令名称)
class ConfigCommand(BaseModel):
""" Send configuration commands to one or more devices"""
devices: list[str] = Field(..., description="Device names from inventory (e.g. ['R1', 'R2'])")
commands : list[str] = Field(..., description="Configuration commands to apply")- \[x\] 将处理show和config的函数馈送到mcp服务器中
- run_show function takes params from the ShowCommand input base model
@mcp.tool(name="run_show")
async def run_show(params: ShowCommand)
- push_config function takes the params from the ConfigCommand input base model
@mcp.tool(name="push_config")
async def push_config(params: ConfigCommand) -> dict
Both functions are feed to the MCP ai server using the @mcp.tool- \[x\] 接下来,将您的mcpserver脚本连接到Claude ai
Run the command "claude mcp add mcp_network_automation -s user -- ./mcp/bin/python MCPServer.py"
Note: mcp_network_automation is name of FastMCP that was imported in McpServer.py script
Run the command "claude mcp list" -> Connected showing that your mcp server is connected to Claude
🔧 使用连接到MCP的claude AI运行show命令的示例
- \[x\] 让Claude检查R2的接口ip地址。
As you can see it will use the run_show function and logs into the router and runs the show ip interface brief to get the ip addresshttps://github.com/user-attachments/assets/6d31d2fd-8353-4548-ae28-f88283dc8a67
- \[x\] 让Claude显示当前在拓扑中运行的路由协议。
It will use the run_show function to fetch and display OSPF running in your topolgogyhttps://github.com/user-attachments/assets/d76d5936-c92b-4448-bc2f-66781b184cb0
- \[x\] 接下来让Claude在界面上配置描述
This will make Claude to us the push_config function to configure the description the interfacehttps://github.com/user-attachments/assets/4306df42-9108-4add-a96b-89f8fa1755ae
🔥 故障排除
三种故障排除方案位于\[troubleshoot.md\]中
