Skip to content
Open
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 @@ -225,6 +225,13 @@ public Mono<McpSchema.ListRootsResult> listRoots() {
* @return A Mono that emits the list of roots result containing
*/
public Mono<McpSchema.ListRootsResult> listRoots(String cursor) {
if (this.clientCapabilities == null) {
return Mono
.error(new IllegalStateException("Client must be initialized. Call the initialize method first!"));
}
if (this.clientCapabilities.roots() == null) {
return Mono.error(new IllegalStateException("Client must be configured with roots capabilities"));
}
return this.session.sendRequest(McpSchema.METHOD_ROOTS_LIST, new McpSchema.PaginatedRequest(cursor),
LIST_ROOTS_RESULT_TYPE_REF);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,40 @@ void testListRootsWithError() {
});
}

@Test
void testListRootsWithNullCapabilities() {
// Given - Create exchange with null capabilities
McpAsyncServerExchange exchangeWithNullCapabilities = new McpAsyncServerExchange("testSessionId", mockSession,
null, clientInfo, McpTransportContext.EMPTY);

StepVerifier.create(exchangeWithNullCapabilities.listRoots()).verifyErrorSatisfies(error -> {
assertThat(error).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be initialized. Call the initialize method first!");
});

// Verify that sendRequest was never called due to null capabilities
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(), any(TypeRef.class));
}

@Test
void testListRootsWithoutRootsCapabilities() {
// Given - Create exchange without roots capabilities
McpSchema.ClientCapabilities capabilitiesWithoutRoots = McpSchema.ClientCapabilities.builder()
.sampling()
.build();

McpAsyncServerExchange exchangeWithoutRoots = new McpAsyncServerExchange("testSessionId", mockSession,
capabilitiesWithoutRoots, clientInfo, McpTransportContext.EMPTY);

StepVerifier.create(exchangeWithoutRoots.listRoots()).verifyErrorSatisfies(error -> {
assertThat(error).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be configured with roots capabilities");
});

// Verify that sendRequest was never called due to missing roots capabilities
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(), any(TypeRef.class));
}

@Test
void testListRootsUnmodifiabilityAfterAccumulation() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,39 @@ void testListRootsWithError() {
assertThatThrownBy(() -> exchange.listRoots()).isInstanceOf(RuntimeException.class).hasMessage("Network error");
}

@Test
void testListRootsWithNullCapabilities() {
// Given - Create exchange with null capabilities
McpAsyncServerExchange asyncExchangeWithNullCapabilities = new McpAsyncServerExchange("testSessionId",
mockSession, null, clientInfo, McpTransportContext.EMPTY);
McpSyncServerExchange exchangeWithNullCapabilities = new McpSyncServerExchange(
asyncExchangeWithNullCapabilities);

assertThatThrownBy(() -> exchangeWithNullCapabilities.listRoots()).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be initialized. Call the initialize method first!");

// Verify that sendRequest was never called due to null capabilities
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(), any(TypeRef.class));
}

@Test
void testListRootsWithoutRootsCapabilities() {
// Given - Create exchange without roots capabilities
McpSchema.ClientCapabilities capabilitiesWithoutRoots = McpSchema.ClientCapabilities.builder()
.sampling()
.build();

McpAsyncServerExchange asyncExchangeWithoutRoots = new McpAsyncServerExchange("testSessionId", mockSession,
capabilitiesWithoutRoots, clientInfo, McpTransportContext.EMPTY);
McpSyncServerExchange exchangeWithoutRoots = new McpSyncServerExchange(asyncExchangeWithoutRoots);

assertThatThrownBy(() -> exchangeWithoutRoots.listRoots()).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be configured with roots capabilities");

// Verify that sendRequest was never called due to missing roots capabilities
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(), any(TypeRef.class));
}

@Test
void testListRootsUnmodifiabilityAfterAccumulation() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,12 +1056,10 @@ void testRootsWithoutCapability() {
assertThat(mcpClient.initialize()).isNotNull();

// Attempt to list roots should fail
try {
mcpClient.callTool(McpSchema.CallToolRequest.builder("tool1").arguments(Map.of()).build());
}
catch (McpError e) {
assertThat(e).isInstanceOf(McpError.class).hasMessage("Roots not supported");
}
assertThatThrownBy(
() -> mcpClient.callTool(McpSchema.CallToolRequest.builder("tool1").arguments(Map.of()).build()))
.isInstanceOf(McpError.class)
.hasMessage("Client must be configured with roots capabilities");
}
finally {
mcpServer.closeGracefully();
Expand Down