health_monitor.cpp

src/mcp/health_monitor.cpp

@file health_monitor.cpp @brief HealthMonitor implementation.

Owners: @tvanfossen

attempt_reconnect

void entropic::HealthMonitor::attempt_reconnect(const std::string &name, WatchEntry &entry)

Attempt reconnection for a single server.

src/mcp/health_monitor.cpp:219

Source · 40 lines
void HealthMonitor::attempt_reconnect(
    const std::string& name,
    WatchEntry& entry) {

    if (policy_.exhausted(entry.reconnect_attempt)) {
        if (entry.status != "error") {
            auto old = entry.status;
            entry.status = "error";
            post_event(name, old, "error");
            logger->error("Server '{}' reconnection exhausted "
                          "after {} attempts",
                          name, entry.reconnect_attempt);
        }
        entry.next_action = std::chrono::steady_clock::time_point::max();
        return;
    }

    if (entry.status != "reconnecting") {
        auto old = entry.status;
        entry.status = "reconnecting";
        post_event(name, old, "reconnecting");
    }

    logger->info("Reconnecting to '{}' (attempt {})",
                 name, entry.reconnect_attempt + 1);

    if (entry.client->connect()) {
        on_reconnect_success(name, entry);
        return;
    }

    // Schedule next attempt with backoff
    auto delay = policy_.delay_ms(entry.reconnect_attempt);
    entry.reconnect_attempt++;
    entry.next_action = std::chrono::steady_clock::now() +
                        std::chrono::milliseconds(delay);

    logger->info("Server '{}' reconnect failed, "
                 "retry in {}ms", name, delay);
}
Activity · 20 steps
lines 223–223◇ if (policy_.exhausted(entry.reconnect_attempt))lines 224–224◇ if (entry.status != "error")lines 225–225auto old = entry.status;lines 226–226entry.status = "error";lines 227–227post_event(name, old, "error");lines 228–230logger->error("Server '{}' reconnection exhausted " "after {} attempts", name, entry.reconnect_attempt);lines 232–232entry.next_action = std::chrono::steady_clock::time_point::max();lines 233–233return;lines 236–236◇ if (entry.status != "reconnecting")lines 237–237auto old = entry.status;lines 238–238entry.status = "reconnecting";lines 239–239post_event(name, old, "reconnecting");lines 242–243logger->info("Reconnecting to '{}' (attempt {})", name, entry.reconnect_attempt + 1);lines 245–245◇ if (entry.client->connect())lines 246–246on_reconnect_success(name, entry);lines 247–247return;lines 251–251auto delay = policy_.delay_ms(entry.reconnect_attempt);lines 252–252entry.reconnect_attempt++;lines 253–254entry.next_action = std::chrono::steady_clock::now() + std::chrono::milliseconds(delay);lines 256–257logger->info("Server '{}' reconnect failed, " "retry in {}ms", name, delay);

Requirements

REQ-MCP-025

Flows

check_server

void entropic::HealthMonitor::check_server(const std::string &name, WatchEntry &entry)

Check one server and handle state transitions.

src/mcp/health_monitor.cpp:174

Source · 32 lines
void HealthMonitor::check_server(
    const std::string& name,
    WatchEntry& entry) {

    bool alive = entry.client->is_connected();

    if (entry.status == "connected" && !alive) {
        // Connection lost — start reconnecting
        auto old = entry.status;
        entry.status = "disconnected";
        entry.reconnect_attempt = 0;
        post_event(name, old, entry.status);
        logger->warn("Server '{}' disconnected", name);
        attempt_reconnect(name, entry);
        return;
    }

    if (entry.status == "disconnected" ||
        entry.status == "reconnecting") {
        attempt_reconnect(name, entry);
        return;
    }

    // Connected + healthy: schedule next check
    if (health_check_interval_ms_ > 0) {
        entry.next_action = std::chrono::steady_clock::now() +
            std::chrono::milliseconds(health_check_interval_ms_);
    } else {
        entry.next_action = std::chrono::steady_clock::now() +
            std::chrono::seconds(5);
    }
}
Activity · 16 steps
lines 178–178bool alive = entry.client->is_connected();lines 180–180◇ if (entry.status == "connected" && !alive)lines 182–182auto old = entry.status;lines 183–183entry.status = "disconnected";lines 184–184entry.reconnect_attempt = 0;lines 185–185post_event(name, old, entry.status);lines 186–186logger->warn("Server '{}' disconnected", name);lines 187–187attempt_reconnect(name, entry);lines 188–188return;lines 191–192◇ if (entry.status == "disconnected" || entry.status == "reconnecting")lines 193–193attempt_reconnect(name, entry);lines 194–194return;lines 198–198◇ if (health_check_interval_ms_ > 0)lines 199–200entry.next_action = std::chrono::steady_clock::now() + std::chrono::milliseconds(health_check_interval_ms_);lines 201–201elselines 202–203entry.next_action = std::chrono::steady_clock::now() + std::chrono::seconds(5);

Requirements

REQ-MCP-025

Flows

monitor_loop

void entropic::HealthMonitor::monitor_loop()

Monitor loop (background thread).

src/mcp/health_monitor.cpp:143

Source · 19 lines
void HealthMonitor::monitor_loop() {
    constexpr auto poll_interval = std::chrono::milliseconds(500);

    while (running_) {
        {
            std::lock_guard<std::mutex> lock(watched_mutex_);
            auto now = std::chrono::steady_clock::now();
            for (auto& [name, entry] : watched_) {
                if (now >= entry.next_action) {
                    check_server(name, entry);
                }
            }
        }

        std::unique_lock<std::mutex> lock(wake_mutex_);
        wake_cv_.wait_for(lock, poll_interval,
                          [this] { return !running_.load(); });
    }
}
Activity · 9 steps
lines 144–144constexpr auto poll_interval = std::chrono::milliseconds(500);lines 146–146◇ while (running_)lines 148–148std::lock_guard<std::mutex> lock(watched_mutex_);lines 149–149auto now = std::chrono::steady_clock::now();lines 150–150◇ for (auto& [name, entry] : watched_)lines 151–151◇ if (now >= entry.next_action)lines 152–152check_server(name, entry);lines 157–157std::unique_lock<std::mutex> lock(wake_mutex_);lines 158–159wake_cv_.wait_for(lock, poll_interval, [this] { return !running_.load(); });

Flows

on_reconnect_success

void entropic::HealthMonitor::on_reconnect_success(const std::string &name, WatchEntry &entry)

Handle a successful reconnect: refresh tools, mark connected, enqueue the event.

src/mcp/health_monitor.cpp:273

Source · 20 lines
void HealthMonitor::on_reconnect_success(const std::string& name,
                                         WatchEntry& entry) {
    auto [added, removed] = entry.client->refresh_tools();
    entry.status = "connected";
    entry.reconnect_attempt = 0;

    HealthEvent evt;
    evt.server_name = name;
    evt.old_status = "reconnecting";
    evt.new_status = "connected";
    evt.added_tools = std::move(added);
    evt.removed_tools = std::move(removed);
    {
        std::lock_guard<std::mutex> lock(event_mutex_);
        event_queue_.push_back(std::move(evt));
    }

    logger->info("Server '{}' reconnected", name);
    entry.next_action = std::chrono::steady_clock::now();
}
Activity · 13 steps
lines 275–275auto [added, removed] = entry.client->refresh_tools();lines 276–276entry.status = "connected";lines 277–277entry.reconnect_attempt = 0;lines 279–279HealthEvent evt;lines 280–280evt.server_name = name;lines 281–281evt.old_status = "reconnecting";lines 282–282evt.new_status = "connected";lines 283–283evt.added_tools = std::move(added);lines 284–284evt.removed_tools = std::move(removed);lines 286–286std::lock_guard<std::mutex> lock(event_mutex_);lines 287–287event_queue_.push_back(std::move(evt));lines 290–290logger->info("Server '{}' reconnected", name);lines 291–291entry.next_action = std::chrono::steady_clock::now();

Requirements

REQ-MCP-025

post_event

void entropic::HealthMonitor::post_event(const std::string &name, const std::string &old_status, const std::string &new_status)

Post a status change event to the queue.

src/mcp/health_monitor.cpp:306

Source · 14 lines
void HealthMonitor::post_event(
    const std::string& name,
    const std::string& old_status,
    const std::string& new_status) {

    HealthEvent evt;
    evt.server_name = name;
    evt.old_status = old_status;
    evt.new_status = new_status;
    {
        std::lock_guard<std::mutex> lock(event_mutex_);
        event_queue_.push_back(std::move(evt));
    }
}
Activity · 6 steps
lines 311–311HealthEvent evt;lines 312–312evt.server_name = name;lines 313–313evt.old_status = old_status;lines 314–314evt.new_status = new_status;lines 316–316std::lock_guard<std::mutex> lock(event_mutex_);lines 317–317event_queue_.push_back(std::move(evt));

Requirements

REQ-MCP-025

Flows

process_events

void entropic::HealthMonitor::process_events()

Drain event queue, invoke callbacks (call on engine thread).

src/mcp/health_monitor.cpp:124

Source · 13 lines
void HealthMonitor::process_events() {
    std::vector<HealthEvent> events;
    {
        std::lock_guard<std::mutex> lock(event_mutex_);
        events.swap(event_queue_);
    }

    for (const auto& event : events) {
        if (status_callback_) {
            status_callback_(event);
        }
    }
}
Activity · 6 steps
lines 125–125std::vector<HealthEvent> events;lines 127–127std::lock_guard<std::mutex> lock(event_mutex_);lines 128–128events.swap(event_queue_);lines 131–131◇ for (const auto& event : events)lines 132–132◇ if (status_callback_)lines 133–133status_callback_(event);

Requirements

REQ-MCP-025

start

void entropic::HealthMonitor::start()

Start the monitoring thread.

src/mcp/health_monitor.cpp:87

Source · 9 lines
void HealthMonitor::start() {
    if (running_) {
        return;
    }
    running_ = true;
    monitor_thread_ = std::thread(
        &HealthMonitor::monitor_loop, this);
    logger->info("Health monitor started");
}
Activity · 5 steps
lines 88–88◇ if (running_)lines 89–89return;lines 91–91running_ = true;lines 92–93monitor_thread_ = std::thread( &HealthMonitor::monitor_loop, this);lines 94–94logger->info("Health monitor started");

Requirements

REQ-MCP-025

watch

void entropic::HealthMonitor::watch(const std::string &name, ExternalMCPClient *client)

Start monitoring a server.

src/mcp/health_monitor.cpp:44

Source · 15 lines
void HealthMonitor::watch(
    const std::string& name,
    ExternalMCPClient* client) {

    std::lock_guard<std::mutex> lock(watched_mutex_);
    WatchEntry entry;
    entry.client = client;
    entry.status = client->is_connected() ? "connected" : "disconnected";
    entry.reconnect_attempt = 0;
    entry.next_action = std::chrono::steady_clock::now();
    watched_[name] = entry;

    logger->info("Watching server '{}'", name);
    wake_cv_.notify_one();
}
Activity · 9 steps
lines 48–48std::lock_guard<std::mutex> lock(watched_mutex_);lines 49–49WatchEntry entry;lines 50–50entry.client = client;lines 51–51entry.status = client->is_connected() ? "connected" : "disconnected";lines 52–52entry.reconnect_attempt = 0;lines 53–53entry.next_action = std::chrono::steady_clock::now();lines 54–54watched_[name] = entry;lines 56–56logger->info("Watching server '{}'", name);lines 57–57wake_cv_.notify_one();

Requirements

REQ-MCP-025