mqtt-mcp

mqtt-mcp

MCP server that bridges MQTT sensor data to MCP, allowing clients to query cached sensor readings and send commands via tools like list_topics, read_topic, and publish.

Category
访问服务器

README

mqtt-mcp

An MQTT to MCP bridge. Devices publish sensor readings to a Mosquitto broker; this server subscribes in the background, caches the last message per topic, and exposes that cache as MCP tools over streamable HTTP. Any MCP client (n8n's MCP Client Tool, Claude Code, the MCP Inspector) can then ask questions like "what is the CO2 in the office?" or send commands back to devices.

ESP32s --publish--> Mosquitto <--subscribe/publish-- mqtt-mcp (this server)
                                                          ^ streamable HTTP :8000/mcp
                                             n8n AI Agent / Claude Code

Tools

Tool What it does
list_topics(prefix="") Topics seen since start, with last payload, age, and count
read_topic(topic) Last message on an exact topic
publish(topic, payload) Send a command; restricted to topics under cmd/
bridge_status() Broker connection state, for telling outages apart from silent devices

The cache is in memory only. After a restart it refills as devices publish (retained messages arrive immediately on reconnect).

Topic conventions

  • sensors/aircube/<metric> for the aircube readings (temperature, co2, humidity)
  • sensors/espcam/<metric> reserved for the FireBeetle + ESP32-CAM vision project
  • cmd/<device>/<action> for commands; only this prefix is writable through MCP

Server setup (Docker host)

  1. Clone, then create broker credentials (writes mosquitto/config/passwd):

    docker compose run --rm mosquitto mosquitto_passwd -c -b /mosquitto/config/passwd mqtt <password>
    
  2. Copy .env.example to .env and fill in the same username and password.

  3. docker compose up -d --build

Mosquitto listens on 1883 (devices connect there), the MCP endpoint is http://<host>:8000/mcp. Neither has TLS and the MCP endpoint has no auth, so keep both LAN-only.

If 8000 or 1883 is already taken on the host, set MCP_HOST_PORT and/or MQTT_HOST_PORT in .env (e.g. MCP_HOST_PORT=8090, MQTT_HOST_PORT=1884) instead of editing docker-compose.yml. The containers still listen on 8000 and 1883 internally; only the LAN-facing published ports change, so devices then connect to the broker at <host>:<MQTT_HOST_PORT>.

If ufw is enabled on the host, open both ports (use the published ports you actually set):

sudo ufw allow 1883/tcp comment 'mosquitto mqtt'
sudo ufw allow 8000/tcp comment 'mqtt-mcp streamable http'
sudo ufw status

Local development (Mac)

MQTT_HOST=<broker-ip> MQTT_USERNAME=mqtt MQTT_PASSWORD=<password> uv run server.py

In a second terminal, generate traffic:

MQTT_HOST=<broker-ip> MQTT_USERNAME=mqtt MQTT_PASSWORD=<password> uv run scripts/fake_sensor.py

Then poke at the tools with the MCP Inspector:

npx @modelcontextprotocol/inspector
# transport: Streamable HTTP, URL: http://localhost:8000/mcp

Connecting from n8n

Add an AI Agent node and attach an MCP Client Tool pointed at http://<host>:8000/mcp (or your published MCP_HOST_PORT).

Set the transport to "HTTP Streamable". This server speaks streamable HTTP only, not SSE. If the node is left on the SSE transport (the default in some versions), the connection fails with SSE error: Non-200 status code (404) because there is no /sse endpoint. Switching the transport to HTTP Streamable fixes it. Verified on n8n 2.28.6.

Note that n8n and this bridge usually run as separate Docker stacks on the same host, so n8n reaches the bridge over the host, not by container name: use the host's LAN IP (e.g. http://192.168.1.50:8075/mcp), not http://mqtt-mcp:8000.

A ready-to-import example workflow (Chat Trigger, AI Agent, Ollama model, MCP client) is in examples/n8n-aircube-agent.json. Import it, then set two things: the MCP endpoint URL (your host LAN IP + port) and the Ollama model — which must support tool calling (e.g. llama3.1, qwen2.5), or the agent will chat without ever invoking the tools.

Configuration

All via environment variables, read in server.py:

Variable Default Purpose
MQTT_HOST localhost Broker hostname
MQTT_PORT 1883 Broker port
MQTT_USERNAME / MQTT_PASSWORD unset Broker credentials
CMD_PREFIX cmd/ Only prefix the publish tool may write to
MCP_HOST 0.0.0.0 MCP listen address
MCP_PORT 8000 MCP listen port

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选