Skip to content
Closed
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,19 +3,20 @@
*/
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.micrometer.common.util.StringUtils;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpError;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage;
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 +26,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 +166,18 @@ public WebFluxSseClientTransport(WebClient.Builder webClientBuilder, ObjectMappe
this.sseEndpoint = sseEndpoint;
}

private boolean isCommentLine(ServerSentEvent<String> event) {
String id = event.id();
String type = event.event();
String data = event.data();
String comment = event.comment();

return StringUtils.isEmpty(id) &&
StringUtils.isEmpty(type) &&
StringUtils.isEmpty(data) &&
comment != null;
}

/**
* 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 +224,10 @@ else if (MESSAGE_EVENT_TYPE.equals(event.event())) {
s.error(ioException);
}
}
else if (isCommentLine(event)) {
logger.debug("Received SSE comment: {}", event.comment());
s.complete();
}
else {
s.error(new McpError("Received unrecognized SSE event type: " + event.event()));
}
Expand Down