Skip to content

Sends a heartbeat (ping) to all connected clients to keep connection… #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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 @@ -229,6 +229,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() {
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