Skip to content

Fix: Handle SSE ping messages in WebFluxSseClientTransport #272

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 @@ -3,10 +3,6 @@
*/
package io.modelcontextprotocol.client.transport;

import java.io.IOException;
import java.util.function.BiConsumer;
import java.util.function.Function;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.spec.McpClientTransport;
Expand All @@ -16,6 +12,10 @@
import io.modelcontextprotocol.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -25,10 +25,9 @@
import reactor.util.retry.Retry;
import reactor.util.retry.Retry.RetrySignal;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.client.WebClient;
import java.io.IOException;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
* Server-Sent Events (SSE) implementation of the
Expand Down Expand Up @@ -166,6 +165,10 @@ public WebFluxSseClientTransport(WebClient.Builder webClientBuilder, ObjectMappe
this.sseEndpoint = sseEndpoint;
}

private boolean isPingEvent(ServerSentEvent<String> event) {
return event.comment() != null && event.comment().startsWith("ping");
}

/**
* Establishes a connection to the MCP server using Server-Sent Events (SSE). This
* method initiates the SSE connection and sets up the message processing pipeline.
Expand Down Expand Up @@ -212,6 +215,10 @@ else if (MESSAGE_EVENT_TYPE.equals(event.event())) {
s.error(ioException);
}
}
else if (isPingEvent(event)) {
logger.debug("Received SSE ping message");
s.complete();
}
else {
s.error(new McpError("Received unrecognized SSE event type: " + event.event()));
}
Expand Down