MCP(模型上下文协议)服务器通过可发现的接口为大语言模型(LLM)代理管道提供MQTT操作。该服务器支持使用通配符匹配的细粒度主题权限,并为MCP客户端提供全面的MQTT功能。
+ 和 #)的细粒度读写权限┌─────────────────┐ ┌──────────────────────────────────┐
│ MCP Client │ │ mcpMQTT Application │
│ │ │ │
│ ┌───────────┐ │ │ ┌─────────────┐ │
│ │ Agent │ │◄──►│ │ MCP Server │ │
│ └───────────┘ │ │ └─────────────┘ │
└─────────────────┘ │ │ │
│ ┌─────────────────────────────┐ │
┌─────────────────┐ │ │ Topic Permission Manager │ │
│ MQTT Broker │◄──►│ └─────────────────────────────┘ │
└─────────────────┘ │ │ │
│ ┌─────────────┐ │
│ │ MQTT Client │ │
│ │ Manager │ │
│ └─────────────┘ │
└──────────────────────────────────┘
pip install mcpMQTT
可以在 ~/.config/mcpmqtt/config.json 路径下创建配置文件,或者在命令行启动工具时使用 --config 参数指定自定义路径:
{
"mqtt": {
"host": "localhost",
"port": 1883,
"username": null,
"password": null,
"keepalive": 60
},
"topics": [
{
"pattern": "sensors/+/temperature",
"permissions": ["read"],
"description": "Temperature sensor data from any location (+ matches single level like 'room1', 'room2'. Known rooms are 'exampleroom1' and 'exampleroom2'). Use subscribe, not read on this topic. Never publish."
},
{
"pattern": "sensors/+/humidity",
"permissions": ["read"],
"description": "Humidity sensor data from any location. (+ matches single level like 'room1', 'room2'. Known rooms are 'exampleroom1' and 'exampleroom2'). Use subscribe, not read on this topic. Never publish. Data returned as %RH"
},
{
"pattern": "actuators/#",
"permissions": ["write"],
"description": "All actuator control topics (# matches multiple levels like 'lights/room1'. To enable a light you write any payload to 'lights/room1/on', to disable you write to 'lights/room1/off')"
},
{
"pattern": "status/system",
"permissions": ["read"],
"description": "System status information - exact topic match"
},
{
"pattern": "commands/+/request",
"permissions": ["write"],
"description": "Command request topics for request/response patterns"
},
{
"pattern": "commands/+/response",
"permissions": ["read"],
"description": "Command response topics for request/response patterns"
}
],
"logging": {
"level": "INFO",
"logfile": null
}
}
mqtt:MQTT代理连接设置topics:带有权限和描述的主题模式logging:应用程序日志级别通配符支持:
+:单级通配符(匹配一个主题级别)#:多级通配符(匹配多个级别,必须位于最后)权限:
read:可以订阅主题并接收消息write:可以向主题发布消息["read", "write"]示例:
sensors/+/temperature 匹配 sensors/room1/temperature、sensors/kitchen/temperatureactuators/# 匹配 actuators/lights、actuators/lights/room1/brightnessstatus/system 精确匹配 status/system使用安装脚本:
mcpMQTT
或者直接使用模块:
python -m mcpMQTT.app.mcp_server
使用自定义配置:
mcpMQTT --config /path/to/config.json --log-level DEBUG
# 或者
python -m mcpMQTT.app.mcp_server --config /path/to/config.json --log-level DEBUG
MCP服务器提供三种MQTT操作工具:
mqtt_publish向MQTT主题发布消息。
{
"topic": "sensors/room1/temperature",
"payload": "22.5",
"qos": 0
}
mqtt_subscribe订阅主题并收集消息。
{
"topic": "sensors/+/temperature",
"timeout": 30,
"max_messages": 5
}
mqtt_read订阅一个主题并等待单条消息。
{
"topic" : "sensors/+/temperature",
"timeout" : 5
}
mqtt_query用于MQTT通信的请求/响应模式。
{
"request_topic": "commands/room1/request",
"response_topic": "commands/room1/response",
"payload": "get_status",
"timeout": 5
}
mcpmqtt://topics/allowed获取带有权限和描述的允许主题模式。
mcpmqtt://topics/examples获取如何使用带有通配符的主题模式的示例。
mcpMQTT/
├── app/
│ ├── __init__.py
│ ├── mcp_server.py # MCP服务器实现和入口点
│ └── mqtt_client.py # 增强型MQTT客户端管理器
├── config/
│ ├── config_manager.py # 配置加载和验证
│ ├── schema.py # Pydantic模型和验证
│ ├── example_config.json # 示例配置文件
│ └── example_with_logging.json # 带有日志配置的示例
├── examples/
│ ├── example_config.json # 基本配置示例
│ └── example_with_logging.json # 启用文件日志的配置
├── pyproject.toml
├── LOGGING_USAGE.md # 详细的日志文档
└── README.md
有关详细的配置示例,请参阅 文件夹:
此MCP服务器使用 stdio 协议。这意味着它应该由你的LLM编排器启动。
典型的配置(mcp.json)可能如下所示:
{
"mcpServers": {
"mqtt": {
"command": "mcpMQTT",
"args": [
"--config /usr/local/etc/mcpMQTT.conf"
],
"env": {
"PYTHONPATH": "/just/an/example/path/"
},
"timeout": 300,
"alwaysAllow": [
"mqtt_read"
]
}
}
}
请记住,此MCP允许代理订阅和发布MQTT代理上与其关联的用户所暴露的所有主题。你必须在MQTT代理上进行细粒度配置,以限制MCP实际可以访问或操作的功能。
请参阅 LICENSE.md 文件。