Applicability
When to Use
✓When you need a response from a remote service
✓When implementing RPC-style communication
✓When synchronous behavior is needed over async channels
Overview
How It Works
The Request-Reply pattern implements synchronous-style communication over MCP servers. The requesting agent sends a message with a unique correlation ID and waits for a response with the same ID. This enables RPC-like behavior while maintaining the decoupled nature of message-based communication.
This is useful when one MCP server needs information from another to complete its processing. For example, a pricing service needs product data from the catalog service before calculating a quote.
Implementation
Code Example
typescript
async function requestReply(service, request, timeout = 5000) {
const correlationId = crypto.randomUUID();
const responsePromise = waitForResponse(correlationId, timeout);
await nats.publish({
subject: `${service}.request`,
data: JSON.stringify({ ...request, correlationId, replyTo: "agent.responses" })
});
return await responsePromise;
}
function waitForResponse(correlationId, timeout) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error("Timeout")), timeout);
nats.subscribe("agent.responses", (msg) => {
const data = JSON.parse(msg);
if (data.correlationId === correlationId) { clearTimeout(timer); resolve(data); }
});
});
}Quick Info
Categorymessaging
ComplexityMedium