NetOps MCP-网络操作工具MCP服务器
一个全面的模型上下文协议(MCP)服务器,通过标准化接口提供对基本网络操作和基础设施工具的访问。
🚀 特性
网络连接工具
- 拼:使用可定制的数据包计数和超时测试主机连接
- 路由跟踪:使用可配置的最大跳数跟踪网络路径
- 港铁:使用实时统计数据监视网络路径
- 远程登录:使用telnet测试端口连接
- Netcat:使用netcat测试端口连接
HTTP/API测试工具
- cURL:执行HTTP请求,完全控制标头、方法和数据
- HTTPie:具有简化语法的替代HTTP客户端
- API测试:使用预期状态代码验证API终结点
DNS工具
- 名称服务器查找:查询各种记录类型的DNS记录
- 挖:高级DNS查询工具
- 主机:简单DNS查找实用程序
网络发现工具
- Nmap:网络扫描和服务枚举
- 端口扫描:目标端口扫描功能
- 服务发现:识别目标上正在运行的服务
系统监控工具
- SS:套接字统计和连接监控
- 网络统计:网络统计和连接信息
- 地址解析协议:地址解析协议表管理
- ARP:测试ARP连接
系统信息工具
- 系统状态:CPU、内存和磁盘使用情况监控
- 进程列表:正在运行进程枚举
- 所需工具检查:验证系统工具可用性
📋 先决条件
所需系统工具
系统上必须安装以下工具:
# Network tools
curl, ping, traceroute, mtr, telnet, nc (netcat)
# DNS tools
nslookup, dig, host
# Network discovery
nmap
# System tools
ss, netstat, arp, arping
# HTTP tools
httpie (optional, for enhanced HTTP testing)Python要求
- Python 3.8+
- uv包管理器(推荐)
🛠️ 安装
使用紫外线(推荐)
# Clone the repository
git clone https://github.com/alpadalar/NetOpsMCP.git
cd NetOpsMCP
# Install dependencies using uv
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -e .使用pip
# Clone the repository
git clone https://github.com/alpadalar/NetOpsMCP.git
cd NetOpsMCP
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -e .使用Docker
# Build and run with Docker Compose
docker compose up -d
# Or build manually
docker build -t netopsmcp .
docker run -p 8815:8815 netopsmcp🚀 快速开始
1.启动服务器
# Using Python directly
python -m netops_mcp.server_http --host 0.0.0.0 --port 8815
# Using Docker
docker compose up -d
# Using the provided script
./start_http_server.sh2.测试服务器
# Health check
curl http://localhost:8815/health
# Test system requirements
curl -X POST http://localhost:8815/netops-mcp \
-H "Content-Type: application/json" \
-d '{"method": "check_required_tools", "params": {}}'3.示例用法
# Ping a host
result = ping_host("google.com", count=4, timeout=10)
# Test HTTP endpoint
result = curl_request("https://httpbin.org/get", method="GET")
# DNS lookup
result = nslookup_query("google.com", record_type="A")
# Network scan
result = nmap_scan("192.168.1.1", ports="1-1000", scan_type="basic")📖 API 参考
网络连接
ping_host(host: str, count: int = 4, timeout: int = 10)
使用ping测试与主机的连接。
参数:
host:目标主机名或IP地址count:ping数据包的数量(默认值:4)timeout:超时时间(秒)(默认值:10)
退货: Ping统计和结果
traceroute_path(target: str, max_hops: int = 30, timeout: int = 30)
跟踪目标的网络路径。
参数:
target:目标主机名或IP地址max_hops:最大跳数(默认值:30)timeout:超时时间(秒)(默认值:30)
退货: 网络路径信息
mtr_monitor(target: str, count: int = 10, timeout: int = 30)
使用MTR监控网络路径。
参数:
target:目标主机名或IP地址count:探头数量(默认值:10)timeout:超时时间(秒)(默认值:30)
退货: 港铁统计数据和跳车信息
HTTP测试
curl_request(url: str, method: str = "GET", headers: dict = None, data: dict = None, timeout: int = 30)
使用curl执行HTTP请求。
参数:
url:目标URLmethod:HTTP方法(GET、POST、PUT、DELETE、PATCH)headers:HTTP标头字典data:请求POST/PUT请求的数据timeout:请求超时(秒)
退货: HTTP响应和定时信息
httpie_request(url: str, method: str = "GET", headers: dict = None, data: dict = None, timeout: int = 30)
使用HTTPie执行HTTP请求。
参数: 与curl_request相同
退货: HTTP响应和定时信息
DNS工具
nslookup_query(domain: str, record_type: str = "A", server: str = None)
使用nslookup查询DNS记录。
参数:
domain:目标域名record_type:DNS记录类型(A、AAAA、MX、NS、TXT、CNAME)server:自定义DNS服务器(可选)
退货: DNS查询结果
dig_query(domain: str, record_type: str = "A", server: str = None)
使用dig查询DNS记录。
参数: 与nslookup_query相同
退货: 详细的DNS查询结果
网络发现
nmap_scan(target: str, ports: str = None, scan_type: str = "basic", timeout: int = 300)
使用nmap扫描网络。
参数:
target:目标主机名、IP或网络范围ports:端口范围(例如,“1-1000”、“804438080”)scan_type:扫描类型(基本、完整、隐形)timeout:扫描超时(秒)
退货: 网络扫描结果
port_scan(target: str, ports: str, timeout: int = 60)
执行目标端口扫描。
参数:
target:目标主机名或IP地址ports:要扫描的端口范围timeout:扫描超时(秒)
退货: 端口扫描结果
系统监控
system_status()
获取系统状态信息。
退货: CPU、内存和磁盘使用统计信息
ss_connections(state: str = None, protocol: str = None)
使用ss显示网络连接。
参数:
state:按连接状态筛选protocol:按协议筛选
退货: 网络连接信息
netstat_connections(state: str = None, protocol: str = None)
使用netstat显示网络连接。
参数: 与ss_connections相同
退货: 网络连接信息
🧪 测试
运行所有测试
# Using pytest
pytest tests/ -v
# Using uv
uv run pytest tests/ -v
# With coverage
pytest tests/ --cov=src --cov-report=html --cov-report=term-missing测试类别
- 单元测试:单个工具功能
- 集成测试:端到端工作流测试
- 模拟测试:命令执行模拟
- 验证测试:输入验证和错误处理
测试覆盖率
测试套件包括:
- ✅ 所有工具方法和功能
- ✅ 输入验证和错误处理
- ✅ 命令执行和输出解析
- ✅ 边缘情况和错误场景
- ✅ 外部依赖关系的模拟测试
要生成覆盖率报告,请执行以下操作:
# Generate HTML coverage report
pytest tests/ --cov=src --cov-report=html
# Generate terminal coverage report
pytest tests/ --cov=src --cov-report=term-missing
# Generate both reports
pytest tests/ --cov=src --cov-report=html --cov-report=term-missing🔧 配置
环境变量
# Server configuration
NETOPS_MCP_HOST=0.0.0.0
NETOPS_MCP_PORT=8815
NETOPS_MCP_LOG_LEVEL=INFO
# Tool timeouts
PING_TIMEOUT=10
TRACEROUTE_TIMEOUT=30
MTR_TIMEOUT=30
CURL_TIMEOUT=30
NMAP_TIMEOUT=300配置文件
服务器将从以下位置自动创建默认配置文件 config/config.example.json 第一次运行时,或者您可以创建 config/config.json 手动:
{
"logging": {
"level": "INFO",
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"file": "logs/netops-mcp.log"
},
"security": {
"allow_privileged_commands": false,
"allowed_hosts": [],
"rate_limit_requests": 100,
"rate_limit_window": 60
},
"network": {
"default_timeout": 30,
"max_scan_timeout": 300,
"allowed_ports": "1-65535"
},
"server": {
"host": "0.0.0.0",
"port": 8815,
"path": "/netops-mcp"
}
}🐳 Docker支持
Docker Compose
version: '3.8'
services:
netopsmcp:
build: .
ports:
- "8815:8815"
environment:
- NETOPS_MCP_HOST=0.0.0.0
- NETOPS_MCP_PORT=8815
volumes:
- ./logs:/app/logs
- ./config:/app/config
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8815/health"]
interval: 30s
timeout: 10s
retries: 3Docker构建
# Build image
docker build -t netopsmcp .
# Run container
docker run -d \
--name netopsmcp \
-p 8815:8815 \
-v $(pwd)/logs:/app/logs \
-v $(pwd)/config:/app/config \
netopsmcp📊 监控和日志记录
日志级别
- 调试:详细的调试信息
- 信息:一般操作信息
- 警告:潜在问题的警告信息
- 错误:失败操作的错误消息
日志文件
logs/netops-mcp.log:主应用程序日志logs/access.log:HTTP访问日志logs/error.log:错误日志
健康检查
# Check server health
curl http://localhost:8815/health
# Check system requirements
curl -X POST http://localhost:8815/netops-mcp \
-H "Content-Type: application/json" \
-d '{"method": "check_required_tools", "params": {}}'🔒 安全注意事项
网络安全
- 防火墙规则:为服务器端口配置适当的防火墙规则
- 访问控制:必要时实施身份验证
- 网络隔离:尽可能在隔离的网络环境中运行
工具安全
- 特权操作:某些工具需要提升权限
- 网络扫描:了解网络扫描的法律影响
- 速率限制:对资源密集型业务实施费率限制
最佳实践
- 输入验证:所有输入在处理前都经过验证
- 错误处理:全面的错误处理和记录
- 超时管理:所有操作的可配置超时
- 资源限制:内置资源使用限制
🚀 生产部署
快速生产设置
- 生成API密钥:
python scripts/generate_api_key.py -n 2 --config config/config.json- 配置安全 (
config/config.json):
{
"security": {
"require_auth": true,
"api_keys": ["your-generated-key-here"],
"rate_limit_requests": 100,
"rate_limit_window": 60
}
}- 使用Docker Compose进行部署:
docker compose up -d- 验证部署:
curl http://localhost:8815/health认证
服务器支持API密钥身份验证以实现安全访问:
# Make authenticated request
curl -X POST http://localhost:8815/netops-mcp \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"method": "ping_host", "params": {"host": "google.com"}}'HTTPS设置(推荐)
使用反向代理(nginx或Caddy)进行HTTPS:
server {
listen 443 ssl http2;
server_name your-domain.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://localhost:8815;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}生产特点
- ✅ API密钥验证:使用Bearer令牌进行安全访问控制
- ✅ 速率限制:内置速率限制(默认为100要求/分钟)
- ✅ 输入验证:全面的输入净化
- ✅ 结构化日志记录:用于生产环境的JSON日志记录
- ✅ 健康检查:内置健康检查端点
- ✅ Docker支持:具有多阶段构建的生产就绪Docker映像
- ✅ 非root用户:在容器中以无特权用户身份运行
- ✅ 资源限制:可配置的CPU和内存限制
- ✅ CORS支持:可配置的web应用程序CORS
- ✅ 安全标头:自动安全标头
CI/CD管道
GitHub操作工作流包括:
- 测试:Python 3.10、3.11、3.12的自动测试
- 代码检查:代码质量检查(黑色、Ruff、mypy)
- 安全:安全扫描(Bandit、Safety、Trivy)
- 发布:自动将Docker镜像发布到GitHub容器注册表
文档
🤝 贡献
开发设置
# Clone repository
git clone https://github.com/alpadalar/NetOpsMCP.git
cd NetOpsMCP
# Install development dependencies
uv pip install -e .
# Run tests
pytest tests/ -v代码风格
- 黑色:代码格式
- 拉夫:梳理和进口分拣
- 米皮:类型检查
测试指南
- 为所有新功能编写测试
- 保持测试覆盖率在90%以上
- 使用有意义的测试名称和描述
- 模拟外部依赖关系
拉取请求流程
- 分叉存储库
- 创建要素分支
- 进行更改
- 添加新功能的测试
- 更新文档
- 提交拉取请求
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🆘 支持
文档
- API参考:参见上文API参考部分
- 配置指南:请参阅上面的配置部分
- 故障排除:请参阅上面的支持部分
问题
- 错误报告:使用GitHub问题
- 功能请求:通过GitHub提交问题
- 安全问题:直接联系维护人员
社区
- 问题:GitHub用于讨论和提问的问题
- 文档:有关综合指南,请参阅上述章节
🙏 致谢
- MCP协议:模型上下文协议规范
- 网络工具:开源网络实用程序
- 测试框架:pytest和相关工具
- 社区:贡献者和用户
______________________________________________________________________
网络运营MCP -通过标准化的工具访问授权网络运营。
托管部署
托管部署可在 Frontier AI.
