mcmqtt 2025.9.17.1
FastMCP MQTT Server - Enabling MQTT integration for MCP clients
License: MIT
Requires Python: >=3.11

# 🚀 mcmqtt - FastMCP MQTT Server

**The most powerful FastMCP MQTT integration server on the planet** 🌍

[![Version](https://img.shields.io/badge/version-2025.09.17-blue.svg)](https://pypi.org/project/mcmqtt/)
[![Python](https://img.shields.io/badge/python-3.11+-green.svg)](https://python.org)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Tests](https://img.shields.io/badge/tests-70%20passing-brightgreen.svg)](#testing)
[![Coverage](https://img.shields.io/badge/coverage-96%25+-brightgreen.svg)](#coverage)

> **Enabling MQTT integration for MCP clients with embedded broker support and fractal agent orchestration**

## ✨ Key Features

- 🔥 **FastMCP Integration**: Native Model Context Protocol server with MQTT tools
- ⚡ **Embedded MQTT Brokers**: Spawn brokers on-demand with zero configuration
- 🏗️ **Modular Architecture**: Clean, testable, maintainable codebase
- 🧪 **Comprehensive Testing**: 70+ tests with 96%+ coverage on core modules
- 🌐 **Cross-Platform**: Designed for Linux, macOS, and Windows (TODO: Test on additional platforms)
- 🔧 **CLI & Programmatic**: Use via command line or integrate into your code
- 📡 **Real-time Coordination**: Perfect for agent swarms and distributed systems

## 🚀 Quick Start

### Installation

**Recommended: Use `uvx` for instant execution** (no installation needed):

```bash
# Run directly with uvx (recommended)
uvx mcmqtt --help

# Start STDIO server for MCP clients
uvx mcmqtt

# HTTP mode for web integration  
uvx mcmqtt --transport http --port 8080
```

**If you insist on traditional installation**:

```bash
# Install with uv
uv add mcmqtt

# Or use pip
pip install mcmqtt
```

### Instant MQTT Magic

```bash
# Start FastMCP MQTT server (default STDIO mode) - Just works!
uvx mcmqtt

# HTTP mode for web integration
uvx mcmqtt --transport http --port 8080

# Connect to existing broker (optional)
uvx mcmqtt --mqtt-host mqtt.example.com --mqtt-port 1883
```

### MCP Integration

Add to your Claude Code MCP configuration:

```bash
# Add mcmqtt as an MCP server (zero configuration!)
claude mcp add task-buzz -- uvx mcmqtt

# Test the connection
claude mcp test task-buzz
```

## ⚙️ Configuration Options

**Primary Interface: MCP Tools** - mcmqtt is designed for MCP clients to manage connections and brokers dynamically via tool calls.

### Optional CLI & Environment Configuration

For startup configuration (when MCP clients need default connectivity):

**CLI Options:**
```bash
uvx mcmqtt --transport stdio          # Default: STDIO mode for MCP
uvx mcmqtt --transport http           # HTTP mode for web integration
uvx mcmqtt --mqtt-host broker.local   # Connect to existing broker
uvx mcmqtt --auto-connect             # Auto-connect on startup
```

**Environment Variables:**
```bash
export MQTT_BROKER_HOST="mqtt.example.com"
export MQTT_BROKER_PORT="1883"
export MQTT_CLIENT_ID="mcmqtt-server"
export MQTT_USERNAME="user"
export MQTT_PASSWORD="pass"
uvx mcmqtt  # Uses environment config
```

**MCP clients control everything else** - broker spawning, connections, subscriptions, and message handling via tool calls.

## 🛠️ Core Features

### 🏃‍♂️ FastMCP MQTT Tools

- `mqtt_connect` - Connect to MQTT brokers
- `mqtt_publish` - Publish messages with QoS support  
- `mqtt_subscribe` - Subscribe to topics with wildcards
- `mqtt_get_messages` - Retrieve received messages
- `mqtt_status` - Get connection and statistics
- `mqtt_spawn_broker` - Create embedded brokers instantly
- `mqtt_list_brokers` - Manage multiple brokers

### 🔧 Embedded Broker Management

**MCP clients can spawn MQTT brokers on-demand using the `mqtt_spawn_broker` tool:**

```bash
# MCP Tool Call Example
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "agent-coordination-broker",
    "port": 1883,
    "host": "127.0.0.1",
    "max_connections": 100,
    "websocket_port": 9001
  }
}
```

**Response:**
```json
{
  "broker_id": "agent-coordination-broker-1726567890",
  "host": "127.0.0.1",
  "port": 1883,
  "websocket_port": 9001,
  "status": "running",
  "mqtt_url": "mqtt://127.0.0.1:1883",
  "websocket_url": "ws://127.0.0.1:9001"
}
```

**Instant broker spawning** - no Docker, no setup, just call the tool and get a running MQTT broker!

### 📡 MQTT Client Integration

**Connect to any MQTT broker using MCP tools:**

```bash
# Connect to broker
{
  "tool": "mqtt_connect",
  "arguments": {
    "broker_host": "mqtt.example.com",
    "broker_port": 1883,
    "client_id": "my-agent",
    "username": "user",
    "password": "pass"
  }
}

# Publish messages 
{
  "tool": "mqtt_publish",
  "arguments": {
    "topic": "sensors/temperature",
    "payload": "23.5",
    "qos": 1,
    "retain": false
  }
}

# Subscribe to topics
{
  "tool": "mqtt_subscribe",
  "arguments": {
    "topic": "sensors/+",
    "qos": 1
  }
}

# Get messages
{
  "tool": "mqtt_get_messages",
  "arguments": {
    "topic": "sensors/temperature",
    "limit": 10
  }
}
```

**Zero coding required** - just call MCP tools and coordinate with any MQTT infrastructure!

## 🏗️ Architecture Excellence

This isn't your typical monolithic MQTT library. mcmqtt features a **clean modular architecture**:

```
mcmqtt/
├── cli/           # Command-line interface & argument parsing
├── config/        # Environment & configuration management  
├── logging/       # Structured logging setup
├── server/        # STDIO & HTTP server runners
├── mqtt/          # Core MQTT client functionality
├── mcp/           # FastMCP server integration
├── broker/        # Embedded broker management
└── middleware/    # Broker middleware & orchestration
```

### 🧪 Testing Excellence

- **70+ comprehensive tests** covering all modules
- **96%+ code coverage** on refactored components
- **Robust mocking** for reliable CI/CD
- **Edge case coverage** for production reliability

## 🌟 Real-World Use Cases

### 🤖 AI Agent Orchestration & Swarm Coordination

**Fractal Agent Architecture**: Build sophisticated agent swarms where parent agents delegate tasks to specialized subagents via MQTT messaging.

```bash
# 1. Setup mcmqtt as MCP server for agent coordination
claude mcp add task-buzz -- uvx mcmqtt

# 2. Now parent Claude Code agents can coordinate with subagents
```

**Example: Multi-Agent Code Analysis Workflow**

```bash
# Parent Agent (you) delegates analysis tasks via MQTT:

# 1. Spawn embedded broker for coordination
mqtt_spawn_broker --name "code-analysis" --port 1883

# 2. Create specialized subagents for different domains  
claude -p "You are a security-analysis-agent. Subscribe to 'tasks/security' topic and analyze code for vulnerabilities. Use mqtt_subscribe and mqtt_publish tools."

claude -p "You are a performance-agent. Subscribe to 'tasks/performance' and optimize code. Report results to 'results/performance'."

claude -p "You are a test-agent. Subscribe to 'tasks/testing' and write comprehensive tests. Publish coverage to 'results/testing'."

# 3. Parent agent coordinates the workflow
mqtt_publish --topic "tasks/security" --payload '{"file": "auth.py", "priority": "high"}'
mqtt_publish --topic "tasks/performance" --payload '{"file": "database.py", "focus": "query_optimization"}'  
mqtt_publish --topic "tasks/testing" --payload '{"file": "api.py", "coverage_target": "95%"}'

# 4. Monitor results from all agents
mqtt_subscribe --topic "results/+" --callback aggregate_analysis_report
```

**Advanced Fractal Patterns:**
- **Recursive Task Delegation**: Subagents can spawn their own sub-subagents for complex domains
- **Dynamic Load Balancing**: Monitor agent workload via heartbeat topics, redistribute tasks automatically
- **Cross-Agent Learning**: Agents share insights via `knowledge/domain` topics
- **Fault Tolerance**: Dead agent detection via missed heartbeats, automatic task redistribution

**Use Cases:**
- **Large Codebase Analysis**: Break down monolith analysis across specialized agents (security, performance, testing, documentation)
- **Multi-Language Projects**: Deploy language-specific agents (Python-agent, JavaScript-agent, Rust-agent) 
- **Distributed Code Review**: Parallel analysis of pull requests with different focuses
- **CI/CD Integration**: Agents monitor git hooks, trigger builds, run tests, deploy services

### 📊 Real-Time IoT & Sensor Data Processing

**Zero-Config IoT Integration**: Perfect for collecting, processing, and routing sensor data without complex infrastructure.

```bash
# Example: Temperature monitoring system
# 1. Start mcmqtt server
uvx mcmqtt

# 2. Use MCP tools to:
#    - Subscribe to sensors/+/temperature 
#    - Publish alerts to alerts/high-temp
#    - Forward to analytics/warehouse
```

**Use Cases:**
- **Smart building automation**: Collect data from thermostats, motion sensors, lighting systems
- **Industrial monitoring**: Equipment health, production metrics, safety alerts
- **Environmental tracking**: Weather stations, air quality monitors, noise levels
- **Agricultural IoT**: Soil moisture, crop monitoring, automated irrigation

### 🔄 Microservice Event Streaming

**Service Mesh Communication**: Replace complex message queues with simple MQTT patterns for microservice coordination.

```bash
# Start dedicated MQTT server for service mesh
uvx mcmqtt --transport http --port 8080

# Services communicate via MQTT topics:
# - orders/created, orders/fulfilled
# - payments/processed, payments/failed  
# - inventory/updated, inventory/low-stock
```

**Use Cases:**
- **Event sourcing**: Capture all state changes as MQTT events for audit trails
- **Saga patterns**: Coordinate complex business transactions across multiple services
- **Cache invalidation**: Notify services when shared data changes
- **Health monitoring**: Services publish heartbeats and status updates

### 🎮 Real-Time Gaming & Live Applications

**Low-Latency Messaging**: Build multiplayer games, live chat, and real-time collaboration tools.

```bash
# Game server coordination
claude mcp add game-events -- uvx mcmqtt --transport http
```

**Use Cases:**
- **Multiplayer game state**: Player positions, actions, world updates
- **Live chat systems**: Message broadcasting, user presence, typing indicators
- **Collaborative editing**: Document changes, cursor positions, user selections
- **Live streaming**: Chat messages, viewer counts, donations, moderation events

### 🏭 DevOps & Infrastructure Automation

**Deployment Orchestration**: Coordinate deployments, monitoring, and incident response across infrastructure.

```bash
# Infrastructure coordination
uvx mcmqtt --mqtt-host production-mqtt.company.com
```

**Use Cases:**
- **CI/CD pipelines**: Build status, deployment triggers, rollback coordination
- **Container orchestration**: Service discovery, load balancer updates, scaling events
- **Monitoring alerts**: Error spikes, performance degradation, security incidents
- **Backup coordination**: Database snapshots, file synchronization, disaster recovery

### 🛍️ E-commerce & Business Process Automation

**Order Processing Workflows**: Handle complex business processes with event-driven patterns.

**Use Cases:**
- **Order fulfillment**: Inventory checks → payment processing → shipping → delivery notifications
- **Customer journey**: Cart abandonment → email campaigns → purchase → support follow-up
- **Supply chain**: Supplier updates → inventory management → demand forecasting
- **Fraud detection**: Transaction monitoring → risk scoring → manual review triggers

### 📱 Mobile & Edge Computing

**Offline-First Applications**: Handle intermittent connectivity and edge computing scenarios.

**Use Cases:**
- **Mobile app synchronization**: User data, settings, offline queue processing
- **Edge device coordination**: Factory floor devices, retail kiosks, field equipment
- **Bandwidth optimization**: Intelligent data batching, compression, prioritization
- **Mesh networking**: Device-to-device communication, local data processing

### 🧠 Multi-Model AI Orchestration & MCP Server Coordination

**The Ultimate AI Party**: Coordinate different AI models, MCP servers, and specialized tools through MQTT messaging for complex multi-step workflows.

```bash
# Setup the coordination hub
claude mcp add task-buzz -- uvx mcmqtt

# Add your MCP server friends to the party
claude mcp add web-search -- uvx mcp-server-fetch
claude mcp add code-search -- uvx searchmcp  
claude mcp add file-ops -- uvx mcp-server-filesystem
claude mcp add db-query -- uvx mcp-server-sqlite
claude mcp add memory-store -- uvx mcp-server-memory
claude mcp add brave-search -- uvx mcp-server-brave-search
```

**Example: Multi-Model Research & Analysis Pipeline**

```bash
# 1. Coordinator agent orchestrates the entire workflow
mqtt_publish --topic "workflow/start" --payload '{
  "task": "analyze_ai_trends_2024",
  "models_needed": ["web_search", "code_analysis", "data_processing", "report_generation"]
}'

# 2. Web Search Agent (Claude + Brave Search MCP)
claude -p "You are web-search-agent. Subscribe to 'tasks/web_search'. Use brave-search tools to find latest AI trends. Publish findings to 'data/web_results'."

# 3. Code Analysis Agent (Claude + Code Search MCP) 
claude -p "You are code-agent. Subscribe to 'tasks/code_analysis'. Use searchmcp to find relevant code examples. Publish to 'data/code_results'."

# 4. Data Processing Agent (Claude + SQLite MCP)
claude -p "You are data-agent. Subscribe to 'data/+' topics. Use sqlite tools to store and analyze collected data. Publish insights to 'insights/processed'."

# 5. Report Generation Agent (Claude + File System MCP)
claude -p "You are report-agent. Subscribe to 'insights/+'. Use filesystem tools to generate comprehensive reports. Publish completion to 'workflow/complete'."

# Coordinator orchestrates the flow
mqtt_publish --topic "tasks/web_search" --payload '{"query": "AI trends 2024", "sources": 10}'
mqtt_publish --topic "tasks/code_analysis" --payload '{"focus": "LLM implementation patterns"}'

# Monitor progress across all agents
mqtt_subscribe --topic "workflow/+" --callback track_pipeline_progress
```

**Advanced Multi-Model Patterns:**

**🔄 Model Chaining & Handoffs:**
```bash
# GPT-4 for initial analysis → Claude for code review → Local model for privacy-sensitive data
mqtt_publish --topic "models/gpt4/analyze" --payload '{"data": "public_dataset.json"}'
# GPT-4 publishes to models/claude/review → Claude publishes to models/local/sensitive
```

**🎭 Specialized Model Routing:**
```bash
# Route different task types to optimal models
mqtt_publish --topic "routing/classify" --payload '{"task": "legal_document_analysis"}'
# Router sends to models/legal_llm/ vs models/code_llm/ vs models/creative_llm/
```

**🧪 A/B Testing & Model Comparison:**
```bash
# Send same prompt to multiple models for comparison
mqtt_publish --topic "models/compare" --payload '{
  "prompt": "Explain quantum computing",
  "models": ["gpt4", "claude", "gemini", "local-llama"],
  "evaluation_criteria": ["accuracy", "clarity", "depth"]
}'
```

**🔗 MCP Server Coordination Examples:**

**Database + Web + Memory Pipeline:**
```bash
# 1. Brave Search finds information → SQLite stores → Memory recalls context
mqtt_publish --topic "research/start" --payload '{"topic": "sustainable_energy"}'

# Web agent searches and stores
brave_search → mqtt_publish("data/raw") → sqlite_store → mqtt_publish("data/indexed")

# Memory agent builds context
memory_store → mqtt_publish("context/ready") → next_agents_process
```

**File System + Code Search + Web Integration:**
```bash
# 1. File system agent monitors project changes
filesystem_watch → mqtt_publish("code/changed") 

# 2. Code search agent analyzes patterns  
searchmcp_analyze → mqtt_publish("patterns/found")

# 3. Web agent researches best practices
web_fetch → mqtt_publish("best_practices/updated")

# 4. Integration agent updates documentation
filesystem_write_docs → mqtt_publish("docs/updated")
```

**Real-World Multi-Model Use Cases:**
- **Research Pipeline**: Web search → document analysis → data extraction → report generation
- **Code Intelligence**: Repository analysis → pattern detection → documentation generation → test creation  
- **Content Creation**: Research → outline → writing → fact-checking → editing → publishing
- **Decision Support**: Data gathering → analysis → modeling → recommendation → validation
- **Quality Assurance**: Multi-model review → consensus building → error detection → improvement suggestions

**🎪 The MCP Server Party Benefits:**
- **Specialized Expertise**: Each MCP server brings unique capabilities (web search, file ops, databases, memory)
- **Fault Tolerance**: If one model/server fails, workflow continues with others
- **Cost Optimization**: Route expensive tasks to premium models, routine tasks to efficient models
- **Privacy Layers**: Sensitive data stays with local models, public data uses cloud models
- **Scalability**: Add new models and MCP servers without changing existing workflows

## ⚡ MCP Server Superpowers: Background Automation via MQTT

**Transform any MCP server into an event-driven powerhouse!** Instead of just responding to direct calls, existing MCP servers can gain "superpowers" through MQTT coordination:

### 🔄 Background Processing Examples

**Google Drive Batch Downloader:**
```bash
# MCP Client publishes download queue
{
  "topic": "gdrive/download-queue",
  "payload": {
    "files": ["doc1.pdf", "spreadsheet.xlsx", "presentation.pptx"],
    "destination": "/local/backup/",
    "batch_id": "backup-2025-09-17"
  }
}

# Google Drive MCP server subscribes and processes in background
# Publishes progress updates via MQTT
{
  "topic": "gdrive/download-progress",
  "payload": {
    "batch_id": "backup-2025-09-17",
    "completed": 2,
    "total": 3,
    "current_file": "presentation.pptx",
    "status": "downloading"
  }
}
```

**Database Backup Orchestration:**
```bash
# Scheduler publishes backup trigger
{
  "topic": "database/backup-trigger",
  "payload": {
    "databases": ["users", "orders", "analytics"],
    "backup_type": "incremental",
    "schedule_id": "daily-backup"
  }
}

# Multiple database MCP servers coordinate the backup
# PostgreSQL MCP server handles users & orders
# MongoDB MCP server handles analytics
# S3 MCP server handles upload coordination
```

**Code Repository Analysis Pipeline:**
```bash
# Git webhook triggers analysis
{
  "topic": "code/analysis-request",
  "payload": {
    "repo": "github.com/user/project",
    "commit": "abc123",
    "analysis_types": ["security", "performance", "documentation"]
  }
}

# Specialized MCP servers process in parallel:
# - Security scanner MCP server
# - Performance analyzer MCP server  
# - Documentation generator MCP server
# Each publishes results to "code/analysis-results/{type}"
```

### 🎯 Event-Driven MCP Patterns

**1. Subscription-Based Processing**
- MCP servers subscribe to relevant MQTT topics
- Process tasks asynchronously in background
- Publish results/progress for other services

**2. Workflow Orchestration**
- Chain multiple MCP servers via MQTT topics
- Each server triggers the next step in the pipeline
- Build complex automation without tight coupling

**3. Real-Time Coordination**
- Multiple MCP servers collaborate on single tasks
- Share progress updates via MQTT
- Coordinate resource usage and scheduling

**4. Event Sourcing & Audit**
- All MCP server actions published as MQTT events
- Build comprehensive audit trails
- Enable replay and debugging capabilities

### 💡 Existing MCP Servers + MQTT = Superpowers

**Transform these common MCP servers:**

| Original MCP Server | + MQTT Superpowers |
|-------------------|--------------------|
| File System MCP | → Background file sync, automated cleanup, distributed backup |
| Database MCP | → Scheduled migrations, replication monitoring, auto-scaling |
| GitHub MCP | → PR auto-review, CI/CD coordination, issue triaging |
| Slack MCP | → Smart notifications, automated responses, team coordination |
| Email MCP | → Automated campaigns, template processing, delivery tracking |
| AWS MCP | → Infrastructure orchestration, cost optimization, auto-scaling |

### 🤝 Agent "Gathering" Patterns

**Multiple MCP clients can access the same MCP server resources via MQTT coordination:**

**Example: File Processing Queue**
```bash
# File server MCP publishes available files
{
  "topic": "files/processing-queue",
  "payload": {
    "file_id": "document_123.pdf",
    "size": "2.5MB",
    "type": "pdf",
    "priority": "high"
  }
}

# Multiple agents "gather" and claim work:
# - PDF analysis agent subscribes to files/processing-queue
# - OCR agent subscribes to files/processing-queue  
# - Translation agent subscribes to files/processing-queue
# First agent to respond gets the work item
```

**Distributed Resource Sharing:**
- **Database connections**: Multiple agents share database MCP server via MQTT coordination
- **File system access**: Agents coordinate file operations through filesystem MCP + MQTT
- **API rate limits**: Share expensive API calls across many agents via API MCP + MQTT
- **Computing resources**: Distribute heavy tasks across multiple MCP servers

**Long-Running Task Coordination:**
```bash
# Agent publishes long-running task request
{
  "topic": "tasks/video-processing",
  "payload": {
    "video_url": "https://example.com/video.mp4",
    "operations": ["transcode", "thumbnail", "metadata"],
    "callback_topic": "results/video-123"
  }
}

# Video processing MCP server handles the work
# Other agents subscribe to "results/video-123" for completion
```

**Key Benefits:**
- **Resource Multiplexing**: Many MCP clients share expensive MCP server resources
- **Distributed Processing**: Long-running tasks don't block individual agents
- **Event-Driven Coordination**: Agents "gather" around shared resources via MQTT
- **Scalable Architecture**: Add more agents without changing MCP server code
- **Fault Tolerance**: If one agent fails, others continue processing
- **Real-Time Updates**: Live progress tracking across all participants

## 🌍 Access the Entire MQTT Ecosystem

**Your MCP crew now has access to MILLIONS of MQTT-enabled devices and services worldwide!**

### 🏭 Industrial & Enterprise Systems

**Factory Automation:**
```bash
# Monitor production line via industrial MQTT broker
{
  "tool": "mqtt_subscribe",
  "arguments": {
    "topic": "factory/line-1/+",
    "qos": 2
  }
}

# Control robotic assembly via MCP
{
  "tool": "mqtt_publish", 
  "arguments": {
    "topic": "factory/robot-arm/commands",
    "payload": {
      "action": "pick_and_place",
      "coordinates": [100, 200, 50],
      "speed": "medium"
    }
  }
}
```

**Enterprise Message Brokers:**
- **AWS IoT Core**: Connect to millions of IoT devices via AWS MQTT
- **Azure IoT Hub**: Integrate with Microsoft's IoT ecosystem
- **Google Cloud IoT**: Access Google's device management platform
- **HiveMQ**: Enterprise-grade MQTT broker integration
- **Eclipse Mosquitto**: Open-source MQTT broker networks

### 🏠 Smart Home & Consumer IoT

**Home Automation:**
```bash
# Control smart lights via Home Assistant MQTT
{
  "topic": "homeassistant/light/living_room/set",
  "payload": {
    "state": "ON",
    "brightness": 180,
    "color": {"r": 255, "g": 200, "b": 100}
  }
}

# Monitor energy usage
{
  "topic": "energy/+/consumption", 
  "payload": "subscribe"
}
```

**Consumer Devices:**
- **Philips Hue**: Smart lighting control
- **Tesla vehicles**: Vehicle data and control (via third-party bridges)
- **Security cameras**: Motion detection, alerts
- **Smart thermostats**: Climate control automation
- **Garage doors, locks, sensors**: Complete home integration

### 📱 Mobile & Web Applications

**Real-Time Apps:**
```bash
# Chat application integration
{
  "topic": "chat/room-123/messages",
  "payload": {
    "user": "AI_Assistant",
    "message": "Analysis complete. Found 3 security issues.",
    "timestamp": "2025-09-17T10:30:00Z"
  }
}

# Live gaming coordination  
{
  "topic": "game/lobby-456/player-actions",
  "payload": {
    "player_id": "ai_coach",
    "action": "strategy_suggestion",
    "data": "Focus defenses on northwest quadrant"
  }
}
```

### 🚗 Transportation & Logistics

**Fleet Management:**
```bash
# Track delivery vehicles
{
  "topic": "fleet/vehicle-789/location",
  "payload": "subscribe"
}

# Optimize routing based on real-time data
{
  "topic": "logistics/route-optimization",
  "payload": {
    "vehicle_id": "truck-123",
    "new_route": ["depot", "customer-A", "customer-C", "customer-B"],
    "estimated_time": "2.5 hours"
  }
}
```

### 🏥 Healthcare & Medical Devices

**Patient Monitoring:**
```bash
# Monitor vital signs from medical IoT devices
{
  "topic": "hospital/room-204/vitals/+",
  "payload": "subscribe"
}

# Alert coordination
{
  "topic": "alerts/critical/room-204", 
  "payload": {
    "alert_type": "heart_rate_anomaly",
    "value": 140,
    "timestamp": "2025-09-17T14:22:15Z",
    "severity": "high"
  }
}
```

### 🌾 Agriculture & Environmental

**Smart Farming:**
```bash
# Soil moisture sensors
{
  "topic": "farm/field-A/sensors/soil_moisture",
  "payload": "subscribe"
}

# Automated irrigation control
{
  "topic": "farm/irrigation/zone-3/control",
  "payload": {
    "action": "start_watering",
    "duration_minutes": 15,
    "flow_rate": "medium"
  }
}
```

### 🏢 Building Management Systems

**Smart Buildings:**
```bash
# HVAC control via BACnet-to-MQTT bridges
{
  "topic": "building/floor-5/hvac/set_temperature",
  "payload": {
    "zone": "conference_room_A",
    "temperature": 21.5,
    "mode": "cooling"
  }
}

# Energy optimization
{
  "topic": "building/power/consumption/+",
  "payload": "subscribe"
}
```

### 🔗 Protocol Bridges & Integrations

**Your MCP crew can reach ANY protocol via MQTT bridges:**
- **Modbus-to-MQTT**: Industrial equipment control
- **BACnet-to-MQTT**: Building automation systems  
- **Zigbee-to-MQTT**: Smart home device networks
- **LoRaWAN-to-MQTT**: Long-range IoT sensors
- **OPC-UA-to-MQTT**: Industrial automation standards
- **CoAP-to-MQTT**: Lightweight IoT device communication
- **WebSocket-to-MQTT**: Browser-based real-time apps

### 🎯 The Power Multiplier Effect

**Before mcmqtt**: Your MCP servers could only talk to each other
**After mcmqtt**: Your MCP crew can coordinate with:
- 🏭 **Billions of IoT devices** across industries
- 🏢 **Enterprise systems** and message brokers  
- 📱 **Mobile applications** and web services
- 🏠 **Consumer smart devices** and home automation
- 🚗 **Vehicles, drones, and transportation systems**
- 🏥 **Medical devices and healthcare systems**
- 🌾 **Agricultural sensors and environmental monitoring**

**Result**: Your AI agents become **universal coordinators** in the physical and digital world!

## 🚀 Ad-Hoc Broker: Instant Infrastructure Magic

**The coolest feature: spawn MQTT brokers instantly for any scenario!** No Docker, no setup, no configuration files - just call a tool and get a running broker.

### 🎯 Unique Ad-Hoc Broker Use Cases

**1. Ephemeral Agent Swarms**
```bash
# Create private broker for temporary agent collaboration
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "code-review-swarm-20250917",
    "port": 0,  # Auto-assign port
    "max_connections": 50
  }
}

# Response: broker running at mqtt://127.0.0.1:34521
# Agents coordinate code review, then broker auto-destroys when done
```

**2. Isolated Testing Environments**  
```bash
# Each test suite gets its own broker
{
  "tool": "mqtt_spawn_broker", 
  "arguments": {
    "name": "test-isolation-${TEST_ID}",
    "port": 0,
    "auth_required": true,
    "username": "test_user",
    "password": "temp_pass_123"
  }
}

# Perfect for parallel test execution without interference
```

**3. Dynamic Gaming Sessions**
```bash
# Spawn broker per game lobby
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "game-lobby-${LOBBY_ID}",
    "websocket_port": 0,  # WebSocket for browser clients
    "max_connections": 20
  }
}

# Players join via WebSocket, real-time game coordination
# Broker dies when game ends - zero cleanup needed
```

**4. Secure Demo Environments** 
```bash
# Customer demo with isolated broker
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "customer-demo-acme-corp",
    "auth_required": true,
    "username": "demo_user",
    "password": "demo_2025_secure",
    "max_connections": 10
  }
}

# Customer gets clean, isolated environment
# No risk of seeing other customer data
```

**5. Development Hot-Swapping**
```bash
# Developer working on feature X
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "dev-feature-x-john",
    "port": 1888,  # Fixed port for easy connection
    "max_connections": 5
  }
}

# Private development broker for experimentation
# Switch between dev/staging/prod brokers instantly
```

**6. Conference/Event Coordination**
```bash
# Spawn broker for conference session
{
  "tool": "mqtt_spawn_broker", 
  "arguments": {
    "name": "pycon-2025-session-123",
    "websocket_port": 9001,
    "max_connections": 500
  }
}

# Live audience interaction, Q&A, polls
# Broker exists only for session duration
```

**7. CI/CD Pipeline Isolation**
```bash
# Each build gets private message bus
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "build-${BUILD_ID}-${COMMIT_HASH}",
    "port": 0,
    "max_connections": 20
  }
}

# Build agents coordinate via private broker
# Test results, deployment status, artifact sharing
# Auto-cleanup when build completes
```

**8. Emergency Response Coordination**
```bash
# Crisis situation needs instant coordination
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "emergency-response-${INCIDENT_ID}",
    "auth_required": true,
    "username": "emergency",
    "password": "secure_incident_comms",
    "max_connections": 100
  }
}

# First responders, emergency services coordinate instantly
# Secure, isolated communication channel
```

**9. Scientific Experiment Coordination**
```bash
# Research experiment needs data coordination
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "experiment-${STUDY_ID}",
    "port": 0,
    "max_connections": 200
  }
}

# Sensors, data collectors, analysis agents coordinate
# Experiment-specific broker with clean data isolation
```

**10. Pop-Up Retail/Event Networks**
```bash
# Temporary retail location needs instant IoT network
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "popup-store-downtown-${DATE}",
    "websocket_port": 8883,
    "max_connections": 100
  }
}

# POS systems, inventory scanners, customer devices
# Instant retail network without IT infrastructure
```

### 🔥 Why Ad-Hoc Brokers Are Revolutionary

**Traditional MQTT Setup:**
1. Install broker software ⏱️ 
2. Configure authentication 🔧
3. Set up networking 🌐
4. Manage certificates 🔐
5. Monitor & maintain 📊
6. Manual cleanup 🗑️

**mcmqtt Ad-Hoc Brokers:**
1. Call `mqtt_spawn_broker` tool ⚡
2. **Done!** 🎉

**Unique Advantages:**
- **Zero Infrastructure**: No servers, containers, or cloud accounts needed
- **Instant Isolation**: Each use case gets its own private message bus
- **Auto-Cleanup**: Brokers disappear when no longer needed
- **Development Speed**: Start messaging in seconds, not hours
- **Cost Efficiency**: No persistent infrastructure costs
- **Security**: Isolated networks with optional authentication
- **Portability**: Works anywhere mcmqtt runs (uvx everywhere!)
- **Scalability**: Spin up dozens of brokers for different purposes

### 🎪 The "Message Bus Vending Machine" Effect

Think of mcmqtt as a **message bus vending machine**:
- Need coordination? Insert tool call, get broker
- Temporary project? Get disposable message bus  
- Isolated testing? Get clean communication channel
- Secure demo? Get authenticated broker instance

**No long-term commitments, no infrastructure overhead, just instant messaging infrastructure whenever you need it!**

## 🔒 Production-Ready: Caddy + mcmqtt = Instant Secure Infrastructure

**Combine mcmqtt with Caddy for automatic HTTPS, reverse proxy, and production-grade security!**

### 🚀 Auto-HTTPS MQTT Brokers

**The Ultimate Combo:**
```bash
# 1. Spawn mcmqtt broker with WebSocket support
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "production-api-broker",
    "websocket_port": 8083,
    "auth_required": true,
    "username": "api_client",
    "password": "secure_2025_prod"
  }
}

# 2. Caddy auto-configures HTTPS proxy (via docker labels)
# Result: wss://mqtt-api.yourdomain.com with auto-renewed Let's Encrypt certs!
```

**Caddyfile Configuration:**
```caddyfile
# Automatic HTTPS for MQTT WebSocket endpoints
mqtt-{subdomain}.yourdomain.com {
    reverse_proxy /mqtt/* localhost:8083
    
    # Auto-generated Let's Encrypt certificates
    tls {
        on_demand
    }
    
    # WebSocket upgrade support
    @websocket {
        header Connection *Upgrade*
        header Upgrade websocket
    }
    reverse_proxy @websocket localhost:8083
}
```

### 🏗️ Enterprise Deployment Patterns

**1. Multi-Tenant SaaS Platform**
```bash
# Customer onboarding triggers secure broker
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "customer-${TENANT_ID}",
    "websocket_port": 0,  # Auto-assign
    "auth_required": true,
    "max_connections": 1000
  }
}

# Caddy automatically creates: wss://customer-123.mqtt.yourapp.com
# Instant secure messaging for new customer with auto-HTTPS
```

**2. API Gateway with MQTT Backend**
```bash
# API endpoint needs real-time coordination
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "api-coordination-${API_VERSION}",
    "websocket_port": 8084,
    "max_connections": 5000
  }
}

# Caddy routes: api.yourapp.com/realtime/* -> secure MQTT broker
# Zero infrastructure setup, production-ready in seconds
```

**3. Microservice Event Bus**
```bash
# Deploy new microservice cluster
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "events-${DEPLOYMENT_ID}",
    "port": 1883,        # Standard MQTT
    "websocket_port": 8883,  # Secure WebSocket
    "auth_required": true
  }
}

# Caddy provides:
# - MQTT over TLS: mqtts://events.internal.yourapp.com:8883
# - WebSocket Secure: wss://events.yourapp.com/ws
# - Automatic certificate management
```

**4. IoT Device Fleet Management**
```bash
# New device fleet deployment
{
  "tool": "mqtt_spawn_broker", 
  "arguments": {
    "name": "iot-fleet-${REGION}-${DEPLOYMENT}",
    "port": 8883,
    "websocket_port": 9001,
    "auth_required": true,
    "max_connections": 10000
  }
}

# Caddy creates: iot-fleet-us-west.yourapp.com
# Devices get automatic HTTPS, load balancing, DDoS protection
```

### 🔧 Docker Compose Integration with caddy-docker-proxy

**Using [caddy-docker-proxy](https://github.com/lucaslorentz/caddy-docker-proxy)** for automatic HTTPS and container discovery.

> **caddy-docker-proxy** automatically generates Caddyfile configurations from Docker container labels, eliminating manual reverse proxy setup. Perfect for dynamic broker spawning!

**Complete Production Stack:**
```yaml
# docker-compose.yml
version: "3.8"

services:
  caddy:
    image: lucaslorentz/caddy-docker-proxy:ci-alpine
    ports:
      - "80:80"
      - "443:443"
    environment:
      # Enable Docker provider for automatic container discovery
      CADDY_INGRESS_NETWORKS: caddy
      # Vultr API token for DNS challenge (wildcard certificates)
      VULTR_API_TOKEN: ${VULTR_API_TOKEN}
    networks:
      - caddy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - caddy_data:/data
      - caddy_config:/config
    restart: unless-stopped
    labels:
      # Global TLS configuration for wildcard certificates
      caddy.tls.dns: vultr

  mcmqtt:
    image: python:3.11-slim
    # http-streamable automatically configures caddy labels
    command: uvx mcmqtt --transport http-streamable --port 3000 --hostname mqtt-control.${DOMAIN}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - caddy
    restart: unless-stopped
    environment:
      - DOMAIN=${DOMAIN}
      - CADDY_NETWORK=caddy
    # Labels automatically added by --transport http-streamable
    # No manual caddy labels needed!
    depends_on:
      - caddy

  # Optional: Web dashboard for broker management
  mqtt-dashboard:
    image: nginx:alpine
    volumes:
      - ./dashboard:/usr/share/nginx/html:ro
    networks:
      - caddy
    labels:
      caddy: mqtt-dashboard.yourdomain.com
      caddy.reverse_proxy: "{{upstreams 80}}"
      caddy.tls.dns: vultr

networks:
  caddy:
    external: false
    name: caddy

volumes:
  caddy_data:
    external: true
  caddy_config:
    external: true
```

**Environment Configuration (.env):**
```bash
# .env file
COMPOSE_PROJECT_NAME=mcmqtt-production

# Vultr DNS challenge for wildcard certificates
VULTR_API_TOKEN=your_vultr_api_key_here

# Domain configuration
DOMAIN=yourdomain.com
MQTT_SUBDOMAIN=mqtt

# Optional: Basic auth for admin interfaces
ADMIN_USER=admin
ADMIN_PASSWORD_HASH=$2a$14$hash_your_password_here
```

**Setup Instructions:**
```bash
# 1. Create external volumes for Caddy data persistence
docker volume create caddy_data
docker volume create caddy_config

# 2. Configure environment variables
cp .env.example .env
# Edit .env with your domain and Vultr token

# 3. Deploy the stack
docker compose up -d

# 4. Verify Caddy is running
docker compose logs caddy

# 5. Test mcmqtt control interface
curl https://mqtt-control.yourdomain.com/health
```

**How Dynamic Broker Spawning Works:**
```bash
# 1. User calls mqtt_spawn_broker tool
{
  "tool": "mqtt_spawn_broker",
  "arguments": {
    "name": "customer-acme-prod",
    "websocket_port": 9001,
    "public_hostname": "acme.mqtt.yourdomain.com",
    "auth_required": true,
    "max_connections": 1000
  }
}

# 2. mcmqtt spawns Docker container with automatic caddy labels:
docker run -d \
  --name mqtt-broker-customer-acme-prod \
  --network caddy \
  --label "caddy=acme.mqtt.yourdomain.com" \
  --label "caddy.reverse_proxy={{upstreams 1883}}" \
  --label "caddy.handle_path=/ws" \
  --label "caddy.handle_path.reverse_proxy={{upstreams 9001}}" \
  --label "caddy.tls.dns=vultr" \
  python:3.11-slim uvx amqtt

# 3. caddy-docker-proxy automatically detects the new container
# 4. Generates Caddyfile configuration instantly:
#    acme.mqtt.yourdomain.com {
#        reverse_proxy mqtt-broker-customer-acme-prod:1883
#        handle_path /ws {
#            reverse_proxy mqtt-broker-customer-acme-prod:9001
#        }
#        tls {
#            dns vultr
#        }
#    }

# 5. Result: https://acme.mqtt.yourdomain.com is LIVE with valid certificate!
```

**Production Deployment Example:**
```bash
# Clone and deploy the complete stack
git clone https://github.com/your-org/mcmqtt-production-stack
cd mcmqtt-production-stack

# Configure your domain and Vultr DNS
cp .env.example .env
# Edit .env with your domain and Vultr API token

# Deploy production stack
make deploy

# Test the deployment
make test-deployment

# Spawn your first public broker
curl -X POST https://mqtt-control.yourdomain.com/api/brokers \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-first-broker",
    "public_hostname": "test.mqtt.yourdomain.com",
    "websocket_port": 9001,
    "auth_required": true
  }'

# Result: https://test.mqtt.yourdomain.com is immediately accessible!
```

### ⚡ Auto-Labeling with `--transport http-streamable`

**Zero configuration Caddy integration!**

```bash
# Traditional: Manual caddy labels
uvx mcmqtt --transport http --port 3000
# Requires manual caddy labels in docker-compose.yml

# Auto-magic: Automatic caddy labels  
uvx mcmqtt