FortiGate MCP服务器
FortiGate MCP服务器-用于管理FortiGate设备的综合模型上下文协议(MCP)服务器。该项目提供对FortiGate设备的编程访问,并支持与Cursor等MCP兼容工具的集成。
🚀 特性
- 设备管理:添加、删除和测试FortiGate设备的连接
- 防火墙管理:列出、创建、更新和删除防火墙规则
- 网络管理:管理地址和服务对象
- 路由管理:管理静态路由和接口
- HTTP传输:使用FastMCP的HTTP MCP协议
- Docker支持:易于安装和部署
- 光标集成:与Cursor IDE完全集成
📋 需求
- Python 3.8+
- FortiGate设备的访问权限
- API令牌或用户名/密码
🛠️ 安装
1.克隆项目
git clone
cd fortigate-mcp-server2.安装依赖项
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt3.配置
编辑 config/config.json 文件:
{
"fortigate": {
"devices": {
"default": {
"host": "192.168.1.1",
"port": 443,
"username": "admin",
"password": "password",
"api_token": "your-api-token",
"vdom": "root",
"verify_ssl": false,
"timeout": 30
}
}
},
"logging": {
"level": "INFO",
"file": "./logs/fortigate_mcp.log"
}
}🚀 用法
启动HTTP服务器
# Start with script
./start_http_server.sh
# Or manually
python -m src.fortigate_mcp.server_http \
--host 0.0.0.0 \
--port 8814 \
--path /fortigate-mcp \
--config config/config.json使用Docker运行
# Build and start
docker-compose up -d
# View logs
docker-compose logs -f fortigate-mcp-server🔧 光标MCP集成
1.光标MCP配置
编辑 ~/.cursor/mcp_servers.json 在光标中:
选项1:命令连接
{
"mcpServers": {
"fortigate-mcp": {
"command": "python",
"args": [
"-m",
"src.fortigate_mcp.server_http",
"--host",
"0.0.0.0",
"--port",
"8814",
"--path",
"/fortigate-mcp",
"--config",
"/path/to/your/config.json"
],
"env": {
"FORTIGATE_MCP_CONFIG": "/path/to/your/config.json"
}
}
}
}选项2:URL连接(推荐)
{
"mcpServers": {
"FortiGateMCP": {
"url": "http://0.0.0.0:8814/fortigate-mcp/",
"transport": "http"
}
}
}2.在游标中使用
要在游标中使用FortiGate MCP:
- 启动服务器:
cd /media/workspace/fortigate-mcp-server
python -m src.fortigate_mcp.server_http --host 0.0.0.0 --port 8814 --path /fortigate-mcp --config config/config.json- 重新启动游标
- 确保MCP服务器正在运行
- 在游标中使用FortiGate命令
📚 API命令
设备管理
list_devices-列出已注册的设备get_device_status-获取设备状态test_device_connection-测试连接add_device-添加新设备remove_device-删除设备discover_vdoms-发现VDOM
防火墙管理
list_firewall_policies-列出防火墙规则create_firewall_policy-创建新规则update_firewall_policy-更新规则delete_firewall_policy-删除规则
网络管理
list_address_objects-列出地址对象create_address_object-创建地址对象list_service_objects-列出服务对象create_service_object-创建服务对象
虚拟IP管理
list_virtual_ips-列出虚拟IPcreate_virtual_ip-创建虚拟IPupdate_virtual_ip-更新虚拟IPget_virtual_ip_detail-获取虚拟IP详细信息delete_virtual_ip-删除虚拟IP
路由管理
list_static_routes-列出静态路线create_static_route-创建静态路由update_static_route-更新静态路由delete_static_route-删除静态路由get_static_route_detail-获取静态路线详细信息get_routing_table-获取路由表list_interfaces-列出接口get_interface_status-获取接口状态
系统命令
health-健康检查test_connection-连接测试get_schema_info-架构信息
🧪 测试
运行测试
# Run all unit tests (default)
python -m pytest
# Run with coverage
python -m pytest --cov=src --cov-report=html
# Run specific test categories
python -m pytest tests/test_device_manager.py
python -m pytest tests/test_fortigate_api.py
python -m pytest tests/test_tools.py
# Run integration tests (requires server running)
python integration_tests.py
# Run only unit tests (default)
python -m pytest tests/
# Run with verbose output
python -m pytest -v
# Run with detailed error information
python -m pytest --tb=long测试类别
- 单元测试:测试单个组件和功能
- 集成测试:测试HTTP服务器功能(需要服务器运行)
- 覆盖:带有HTML输出的代码覆盖率报告
HTTP服务器测试
# Run test script
python test_http_server.py手动测试
# Health check
curl -X POST http://localhost:8814/fortigate-mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "health", "params": {}}'
# List devices
curl -X POST http://localhost:8814/fortigate-mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "list_devices", "params": {}}'📁 项目结构
fortigate-mcp-server/
├── src/
│ └── fortigate_mcp/
│ ├── __init__.py
│ ├── server_http.py # HTTP MCP server
│ ├── config/ # Configuration management
│ ├── core/ # Core components
│ ├── tools/ # MCP tools
│ └── formatting/ # Response formatting
├── config/
│ ├── config.json # Main configuration
│ └── config.example.json # Example configuration
├── examples/
│ └── cursor_mcp_config.json # Cursor MCP config
├── logs/ # Log files
├── tests/ # Test files
├── docker-compose.yml # Docker compose
├── Dockerfile # Docker image
├── start_http_server.sh # Startup script
├── test_http_server.py # Test script
└── README.md # This file🔍 故障排除
常见问题
- 连接错误
- 确保FortiGate设备可访问 - 验证API令牌或用户名/密码 - 使用 verify_ssl: false SSL证书问题
- 端口冲突
- 确保端口8814可用 - 使用更改端口 --port 参数
- 配置错误
- 确保 config.json 格式正确 - 检查JSON语法
- 光标MCP连接问题
- 确保服务器正在运行 - 验证URL是否正确 - 重新启动游标
日志
使用以下方式检查日志:
# HTTP server logs
tail -f logs/fortigate_mcp.log
# Docker logs
docker-compose logs -f fortigate-mcp-server🔒 安全
建议
- 使用API令牌
- 使用API令牌而不是用户名/密码 - 安全地存储令牌
- SSL证书
- 在生产中使用SSL证书 - 集 verify_ssl: true
- 网络安全
- 仅在安全网络上运行MCP服务器 - 使用防火墙规则限制访问
- 速率限制
- 启用速率限制 - 限制API调用
🤝 贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📄 许可证
该项目根据MIT许可证获得许可。看 LICENSE 文件以获取详细信息。
🙏 致谢
- FastMCP -用于MCP HTTP传输
- FortiGate API -用于FortiGate集成
- 光标 -用于MCP支持
📞 支持
对于问题:
- 使用 问题 页
- 检查文档
- 查看日志
______________________________________________________________________
备注:该项目已使用FortiGate设备进行了测试。请在投入生产前进行全面测试。
