FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reject roots listing before sending unsupported client requests by karthiksenv · Pull Request #1160 · modelcontextprotocol/java-sdk · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) .txt  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions disclosure.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This change was submitted despite me reading the rules and understanding AI contribution guidelines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 root listing capabilities"));
}
return this.session.sendRequest(McpSchema.METHOD_ROOTS_LIST, new McpSchema.PaginatedRequest(cursor),
LIST_ROOTS_RESULT_TYPE_REF);
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import io.modelcontextprotocol.spec.McpServerSession;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;
Expand All @@ -29,6 +31,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -85,6 +88,54 @@ void testListRootsWithSinglePage() {
}).verifyComplete();
}

@ParameterizedTest
@ValueSource(booleans = { false, true })
void testListRootsWithoutCapabilities(boolean paginated) {
exchange = new McpAsyncServerExchange("testSessionId", mockSession, null, clientInfo,
McpTransportContext.EMPTY);
when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(McpSchema.PaginatedRequest.class),
any(TypeRef.class)))
.thenReturn(Mono.just(McpSchema.ListRootsResult.builder(List.of()).build()));

Mono<McpSchema.ListRootsResult> result = paginated ? exchange.listRoots("cursor") : exchange.listRoots();
verifyNoInteractions(mockSession);
StepVerifier.create(result)
.verifyErrorSatisfies(error -> assertThat(error).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be initialized. Call the initialize method first!"));
verifyNoInteractions(mockSession);
}

@ParameterizedTest
@ValueSource(booleans = { false, true })
void testListRootsWithoutRootsCapability(boolean paginated) {
exchange = new McpAsyncServerExchange("testSessionId", mockSession,
McpSchema.ClientCapabilities.builder().build(), clientInfo, McpTransportContext.EMPTY);
when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(McpSchema.PaginatedRequest.class),
any(TypeRef.class)))
.thenReturn(Mono.just(McpSchema.ListRootsResult.builder(List.of()).build()));

Mono<McpSchema.ListRootsResult> result = paginated ? exchange.listRoots("cursor") : exchange.listRoots();
verifyNoInteractions(mockSession);
StepVerifier.create(result)
.verifyErrorSatisfies(error -> assertThat(error).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be configured with root listing capabilities"));
verifyNoInteractions(mockSession);
}

@Test
void testListRootsWithoutListChangedSupport() {
exchange = new McpAsyncServerExchange("testSessionId", mockSession,
McpSchema.ClientCapabilities.builder().roots(false).build(), clientInfo, McpTransportContext.EMPTY);
McpSchema.ListRootsResult result = McpSchema.ListRootsResult.builder(List.of()).build();
when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), eq(new McpSchema.PaginatedRequest("cursor")),
any(TypeRef.class)))
.thenReturn(Mono.just(result));

StepVerifier.create(exchange.listRoots("cursor")).expectNext(result).verifyComplete();
verify(mockSession).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), eq(new McpSchema.PaginatedRequest("cursor")),
any(TypeRef.class));
}

@Test
void testListRootsWithMultiplePages() {

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,10 @@ void testRootsWithoutCapability(String clientType) {
.tool(Tool.builder("tool1", EMPTY_JSON_SCHEMA).description("tool1 description").build())
.callHandler((exchange, request) -> {

exchange.listRoots(); // try to list roots
assertThatThrownBy(exchange::listRoots).isInstanceOf(IllegalStateException.class)
.hasMessage("Client must be configured with root listing capabilities");

return mock(CallToolResult.class);
return CallToolResult.builder().addTextContent("Roots capability rejected").build();
})
.build();

Expand All @@ -1145,13 +1146,10 @@ void testRootsWithoutCapability(String clientType) {

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");
}
CallToolResult result = mcpClient
.callTool(McpSchema.CallToolRequest.builder("tool1").arguments(Map.of()).build());
assertThat(result.isError()).isFalse();
assertThat(result.content()).containsExactly(new McpSchema.TextContent("Roots capability rejected"));
}
finally {
mcpServer.closeGracefully();
Expand Down

Back | FazBrowse Home | New Git URL