FortiGate MCP Server
A production-ready Model Context Protocol (MCP) server for managing FortiGate firewalls
Features • Quick Start • Configuration • Tools • Architecture • Security • Testing
______________________________________________________________________
概述
FortiGate MCP服务器通过以下方式公开FortiGate防火墙管理功能 模型上下文协议,使AI助手和MCP兼容工具能够以编程方式管理防火墙策略、网络对象、路由和设备配置。
内置于 完全异步Python、持久HTTP连接池和安全第一默认值。
特性
设备管理
- 支持多设备并行管理
- API令牌和基本身份验证
- 连接测试和健康监测
- VDOM发现和每个VDOM操作
防火墙策略管理
- 防火墙策略的完整CRUD
- 包含地址/服务对象解析的策略详细信息
- VDOM范围的操作
网络对象管理
- 地址对象(子网、IP范围、FQDN)
- 服务对象(TCP/UDP/SCTP,带端口范围)
虚拟IP管理
- NAT/DNAT虚拟IP
- 端口转发配置
- 特定于协议的VIP规则
路由
- 静态路由CRUD操作
- 路由表检查
- 接口列表和状态监控
基础设施
- 具有的完全异步API客户端
httpx.AsyncClient连接池 - STDIO和HTTP传输模式
- 带验证的Pydantic配置模型
- 使用API调用跟踪的结构化日志记录
- 速率限制支持
快速开始
先决条件
- Python 3.11+
- 访问启用API的FortiGate设备
- API令牌(推荐)或管理员凭据
安装
git clone https://github.com/Aprazor/fortigate-mcp-server.git
cd fortigate-mcp-server
python -m venv .venv
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
pip install -e .配置
创建配置文件(例如。, config/config.json):
{
"fortigate": {
"devices": {
"fw-primary": {
"host": "192.168.1.1",
"port": 443,
"api_token": "your-api-token-here",
"vdom": "root",
"verify_ssl": true,
"timeout": 30
}
}
},
"server": {
"name": "fortigate-mcp-server",
"host": "0.0.0.0",
"port": 8814
},
"auth": {
"require_auth": false,
"allowed_origins": []
},
"logging": {
"level": "INFO",
"console": true
}
}运行服务器
STDIO模式 (用于直接MCP客户端集成):
export FORTIGATE_MCP_CONFIG=config/config.json
python -m src.fortigate_mcp.serverHTTP模式 (用于基于网络的访问):
python -m src.fortigate_mcp.server_http \
--host 0.0.0.0 \
--port 8814 \
--config config/config.jsonMCP客户端集成
克劳德桌面/克劳德代码 (~/.claude/mcp_servers.json):
{
"mcpServers": {
"fortigate": {
"command": "python",
"args": ["-m", "src.fortigate_mcp.server"],
"env": {
"FORTIGATE_MCP_CONFIG": "/path/to/config.json"
}
}
}
}光标IDE (~/.cursor/mcp_servers.json):
{
"mcpServers": {
"FortiGateMCP": {
"url": "http://localhost:8814/fortigate-mcp/",
"transport": "http"
}
}
}可用工具
设备管理(6个工具)
| 工具 | 说明 |
|---|---|
list_devices | 列出所有已注册的FortiGate设备 |
get_device_status | 获取设备的系统状态 |
test_device_connection | 测试与设备的连接 |
add_device | 注册新的FortiGate设备 |
remove_device | 删除已注册的设备 |
discover_vdoms | 在设备上发现虚拟域 |
防火墙策略管理(5个工具)
| 工具 | 说明 |
|---|---|
list_firewall_policies | 列出所有防火墙策略 |
create_firewall_policy | 创建新的防火墙策略 |
update_firewall_policy | 更新现有策略 |
get_firewall_policy_detail | 获取包含已解析对象的策略 |
delete_firewall_policy | 删除防火墙策略 |
网络对象管理(4个工具)
| 工具 | 说明 |
|---|---|
list_address_objects | 列出防火墙地址对象 |
create_address_object | 创建地址对象(子网/范围/FQDN) |
list_service_objects | 列出防火墙服务对象 |
create_service_object | 创建服务对象(TCP/UDP/SCTP) |
虚拟IP管理(5个工具)
| 工具 | 说明 |
|---|---|
list_virtual_ips | 列出虚拟IP配置 |
create_virtual_ip | 使用可选端口转发创建VIP |
update_virtual_ip | 更新虚拟IP配置 |
get_virtual_ip_detail | 获取详细的VIP信息 |
delete_virtual_ip | 删除虚拟IP |
路线管理(8个工具)
| 工具 | 说明 |
|---|---|
list_static_routes | 列出已配置的静态路由 |
create_static_route | 创建新的静态路由 |
update_static_route | 更新现有静态路由 |
delete_static_route | 删除静态路由 |
get_static_route_detail | 获取详细的路线信息 |
get_routing_table | 获取活动路由表 |
list_interfaces | 列出网络接口 |
get_interface_status | 获取接口运行状态 |
系统工具(2个工具)
| 工具 | 说明 |
|---|---|
health_check | 服务器运行状况和设备连接状态 |
get_server_info | 服务器版本和配置信息 |
建筑
fortigate-mcp-server/
├── src/fortigate_mcp/
│ ├── server.py # STDIO MCP server (FastMCP)
│ ├── server_http.py # HTTP MCP server (FastMCP)
│ ├── config/
│ │ ├── loader.py # Configuration file loading
│ │ └── models.py # Pydantic config models
│ ├── core/
│ │ ├── fortigate.py # Async API client + device manager
│ │ └── logging.py # Structured logging setup
│ ├── tools/
│ │ ├── base.py # Base tool class (error handling, formatting)
│ │ ├── definitions.py # Tool description constants
│ │ ├── device.py # Device management tools
│ │ ├── firewall.py # Firewall policy tools
│ │ ├── network.py # Address/service object tools
│ │ ├── routing.py # Routing and interface tools
│ │ └── virtual_ip.py # Virtual IP tools
│ └── formatting/
│ ├── formatters.py # MCP content formatters
│ └── templates.py # Response templates
└── tests/
├── conftest.py # Shared fixtures (AsyncMock)
├── test_config.py # Configuration model tests
├── test_device_manager.py # Device manager lifecycle tests
├── test_fortigate_api.py # Async API client tests
├── test_formatting.py # Response formatting tests
└── test_tools.py # Tool integration tests设计原则
- 完全异步:所有API调用都使用
httpx.AsyncClient每个设备都有持久连接池 - 默认安全:启用SSL验证,空CORS源,无通配符默认值
- 清洁分离:配置模型、API客户端、工具逻辑和格式是独立的层
- 错误分类:FortiGate API错误被映射到具有HTTP状态感知功能的用户友好消息
安全
此服务器的设计默认为安全第一:
| 设置 | 默认值 | 说明 |
|---|---|---|
verify_ssl | true | SSL证书验证已启用 |
allowed_origins | [] | 不允许有CORS源(明确选择加入) |
require_auth | false | MCP服务器身份验证(启用生产) |
生产建议:
- 使用 API令牌 而不是用户名/密码身份验证
- 保持
verify_ssl: true除非使用自签名证书进行测试 - 设置显式
allowed_origins使用HTTP传输时 - 启用
require_auth为MCP服务器本身配置API令牌 - 在受信任的网络上或反向代理后面运行服务器
- 将环境变量用于敏感配置值
测试
该项目包括117个测试,涵盖了整个异步堆栈:
# Run all tests
python -m pytest
# Run with verbose output
python -m pytest -v
# Run specific test module
python -m pytest tests/test_tools.py
# Run with coverage report
python -m pytest --cov=src --cov-report=html测试覆盖率
| 模块 | 覆盖范围 |
|---|---|
| 配置模型 | 安全默认值、验证、Pydantic模型 |
| API客户端 | 异步HTTP、连接池、错误处理 |
| 设备管理器 | 生命周期(添加/删除/列出),异步操作 |
| 工具类 | 所有CRUD操作、VDOM支持、错误路径 |
| 格式化 | 模板、内容渲染、边缘案例 |
故障排除
连接被拒绝
- 验证FortiGate设备是否可访问以及API是否已启用
- 检查端口(默认443)是否未被网络防火墙阻止
身份验证失败(401)
- 验证您的API令牌是否有效并具有适当的权限
- 对于基本身份验证,请确认用户名/密码是否正确
SSL证书错误
- 对于实验室环境中的自签名证书,请设置
verify_ssl: false - 对于生产,请在FortiGate设备上安装有效的证书
找不到VDOM
- 使用
discover_vdoms列出设备上可用的VDOM - 确保VDOM名称完全匹配(区分大小写)
贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/my-feature) - 为新功能编写测试
- 确保所有测试通过(
python -m pytest) - 提交您的更改(
git commit -m 'Add my feature') - 推到您的分支(
git push origin feature/my-feature) - 打开拉取请求
许可证
该项目根据MIT许可证获得许可。看 许可证 文件以获取详细信息。
致谢
- 模型上下文协议 -协议规范
- FastMCP -Python MCP服务器框架
- FortiGate REST API -FortiGate API文件
- httpx -异步HTTP客户端
