MQTT Event Bus for AI Agents: A Safe Command Pattern
Article
Jul 27, 2026
4 min read
UllrAI

MQTT Event Bus for AI Agents: A Safe Command Pattern

Design a safe MQTT command path for AI agents with typed tools, narrow topic permissions, consequence-based approval, expiry, idempotency, acknowledgments, and kill switches.

MQTTAI AgentsCommand SafetyAccess ControlArchitecture

The dangerous step in connecting an AI agent to MQTT is not reading telemetry. It is turning a model decision into a command that may arrive late, run twice, target the wrong device, or exceed the authority of the person who asked for it.

MQTT can provide a clear boundary between the agent and device fleet, but broker delivery guarantees do not make an agent command safe. Safety comes from the policy, validation, approval, expiry, and device-side enforcement around the command path.

Start with command failure modes

Design controls against concrete failures rather than the abstract idea of an unreliable model:

Failure modeRequired control
Prompt content tries to choose a topicTyped tool; server resolves the destination
Agent acts on stale telemetryState version check and command expiry
Retry repeats a physical actionIdempotency key and device-side deduplication
Low-risk authority reaches a critical actuatorConsequence class and independent approval
Publish succeeds but the device does nothingCorrelated result topic and explicit timeout
Agent or command service is compromisedNarrow identity, rate limit, and external kill switch

Put a command gateway between the agent and MQTT

A production flow usually has five parts:

  1. Devices and gateways publish telemetry and state to MQTT topics.
  2. The broker authenticates clients and enforces publish and subscribe policy.
  3. An ingestion service validates payloads, enriches context, and exposes a narrow event stream to the agent.
  4. The agent proposes an action through a typed tool rather than publishing arbitrary MQTT messages.
  5. A command service checks policy, approval state, freshness, and device capability before publishing.

This separation prevents prompt content from becoming a broker credential or topic filter.

device -> MQTT broker -> validated event -> agent
agent -> typed tool -> policy and approval -> MQTT command -> device

Separate observation from control identities

Give observation and control different topics, identities, and permissions.

sites/{siteId}/devices/{deviceId}/telemetry
sites/{siteId}/devices/{deviceId}/state
sites/{siteId}/devices/{deviceId}/commands/request
sites/{siteId}/devices/{deviceId}/commands/result

The service that supplies context to the agent may subscribe to telemetry. It should not automatically inherit permission to publish commands. The command executor should have the narrowest publish scope possible and no ability to edit its own policy.

Expose typed operations, not a publish tool

Do not expose a generic tool such as mqtt_publish(topic, payload) to the model. Define operations around business intent:

{
  "operation": "set_temperature_target",
  "deviceId": "chiller-07",
  "targetCelsius": 6,
  "reason": "Reduce deviation from the approved range",
  "requestId": "req_01J..."
}

The command service resolves the destination topic, validates the target range, verifies the device capability, and creates an audit record. The agent never chooses a wildcard topic or embeds a credential.

Approve commands by consequence

Not every action needs a human, but every action needs an explicit consequence class.

ClassExampleControl
ObserveRead latest temperatureAutomatic, read-only
RecommendSuggest a maintenance windowAutomatic proposal
Reversible controlAdjust a non-critical set point within boundsPolicy-gated
High consequenceStop equipment or unlock accessExplicit approval and independent safeguards

Physical systems must retain local safety limits. A cloud agent should never be the only protection against an unsafe actuator command.

Expire and deduplicate every command

An instruction can become dangerous when delivered after its context is stale. Include:

  • a unique request ID;
  • issued and expiry timestamps;
  • expected device state or version;
  • the initiating principal;
  • the policy decision and approval record;
  • an idempotency key.

MQTT 5 message expiry helps prevent late delivery, but the device should still reject expired or out-of-sequence commands.

Require evidence of device execution

A successful MQTT publish only confirms the broker accepted a message according to the chosen QoS flow. It does not prove the device performed the action.

Use a separate result topic with a correlation ID:

{
  "requestId": "req_01J...",
  "status": "applied",
  "deviceStateVersion": 1843,
  "observedAt": "2026-07-27T09:30:00Z"
}

The workflow can then distinguish accepted, delivered, applied, rejected, expired, and timed-out states.

Production controls to verify

Before connecting an agent to MQTT:

  • issue unique service identities and rotate credentials;
  • allow-list exact topic patterns per tool;
  • validate payloads against versioned schemas;
  • cap command rate and concurrency per device;
  • log proposals, approvals, policy decisions, publishes, and results;
  • test duplicate delivery, reconnects, stale retained state, and partial failure;
  • provide a kill switch outside the agent path.

Start with read-only telemetry and offline recommendations. Add bounded command execution only after the audit trail and failure handling are observable.

Read the MQTT security guide for identity and authorization design, the MQTT 5 migration guide for session and message expiry, and MQTT vs Kafka when the agent also needs durable event replay.

Turn the design into enforceable access

Review TLS, device identity, and topic permissions before connecting production data.