Applicability
When to Use
✓When different services need different subsets of events
✓When you want to decouple producers from consumers
✓When new consumers need to be added without changing producers
Overview
How It Works
The Pub-Sub pattern with topic routing enables selective event delivery across MCP servers. Events are published with a topic (e.g., 'order.created', 'order.shipped'), and consumers subscribe to specific topic patterns. The MQTT or Kafka MCP Server acts as the message broker.
This pattern excels when you have many event types and each consumer only cares about a subset. For example, the billing service subscribes to 'order.*' while the shipping service only subscribes to 'order.paid'. The broker handles routing, so producers don't need to know about consumers.
Implementation
Code Example
typescript
const subscriptions = {
"order.created": [logEvent, notifyWarehouse],
"order.paid": [triggerShipping, updateCRM],
"order.shipped": [notifyCustomer, updateTracking],
};
async function publish(topic, event) {
await kafka.produce({ topic, messages: [{ value: JSON.stringify(event) }] });
}
async function processMessage(topic, message) {
const handlers = subscriptions[topic] || [];
await Promise.all(handlers.map(h => h(JSON.parse(message.value))));
}Quick Info
Categorymessaging
ComplexityMedium