Skip to content

fix: improve SSE event handling to gracefully ignore unrecognized events #423

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

Closed
Show file tree
Hide file tree
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 @@ -432,7 +432,8 @@ private Tuple2<Optional<String>, Iterable<McpSchema.JSONRPCMessage>> parse(Serve
}
}
else {
throw new McpError("Received unrecognized SSE event type: " + event.event());
logger.debug("Received SSE event with type: {}", event);
return Tuples.of(Optional.empty(), List.of());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ else if (MESSAGE_EVENT_TYPE.equals(event.event())) {
}
}
else {
s.error(new McpError("Received unrecognized SSE event type: " + event.event()));
logger.debug("Received unrecognized SSE event type: {}", event);
s.complete();
}
}).transform(handler)).subscribe();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

Expand Down Expand Up @@ -77,6 +78,11 @@ public int getInboundMessageCount() {
return inboundMessageCount.get();
}

public void simulateSseComment(String comment) {
events.tryEmitNext(ServerSentEvent.<String>builder().comment(comment).build());
inboundMessageCount.incrementAndGet();
}

public void simulateEndpointEvent(String jsonMessage) {
events.tryEmitNext(ServerSentEvent.<String>builder().event("endpoint").data(jsonMessage).build());
inboundMessageCount.incrementAndGet();
Expand Down Expand Up @@ -158,6 +164,27 @@ void testBuilderPattern() {
assertThatCode(() -> transport4.closeGracefully().block()).doesNotThrowAnyException();
}

@Test
void testCommentSseMessage() {
// If the line starts with a character (:) are comment lins and should be ingored
// https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation

CopyOnWriteArrayList<Throwable> droppedErrors = new CopyOnWriteArrayList<>();
reactor.core.publisher.Hooks.onErrorDropped(droppedErrors::add);

try {
// Simulate receiving the SSE comment line
transport.simulateSseComment("sse comment");

StepVerifier.create(transport.closeGracefully()).verifyComplete();

assertThat(droppedErrors).hasSize(0);
}
finally {
reactor.core.publisher.Hooks.resetOnErrorDropped();
}
}

@Test
void testMessageProcessing() {
// Create a test message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,8 @@ else if (MESSAGE_EVENT_TYPE.equals(responseEvent.sseEvent().event())) {
return Flux.just(message);
}
else {
logger.error("Received unrecognized SSE event type: {}",
responseEvent.sseEvent().event());
sink.error(new McpError(
"Received unrecognized SSE event type: " + responseEvent.sseEvent().event()));
logger.debug("Received unrecognized SSE event type: {}", responseEvent.sseEvent());
sink.success();
}
}
catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ private Mono<Disposable> reconnect(McpTransportStream<Disposable> stream) {
"Error parsing JSON-RPC message: " + responseEvent.sseEvent().data()));
}
}
else {
logger.debug("Received SSE event with type: {}", responseEvent.sseEvent());
return Flux.empty();
}
}
else if (statusCode == METHOD_NOT_ALLOWED) { // NotAllowed
logger.debug("The server does not support SSE streams, using request-response mode.");
Expand Down