Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

package io.modelcontextprotocol.server;

import java.util.ArrayList;
import java.util.Collections;

import com.fasterxml.jackson.core.type.TypeReference;
import io.modelcontextprotocol.spec.McpError;
import io.modelcontextprotocol.spec.McpSchema;
Expand Down Expand Up @@ -126,7 +129,19 @@ public Mono<McpSchema.ElicitResult> createElicitation(McpSchema.ElicitRequest el
* @return A Mono that emits the list of roots result.
*/
public Mono<McpSchema.ListRootsResult> listRoots() {
return this.listRoots(null);

// @formatter:off
return this.listRoots(McpSchema.FIRST_PAGE)
.expand(result -> (result.nextCursor() != null) ?
this.listRoots(result.nextCursor()) : Mono.empty())
.reduce(new McpSchema.ListRootsResult(new ArrayList<>(), null),
(allRootsResult, result) -> {
allRootsResult.roots().addAll(result.roots());
return allRootsResult;
})
.map(result -> new McpSchema.ListRootsResult(Collections.unmodifiableList(result.roots()),
result.nextCursor()));
// @formatter:on
}

/**
Expand Down
10 changes: 9 additions & 1 deletion mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1615,11 +1615,19 @@ public record Root( // @formatter:off
*
* @param roots An array of Root objects, each representing a root directory or file
* that the server can operate on.
* @param nextCursor An optional cursor for pagination. If present, indicates there
* are more roots available. The client can use this cursor to request the next page
* of results by sending a roots/list request with the cursor parameter set to this
*/
@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record ListRootsResult( // @formatter:off
@JsonProperty("roots") List<Root> roots) {
@JsonProperty("roots") List<Root> roots,
@JsonProperty("nextCursor") String nextCursor) {

public ListRootsResult(List<Root> roots) {
this(roots, null);
}
} // @formatter:on

}
Loading