Skip to main content

MQTT Model Context Protocol Server for AI-powered smart home control

Project description

MQTT MCP Server

A Model Context Protocol (MCP) server that enables AI assistants to discover, monitor, and control smart home devices through MQTT.

Features

  • 🔍 Topic Discovery: Scan and search MQTT topics with keyword filtering
  • 📊 Value Reading: Read current values from specific topics with caching
  • 📤 Publishing: Send commands to MQTT devices with validation
  • 📹 Event Recording: Monitor MQTT traffic in real-time

Installation

# Install from source
pip install -e .

# Or install dependencies directly
pip install mcp aiomqtt pydantic

Configuration

Configure the MQTT connection using environment variables:

export MQTT_HOST="localhost"      # Default: localhost
export MQTT_PORT="1883"           # Default: 1883
export MQTT_USERNAME="user"       # Optional
export MQTT_PASSWORD="pass"       # Optional

MCP Client Configuration

Claude Desktop

Add to your Claude Desktop configuration:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "mqtt": {
      "command": "python",
      "args": ["-m", "mqtt_mcp.server"],
      "env": {
        "MQTT_HOST": "10.0.20.104",
        "MQTT_PORT": "1883",
        "MQTT_USERNAME": "mqtt",
        "MQTT_PASSWORD": "mqtt"
      }
    }
  }
}

Claude Code

Add to your project settings:

{
  "mcp": {
    "mqtt": {
      "command": "python",
      "args": ["-m", "mqtt_mcp.server"],
      "env": {
        "MQTT_HOST": "localhost",
        "MQTT_PORT": "1883"
      }
    }
  }
}

Cursor / Other MCP Clients

Use the standard MCP configuration format with stdio transport:

{
  "mqtt-server": {
    "transport": "stdio",
    "command": "mqtt-mcp-server",
    "env": {
      "MQTT_HOST": "your-broker-host",
      "MQTT_PORT": "1883"
    }
  }
}

Tools

1. topics - Discover MQTT Topics

Scan the MQTT broker to discover active topics.

Parameters:

  • scan_timeout (int, 1-60): How long to scan for topics (default: 10)
  • keywords (list): Filter topics by keywords (OR logic)
  • limit (int, 1-200): Maximum results to return (default: 50)
  • offset (int): Pagination offset (default: 0)

Example:

{
  "tool": "topics",
  "arguments": {
    "scan_timeout": 15,
    "keywords": ["temperature", "sensor"],
    "limit": 20
  }
}

2. value - Read Topic Values

Read current values from specific MQTT topics with caching support.

Parameters:

  • topics (list, required): Topic paths to read
  • timeout (int, 1-60): Wait time per topic (default: 5)

Example:

{
  "tool": "value",
  "arguments": {
    "topics": ["sensor/temperature", "sensor/humidity"],
    "timeout": 10
  }
}

3. publish - Send Messages

Publish messages to MQTT topics with validation.

Parameters:

  • messages (list, required): Messages to publish
    • topic (string, required): Topic to publish to
    • payload (any, required): Message payload
    • qos (int, 0-2): Quality of Service (default: 1)
    • retain (bool): Retain message (default: false)
  • timeout (int, 1-30): Network timeout (default: 3)

Example:

{
  "tool": "publish",
  "arguments": {
    "messages": [
      {
        "topic": "device/light/set",
        "payload": {"state": "ON", "brightness": 80},
        "qos": 1,
        "retain": false
      }
    ]
  }
}

4. record - Monitor Events

Record MQTT events in real-time for analysis.

Parameters:

  • timeout (int, 1-300): Recording duration (default: 30)
  • topics (list): Specific topics to monitor
  • keywords (list): Filter by keywords (OR logic)

Example:

{
  "tool": "record",
  "arguments": {
    "timeout": 60,
    "keywords": ["motion", "door"]
  }
}

Cache Management

The server maintains a cache of discovered topics and their last values in ~/.mqtt-mcp-cache.json. This cache:

  • Speeds up topic value reading
  • Preserves discovered topics between sessions
  • Updates automatically during operations

Error Handling

All tools provide detailed error messages with suggestions:

  • Connection errors include broker details
  • Timeout errors suggest using topic discovery
  • Validation errors list specific issues

Logging

All logs are sent to stderr to maintain clean JSON-RPC communication on stdout. Monitor logs with:

mqtt-mcp-server 2>mqtt.log

Development

# Clone repository
git clone https://github.com/yourusername/mqtt-mcp-server.git
cd mqtt-mcp-server

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or venv\Scripts\activate on Windows

# Install in development mode
pip install -e .

# Run tests
python tests/test_topics.py
python tests/test_value.py
python tests/test_publish.py
python tests/test_record.py

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mqtt_mcp_server-1.0.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mqtt_mcp_server-1.0.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file mqtt_mcp_server-1.0.0.tar.gz.

File metadata

  • Download URL: mqtt_mcp_server-1.0.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for mqtt_mcp_server-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ed734c42eff9c8f9b66ac416645c0c66b0102c7ecde4b5cd724b03e33c430db2
MD5 b0c2707a7dc089303476fdcf3e8d1954
BLAKE2b-256 15eff300e0efef72b511685415e3a36d667bd135dc5758b4baa10bf2762d502e

See more details on using hashes here.

File details

Details for the file mqtt_mcp_server-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mqtt_mcp_server-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a3e268ab1826f5b92d5423e29534b026708b56cd665e5f778704608ab112d04
MD5 499d5c9bee7fba72d1de2e9d757c5442
BLAKE2b-256 f66be27d7778dc545f0d2318786c7b7180852e3323b6abe92b5b7e31cc637ede

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page