Skip to content

fix(client): sendMessage() should fail immediately when connect() fails #323

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 @@ -93,8 +93,8 @@ public class HttpClientSseClientTransport implements McpClientTransport {
/** Flag indicating if the transport is in closing state */
private volatile boolean isClosing = false;

/** Latch for coordinating endpoint discovery */
private final CountDownLatch closeLatch = new CountDownLatch(1);
/** Future for coordinating endpoint discovery */
private final CompletableFuture<Void> readyFuture = new CompletableFuture<>();

/** Holds the discovered message endpoint URL */
private final AtomicReference<String> messageEndpoint = new AtomicReference<>();
Expand Down Expand Up @@ -353,7 +353,7 @@ public void onEvent(SseEvent event) {
if (ENDPOINT_EVENT_TYPE.equals(event.type())) {
String endpoint = event.data();
messageEndpoint.set(endpoint);
closeLatch.countDown();
readyFuture.complete(null);
future.complete(null);
}
else if (MESSAGE_EVENT_TYPE.equals(event.type())) {
Expand All @@ -376,6 +376,7 @@ public void onError(Throwable error) {
logger.error("SSE connection error", error);
future.completeExceptionally(error);
}
readyFuture.cancel(true);
}
});

Expand All @@ -399,11 +400,8 @@ public Mono<Void> sendMessage(JSONRPCMessage message) {
}

try {
if (!closeLatch.await(10, TimeUnit.SECONDS)) {
return Mono.error(new McpError("Failed to wait for the message endpoint"));
}
}
catch (InterruptedException e) {
readyFuture.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
return Mono.error(new McpError("Failed to wait for the message endpoint"));
}

Expand Down