|
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,9 +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); |
273 | | - |
| 271 | + this.sessionSupplier = ctx -> new McpClientSession(requestTimeout, transport, requestHandlers, |
| 272 | + notificationHandlers, con -> con.contextWrite(ctx)); |
274 | 273 | } |
275 | 274 |
|
276 | 275 | private void handleException(Throwable t) { |
@@ -401,9 +400,8 @@ public Mono<McpSchema.InitializeResult> initialize() { |
401 | 400 | return withSession("by explicit API call", init -> Mono.just(init.get())); |
402 | 401 | } |
403 | 402 |
|
404 | | - private Mono<McpSchema.InitializeResult> doInitialize(Initialization initialization) { |
405 | | - |
406 | | - initialization.setMcpClientSession(this.sessionSupplier.get()); |
| 403 | + private Mono<McpSchema.InitializeResult> doInitialize(Initialization initialization, ContextView ctx) { |
| 404 | + initialization.setMcpClientSession(this.sessionSupplier.apply(ctx)); |
407 | 405 |
|
408 | 406 | McpClientSession mcpClientSession = initialization.mcpSession(); |
409 | 407 |
|
@@ -493,14 +491,14 @@ Mono<Void> closeGracefully() { |
493 | 491 | * @return A Mono that completes with the result of the operation |
494 | 492 | */ |
495 | 493 | private <T> Mono<T> withSession(String actionName, Function<Initialization, Mono<T>> operation) { |
496 | | - return Mono.defer(() -> { |
| 494 | + return Mono.deferContextual(ctx -> { |
497 | 495 | Initialization newInit = Initialization.create(); |
498 | 496 | Initialization previous = this.initializationRef.compareAndExchange(null, newInit); |
499 | 497 |
|
500 | 498 | boolean needsToInitialize = previous == null; |
501 | 499 | logger.debug(needsToInitialize ? "Initialization process started" : "Joining previous initialization"); |
502 | 500 |
|
503 | | - Mono<McpSchema.InitializeResult> initializationJob = needsToInitialize ? doInitialize(newInit) |
| 501 | + Mono<McpSchema.InitializeResult> initializationJob = needsToInitialize ? doInitialize(newInit, ctx) |
504 | 502 | : previous.await(); |
505 | 503 |
|
506 | 504 | return initializationJob.map(initializeResult -> this.initializationRef.get()) |
|
0 commit comments