Repository navigation
Expand file tree
/
Copy pathlistroots-fix.diff
More file actions
77 lines (75 loc) · 3.57 KB
/
Copy pathlistroots-fix.diff
File metadata and controls
77 lines (75 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
diff --git a/mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java b/mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java
index e27d612..d977ac9 100644
--- a/mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java
+++ b/mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java
@@ -225,6 +225,13 @@ public class McpAsyncServerExchange {
* @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);
}
diff --git a/mcp-core/src/test/java/io/modelcontextprotocol/server/McpAsyncServerExchangeTests.java b/mcp-core/src/test/java/io/modelcontextprotocol/server/McpAsyncServerExchangeTests.java
index f4f76b1..b556a8a 100644
--- a/mcp-core/src/test/java/io/modelcontextprotocol/server/McpAsyncServerExchangeTests.java
+++ b/mcp-core/src/test/java/io/modelcontextprotocol/server/McpAsyncServerExchangeTests.java
@@ -157,6 +157,54 @@ class McpAsyncServerExchangeTests {
}).verifyComplete();
}
+ @Test
+ void testListRootsWithNullCapabilities() {
+
+ 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() {
+
+ 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 testListRootsWithSpecificCursorAndNullCapabilities() {
+
+ McpAsyncServerExchange exchangeWithNullCapabilities = new McpAsyncServerExchange("testSessionId",
+ mockSession, null, clientInfo, McpTransportContext.EMPTY);
+
+ StepVerifier.create(exchangeWithNullCapabilities.listRoots("someCursor")).verifyErrorSatisfies(error -> {
+ assertThat(error).isInstanceOf(IllegalStateException.class)
+ .hasMessage("Client must be initialized. Call the initialize method first!");
+ });
+
+ verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(), any(TypeRef.class));
+ }
+
@Test
void testListRootsWithError() {