|
11 | 11 | import java.util.concurrent.ConcurrentHashMap;
|
12 | 12 | import java.util.concurrent.atomic.AtomicReference;
|
13 | 13 | import java.util.function.Function;
|
14 |
| -import java.util.function.Supplier; |
15 | 14 |
|
16 | 15 | import org.slf4j.Logger;
|
17 | 16 | import org.slf4j.LoggerFactory;
|
|
42 | 41 | import reactor.core.publisher.Flux;
|
43 | 42 | import reactor.core.publisher.Mono;
|
44 | 43 | import reactor.core.publisher.Sinks;
|
| 44 | +import reactor.util.context.ContextView; |
45 | 45 |
|
46 | 46 | /**
|
47 | 47 | * The Model Context Protocol (MCP) client implementation that provides asynchronous
|
@@ -161,7 +161,7 @@ public class McpAsyncClient {
|
161 | 161 | * The MCP session supplier that manages bidirectional JSON-RPC communication between
|
162 | 162 | * clients and servers.
|
163 | 163 | */
|
164 |
| - private final Supplier<McpClientSession> sessionSupplier; |
| 164 | + private final Function<ContextView, McpClientSession> sessionSupplier; |
165 | 165 |
|
166 | 166 | /**
|
167 | 167 | * Create a new McpAsyncClient with the given transport and session request-response
|
@@ -268,8 +268,8 @@ public class McpAsyncClient {
|
268 | 268 | asyncLoggingNotificationHandler(loggingConsumersFinal));
|
269 | 269 |
|
270 | 270 | this.transport.setExceptionHandler(this::handleException);
|
271 |
| - this.sessionSupplier = () -> new McpClientSession(requestTimeout, transport, requestHandlers, |
272 |
| - notificationHandlers); |
| 271 | + this.sessionSupplier = ctx -> new McpClientSession(requestTimeout, transport, requestHandlers, |
| 272 | + notificationHandlers, con -> con.contextWrite(ctx)); |
273 | 273 |
|
274 | 274 | }
|
275 | 275 |
|
@@ -401,9 +401,8 @@ public Mono<McpSchema.InitializeResult> initialize() {
|
401 | 401 | return withSession("by explicit API call", init -> Mono.just(init.get()));
|
402 | 402 | }
|
403 | 403 |
|
404 |
| - private Mono<McpSchema.InitializeResult> doInitialize(Initialization initialization) { |
405 |
| - |
406 |
| - initialization.setMcpClientSession(this.sessionSupplier.get()); |
| 404 | + private Mono<McpSchema.InitializeResult> doInitialize(Initialization initialization, ContextView ctx) { |
| 405 | + initialization.setMcpClientSession(this.sessionSupplier.apply(ctx)); |
407 | 406 |
|
408 | 407 | McpClientSession mcpClientSession = initialization.mcpSession();
|
409 | 408 |
|
@@ -493,14 +492,14 @@ Mono<Void> closeGracefully() {
|
493 | 492 | * @return A Mono that completes with the result of the operation
|
494 | 493 | */
|
495 | 494 | private <T> Mono<T> withSession(String actionName, Function<Initialization, Mono<T>> operation) {
|
496 |
| - return Mono.defer(() -> { |
| 495 | + return Mono.deferContextual(ctx -> { |
497 | 496 | Initialization newInit = Initialization.create();
|
498 | 497 | Initialization previous = this.initializationRef.compareAndExchange(null, newInit);
|
499 | 498 |
|
500 | 499 | boolean needsToInitialize = previous == null;
|
501 | 500 | logger.debug(needsToInitialize ? "Initialization process started" : "Joining previous initialization");
|
502 | 501 |
|
503 |
| - Mono<McpSchema.InitializeResult> initializationJob = needsToInitialize ? doInitialize(newInit) |
| 502 | + Mono<McpSchema.InitializeResult> initializationJob = needsToInitialize ? doInitialize(newInit, ctx) |
504 | 503 | : previous.await();
|
505 | 504 |
|
506 | 505 | return initializationJob.map(initializeResult -> this.initializationRef.get())
|
|
0 commit comments