Skip to content

Commit e6559ca

Browse files
committed
fix McpAsyncClient#listTools prevent infinite recursion
Signed-off-by: lance <leehaut@gmail.com>
1 parent c39f515 commit e6559ca

File tree

2 files changed

+298
-287
lines changed

2 files changed

+298
-287
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ public Mono<Void> closeGracefully() {
402402
// --------------------------
403403
// Initialization
404404
// --------------------------
405+
405406
/**
406407
* The initialization phase should be the first interaction between client and server.
407408
* The client will ensure it happens in case it has not been explicitly called and in
@@ -448,6 +449,7 @@ public Mono<Object> ping() {
448449
// --------------------------
449450
// Roots
450451
// --------------------------
452+
451453
/**
452454
* Adds a new root to the client's root list.
453455
* @param root The root to add.
@@ -625,16 +627,13 @@ private McpSchema.CallToolResult validateToolResult(String toolName, McpSchema.C
625627
* @return A Mono that emits the list of all tools result
626628
*/
627629
public Mono<McpSchema.ListToolsResult> listTools() {
628-
return this.listTools(McpSchema.FIRST_PAGE)
629-
.expand(result -> {
630-
String next = result.nextCursor();
631-
return (next != null && !next.isEmpty()) ? this.listTools(next) : Mono.empty();
632-
})
633-
.reduce(new McpSchema.ListToolsResult(new ArrayList<>(), null), (allToolsResult, result) -> {
634-
allToolsResult.tools().addAll(result.tools());
635-
return allToolsResult;
636-
})
637-
.map(result -> new McpSchema.ListToolsResult(Collections.unmodifiableList(result.tools()), null));
630+
return this.listTools(McpSchema.FIRST_PAGE).expand(result -> {
631+
String next = result.nextCursor();
632+
return (next != null && !next.isEmpty()) ? this.listTools(next) : Mono.empty();
633+
}).reduce(new McpSchema.ListToolsResult(new ArrayList<>(), null), (allToolsResult, result) -> {
634+
allToolsResult.tools().addAll(result.tools());
635+
return allToolsResult;
636+
}).map(result -> new McpSchema.ListToolsResult(Collections.unmodifiableList(result.tools()), null));
638637
}
639638

640639
/**

0 commit comments

Comments
 (0)