Beyond the Broker: Building Intelligent AI Agents with MQTT and Sparkplug B

January 13, 2026

Introduction: MQTT as the Nervous System

In the world of Industrial IoT (IIoT), MQTT has firmly established itself as the de facto standard for messaging. Its lightweight, publish-subscribe model makes it the perfect "nervous system" for the factory floor, efficiently relaying data from thousands of sensors and devices to interested applications. However, raw MQTT, for all its strengths, is fundamentally stateless. It tells you what is happening right now, but not the context or state of the device that sent the data.

This is where Sparkplug B comes in. As a specification that sits on top of MQTT, Sparkplug B adds critical features for industrial applications: state management, standardized data models, and automatic discovery of devices.

When you combine the real-time capabilities of MQTT, the contextual richness of Sparkplug B, and the reasoning power of AI Agents, you unlock a new level of industrial automation. This is not just about data visualization; it's about creating intelligent systems that understand the state of your entire operation and can act autonomously.

This advanced guide will explore the architecture and implementation of AI agents that leverage both standard MQTT and the Sparkplug B specification. We will demonstrate how a platform like MQTTfy can be used to build agents that are not only triggered by data but can also intelligently command and control devices using these protocols.

The Power of Sparkplug B with MQTT

Before diving into the AI agents, it's crucial to understand what Sparkplug B brings to the table. Standard MQTT is like sending postcards: each message is a discrete piece of information. Sparkplug B turns it into a live, shared phone directory.

  1. Stateful Awareness: Every Sparkplug B client maintains a "Last Will and Testament" message. If a device disconnects unexpectedly, the MQTT broker automatically publishes its "death certificate," immediately notifying all systems that the device is offline.
  2. Standardized Payload: Sparkplug B defines a strict payload format using Google's Protocol Buffers. This eliminates ambiguity. Data is sent with its name, datatype, timestamp, and properties, ensuring all applications interpret it correctly.
  3. Birth & Death Certificates: When a device or application (an "Edge of Network" or EON node) comes online, it publishes a "birth certificate" that announces all of its capabilities and metrics. This allows for plug-and-play discovery.
  4. Report by Exception: Devices only publish data when it changes, significantly reducing network traffic compared to polling-based systems.

This stateful, structured approach is the perfect foundation for building intelligent AI agents.

Architectural Model

An AI agent interacts with MQTT and Sparkplug B as both a subscriber and a publisher. It listens for data and events, and when its goal dictates, it publishes commands back to the devices.

hybrid-mqtt-sparkplug-b-ai-agent-industrial-architecture

This diagram shows the agent subscribing to multiple data types. It receives structured NDATA (Node Data) from a Sparkplug-aware PLC and unstructured JSON from a simpler MQTT sensor. Based on this information, it can publish a NCMD (Node Command) back to the PLC or a simple JSON command to the sensor.

Building a Hybrid MQTT/Sparkplug B AI Agent

Let's design an AI agent that manages a bottling line. We have two systems:

  • A modern bottling machine (bottler-01) that communicates via Sparkplug B. It publishes metrics like Fill_Level and Cap_Torque. It can be controlled via NCMD messages to a Machine/RUN metric.
  • An older labeling machine (labeler-01) that uses standard MQTT. It publishes a simple JSON message {"status": "online"} to machines/labeler-01/status and accepts a {"command": "start"} message on machines/labeler-01/command.

Agent Goal (System Prompt):

"You are an AI Line Supervisor. Your goal is to start the bottling line sequence. First, you must use the Sparkplug B tool to command the bottler-01 machine to run by writing a true boolean value to its Machine/RUN metric. After confirming the bottler is running, you must check the status of the labeler-01 machine. If the labeler is online, use the standard MQTT tool to send a 'start' command to it."

Agent Tools (Configured in MQTTfy):

  1. Command Bottler (Sparkplug B Tool)

    • Type: SPARKPLUG
    • Broker/Port: (Configured)
    • Group ID: Site-A
    • Edge Node ID: bottler-01
    • Description for AI: "Sends a Sparkplug B Node Command (NCMD) to the bottler machine. Use this to start or stop the machine by writing to the 'Machine/RUN' metric."
  2. Command Labeler (MQTT Tool)

    • Type: MQTT
    • Broker/Port: (Configured)
    • Topic: machines/labeler-01/command
    • Description for AI: "Publishes a standard JSON command to the labeling machine."
  3. Check Labeler Status (MQTT Tool - for reading)

    • This isn't an "action" tool but represents the agent's ability to read data from machines/labeler-01/status, which is part of its trigger mechanism.

The Autonomous Workflow

A user gives the agent a simple prompt: "Start the bottling line."

  1. Reasoning (Goal Analysis): The agent processes the user's request against its system prompt. It understands the first step is to start bottler-01.
  2. Tool Selection (Sparkplug B): It identifies the Command Bottler tool as the correct one. The agent's AI core knows from the tool's description and its goal that it needs to send an NCMD with a specific metric payload.
  3. Tool Execution (Sparkplug B): The agent invokes the tool. The underlying platform code constructs the Sparkplug B payload using Protocol Buffers.
    • Topic: spBv1.0/Site-A/NCMD/bottler-01
    • Payload (Protobuf):
      timestamp: 1677612000000
      metrics: [
        {
          name: "Machine/RUN",
          datatype: 11, // Boolean
          boolean_value: true
        }
      ]
      seq: 1
      
    The platform publishes this binary payload to the MQTT broker. The bottler-01 EON node receives the command and starts running.
  4. State Confirmation: The bottler-01 machine, upon starting, publishes an NDATA message confirming its new state, which includes the Machine/RUN metric now being true. The agent receives this message, confirming its action was successful.
  5. Reasoning (Next Step): The agent's goal says the next step is to deal with the labeler. It needs to know the labeler's status. It accesses the latest message received from machines/labeler-01/status, which is {"status": "online"}.
  6. Tool Selection (MQTT): The agent sees the labeler is online and decides to use the Command Labeler tool.
  7. Tool Execution (MQTT): It invokes the tool, publishing a simple JSON string payload:
    • Topic: machines/labeler-01/command
    • Payload (String): {"command": "start"} The older labeling machine receives this command and starts.
  8. Conclusion: The agent logs its final status: "Bottling line start sequence complete. Both bottler and labeler are running."

Why This Hybrid Approach is Superior

This example demonstrates the power of an AI agent that is fluent in both standard MQTT and Sparkplug B.

  • Interoperability: The agent seamlessly orchestrates new, Sparkplug-aware equipment with legacy MQTT devices. It doesn't force you into a single standard.
  • Intelligence over Hard-coding: The agent's behavior is guided by its natural language goal, not a rigid script. If the labeler had been offline, the agent could be programmed with a goal to "wait and retry" or "alert a human," decisions it would make autonomously based on the situation.
  • Abstraction: The user or developer interacting with the agent doesn't need to know the intricacies of Protocol Buffers or the exact command JSON for each device. They interact with named, descriptive tools. The agent handles the low-level protocol details.
  • State-Driven Actions: The agent's ability to confirm the bottler's state change via the NDATA message before proceeding is a key feature of robust automation, made simple by Sparkplug B.

Conclusion

The combination of MQTT's real-time transport, Sparkplug B's industrial-grade state and data modeling, and the reasoning engine of an AI agent represents a paradigm shift in industrial automation. By building agents that are "bilingual" in both raw MQTT and Sparkplug B, you can create a truly unified, intelligent, and autonomous control plane for your factory floor. This allows you to modernize your operations incrementally, integrating new smart devices while still commanding and controlling legacy systems, all orchestrated by goal-oriented AI.