13
13
import java .util .function .Function ;
14
14
import java .util .function .Supplier ;
15
15
16
+ import org .slf4j .Logger ;
17
+ import org .slf4j .LoggerFactory ;
18
+
16
19
import com .fasterxml .jackson .core .type .TypeReference ;
20
+
17
21
import io .modelcontextprotocol .spec .McpClientSession ;
18
22
import io .modelcontextprotocol .spec .McpClientSession .NotificationHandler ;
19
23
import io .modelcontextprotocol .spec .McpClientSession .RequestHandler ;
35
39
import io .modelcontextprotocol .spec .McpTransportSessionNotFoundException ;
36
40
import io .modelcontextprotocol .util .Assert ;
37
41
import io .modelcontextprotocol .util .Utils ;
38
- import org .slf4j .Logger ;
39
- import org .slf4j .LoggerFactory ;
40
42
import reactor .core .publisher .Flux ;
41
43
import reactor .core .publisher .Mono ;
42
44
import reactor .core .publisher .Sinks ;
@@ -656,7 +658,7 @@ public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToo
656
658
}
657
659
658
660
/**
659
- * Retrieves the list of all tools provided by the server.
661
+ * Retrieves the first page of tools provided by the server.
660
662
* @return A Mono that emits the list of tools result.
661
663
*/
662
664
public Mono <McpSchema .ListToolsResult > listTools () {
@@ -679,18 +681,26 @@ public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
679
681
});
680
682
}
681
683
682
- private NotificationHandler asyncToolsChangeNotificationHandler (
683
- List <Function <List <McpSchema .Tool >, Mono <Void >>> toolsChangeConsumers ) {
684
- // TODO: params are not used yet
685
- return params -> this .listTools ().expand (result -> {
684
+ /**
685
+ * Retrieves list of all tools provided by the server after handling pagination.
686
+ * @return A Mono that emits a list of all tools
687
+ */
688
+ public Mono <List <McpSchema .Tool >> listAllTools () {
689
+ return this .listTools ().expand (result -> {
686
690
if (result .nextCursor () != null ) {
687
691
return this .listTools (result .nextCursor ());
688
692
}
689
693
return Mono .empty ();
690
- }).reduce (new ArrayList <McpSchema . Tool >(), (allTools , result ) -> {
694
+ }).reduce (new ArrayList <>(), (allTools , result ) -> {
691
695
allTools .addAll (result .tools ());
692
696
return allTools ;
693
- })
697
+ });
698
+ }
699
+
700
+ private NotificationHandler asyncToolsChangeNotificationHandler (
701
+ List <Function <List <McpSchema .Tool >, Mono <Void >>> toolsChangeConsumers ) {
702
+ // TODO: params are not used yet
703
+ return params -> this .listAllTools ()
694
704
.flatMap (allTools -> Flux .fromIterable (toolsChangeConsumers )
695
705
.flatMap (consumer -> consumer .apply (allTools ))
696
706
.onErrorResume (error -> {
@@ -714,9 +724,9 @@ private NotificationHandler asyncToolsChangeNotificationHandler(
714
724
};
715
725
716
726
/**
717
- * Retrieves the list of all resources provided by the server. Resources represent any
718
- * kind of UTF-8 encoded data that an MCP server makes available to clients, such as
719
- * database records, API responses, log files, and more.
727
+ * Retrieves the first page of resources provided by the server. Resources represent
728
+ * any kind of UTF-8 encoded data that an MCP server makes available to clients, such
729
+ * as database records, API responses, log files, and more.
720
730
* @return A Mono that completes with the list of resources result.
721
731
* @see McpSchema.ListResourcesResult
722
732
* @see #readResource(McpSchema.Resource)
@@ -745,6 +755,26 @@ public Mono<McpSchema.ListResourcesResult> listResources(String cursor) {
745
755
});
746
756
}
747
757
758
+ /**
759
+ * Retrieves the list of all resources provided by the server. Resources represent any
760
+ * kind of UTF-8 encoded data that an MCP server makes available to clients, such as
761
+ * database records, API responses, log files, and more.
762
+ * @return A Mono that completes with list of all resources.
763
+ * @see McpSchema.Resource
764
+ * @see #readResource(McpSchema.Resource)
765
+ */
766
+ public Mono <List <McpSchema .Resource >> listAllResources () {
767
+ return this .listResources ().expand (result -> {
768
+ if (result .nextCursor () != null ) {
769
+ return this .listResources (result .nextCursor ());
770
+ }
771
+ return Mono .empty ();
772
+ }).reduce (new ArrayList <>(), (allResources , result ) -> {
773
+ allResources .addAll (result .resources ());
774
+ return allResources ;
775
+ });
776
+ }
777
+
748
778
/**
749
779
* Reads the content of a specific resource identified by the provided Resource
750
780
* object. This method fetches the actual data that the resource represents.
@@ -777,7 +807,7 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceReq
777
807
}
778
808
779
809
/**
780
- * Retrieves the list of all resource templates provided by the server. Resource
810
+ * Retrieves the first page of resource templates provided by the server. Resource
781
811
* templates allow servers to expose parameterized resources using URI templates,
782
812
* enabling dynamic resource access based on variable parameters.
783
813
* @return A Mono that completes with the list of resource templates result.
@@ -806,6 +836,25 @@ public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String
806
836
});
807
837
}
808
838
839
+ /**
840
+ * Retrieves the list of all resource templates provided by the server. Resource
841
+ * templates allow servers to expose parameterized resources using URI templates,
842
+ * enabling dynamic resource access based on variable parameters.
843
+ * @return A Mono that completes with the list of all resource templates.
844
+ * @see McpSchema.ResourceTemplate
845
+ */
846
+ public Mono <List <McpSchema .ResourceTemplate >> listAllResourceTemplates () {
847
+ return this .listResourceTemplates ().expand (result -> {
848
+ if (result .nextCursor () != null ) {
849
+ return this .listResourceTemplates (result .nextCursor ());
850
+ }
851
+ return Mono .empty ();
852
+ }).reduce (new ArrayList <>(), (allResourceTemplates , result ) -> {
853
+ allResourceTemplates .addAll (result .resourceTemplates ());
854
+ return allResourceTemplates ;
855
+ });
856
+ }
857
+
809
858
/**
810
859
* Subscribes to changes in a specific resource. When the resource changes on the
811
860
* server, the client will receive notifications through the resources change
@@ -855,7 +904,7 @@ private NotificationHandler asyncResourcesChangeNotificationHandler(
855
904
};
856
905
857
906
/**
858
- * Retrieves the list of all prompts provided by the server.
907
+ * Retrieves the first page of prompts provided by the server.
859
908
* @return A Mono that completes with the list of prompts result.
860
909
* @see McpSchema.ListPromptsResult
861
910
* @see #getPrompt(GetPromptRequest)
@@ -876,6 +925,24 @@ public Mono<ListPromptsResult> listPrompts(String cursor) {
876
925
.sendRequest (McpSchema .METHOD_PROMPT_LIST , new PaginatedRequest (cursor ), LIST_PROMPTS_RESULT_TYPE_REF ));
877
926
}
878
927
928
+ /**
929
+ * Retrieves the list of all prompts provided by the server.
930
+ * @return A Mono that completes with the list of all prompts.
931
+ * @see McpSchema.Prompt
932
+ * @see #getPrompt(GetPromptRequest)
933
+ */
934
+ public Mono <List <McpSchema .Prompt >> listAllPrompts () {
935
+ return this .listPrompts ().expand (result -> {
936
+ if (result .nextCursor () != null ) {
937
+ return this .listPrompts (result .nextCursor ());
938
+ }
939
+ return Mono .empty ();
940
+ }).reduce (new ArrayList <>(), (allPrompts , result ) -> {
941
+ allPrompts .addAll (result .prompts ());
942
+ return allPrompts ;
943
+ });
944
+ }
945
+
879
946
/**
880
947
* Retrieves a specific prompt by its ID. This provides the complete prompt template
881
948
* including all parameters and instructions for generating AI content.
0 commit comments