13
13
import java .util .concurrent .atomic .AtomicBoolean ;
14
14
import java .util .function .Function ;
15
15
16
+ import io .modelcontextprotocol .logger .McpLogger ;
16
17
import io .modelcontextprotocol .schema .McpType ;
17
18
import io .modelcontextprotocol .session .McpClientSession ;
18
19
import io .modelcontextprotocol .session .McpClientSession .NotificationHandler ;
33
34
import io .modelcontextprotocol .spec .McpTransport ;
34
35
import io .modelcontextprotocol .util .Assert ;
35
36
import io .modelcontextprotocol .util .Utils ;
36
- import org .slf4j .Logger ;
37
- import org .slf4j .LoggerFactory ;
38
37
import reactor .core .publisher .Flux ;
39
38
import reactor .core .publisher .Mono ;
40
39
import reactor .core .publisher .Sinks ;
78
77
*/
79
78
public class McpAsyncClient implements McpClient {
80
79
81
- private static final Logger logger = LoggerFactory .getLogger (McpAsyncClient .class );
82
-
83
80
private static final McpType <Void > VOID_TYPE_REFERENCE = McpType .of (Void .class );
84
81
85
82
protected final Sinks .One <McpSchema .InitializeResult > initializedSink = Sinks .one ();
@@ -145,6 +142,8 @@ public class McpAsyncClient implements McpClient {
145
142
*/
146
143
private final McpTransport transport ;
147
144
145
+ private final McpLogger logger ;
146
+
148
147
/**
149
148
* Supported protocol versions.
150
149
*/
@@ -159,7 +158,7 @@ public class McpAsyncClient implements McpClient {
159
158
* @param features the MCP Client supported features.
160
159
*/
161
160
McpAsyncClient (McpClientTransport transport , Duration requestTimeout , Duration initializationTimeout ,
162
- McpClientFeatures .Async features ) {
161
+ McpClientFeatures .Async features , McpLogger logger ) {
163
162
164
163
Assert .notNull (transport , "Transport must not be null" );
165
164
Assert .notNull (requestTimeout , "Request timeout must not be null" );
@@ -170,6 +169,7 @@ public class McpAsyncClient implements McpClient {
170
169
this .transport = transport ;
171
170
this .roots = new ConcurrentHashMap <>(features .roots ());
172
171
this .initializationTimeout = initializationTimeout ;
172
+ this .logger = logger ;
173
173
174
174
// Request Handlers
175
175
Map <String , RequestHandler <?>> requestHandlers = new HashMap <>();
@@ -194,7 +194,7 @@ public class McpAsyncClient implements McpClient {
194
194
// Tools Change Notification
195
195
List <Function <List <McpSchema .Tool >, Mono <Void >>> toolsChangeConsumersFinal = new ArrayList <>();
196
196
toolsChangeConsumersFinal
197
- .add ((notification ) -> Mono .fromRunnable (() -> logger .debug ("Tools changed: {}" , notification )));
197
+ .add ((notification ) -> Mono .fromRunnable (() -> logger .debug ("Tools changed: %s" . formatted ( notification ) )));
198
198
199
199
if (!Utils .isEmpty (features .toolsChangeConsumers ())) {
200
200
toolsChangeConsumersFinal .addAll (features .toolsChangeConsumers ());
@@ -204,8 +204,8 @@ public class McpAsyncClient implements McpClient {
204
204
205
205
// Resources Change Notification
206
206
List <Function <List <McpSchema .Resource >, Mono <Void >>> resourcesChangeConsumersFinal = new ArrayList <>();
207
- resourcesChangeConsumersFinal
208
- .add (( notification ) -> Mono . fromRunnable (() -> logger .debug ("Resources changed: {}" , notification )));
207
+ resourcesChangeConsumersFinal . add (( notification ) -> Mono
208
+ .fromRunnable (() -> logger .debug ("Resources changed: %s" . formatted ( notification ) )));
209
209
210
210
if (!Utils .isEmpty (features .resourcesChangeConsumers ())) {
211
211
resourcesChangeConsumersFinal .addAll (features .resourcesChangeConsumers ());
@@ -216,8 +216,8 @@ public class McpAsyncClient implements McpClient {
216
216
217
217
// Prompts Change Notification
218
218
List <Function <List <McpSchema .Prompt >, Mono <Void >>> promptsChangeConsumersFinal = new ArrayList <>();
219
- promptsChangeConsumersFinal
220
- . add (( notification ) -> Mono .fromRunnable (() -> logger .debug ("Prompts changed: {}" , notification )));
219
+ promptsChangeConsumersFinal . add (
220
+ ( notification ) -> Mono .fromRunnable (() -> logger .debug ("Prompts changed: %s" . formatted ( notification ) )));
221
221
if (!Utils .isEmpty (features .promptsChangeConsumers ())) {
222
222
promptsChangeConsumersFinal .addAll (features .promptsChangeConsumers ());
223
223
}
@@ -226,7 +226,8 @@ public class McpAsyncClient implements McpClient {
226
226
227
227
// Utility Logging Notification
228
228
List <Function <LoggingMessageNotification , Mono <Void >>> loggingConsumersFinal = new ArrayList <>();
229
- loggingConsumersFinal .add ((notification ) -> Mono .fromRunnable (() -> logger .debug ("Logging: {}" , notification )));
229
+ loggingConsumersFinal
230
+ .add ((notification ) -> Mono .fromRunnable (() -> logger .debug ("Logging: %s" .formatted (notification ))));
230
231
if (!Utils .isEmpty (features .loggingConsumers ())) {
231
232
loggingConsumersFinal .addAll (features .loggingConsumers ());
232
233
}
@@ -344,9 +345,9 @@ public Mono<McpSchema.InitializeResult> initialize() {
344
345
this .serverInstructions = initializeResult .instructions ();
345
346
this .serverInfo = initializeResult .serverInfo ();
346
347
347
- logger .info ("Server response with Protocol: {} , Capabilities: {} , Info: {} and Instructions {}" ,
348
+ logger .info ("Server response with Protocol: %s , Capabilities: %s , Info: %s and Instructions %s" . formatted (
348
349
initializeResult .protocolVersion (), initializeResult .capabilities (), initializeResult .serverInfo (),
349
- initializeResult .instructions ());
350
+ initializeResult .instructions ())) ;
350
351
351
352
if (!this .protocolVersions .contains (initializeResult .protocolVersion ())) {
352
353
return Mono .error (new McpError (
@@ -414,7 +415,7 @@ public Mono<Void> addRoot(Root root) {
414
415
415
416
this .roots .put (root .uri (), root );
416
417
417
- logger .debug ("Added root: {}" , root );
418
+ logger .debug ("Added root: %s" . formatted ( root ) );
418
419
419
420
if (this .clientCapabilities .roots ().listChanged ()) {
420
421
if (this .isInitialized ()) {
@@ -445,7 +446,7 @@ public Mono<Void> removeRoot(String rootUri) {
445
446
Root removed = this .roots .remove (rootUri );
446
447
447
448
if (removed != null ) {
448
- logger .debug ("Removed Root: {}" , rootUri );
449
+ logger .debug ("Removed Root: %s" . formatted ( rootUri ) );
449
450
if (this .clientCapabilities .roots ().listChanged ()) {
450
451
if (this .isInitialized ()) {
451
452
return this .rootsListChangedNotification ();
0 commit comments