MQTT代理交互服务

EMQX MCP Server是一个实现Model Context Protocol (MCP)的服务,提供与EMQX MQTT代理的交互功能,支持MQTT客户端管理、消息发布和主题订阅。

  • by Benniu
  • Python
  • 本地部署
  • 27 星标
  • 更新于
在 GitHub 查看 Benniu/emqx-mcp-server

使用说明

EMQX MCP服务器

A. 模型上下文协议(MCP) 提供EMQX MQTT代理交互的服务器实现。 使MCP客户端能够与上的MQTT集群交互 EMQX云 或自托管集群

特性

MQTT客户端管理

  • 客户端列表:使用灵活的筛选选项查看所有连接的MQTT客户端
  • 客户信息:检索特定客户的详细信息
  • 连接控制:断开有问题或过时的客户端与代理的连接
  • 灵活筛选:按节点、用户名、客户端ID、连接状态等筛选客户端

MQTT消息发布

  • 基于主题的发布:向任何MQTT主题发送消息
  • QoS控制:选择服务质量级别(0、1或2)以实现可靠交付
  • 消息保留:为新订阅者保留消息的选项
  • 自定义有效载荷:支持任何消息内容格式

MQTT主题订阅(SSE)

  • 实时订阅:通过服务器发送事件(SSE)订阅MQTT主题
  • 持续时间控制:配置监听消息的时间(1-300秒)
  • 邮件限制:设置每个订阅要收集的最大邮件数
  • 自动解析:消息从SSE事件流中自动解析

工具

list_mqtt_客户端

  • 列出连接到EMQX群集的MQTT客户端
  • 输入:
    • 页码(数字,可选):页码(默认值:1)
    • 限制(数量,可选):每页结果(默认值:100,最大10000)
    • node(字符串,可选):按特定节点名称筛选
    • clientid(字符串,可选):按特定客户端ID筛选
    • 用户名(字符串,可选):按特定用户名筛选
    • ip_address(字符串,可选):按客户端ip地址筛选
    • conn_state(字符串,可选):按连接状态筛选
    • clean_start(布尔值,可选):按clean start标志筛选
    • proto_ver(字符串,可选):按协议版本筛选
    • like_clientid(字符串,可选):按客户端ID模式进行模糊搜索
    • like_username(字符串,可选):按用户名模式进行模糊搜索
    • like_ip_address(字符串,可选):按ip地址模式进行模糊搜索

get_mqtt_client

  • 通过客户端ID获取特定MQTT客户端的详细信息
  • 输入:
    • clientid(string,必填):要检索的客户端的唯一标识符

kick_mqt_client

  • 通过客户端ID断开客户端与MQTT代理的连接
  • 输入:
    • clientid(string,必填):要断开连接的客户端的唯一标识符

publish_mqtt_message

  • 将MQTT消息发布到EMQX云或自我管理部署上的EMQX群集
  • 输入:
    • topic(字符串,必填):要发布到的MQTT主题
    • payload(字符串,必填):要发布的消息内容
    • qos(数字,可选):服务质量级别(0、1或2)(默认值:0)
    • retain(boolean,可选):是否保留消息(默认值:false)

subscribe_mqtt_topic

  • 通过EMQX Cloud上的SSE订阅MQTT主题,并在指定的持续时间内收集消息
  • 输入:
    • topic(字符串,必填):要订阅的MQTT主题
    • 持续时间(数字,可选):以秒为单位收听多长时间(1-300,默认值:30)
    • max_messages(number,可选):要收集的最大消息数(1-1000,默认值:100)
  • 返回:包含主题、message_count和messages数组的对象

设置EMQX集群

在使用EMQX MCP服务器工具之前,您需要使用正确配置的API密钥和客户端身份验证来设置EMQX集群。有几个选项:

  1. EMQX云无服务器部署:
  • 最简单的开始方式。
  • 从EMQX Cloud获得免费的无服务器部署
  • 注册地址: EMQX云无服务器
  1. EMQX云专用部署:
  • 为生产工作负载提供专用资源
  • 提供增强的性能、可靠性和定制选项
  • 支持各种云提供商(AWS、GCP、Azure)
  • 包括专业SLA和支持
  • 在以下位置创建部署 EMQX云专用
  1. 自托管EMQX平台:
  • 在本地下载并部署EMQX平台
  • 请遵循以下安装说明 EMQX平台

注: 这 subscribe_mqtt_topic 该工具使用SSE(服务器发送事件)端点,该端点在EMQX Cloud部署上可用。并非所有自托管EMQX部署都支持SSE订阅端点。

使用Claude桌面应用程序在本地运行

选项1:通过Smithery安装

通过以下方式自动为Claude Desktop安装emqx mcp服务器 铁匠铺:

npx -y @smithery/cli install @Benniu/emqx-mcp-server --client claude

选项2:Docker

  1. 安装 Claude桌面应用程序 如果你还没有这样做。

  2. 拉取图像:

    docker pull benniuji/emqx-mcp-server
    
  3. 将以下内容添加到您的 claude_desktop_config.json 文件:

    • 在MacOS上: ~/Library/Application\ Support/Claude/claude_desktop_config.json
    • 在Windows上: %APPDATA%/Claude/claude_desktop_config.json
    {
      "mcpServers": {
        "EMQX_MCP_Server": {
          "command": "docker",
          "args": [
            "run",
            "-i",
            "--rm",
            "-e", "EMQX_API_URL=https://your-emqx-cloud-instance.com:8443/api/v5",
            "-e", "EMQX_API_KEY=<YOUR-API-KEY>",
            "-e", "EMQX_API_SECRET=<YOUR-API-SECRET>",
            "benniuji/emqx-mcp-server"
          ]
        }
      }
    }
    

    注意:更新env变量:EMQX_API_URL, EMQX_API_KEY, EMQX_API_SECRET

  4. 打开或重新启动Claude桌面应用程序

  5. 尝试让Claude对MQTT代理执行一些操作(例如,让它向主题发布MQTT消息)。

选项3:手动安装

首先,确保你有 uv 可执行文件已安装。如果没有,您可以按照说明进行安装 这里.

  1. 安装 Claude桌面应用程序 如果你还没有这样做。
  2. 克隆此存储库。
  3. 将以下内容添加到您的 claude_desktop_config.json 文件:
    • 在MacOS上: ~/Library/Application\ Support/Claude/claude_desktop_config.json
    • 在Windows上: %APPDATA%/Claude/claude_desktop_config.json
    {
      "mcpServers": {
        "EMQX_MCP_Server": {
          "command": "uv",
          "args": [
            "--directory",
            "path/to/repo/src/emqx_mcp_server",
            "run",
            "emqx-mcp-server"
          ],
          "env":{
            "EMQX_API_URL":"https://your-emqx-cloud-instance.com:8443/api/v5",
            "EMQX_API_KEY":"<YOUR-API-KEY>",
            "EMQX_API_SECRET":"<YOUR-API-SECRET>"
          }
        }
      }
    }
    
    注意:请确保更新 path/to/repo/src/emqx_mcp_server 到实际路径并修改环境变量 EMQX_API_URL, EMQX_API_KEY,以及 EMQX_API_SECRET 以匹配您的EMQX实例。
  4. 打开或重新启动Claude桌面应用程序
  5. 尝试让Claude对MQTT代理执行一些操作(例如,让它向主题发布MQTT消息)。

示例提示

用户:

Please list the clients connected to the MQTT broker.

LLM

Here are the clients currently connected to your MQTT broker:
Connected Client:
  - Client ID: mqttx_2232bdb1
  - Username: test
  - Protocol: MQTT v5
  ...
There is currently 1 client connected to your MQTT broker. Would you like more detailed information about this specific client or would you like to perform another action with your MQTT broker?

用户:

Yes, please provide the detail info of this client. 

LLM

I'll retrieve the detailed information for this client for you.
...
Here's the detailed information for client "mqttx_2232bdb1":

**Basic Information:**
-   **Client ID:** mqttx_2232bdb1
-   **Username:** test
...
**Connection Details:**
-   **Connected:** Yes
-   **Created At:** 2025-03-02T05:29:51.103+00:00
...
Would you like to perform any actions with this client, such as publishing a message to a topic or disconnecting the client?

用户:

Please write a poem and send it to the topic '/poem' with QoS 1.

LLM

I'll create a poem and publish it to the topic '/poem' with QoS 1. Let me do that for you.
...
I've successfully published the poem to the topic '/poem' with QoS 1. The message has been delivered with ID: 00062F55A1EFB1156ABC481240B30000.

The poem I sent was:

- Signals in the ether,
- Invisible threads that bind,
- Devices speak in whispers,
- Across the digital divide.
- Messages like leaves falling,
- From cloud to waiting ground,
- Connected yet untethered,
- Where silence makes no sound.
- Brokers of the modern age,
- Routing dreams from here to there,
- In this web of connections,
- We find ourselves everywhere.
- Like stars in constellation,
- Each point of light has worth,
- The language of machines,
- Redefining our Earth.

Is there anything else you'd like to do with your MQTT broker?

用户:

Please subscribe to the topic 'sensor/temperature' for 10 seconds and show me the messages.

LLM

I'll subscribe to the topic 'sensor/temperature' for 10 seconds to collect messages.
...
I received 3 messages from topic 'sensor/temperature' during the 10-second subscription:

1. {"temperature": 23.5, "unit": "celsius", "timestamp": "2025-03-02T10:00:01Z"}
2. {"temperature": 23.6, "unit": "celsius", "timestamp": "2025-03-02T10:00:04Z"}
3. {"temperature": 23.4, "unit": "celsius", "timestamp": "2025-03-02T10:00:07Z"}

Would you like to subscribe for a longer duration or take any other action?

用户:

Please kick out the client mqttx_2232bdb1

LLM

I'll disconnect the client from the MQTT broker for you.
...
The client has been successfully disconnected. The system returned:
> Client mqttx_2232bdb1 has been disconnected
The MQTT client with ID "mqttx_2232bdb1" is now disconnected from your broker. Is there anything else you would like to do with your MQTT broker?

许可证

此项目根据Apache许可证版本2.0获得许可-请参阅 许可证 文件以获取详细信息。