网络MCP服务器
模型上下文协议(MCP)服务器,为AI代理提供网络诊断工具。旨在将繁重的网络分析卸载到服务器上,并返回针对LLM消费优化的结构化、可操作的数据。
](https://pypi.org/project/network-mcp/)  
特性
- 连接性测试:ping、跟踪路由、DNS查找、端口检查、MTR
- 批量操作:同时测试多个主机/端口
- 本地网络信息:获取接口、路由、DNS配置、ARP表、连接(跨平台)
- Pcap分析:使用scapy分析数据包捕获(不需要tshark)
- 自定义过滤器:执行高级查询的scapy过滤器表达式(AST验证)
- 安全控制:用于目标验证的可配置列表/块列表(在连接工具上强制执行)
- PCAP路径护栏:限制允许服务器从哪些目录读取捕获
- 智能摘要:返回人类可读的摘要和结构化数据
安装
pip install network-mcp或者从源代码安装:
git clone https://github.com/labeveryday/network-mcp.git
cd network-mcp
pip install -e .快速开始
运行MCP服务器:
network-mcp或者用Python:
python -m network_mcp.server建议首次运行(帮助代理决定此主机上的安全/可用内容):
- 检查功能:呼叫
capabilities查看已安装的二进制文件(如mtr)、主动安全策略和PCAP路径护栏。 - 决定政策:如果您需要放松/收紧目标验证或PCAP访问,请在启动前设置env变量。
- 默认情况下运行单元测试:
pytest除非您选择加入,否则跳过集成测试(请参阅开发)。
可用工具
诊断
| 工具 | 说明 |
|---|---|
capabilities | 报告运行时功能(已安装的二进制文件,如 mtr、主动安全策略、pcap路径护栏),以便代理可以计划工具的使用 |
规划(纯CIDR/VLAN数学)
专为一级/二级NOC工作流程而设计。这些工具是 纯净 (无网络调用,确定性输出),可安全用于规划和验证。
| 工具 | 说明 |
|---|---|
cidr_info | CIDR原语(IPv4/IPv6):网络、可用范围、掩码/通配符、计数 |
ip_in_subnet | 检查IP是否在子网中,以及它是否是可用的主机地址 |
subnet_split | 将CIDR拆分为大小相等的子子网(通过新前缀或两次计数的幂) |
cidr_summarize | 将CIDR折叠/聚合到汇总路由中(IPv4/IPv6单独处理) |
check_overlaps | 查找CIDR之间的重叠/包含冲突 |
validate_vlan_map | 验证每个VLAN 1个子网的映射和曲面重叠 |
find_vlan_for_ip | 从提供的VLAN映射中查找与IP匹配的VLAN(第1层“它属于哪里?”) |
ip_in_vlan | 检查IP是否属于VLAN;如果没有,请提供最佳猜测VLAN匹配(唯一时) |
plan_subnets | 从父IPv4块分配VLAN子网(确定性) |
输入示例
- VLAN映射(接受两种格式):
{
"10": "192.168.10.0/24",
"20": { "cidr": "192.168.20.0/24", "name": "Voice" }
}plan_subnets要求(支持别名:hosts和prefix):
[
{ "vlan_id": 10, "name": "Users", "hosts": 120 },
{ "vlan_id": 20, "name": "Voice", "hosts": 60 },
{ "vlan_id": 30, "name": "Printers", "prefix": 26 }
]外部Intel
| 工具 | 说明 |
|---|---|
rdap_lookup | 使用RDAP对域名和IP进行WHOIS风格的查找 |
asn_lookup | IP的源ASN查找(BGP源intel) |
连接工具
| 工具 | 说明 |
|---|---|
ping | ICMP ping与延迟统计和数据包丢失 |
traceroute | 路径分析显示每一跳的延迟 |
dns_lookup | DNS解析(A、AAAA、MX、TXT等)和反向查找 |
port_check | 带有横幅抓取的TCP端口连接测试 |
mtr | 跟踪路由+ping与每跳统计相结合 |
批量操作
| 工具 | 说明 |
|---|---|
batch_ping | 同时Ping多个主机 |
batch_port_check | 检查单个主机上的多个端口 |
batch_dns_lookup | 并行解析多个主机名 |
本地网络信息工具
适用于Linux、macOS和Windows的跨平台工具。
| 工具 | 说明 |
|---|---|
get_interfaces | 列出带有IP、MAC和状态的网络接口 |
get_routes | 使用默认网关获取路由表 |
get_dns_config | 获取配置的DNS服务器和搜索域 |
get_arp_table | 获取ARP缓存(IP到MAC映射) |
get_connections | 列出活动TCP/UDP连接 |
get_public_ip | 获取从互联网上看到的公共/外部IP地址 |
Pcap分析工具
| 工具 | 说明 |
|---|---|
pcap_summary | 高级捕获统计数据:数据包、持续时间、协议、顶级通话者 |
get_conversations | 端点之间的网络流/对话 |
analyze_throughput | 每个会话/流的观测吞吐量(Mbps),包括主导方向和持续时间 |
find_tcp_issues | 检测重传、重置、零窗口、复制ACK |
analyze_dns_traffic | DNS查询、失败、响应缓慢 |
filter_packets | 按IP、端口或协议提取数据包 |
get_protocol_hierarchy | 按数据包和字节细分的协议 |
custom_scapy_filter | 执行自定义scapy过滤器表达式 |
IDE集成
克劳德桌面版
添加 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)或 %APPDATA%\Claude\claude_desktop_config.json (Windows):
{
"mcpServers": {
"network-tools": {
"command": ["network-mcp"]
}
}
}光标
添加到MCP设置中:
{
"mcpServers": {
"network-tools": {
"command": ["network-mcp"]
}
}
}使用紫外线
如果您安装了uv:
{
"mcpServers": {
"network-tools": {
"command": "uv",
"args": ["run", "--directory", "/path/to/network-mcp", "network-mcp"]
}
}
}示例响应
拼
{
"success": true,
"target": "google.com",
"resolved_ip": "142.250.80.46",
"packets_sent": 4,
"packets_received": 4,
"packet_loss_percent": 0.0,
"min_latency_ms": 11.2,
"avg_latency_ms": 12.8,
"max_latency_ms": 15.1,
"summary": "google.com is reachable. 4/4 packets received, avg latency 12.8ms"
}批量Ping
{
"success": true,
"total_targets": 3,
"successful": 3,
"failed": 0,
"results": [
{"target": "8.8.8.8", "success": true, "avg_latency_ms": 12.5},
{"target": "1.1.1.1", "success": true, "avg_latency_ms": 8.2},
{"target": "google.com", "success": true, "avg_latency_ms": 15.1}
],
"summary": "Batch ping: 3/3 targets reachable"
}TCP问题检测
{
"success": true,
"file_path": "/tmp/capture.pcap",
"total_tcp_packets": 15234,
"issues": [
{
"issue_type": "retransmission",
"count": 47,
"severity": "medium",
"recommendation": "Retransmissions indicate packet loss. Check for network congestion."
}
],
"has_issues": true,
"summary": "TCP issues detected in 15234 packets: 47 retransmissions"
}吞吐量(来自PCAP)
{
"success": true,
"file_path": "/tmp/capture.pcap",
"total_packets_scanned": 100000,
"conversations_analyzed": 87,
"top_n": 3,
"sort_by": "mbps_total",
"conversations": [
{
"src_ip": "10.0.0.10",
"src_port": 51544,
"dst_ip": "93.184.216.34",
"dst_port": 443,
"protocol": "TCP",
"packets_total": 1240,
"bytes_total": 18423333,
"duration_seconds": 9.84,
"start_time": 1734567890.01,
"end_time": 1734567899.85,
"packets_forward": 820,
"bytes_forward": 17600000,
"packets_reverse": 420,
"bytes_reverse": 823333,
"mbps_forward": 14.308,
"mbps_reverse": 0.669,
"mbps_total": 14.977,
"direction": "10.0.0.10:51544 -> 93.184.216.34:443"
}
],
"summary": "Throughput analysis: 87 conversations from 100000 packets. Top flow 10.0.0.10:51544 -> 93.184.216.34:443 at ~14.977 Mbps over 9.84s"
}获取接口
{
"success": true,
"interfaces": [
{
"name": "en0",
"status": "up",
"mac_address": "00:11:22:33:44:55",
"ipv4_addresses": ["192.168.1.100"],
"ipv6_addresses": ["fe80::1"],
"netmask": "255.255.255.0",
"mtu": 1500
}
],
"default_interface": "en0",
"summary": "Found 5 interfaces (3 up). Primary: en0"
}获取公共IP
{
"success": true,
"public_ip": "203.0.113.42",
"service_used": "ipify.org",
"summary": "Public IP: 203.0.113.42 (via ipify.org)"
}RDAP查找(WHOIS风格)
{
"success": true,
"query": "1.1.1.1",
"query_type": "ip",
"rdap_url": "https://rdap.org/ip/1.1.1.1",
"handle": "NET-1-1-1-0-1",
"country": "AU",
"start_address": "1.1.1.0",
"end_address": "1.1.1.255",
"summary": "RDAP 1.1.1.1: 1.1.1.0–1.1.1.255 (AU), handle NET-1-1-1-0-1"
}ASN查找
{
"success": true,
"ip": "1.1.1.1",
"asn": "13335",
"prefix": "1.1.1.0/24",
"country": "AU",
"registry": "apnic",
"allocated": "2011-08-11",
"as_name": "CLOUDFLARENET",
"summary": "1.1.1.1 originates from AS13335 (CLOUDFLARENET), prefix 1.1.1.0/24"
}配置
创建 config.yaml 在您的工作目录中或 ~/.network-mcp/config.yaml:
security:
# Only allow these targets (glob patterns, CIDR ranges)
allowed_targets:
- "*.company.com"
- "10.0.0.0/8"
- "192.168.0.0/16"
# Block these targets
blocked_targets:
- "*.gov"
- "localhost"
- "127.0.0.0/8"
# Block private IPs
block_private: false
# Block cloud metadata endpoints (AWS, GCP, etc.)
block_cloud_metadata: true
pcap:
max_packets: 100000
allow_custom_filters: true
# Restrict which directories the server is allowed to read pcaps from.
# (Paths are resolved before checking.)
allowed_paths:
- "."
- "~/Documents"
- "/tmp"环境变量:
NETWORK_MCP_ALLOWED_TARGETS="*.company.com,10.0.0.0/8"
NETWORK_MCP_BLOCKED_TARGETS="*.gov,localhost"
NETWORK_MCP_BLOCK_PRIVATE="true"
NETWORK_MCP_MAX_PACKETS="50000"
NETWORK_MCP_PCAP_ALLOWED_PATHS=".,~/Documents,/tmp"为什么选择MCP for Network Tools?
代币效率:LLM有上下文限制。服务器进行繁重的处理(解析10万个数据包),并返回简洁的摘要而不是原始数据。
更好的推理:法学硕士擅长决策 *什么* 进行调查,而不是解析原始输出。结构化数据有助于做出更好的决策。
一致性:服务器端处理是确定性的。您不需要每次都依赖LLM来正确解释traceroute输出。
例子
这 examples/ 目录包含使用以下命令的工作示例 Strands代理商:
| 示例 | 说明 |
|---|---|
ollama_agent.py | 使用Ollama的交互式聊天代理(本地模型) |
incident-demo/ | 自给自足的演示:人工智能通过语音警报诊断网络故障 |
eval_agent.py | 评估模型使用网络工具的情况 |
快速启动:
cd examples
pip install strands-agents strands-agents-tools 'strands-agents[ollama]'
python ollama_agent.py看 examples/README.md 获取完整文档。
发展
# Clone and install dev dependencies
git clone https://github.com/labeveryday/network-mcp.git
cd network-mcp
pip install -e ".[dev]"
# Run unit tests (integration tests are skipped by default)
pytest
# Run integration tests (requires system tools and/or network access)
pytest -m integration
# Run linting
ruff check .项目结构
network-mcp/
├── src/network_mcp/
│ ├── __init__.py
│ ├── server.py # FastMCP server
│ ├── config.py # Configuration and security
│ ├── tools/
│ │ ├── connectivity.py # ping, traceroute, dns, port_check, mtr, batch ops
│ │ ├── local.py # local network info (interfaces, routes, etc.)
│ │ └── pcap.py # pcap analysis tools
│ └── models/
│ └── responses.py # Pydantic response models
├── tests/
├── pyproject.toml
└── README.md需求
- Python 3.10+
- 系统工具:
ping,traceroute(大多数系统的标准配置) - 可选:
mtrMTR工具
许可证
MIT许可证-请参阅 许可证 文件以获取详细信息。
