Back to MCP directory
publicPublicdnsLocal runtime

mqttx-mcp-sse-server

一个基于SSE传输的MQTTX服务实现,通过MCP协议提供MQTT代理连接功能,支持订阅、发布等操作。

article

README

🚀 MQTTX SSE 服务器

MQTTX SSE 服务器实现了 Model-Context Protocol (MCP),借助 Server-Sent Events (SSE) 传输层,可在 MQTT 上执行各类操作,为 AI 助手与外部工具和服务的交互提供了强大支持。

✨ 主要特性

  • 实现 MCP 协议版本 2024 - 11 - 05,紧跟协议标准。
  • 采用 SSE(Server - Sent Events)作为传输层,保障实时消息传递。
  • 支持通过 MCP 工具进行 MQTT 操作,包括连接到 MQTT 代理、订阅 MQTT 主题以及发布消息到 MQTT 主题。
  • 能够实时传递来自订阅主题的消息。
  • 具备多客户端会话管理能力,高效处理多个客户端的连接。

🚀 快速开始

先决条件

  • Node.js(v14 或更高版本)
  • npm

📦 安装指南

# 克隆仓库
git clone https://github.com/yourusername/mqttx-sse-server.git
cd mqttx-sse-server

# 安装依赖项
npm install

运行服务器

npm start

服务器将在默认端口 4000 上运行。

💻 使用示例

配置 MQTTX

要将此 MCP 服务器与 MQTTX 一起使用,请在您的 MQTTX 设置中添加以下配置:

{
  "mcpServers": {
    "mqttx-server": {
      "url": "http://localhost:4000/mqttx/sse"
    }
  }
}

📚 详细文档

MCP 协议实现

此服务器实现了 Model - Context Protocol,包含以下组件:

  • SSE 连接:建立持久连接以接收实时更新。
  • JSON - RPC API:处理工具调用和响应,符合 MCP 规范。
  • 工具接口:通过标准化的 MCP 工具提供 MQTT 功能。
  • 会话管理:跟踪客户端会话及其 MQTT 连接。

📄 API 文档

SSE 连接

建立一个用于接收服务器事件的持久连接。

GET /mqttx/sse

响应事件:

  • endpoint:包含进行 JSON - RPC 调用的 URL。
  • heartbeat:定期心跳以保持连接 alive。
  • message:包含 JSON - RPC 响应。

JSON - RPC 命令

所有命令均发送到消息端点,使用您的会话 ID:

POST /mqttx/message?sessionId=xxx
初始化
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize"
}
列出工具
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/list"
}
MQTT 连接
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "mqttConnect",
    "arguments": {
      "host": "broker.example.com",
      "port": 1883,
      "clientId": "mqttx-client"
    }
  }
}
MQTT 订阅
{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "tools/call",
  "params": {
      "name": "mqttSubscribe",
      "arguments": {
        "topic": "test/topic",
        "qos": 0
      }
  }
}
MQTT 发布
{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "tools/call",
  "params": {
      "name": "mqttPublish",
      "arguments": {
        "topic": "test/topic",
        "payload": "Hello MQTT!",
        "qos": 0,
        "retain": false
      }
  }
}
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client