@@ -84,8 +84,8 @@ public class McpAsyncClient {
84
84
private final ConcurrentHashMap <String , Root > roots ;
85
85
86
86
/**
87
- * MCP provides a standardized way for servers to request LLM sampling (“ completions”
88
- * or “ generations” ) from language models via clients. This flow allows clients to
87
+ * MCP provides a standardized way for servers to request LLM sampling (" completions"
88
+ * or " generations" ) from language models via clients. This flow allows clients to
89
89
* maintain control over model access, selection, and permissions while enabling
90
90
* servers to leverage AI capabilities—with no server API keys necessary. Servers can
91
91
* request text or image-based interactions and optionally include context from MCP
@@ -108,10 +108,13 @@ public class McpAsyncClient {
108
108
* timeout.
109
109
* @param transport the transport to use.
110
110
* @param requestTimeout the session request-response timeout.
111
- * @param rootsListProviders the list of suppliers that provide the list of roots
112
- * backing the roots list request.
113
- * @param rootsListChangedNotification whether the client supports roots/list_changed
114
- * notification.
111
+ * @param clientInfo the client implementation information.
112
+ * @param clientCapabilities the client capabilities.
113
+ * @param roots the roots.
114
+ * @param toolsChangeConsumers the tools change consumers.
115
+ * @param resourcesChangeConsumers the resources change consumers.
116
+ * @param promptsChangeConsumers the prompts change consumers.
117
+ * @param samplingHandler the sampling handler.
115
118
*/
116
119
public McpAsyncClient (McpTransport transport , Duration requestTimeout , Implementation clientInfo ,
117
120
ClientCapabilities clientCapabilities , Map <String , Root > roots ,
@@ -215,9 +218,9 @@ public McpAsyncClient(McpTransport transport, Duration requestTimeout, Implement
215
218
*/
216
219
public Mono <McpSchema .InitializeResult > initialize () {
217
220
McpSchema .InitializeRequest initializeRequest = new McpSchema .InitializeRequest (// @formatter:off
218
- McpSchema .LATEST_PROTOCOL_VERSION ,
219
- this .clientCapabilities ,
220
- this .clientInfo ); // @formatter:on
221
+ McpSchema .LATEST_PROTOCOL_VERSION ,
222
+ this .clientCapabilities ,
223
+ this .clientInfo ); // @formatter:on
221
224
222
225
Mono <McpSchema .InitializeResult > result = this .mcpSession .sendRequest ("initialize" , initializeRequest ,
223
226
new TypeReference <McpSchema .InitializeResult >() {
@@ -239,10 +242,17 @@ public Mono<McpSchema.InitializeResult> initialize() {
239
242
});
240
243
}
241
244
245
+ /**
246
+ * Closes the client connection immediately.
247
+ */
242
248
public void close () {
243
249
this .mcpSession .close ();
244
250
}
245
251
252
+ /**
253
+ * Gracefully closes the client connection.
254
+ * @return A Mono that completes when the connection is closed
255
+ */
246
256
public Mono <Void > closeGracefully () {
247
257
return this .mcpSession .closeGracefully ();
248
258
}
@@ -252,7 +262,8 @@ public Mono<Void> closeGracefully() {
252
262
// --------------------------
253
263
254
264
/**
255
- * Send a synchronous ping request.
265
+ * Sends a ping request to the server.
266
+ * @return A Mono that completes with the server's ping response
256
267
*/
257
268
public Mono <Object > ping () {
258
269
return this .mcpSession .sendRequest ("ping" , null , new TypeReference <Object >() {
@@ -262,6 +273,11 @@ public Mono<Object> ping() {
262
273
// --------------------------
263
274
// Roots
264
275
// --------------------------
276
+ /**
277
+ * Adds a new root to the client's root list.
278
+ * @param root The root to add
279
+ * @return A Mono that completes when the root is added and notifications are sent
280
+ */
265
281
public Mono <Void > addRoot (Root root ) {
266
282
267
283
if (root == null ) {
@@ -286,6 +302,11 @@ public Mono<Void> addRoot(Root root) {
286
302
return Mono .empty ();
287
303
}
288
304
305
+ /**
306
+ * Removes a root from the client's root list.
307
+ * @param rootUri The URI of the root to remove
308
+ * @return A Mono that completes when the root is removed and notifications are sent
309
+ */
289
310
public Mono <Void > removeRoot (String rootUri ) {
290
311
291
312
if (rootUri == null ) {
@@ -309,8 +330,9 @@ public Mono<Void> removeRoot(String rootUri) {
309
330
}
310
331
311
332
/**
312
- * Manually, send a roots/list_changed notification. The addRoot and removeRoot
333
+ * Manually sends a roots/list_changed notification. The addRoot and removeRoot
313
334
* methods automatically send the roots/list_changed notification.
335
+ * @return A Mono that completes when the notification is sent
314
336
*/
315
337
public Mono <Void > rootsListChangedNotification () {
316
338
return this .mcpSession .sendNotification ("notifications/roots/list_changed" );
@@ -436,16 +458,16 @@ public Mono<Void> handle(Object params) {
436
458
437
459
/**
438
460
* Send a resources/list request.
439
- * @return the list of resources result.
461
+ * @return A Mono that completes with the list of resources result
440
462
*/
441
463
public Mono <McpSchema .ListResourcesResult > listResources () {
442
464
return this .listResources (null );
443
465
}
444
466
445
467
/**
446
468
* Send a resources/list request.
447
- * @param cursor the cursor
448
- * @return the list of resources result.
469
+ * @param cursor the cursor for pagination
470
+ * @return A Mono that completes with the list of resources result
449
471
*/
450
472
public Mono <McpSchema .ListResourcesResult > listResources (String cursor ) {
451
473
return this .mcpSession .sendRequest ("resources/list" , new McpSchema .PaginatedRequest (cursor ),
@@ -455,16 +477,16 @@ public Mono<McpSchema.ListResourcesResult> listResources(String cursor) {
455
477
/**
456
478
* Send a resources/read request.
457
479
* @param resource the resource to read
458
- * @return the resource content.
480
+ * @return A Mono that completes with the resource content
459
481
*/
460
482
public Mono <McpSchema .ReadResourceResult > readResource (McpSchema .Resource resource ) {
461
483
return this .readResource (new McpSchema .ReadResourceRequest (resource .uri ()));
462
484
}
463
485
464
486
/**
465
487
* Send a resources/read request.
466
- * @param readResourceRequest the read resource request.
467
- * @return the resource content.
488
+ * @param readResourceRequest the read resource request
489
+ * @return A Mono that completes with the resource content
468
490
*/
469
491
public Mono <McpSchema .ReadResourceResult > readResource (McpSchema .ReadResourceRequest readResourceRequest ) {
470
492
return this .mcpSession .sendRequest ("resources/read" , readResourceRequest , READ_RESOURCE_RESULT_TYPE_REF );
@@ -475,7 +497,7 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceReq
475
497
* templates. Arguments may be auto-completed through the completion API.
476
498
*
477
499
* Request a list of resource templates the server has.
478
- * @return the list of resource templates result.
500
+ * @return A Mono that completes with the list of resource templates result
479
501
*/
480
502
public Mono <McpSchema .ListResourceTemplatesResult > listResourceTemplates () {
481
503
return this .listResourceTemplates (null );
@@ -486,8 +508,8 @@ public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates() {
486
508
* templates. Arguments may be auto-completed through the completion API.
487
509
*
488
510
* Request a list of resource templates the server has.
489
- * @param cursor the cursor
490
- * @return the list of resource templates result.
511
+ * @param cursor the cursor for pagination
512
+ * @return A Mono that completes with the list of resource templates result
491
513
*/
492
514
public Mono <McpSchema .ListResourceTemplatesResult > listResourceTemplates (String cursor ) {
493
515
return this .mcpSession .sendRequest ("resources/templates/list" , new McpSchema .PaginatedRequest (cursor ),
@@ -496,7 +518,8 @@ public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String
496
518
497
519
/**
498
520
* List Changed Notification. When the list of available resources changes, servers
499
- * that declared the listChanged capability SHOULD send a notification:
521
+ * that declared the listChanged capability SHOULD send a notification.
522
+ * @return A Mono that completes when the notification is sent
500
523
*/
501
524
public Mono <Void > sendResourcesListChanged () {
502
525
return this .mcpSession .sendNotification ("notifications/resources/list_changed" );
@@ -509,7 +532,8 @@ public Mono<Void> sendResourcesListChanged() {
509
532
*
510
533
* Send a resources/subscribe request.
511
534
* @param subscribeRequest the subscribe request contains the uri of the resource to
512
- * subscribe to.
535
+ * subscribe to
536
+ * @return A Mono that completes when the subscription is complete
513
537
*/
514
538
public Mono <Void > subscribeResource (McpSchema .SubscribeRequest subscribeRequest ) {
515
539
return this .mcpSession .sendRequest ("resources/subscribe" , subscribeRequest , VOID_TYPE_REFERENCE );
@@ -518,7 +542,8 @@ public Mono<Void> subscribeResource(McpSchema.SubscribeRequest subscribeRequest)
518
542
/**
519
543
* Send a resources/unsubscribe request.
520
544
* @param unsubscribeRequest the unsubscribe request contains the uri of the resource
521
- * to unsubscribe from.
545
+ * to unsubscribe from
546
+ * @return A Mono that completes when the unsubscription is complete
522
547
*/
523
548
public Mono <Void > unsubscribeResource (McpSchema .UnsubscribeRequest unsubscribeRequest ) {
524
549
return this .mcpSession .sendRequest ("resources/unsubscribe" , unsubscribeRequest , VOID_TYPE_REFERENCE );
@@ -554,25 +579,25 @@ public Mono<Void> handle(Object params) {
554
579
555
580
/**
556
581
* List all available prompts.
557
- * @return the list of prompts result.
582
+ * @return A Mono that completes with the list of prompts result
558
583
*/
559
584
public Mono <ListPromptsResult > listPrompts () {
560
585
return this .listPrompts (null );
561
586
}
562
587
563
588
/**
564
589
* List all available prompts.
565
- * @param cursor the cursor
566
- * @return the list of prompts result.
590
+ * @param cursor the cursor for pagination
591
+ * @return A Mono that completes with the list of prompts result
567
592
*/
568
593
public Mono <ListPromptsResult > listPrompts (String cursor ) {
569
594
return this .mcpSession .sendRequest ("prompts/list" , new PaginatedRequest (cursor ), LIST_PROMPTS_RESULT_TYPE_REF );
570
595
}
571
596
572
597
/**
573
598
* Get a prompt by its id.
574
- * @param getPromptRequest the get prompt request.
575
- * @return the get prompt result.
599
+ * @param getPromptRequest the get prompt request
600
+ * @return A Mono that completes with the get prompt result
576
601
*/
577
602
public Mono <GetPromptResult > getPrompt (GetPromptRequest getPromptRequest ) {
578
603
return this .mcpSession .sendRequest ("prompts/get" , getPromptRequest , GET_PROMPT_RESULT_TYPE_REF );
@@ -582,6 +607,7 @@ public Mono<GetPromptResult> getPrompt(GetPromptRequest getPromptRequest) {
582
607
* (Server) An optional notification from the server to the client, informing it that
583
608
* the list of prompts it offers has changed. This may be issued by servers without
584
609
* any previous subscription from the client.
610
+ * @return A Mono that completes when the notification is sent
585
611
*/
586
612
public Mono <Void > promptListChangedNotification () {
587
613
return this .mcpSession .sendNotification ("notifications/prompts/list_changed" );
0 commit comments