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

Reject listRoots when client lacks roots capability by Yerang-RB · Pull Request #1137 · 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  (2) All 1 file type 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
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 roots 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 @@ -207,6 +207,40 @@ void testListRootsUnmodifiabilityAfterAccumulation() {
}).verifyComplete();
}

@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 testGetClientCapabilities() {
assertThat(exchange.getClientCapabilities()).isEqualTo(clientCapabilities);
Expand Down

Back | FazBrowse Home | New Git URL