Skip to content

Commit 4aeeb1f

Browse files
committed
refactor: make listRoots() return unmodifiable list and add comprehensive tests
- Refactor McpAsyncServerExchange.listRoots() to return Collections.unmodifiableList - Fix typo in variable name (allRootssResult -> allRootsResult) - Improve code formatting and readability with ternary operator - Add comprehensive test suite for McpAsyncServerExchange covering: - listRoots() pagination scenarios and edge cases - Logging notification with level filtering - Elicitation creation with various capabilities - Message creation with sampling capabilities - Error handling and validation scenarios Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent 555c16a commit 4aeeb1f

File tree

2 files changed

+710
-9
lines changed

2 files changed

+710
-9
lines changed

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package io.modelcontextprotocol.server;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89

910
import com.fasterxml.jackson.core.type.TypeReference;
1011
import io.modelcontextprotocol.spec.McpError;
@@ -129,15 +130,18 @@ public Mono<McpSchema.ElicitResult> createElicitation(McpSchema.ElicitRequest el
129130
*/
130131
public Mono<McpSchema.ListRootsResult> listRoots() {
131132

132-
return this.listRoots(McpSchema.FIRST_PAGE).expand(result -> {
133-
if (result.nextCursor() != null) {
134-
return this.listRoots(result.nextCursor());
135-
}
136-
return Mono.empty();
137-
}).reduce(new McpSchema.ListRootsResult(new ArrayList<>(), null), (allRootssResult, result) -> {
138-
allRootssResult.roots().addAll(result.roots());
139-
return allRootssResult;
140-
});
133+
// @formatter:off
134+
return this.listRoots(McpSchema.FIRST_PAGE)
135+
.expand(result -> (result.nextCursor() != null) ?
136+
this.listRoots(result.nextCursor()) : Mono.empty())
137+
.reduce(new McpSchema.ListRootsResult(new ArrayList<>(), null),
138+
(allRootsResult, result) -> {
139+
allRootsResult.roots().addAll(result.roots());
140+
return allRootsResult;
141+
})
142+
.map(result -> new McpSchema.ListRootsResult(Collections.unmodifiableList(result.roots()),
143+
result.nextCursor()));
144+
// @formatter:on
141145
}
142146

143147
/**

0 commit comments

Comments
 (0)