Applicability
When to Use
✓When MCP server failures should not crash the entire system
✓When you need graceful degradation
✓When services have known failure patterns
Overview
How It Works
The Circuit Breaker pattern tracks failures for each MCP server connection. When failures exceed a threshold, the circuit 'opens' and all subsequent calls fail immediately without hitting the server. After a cooldown period, the circuit moves to 'half-open' and allows one test request. If it succeeds, the circuit closes; if it fails, it opens again.
This prevents wasting time on servers that are down and gives them time to recover. In an MCP architecture, each server connection has its own circuit breaker with independent thresholds.
Implementation
Code Example
typescript
class CircuitBreaker {
state = "closed"; failures = 0; lastFailure = 0;
constructor(public threshold = 5, public cooldown = 30000) {}
async call(fn) {
if (this.state === "open") {
if (Date.now() - this.lastFailure > this.cooldown) {
this.state = "half-open";
} else {
throw new Error("Circuit breaker is open");
}
}
try {
const result = await fn();
this.failures = 0;
this.state = "closed";
return result;
} catch (error) {
this.failures++;
this.lastFailure = Date.now();
if (this.failures >= this.threshold) this.state = "open";
throw error;
}
}
}
const breakers = { postgres: new CircuitBreaker(), redis: new CircuitBreaker(3, 10000) };
async function safeQuery(sql, params) {
return breakers.postgres.call(() => postgres.query(sql, params));
}Quick Info
Categoryresilience
ComplexityMedium