Skip to content
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 @@ -387,7 +387,12 @@ public Mono<McpSchema.InitializeResult> initialize() {
return withSession("by explicit API call", init -> Mono.just(init.get()));
}

private Mono<McpSchema.InitializeResult> doInitialize(McpClientSession mcpClientSession) {
private Mono<McpSchema.InitializeResult> doInitialize(Initialization initializaiton) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in "initializaiton" -> should be initialization.


initializaiton.setMcpClientSession(this.sessionSupplier.get());

McpClientSession mcpClientSession = initializaiton.mcpSession();

String latestVersion = this.protocolVersions.get(this.protocolVersions.size() - 1);

McpSchema.InitializeRequest initializeRequest = new McpSchema.InitializeRequest(// @formatter:off
Expand All @@ -410,6 +415,9 @@ private Mono<McpSchema.InitializeResult> doInitialize(McpClientSession mcpClient

return mcpClientSession.sendNotification(McpSchema.METHOD_NOTIFICATION_INITIALIZED, null)
.thenReturn(initializeResult);
}).doOnNext(initializaiton::complete).onErrorResume(ex -> {
initializaiton.error(ex);
return Mono.error(ex);
});
}

Expand Down Expand Up @@ -477,15 +485,9 @@ private <T> Mono<T> withSession(String actionName, Function<Initialization, Mono

boolean needsToInitialize = previous == null;
logger.debug(needsToInitialize ? "Initialization process started" : "Joining previous initialization");
if (needsToInitialize) {
newInit.setMcpClientSession(this.sessionSupplier.get());
}

Mono<McpSchema.InitializeResult> initializationJob = needsToInitialize
? doInitialize(newInit.mcpSession()).doOnNext(newInit::complete).onErrorResume(ex -> {
newInit.error(ex);
return Mono.error(ex);
}) : previous.await();
Mono<McpSchema.InitializeResult> initializationJob = needsToInitialize ? doInitialize(newInit)
: previous.await();

return initializationJob.map(initializeResult -> this.initializationRef.get())
.timeout(this.initializationTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,27 @@ private String generateRequestId() {
public <T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef) {
String requestId = this.generateRequestId();

return Mono.deferContextual(ctx -> Mono.<McpSchema.JSONRPCResponse>create(sink -> {
return Mono.deferContextual(ctx -> Mono.<McpSchema.JSONRPCResponse>create(responseSink -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

responseSink vs resultSink still feel quite similar. I have a different proposal.
Sink from Mono.create shall be called pendingResponseSink.
Sink for handle operator shall be called deliveredResponseSink.
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

logger.debug("Sending message for method {}", method);
this.pendingResponses.put(requestId, sink);
this.pendingResponses.put(requestId, responseSink);
McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, method,
requestId, requestParams);
this.transport.sendMessage(jsonrpcRequest).contextWrite(ctx).subscribe(v -> {
}, error -> {
this.pendingResponses.remove(requestId);
sink.error(error);
responseSink.error(error);
});
})).timeout(this.requestTimeout).handle((jsonRpcResponse, sink) -> {
})).timeout(this.requestTimeout).handle((jsonRpcResponse, resultSink) -> {
if (jsonRpcResponse.error() != null) {
logger.error("Error handling request: {}", jsonRpcResponse.error());
sink.error(new McpError(jsonRpcResponse.error()));
resultSink.error(new McpError(jsonRpcResponse.error()));
}
else {
if (typeRef.getType().equals(Void.class)) {
sink.complete();
resultSink.complete();
}
else {
sink.next(this.transport.unmarshalFrom(jsonRpcResponse.result(), typeRef));
resultSink.next(this.transport.unmarshalFrom(jsonRpcResponse.result(), typeRef));
}
}
});
Expand Down