Skip to content
Closed
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,28 @@ public Mono<Void> closeGracefully() {
.then();
}

/**
* Sends a heartbeat (ping) to all connected clients to keep connections alive. This
* method sends ping notifications to all active sessions without expecting a
* response, which helps prevent connection timeouts.
* @return A Mono that completes when heartbeat has been sent to all sessions
*/
public Mono<Void> sendHeartbeat() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a configuration to do the scheduling of sending

if (sessions.isEmpty()) {
logger.debug("No active sessions to send heartbeat to");
return Mono.empty();
}

logger.debug("Sending heartbeat to {} active sessions", sessions.size());

return Flux.fromIterable(sessions.values())
.flatMap(session -> session.sendNotification(McpSchema.METHOD_PING, null)
.doOnSuccess(v -> logger.trace("Heartbeat sent successfully to session {}", session.getId()))
.doOnError(e -> logger.warn("Heartbeat failed for session {}: {}", session.getId(), e.getMessage()))
.onErrorComplete()) // Continue with other sessions even if one fails
.then();
}

/**
* Returns the WebFlux router function that defines the transport's HTTP endpoints.
* This router function should be integrated into the application's web configuration.
Expand Down