使用说明
Fledge MCP服务器
这是一个模型上下文协议(MCP)服务器,它将Fledge功能连接到Cursor AI,允许AI通过自然语言命令与Fledge实例交互。
先决条件
- Fledge安装在本地或可通过API访问(默认值:http://localhost:8081)
- 已安装Cursor AI
- Python 3.8+
安装
- 克隆此存储库:
git clone https://github.com/Krupalp525/fledge-mcp.git
cd fledge-mcp
- 安装依赖项:
pip install -r requirements.txt
运行服务器
- 确保Fledge正在运行:
fledge start
- 启动MCP服务器:
python mcp_server.py
对于使用API密钥身份验证的安全操作:
python secure_mcp_server.py
- 通过访问健康端点验证其是否正常工作:
curl http://localhost:8082/health
您应该收到“Fledge MCP服务器正在运行”作为响应。
连接到游标
-
在光标中,转到设置>MCP服务器
-
添加新服务器:
- 网址:http://localhost:8082/tools
- 工具文件:上传包含的Tools.json或指向其本地路径
-
对于安全服务器,使用安全服务器启动时生成的API_Key.txt文件中的值配置“X-API-Key”标头。
-
测试:打开Cursor的Composer(Ctrl+I),键入“Check if Fledge API is reachable”,AI应该调用
validate_api_connection工具。
可用工具
数据访问和管理
- 获取传感器数据:通过可选的时间范围和限制过滤从Fledge获取传感器数据
- list_sensors:列出Fledge中可用的所有传感器
- 摄入测试数据:将测试数据摄入Fledge,可选择批次计数
服务控制
- get_service_status:获取所有Fledge服务的状态
- start_stop_service:按类型启动或停止Fledge服务
- update_config:更新Fledge配置参数
前端代码生成
- generate_ui_组件:为Fledge数据可视化生成React组件
- fetch_sample_front:获取不同框架的前端模板示例
- 建议改进:获取AI支持的改进UI代码的建议
实时数据流
- subscribe_to_sensor:设置传感器数据更新的订阅
- get_latest_reading:获取特定传感器的最新读数
调试和验证
- validate_api_connection:检查Fledge API是否可访问
- 模拟前端请求:使用不同的方法和有效负载测试API请求
文档和架构
- get_api_schema:获取有关可用Fledge API端点的信息
- list_plugins:列出可用的Fledge插件
高级AI辅助功能
- generate_mock_data:生成真实的模拟传感器数据以供测试
测试API
您可以使用附带的测试脚本测试服务器:
# For standard server
python test_mcp.py
# For secure server with API key
python test_secure_mcp.py
安全选项
安全服务器(secure_mcp_server.py)添加了API密钥身份验证:
- 在第一次运行时,它生成一个存储在API_key.txt中的API密钥
- 所有请求都必须在X-API-key标头中包含此密钥
- 无需身份验证,健康检查端点仍可访问
API请求示例
# Validate API connection
curl -X POST -H "Content-Type: application/json" -d '{"name": "validate_api_connection"}' http://localhost:8082/tools
# Generate mock data
curl -X POST -H "Content-Type: application/json" -d '{"name": "generate_mock_data", "parameters": {"sensor_id": "temp1", "count": 5}}' http://localhost:8082/tools
# Generate React chart component
curl -X POST -H "Content-Type: application/json" -d '{"name": "generate_ui_component", "parameters": {"component_type": "chart", "sensor_id": "temp1"}}' http://localhost:8082/tools
# For secure server, add API key header
curl -X POST -H "Content-Type: application/json" -H "X-API-Key: YOUR_API_KEY" -d '{"name": "list_sensors"}' http://localhost:8082/tools
扩展服务器
要添加更多工具,请执行以下操作:
- 将工具定义添加到
tools.json - 在中实现工具处理程序
mcp_server.py和secure_mcp_server.py
生产注意事项
对于生产部署:
- 使用HTTPS
- 部署在Nginx等反向代理后面
- 实现更强大的身份验证(JWT、OAuth)
- 添加速率限制
- 为订阅设置持久数据存储
部署在Smithery.ai上
Fledge MCP服务器可以部署在Smithery.ai上,以增强可扩展性和可用性。按照以下步骤进行部署:
-
先决条件
- Docker已安装在本地计算机上
- Smithery.ai帐户
- 已安装Smithery CLI工具
-
构建和部署
# Build the Docker image docker build -t fledge-mcp . # Deploy to Smithery.ai smithery deploy -
配置 这
smithery.json文件包含部署的配置:- 端口8082上的WebSocket传输
- 可配置Fledge API URL
- 工具定义和参数
- 超时设置
-
环境变量 在Smithery.ai仪表板中设置以下环境变量:
FLEDGE_API_URL:您的Fledge API端点API_KEY:您的安全API密钥(如果使用安全模式)
-
验证 部署后,验证您的服务器是否正在运行:
smithery status fledge-mcp -
监控 通过Smithery.ai仪表板监控您的部署:
- 实时日志
- 性能指标
- 误差跟踪
- 资源使用
-
更新中 要更新部署,请执行以下操作:
# Build new image docker build -t fledge-mcp . # Deploy updates smithery deploy --update
JSON-RPC协议支持
服务器通过WebSocket使用JSON-RPC 2.0实现模型上下文协议(MCP)。支持以下方法:
-
初始化
{ "jsonrpc": "2.0", "method": "initialize", "params": {}, "id": "1" }答复:
{ "jsonrpc": "2.0", "result": { "serverInfo": { "name": "fledge-mcp", "version": "1.0.0", "description": "Fledge Model Context Protocol (MCP) Server", "vendor": "Fledge", "capabilities": { "tools": true, "streaming": true, "authentication": "api_key" } }, "configSchema": { "type": "object", "properties": { "fledge_api_url": { "type": "string", "description": "Fledge API URL", "default": "http://localhost:8081/fledge" } } } }, "id": "1" } -
工具/列表
{ "jsonrpc": "2.0", "method": "tools/list", "params": {}, "id": "2" }响应:返回可用工具及其参数的列表。
-
工具/调用
{ "jsonrpc": "2.0", "method": "tools/call", "params": { "name": "get_sensor_data", "parameters": { "sensor_id": "temp1", "limit": 10 } }, "id": "3" }
错误代码
服务器遵循标准JSON-RPC 2.0错误代码:
- -32700:分析错误
- -32600:无效请求
- -32601:未找到方法
- -32602:参数无效
- -32000:服务器错误