MQTT-Bridge
Provides MQTT communication capabilities for Large Language Models and other clients, enabling connections to MQTT brokers, publishing and subscribing to topics, and managing real-time messaging workflows.
README
MQTT-Bridge
MQTT-Bridge provides MQTT communication capabilities for Large Language Models and other clients. It enables connections to MQTT brokers, publishing and subscribing to topics, and managing real-time messaging workflows.
Features
- MQTT Broker Connection: Connect to any MQTT broker with authentication support
- Message Publishing: Publish messages to topics with configurable QoS and retain settings
- Topic Subscription: Subscribe to topics with wildcard support (+ and #)
- Connection Management: Manage multiple concurrent MQTT connections
- Message Monitoring: Track and analyze received messages
- Real-time Updates: Live message reception and connection status monitoring
- Subscription Persistence: Maintain subscriptions across server restarts
Components
Resources
MQTT-Bridge provides these resources:
- Connection Status: Real-time status and details for each MQTT connection
- Message History: Recent MQTT messages with filtering capabilities
Prompts
MQTT-Bridge offers intelligent prompts for:
- mqtt-connection-status: Get formatted status of all MQTT connections
- mqtt-message-analysis: Analyze recent MQTT messages with topic filtering
Tools
MQTT-Bridge implements these MQTT tools:
-
mqtt-connect: Connect to an MQTT broker
- Supports authentication (username/password)
- Configurable client ID, keep-alive, and port settings
- Automatically restores saved subscriptions
-
mqtt-disconnect: Disconnect from an MQTT broker
-
mqtt-publish: Publish messages to topics
- Configurable QoS levels (0, 1, 2)
- Retain message support
-
mqtt-subscribe: Subscribe to topics
- Wildcard support (+ for single level, # for multi-level)
- Configurable QoS levels
- Subscriptions are persisted automatically
-
mqtt-unsubscribe: Unsubscribe from topics
- Automatically updates persistence storage
-
mqtt-list-connections: List all connections and their status
-
mqtt-get-messages: Retrieve recent messages with optional filtering
-
mqtt-get-persistent-subscriptions: Retrieve persisted subscription information
- View all subscriptions across connections
- Filter by connection ID
- Shows topic and QoS information
-
mqtt-delete-subscription: Delete specific subscriptions from persistence
- Delete from specific connection or all connections
- Updates runtime tracking for active connections
-
mqtt-delete-all-subscriptions: Delete all persistent subscriptions
- Nuclear option with confirmation required
- Completely clears subscription persistence
Installation
Prerequisites
- Python 3.12 or higher
- uv package manager (recommended) or pip
Setup
- Clone or download this repository
- Install dependencies:
uv sync --dev --all-extras
Alternatively with pip:
pip install -e .
Configuration
Claude Desktop
Add this configuration to your Claude Desktop config file:
MacOS: ~/Library/Application\ Support/Claude/claude_desktop_config.json
Windows: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"mqtt-bridge": {
"command": "python",
"args": ["-m", "mqtt_bridge.server"]
}
}
}
Project Structure
src/
mqtt_bridge/
__init__.py
server.py # Main server implementation
subscription_persistence.py # Subscription persistence implementation
.github/
copilot-instructions.md # GitHub Copilot workspace instructions
.vscode/
mcp.json # VS Code MCP configuration
tasks.json # Build and run tasks
Subscription Persistence
The server implements subscription persistence to maintain MQTT subscriptions across server restarts:
Features
- Automatically saves subscriptions to disk when they are created or removed
- Restores subscriptions when connections are re-established
- Preserves QoS levels for each subscription
- Gracefully handles shutdown to ensure latest subscription state is saved
Configuration
Subscription persistence can be configured via environment variables:
| Variable | Description | Default |
|---|---|---|
MQTT_BRIDGE_PERSISTENCE_DIR |
Directory for persistence files | ~/.mqtt-bridge |
MQTT_BRIDGE_SUBSCRIPTIONS_FILE |
Filename for subscriptions | subscriptions.json |
Storage Format
Subscriptions are stored in a JSON file with the following structure:
{
"connection_id1": [
{"topic": "topic1", "qos": 0},
{"topic": "topic2", "qos": 1}
],
"connection_id2": [
{"topic": "topic3", "qos": 2}
]
}
Testing
The scripts/test_subscription_persistence.py script can be used to test the subscription persistence functionality.
Subscription Management
The server provides advanced subscription management capabilities:
Viewing Subscriptions
Use the mqtt-get-persistent-subscriptions tool to view all persistent subscriptions:
Show me all persistent MQTT subscriptions
Filter by connection ID:
Show the persistent subscriptions for connection "my-connection"
Deleting Subscriptions
Delete a specific subscription from all connections:
Delete subscription for topic "sensors/temperature"
Delete a subscription from a specific connection:
Delete the "sensors/temperature" subscription from connection "my-connection"
Nuclear Option
Delete all persistent subscriptions (requires confirmation):
Delete all persistent subscriptions
pyproject.toml # Project configuration README.md # This file
### Running the Server
For development:
```bash
uv run python -m mqtt_bridge.server
For production:
python -m mqtt_bridge.server
Testing MQTT Functionality
You can test the server with public MQTT brokers:
- test.mosquitto.org: Public test broker (port 1883)
- broker.emqx.io: Public test broker (port 1883)
- broker.hivemq.com: Public test broker (port 1883)
Debugging
The server logs to stderr, which you can monitor for debugging:
- Connection events
- Message reception
- Error conditions
Security Considerations
- Use secure MQTT brokers (TLS) in production
- Implement proper authentication credentials
- Consider message encryption for sensitive data
- Monitor and limit message rate for public brokers
License
MIT two-clause license applies to this project.
Contributing
- Follow the MCP server development guidelines
- Ensure all MQTT operations handle errors gracefully
- Add tests for new functionality
- Update documentation for new features
Support
For issues and questions:
- Check the MCP documentation: https://modelcontextprotocol.io/
- Review MQTT protocol specifications: https://mqtt.org/
- Test with public MQTT brokers before reporting connectivity issues
Development
Building and Publishing
To prepare the package for distribution:
- Sync dependencies and update lockfile:
uv sync
- Build package distributions:
uv build
This will create source and wheel distributions in the dist/ directory.
Debugging
Since MCP servers run over stdio, debugging can be challenging. For the best debugging experience, we recommend using the MCP Inspector.
Upon launching, the Inspector will display a URL that you can access in your browser to begin debugging.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。