Skip to content

Commit 5be23a9

Browse files
Create the reduce accumulator per subscription in the list* methods (#1175)
The Mono returned by listTools, listResources, listResourceTemplates, listPrompts and listRoots can now safely be re-subscribed. Co-authored-by: kamisheng <1659225248@qq.com>
1 parent e6c532e commit 5be23a9

4 files changed

Lines changed: 106 additions & 5 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ public Mono<McpSchema.ListToolsResult> listTools() {
734734
return this.listTools(McpSchema.FIRST_PAGE).expand(result -> {
735735
String next = result.nextCursor();
736736
return (next != null && !next.isEmpty()) ? this.listTools(next) : Mono.empty();
737-
}).reduce(new ArrayList<McpSchema.Tool>(), (accumulated, result) -> {
737+
}).reduceWith(ArrayList<McpSchema.Tool>::new, (accumulated, result) -> {
738738
accumulated.addAll(result.tools());
739739
return accumulated;
740740
}).map(all -> McpSchema.ListToolsResult.builder(Collections.unmodifiableList(all)).build());
@@ -821,7 +821,7 @@ public Mono<McpSchema.ListResourcesResult> listResources() {
821821
return this.listResources(McpSchema.FIRST_PAGE).expand(result -> {
822822
String next = result.nextCursor();
823823
return (next != null && !next.isEmpty()) ? this.listResources(next) : Mono.empty();
824-
}).reduce(new ArrayList<McpSchema.Resource>(), (accumulated, result) -> {
824+
}).reduceWith(ArrayList<McpSchema.Resource>::new, (accumulated, result) -> {
825825
accumulated.addAll(result.resources());
826826
return accumulated;
827827
}).map(all -> McpSchema.ListResourcesResult.builder(Collections.unmodifiableList(all)).build());
@@ -907,7 +907,7 @@ public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates() {
907907
return this.listResourceTemplates(McpSchema.FIRST_PAGE).expand(result -> {
908908
String next = result.nextCursor();
909909
return (next != null && !next.isEmpty()) ? this.listResourceTemplates(next) : Mono.empty();
910-
}).reduce(new ArrayList<McpSchema.ResourceTemplate>(), (accumulated, result) -> {
910+
}).reduceWith(ArrayList<McpSchema.ResourceTemplate>::new, (accumulated, result) -> {
911911
accumulated.addAll(result.resourceTemplates());
912912
return accumulated;
913913
}).map(all -> McpSchema.ListResourceTemplatesResult.builder(Collections.unmodifiableList(all)).build());
@@ -1026,7 +1026,7 @@ public Mono<ListPromptsResult> listPrompts() {
10261026
return this.listPrompts(McpSchema.FIRST_PAGE).expand(result -> {
10271027
String next = result.nextCursor();
10281028
return (next != null && !next.isEmpty()) ? this.listPrompts(next) : Mono.empty();
1029-
}).reduce(new ArrayList<McpSchema.Prompt>(), (accumulated, result) -> {
1029+
}).reduceWith(ArrayList<McpSchema.Prompt>::new, (accumulated, result) -> {
10301030
accumulated.addAll(result.prompts());
10311031
return accumulated;
10321032
}).map(all -> McpSchema.ListPromptsResult.builder(Collections.unmodifiableList(all)).build());

‎mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public Mono<McpSchema.ListRootsResult> listRoots() {
209209
return this.listRoots(McpSchema.FIRST_PAGE)
210210
.expand(result -> (result.nextCursor() != null) ?
211211
this.listRoots(result.nextCursor()) : Mono.empty())
212-
.reduce(McpSchema.ListRootsResult.builder(new ArrayList<>()).build(),
212+
.reduceWith(() -> McpSchema.ListRootsResult.builder(new ArrayList<>()).build(),
213213
(allRootsResult, result) -> {
214214
allRootsResult.roots().addAll(result.roots());
215215
return allRootsResult;

‎mcp-core/src/test/java/io/modelcontextprotocol/server/McpAsyncServerExchangeTests.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ void testListRootsWithSinglePage() {
8585
}).verifyComplete();
8686
}
8787

88+
@Test
89+
void testListRootsCanBeResubscribed() {
90+
91+
List<McpSchema.Root> roots = Arrays
92+
.asList(McpSchema.Root.builder("file:///home/user/project1").name("Project 1").build());
93+
McpSchema.ListRootsResult singlePageResult = McpSchema.ListRootsResult.builder(roots).build();
94+
95+
when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(McpSchema.PaginatedRequest.class),
96+
any(TypeRef.class)))
97+
.thenReturn(Mono.just(singlePageResult));
98+
99+
Mono<McpSchema.ListRootsResult> listRoots = exchange.listRoots();
100+
101+
// The same Mono must be safe to subscribe more than once: the accumulator used to
102+
// aggregate the pages has to be created per subscription, not once when
103+
// listRoots()
104+
// is called.
105+
StepVerifier.create(listRoots).assertNext(result -> assertThat(result.roots()).hasSize(1)).verifyComplete();
106+
StepVerifier.create(listRoots).assertNext(result -> assertThat(result.roots()).hasSize(1)).verifyComplete();
107+
}
108+
88109
@Test
89110
void testListRootsWithMultiplePages() {
90111

‎mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ void testListAllTools() {
206206
});
207207
}
208208

209+
@Test
210+
void testListAllToolsCanBeResubscribed() {
211+
withClient(createMcpTransport(), mcpAsyncClient -> {
212+
Mono<McpSchema.ListToolsResult> listTools = mcpAsyncClient.listTools();
213+
214+
AtomicInteger firstSubscriptionSize = new AtomicInteger();
215+
StepVerifier.create(mcpAsyncClient.initialize().then(listTools)).consumeNextWith(result -> {
216+
firstSubscriptionSize.set(result.tools().size());
217+
assertThat(result.tools()).isNotEmpty();
218+
}).verifyComplete();
219+
220+
// The same Mono must be safe to subscribe again: every subscription has to
221+
// start from an empty accumulator instead of appending to the previous
222+
// result.
223+
StepVerifier.create(listTools)
224+
.consumeNextWith(result -> assertThat(result.tools()).hasSize(firstSubscriptionSize.get()))
225+
.verifyComplete();
226+
});
227+
}
228+
209229
@Test
210230
void testListAllToolsReturnsImmutableList() {
211231
withClient(createMcpTransport(), mcpAsyncClient -> {
@@ -378,6 +398,26 @@ void testListAllResources() {
378398
});
379399
}
380400

401+
@Test
402+
void testListAllResourcesCanBeResubscribed() {
403+
withClient(createMcpTransport(), mcpAsyncClient -> {
404+
Mono<McpSchema.ListResourcesResult> listResources = mcpAsyncClient.listResources();
405+
406+
AtomicInteger firstSubscriptionSize = new AtomicInteger();
407+
StepVerifier.create(mcpAsyncClient.initialize().then(listResources)).consumeNextWith(result -> {
408+
firstSubscriptionSize.set(result.resources().size());
409+
assertThat(result.resources()).isNotEmpty();
410+
}).verifyComplete();
411+
412+
// The same Mono must be safe to subscribe again: every subscription has to
413+
// start from an empty accumulator instead of appending to the previous
414+
// result.
415+
StepVerifier.create(listResources)
416+
.consumeNextWith(result -> assertThat(result.resources()).hasSize(firstSubscriptionSize.get()))
417+
.verifyComplete();
418+
});
419+
}
420+
381421
@Test
382422
void testListAllResourcesReturnsImmutableList() {
383423
withClient(createMcpTransport(), mcpAsyncClient -> {
@@ -443,6 +483,26 @@ void testListAllPrompts() {
443483
});
444484
}
445485

486+
@Test
487+
void testListAllPromptsCanBeResubscribed() {
488+
withClient(createMcpTransport(), mcpAsyncClient -> {
489+
Mono<McpSchema.ListPromptsResult> listPrompts = mcpAsyncClient.listPrompts();
490+
491+
AtomicInteger firstSubscriptionSize = new AtomicInteger();
492+
StepVerifier.create(mcpAsyncClient.initialize().then(listPrompts)).consumeNextWith(result -> {
493+
firstSubscriptionSize.set(result.prompts().size());
494+
assertThat(result.prompts()).isNotEmpty();
495+
}).verifyComplete();
496+
497+
// The same Mono must be safe to subscribe again: every subscription has to
498+
// start from an empty accumulator instead of appending to the previous
499+
// result.
500+
StepVerifier.create(listPrompts)
501+
.consumeNextWith(result -> assertThat(result.prompts()).hasSize(firstSubscriptionSize.get()))
502+
.verifyComplete();
503+
});
504+
}
505+
446506
@Test
447507
void testListAllPromptsReturnsImmutableList() {
448508
withClient(createMcpTransport(), mcpAsyncClient -> {
@@ -636,6 +696,26 @@ void testListAllResourceTemplates() {
636696
});
637697
}
638698

699+
@Test
700+
void testListAllResourceTemplatesCanBeResubscribed() {
701+
withClient(createMcpTransport(), mcpAsyncClient -> {
702+
Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates = mcpAsyncClient.listResourceTemplates();
703+
704+
AtomicInteger firstSubscriptionSize = new AtomicInteger();
705+
StepVerifier.create(mcpAsyncClient.initialize().then(listResourceTemplates)).consumeNextWith(result -> {
706+
firstSubscriptionSize.set(result.resourceTemplates().size());
707+
assertThat(result.resourceTemplates()).isNotEmpty();
708+
}).verifyComplete();
709+
710+
// The same Mono must be safe to subscribe again: every subscription has to
711+
// start from an empty accumulator instead of appending to the previous
712+
// result.
713+
StepVerifier.create(listResourceTemplates)
714+
.consumeNextWith(result -> assertThat(result.resourceTemplates()).hasSize(firstSubscriptionSize.get()))
715+
.verifyComplete();
716+
});
717+
}
718+
639719
@Test
640720
void testListAllResourceTemplatesReturnsImmutableList() {
641721
withClient(createMcpTransport(), mcpAsyncClient -> {

0 commit comments

Comments
 (0)