Skip to content

Commit f873a95

Browse files
authored
Merge branch 'modelcontextprotocol:main' into StreamableHttpServerTransportProvider
2 parents 7817da6 + d9006ec commit f873a95

File tree

9 files changed

+1096
-56
lines changed

9 files changed

+1096
-56
lines changed

mcp-spring/mcp-spring-webflux/src/test/java/io/modelcontextprotocol/WebFluxSseIntegrationTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,6 @@ void testCreateElicitationWithRequestTimeoutSuccess(String clientType) {
432432
Function<ElicitRequest, ElicitResult> elicitationHandler = request -> {
433433
assertThat(request.message()).isNotEmpty();
434434
assertThat(request.requestedSchema()).isNotNull();
435-
try {
436-
TimeUnit.SECONDS.sleep(2);
437-
}
438-
catch (InterruptedException e) {
439-
throw new RuntimeException(e);
440-
}
441435
return new ElicitResult(ElicitResult.Action.ACCEPT, Map.of("message", request.message()));
442436
};
443437

@@ -491,14 +485,18 @@ void testCreateElicitationWithRequestTimeoutSuccess(String clientType) {
491485
@ValueSource(strings = { "httpclient", "webflux" })
492486
void testCreateElicitationWithRequestTimeoutFail(String clientType) {
493487

488+
var latch = new CountDownLatch(1);
494489
// Client
495490
var clientBuilder = clientBuilders.get(clientType);
496491

497492
Function<ElicitRequest, ElicitResult> elicitationHandler = request -> {
498493
assertThat(request.message()).isNotEmpty();
499494
assertThat(request.requestedSchema()).isNotNull();
495+
500496
try {
501-
TimeUnit.SECONDS.sleep(2);
497+
if (!latch.await(2, TimeUnit.SECONDS)) {
498+
throw new RuntimeException("Timeout waiting for elicitation processing");
499+
}
502500
}
503501
catch (InterruptedException e) {
504502
throw new RuntimeException(e);
@@ -536,7 +534,7 @@ void testCreateElicitationWithRequestTimeoutFail(String clientType) {
536534

537535
var mcpServer = McpServer.async(mcpServerTransportProvider)
538536
.serverInfo("test-server", "1.0.0")
539-
.requestTimeout(Duration.ofSeconds(1))
537+
.requestTimeout(Duration.ofSeconds(1)) // 1 second.
540538
.tools(tool)
541539
.build();
542540

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ void testListAllTools() {
182182
});
183183
}
184184

185+
@Test
186+
void testListAllToolsReturnsImmutableList() {
187+
withClient(createMcpTransport(), mcpAsyncClient -> {
188+
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listTools()))
189+
.consumeNextWith(result -> {
190+
assertThat(result.tools()).isNotNull();
191+
// Verify that the returned list is immutable
192+
assertThatThrownBy(() -> result.tools().add(new Tool("test", "test", "{\"type\":\"object\"}")))
193+
.isInstanceOf(UnsupportedOperationException.class);
194+
})
195+
.verifyComplete();
196+
});
197+
}
198+
185199
@Test
186200
void testPingWithoutInitialization() {
187201
verifyCallSucceedsWithImplicitInitialization(client -> client.ping(), "pinging the server");
@@ -333,6 +347,21 @@ void testListAllResources() {
333347
});
334348
}
335349

350+
@Test
351+
void testListAllResourcesReturnsImmutableList() {
352+
withClient(createMcpTransport(), mcpAsyncClient -> {
353+
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listResources()))
354+
.consumeNextWith(result -> {
355+
assertThat(result.resources()).isNotNull();
356+
// Verify that the returned list is immutable
357+
assertThatThrownBy(
358+
() -> result.resources().add(Resource.builder().uri("test://uri").name("test").build()))
359+
.isInstanceOf(UnsupportedOperationException.class);
360+
})
361+
.verifyComplete();
362+
});
363+
}
364+
336365
@Test
337366
void testMcpAsyncClientState() {
338367
withClient(createMcpTransport(), mcpAsyncClient -> {
@@ -384,6 +413,20 @@ void testListAllPrompts() {
384413
});
385414
}
386415

416+
@Test
417+
void testListAllPromptsReturnsImmutableList() {
418+
withClient(createMcpTransport(), mcpAsyncClient -> {
419+
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listPrompts()))
420+
.consumeNextWith(result -> {
421+
assertThat(result.prompts()).isNotNull();
422+
// Verify that the returned list is immutable
423+
assertThatThrownBy(() -> result.prompts().add(new Prompt("test", "test", null)))
424+
.isInstanceOf(UnsupportedOperationException.class);
425+
})
426+
.verifyComplete();
427+
});
428+
}
429+
387430
@Test
388431
void testGetPromptWithoutInitialization() {
389432
GetPromptRequest request = new GetPromptRequest("simple_prompt", Map.of());
@@ -553,6 +596,21 @@ void testListAllResourceTemplates() {
553596
});
554597
}
555598

599+
@Test
600+
void testListAllResourceTemplatesReturnsImmutableList() {
601+
withClient(createMcpTransport(), mcpAsyncClient -> {
602+
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listResourceTemplates()))
603+
.consumeNextWith(result -> {
604+
assertThat(result.resourceTemplates()).isNotNull();
605+
// Verify that the returned list is immutable
606+
assertThatThrownBy(() -> result.resourceTemplates()
607+
.add(new McpSchema.ResourceTemplate("test://template", "test", null, null, null)))
608+
.isInstanceOf(UnsupportedOperationException.class);
609+
})
610+
.verifyComplete();
611+
});
612+
}
613+
556614
// @Test
557615
void testResourceSubscription() {
558616
withClient(createMcpTransport(), mcpAsyncClient -> {

mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.time.Duration;
77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.Map;
@@ -187,6 +188,9 @@ public class McpAsyncClient {
187188
// Request Handlers
188189
Map<String, RequestHandler<?>> requestHandlers = new HashMap<>();
189190

191+
// Ping MUST respond with an empty data, but not NULL response.
192+
requestHandlers.put(McpSchema.METHOD_PING, params -> Mono.just(Map.of()));
193+
190194
// Roots List Request Handler
191195
if (this.clientCapabilities.roots() != null) {
192196
requestHandlers.put(McpSchema.METHOD_ROOTS_LIST, rootsListRequestHandler());
@@ -674,15 +678,13 @@ public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToo
674678
* @return A Mono that emits the list of all tools result
675679
*/
676680
public Mono<McpSchema.ListToolsResult> listTools() {
677-
return this.listTools(McpSchema.FIRST_PAGE).expand(result -> {
678-
if (result.nextCursor() != null) {
679-
return this.listTools(result.nextCursor());
680-
}
681-
return Mono.empty();
682-
}).reduce(new McpSchema.ListToolsResult(new ArrayList<>(), null), (allToolsResult, result) -> {
683-
allToolsResult.tools().addAll(result.tools());
684-
return allToolsResult;
685-
});
681+
return this.listTools(McpSchema.FIRST_PAGE)
682+
.expand(result -> (result.nextCursor() != null) ? this.listTools(result.nextCursor()) : Mono.empty())
683+
.reduce(new McpSchema.ListToolsResult(new ArrayList<>(), null), (allToolsResult, result) -> {
684+
allToolsResult.tools().addAll(result.tools());
685+
return allToolsResult;
686+
})
687+
.map(result -> new McpSchema.ListToolsResult(Collections.unmodifiableList(result.tools()), null));
686688
}
687689

688690
/**
@@ -736,15 +738,13 @@ private NotificationHandler asyncToolsChangeNotificationHandler(
736738
* @see #readResource(McpSchema.Resource)
737739
*/
738740
public Mono<McpSchema.ListResourcesResult> listResources() {
739-
return this.listResources(McpSchema.FIRST_PAGE).expand(result -> {
740-
if (result.nextCursor() != null) {
741-
return this.listResources(result.nextCursor());
742-
}
743-
return Mono.empty();
744-
}).reduce(new McpSchema.ListResourcesResult(new ArrayList<>(), null), (allResourcesResult, result) -> {
745-
allResourcesResult.resources().addAll(result.resources());
746-
return allResourcesResult;
747-
});
741+
return this.listResources(McpSchema.FIRST_PAGE)
742+
.expand(result -> (result.nextCursor() != null) ? this.listResources(result.nextCursor()) : Mono.empty())
743+
.reduce(new McpSchema.ListResourcesResult(new ArrayList<>(), null), (allResourcesResult, result) -> {
744+
allResourcesResult.resources().addAll(result.resources());
745+
return allResourcesResult;
746+
})
747+
.map(result -> new McpSchema.ListResourcesResult(Collections.unmodifiableList(result.resources()), null));
748748
}
749749

750750
/**
@@ -806,17 +806,16 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceReq
806806
* @see McpSchema.ListResourceTemplatesResult
807807
*/
808808
public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates() {
809-
return this.listResourceTemplates(McpSchema.FIRST_PAGE).expand(result -> {
810-
if (result.nextCursor() != null) {
811-
return this.listResourceTemplates(result.nextCursor());
812-
}
813-
return Mono.empty();
814-
})
809+
return this.listResourceTemplates(McpSchema.FIRST_PAGE)
810+
.expand(result -> (result.nextCursor() != null) ? this.listResourceTemplates(result.nextCursor())
811+
: Mono.empty())
815812
.reduce(new McpSchema.ListResourceTemplatesResult(new ArrayList<>(), null),
816813
(allResourceTemplatesResult, result) -> {
817814
allResourceTemplatesResult.resourceTemplates().addAll(result.resourceTemplates());
818815
return allResourceTemplatesResult;
819-
});
816+
})
817+
.map(result -> new McpSchema.ListResourceTemplatesResult(
818+
Collections.unmodifiableList(result.resourceTemplates()), null));
820819
}
821820

822821
/**
@@ -911,15 +910,13 @@ private NotificationHandler asyncResourcesUpdatedNotificationHandler(
911910
* @see #getPrompt(GetPromptRequest)
912911
*/
913912
public Mono<ListPromptsResult> listPrompts() {
914-
return this.listPrompts(McpSchema.FIRST_PAGE).expand(result -> {
915-
if (result.nextCursor() != null) {
916-
return this.listPrompts(result.nextCursor());
917-
}
918-
return Mono.empty();
919-
}).reduce(new ListPromptsResult(new ArrayList<>(), null), (allPromptsResult, result) -> {
920-
allPromptsResult.prompts().addAll(result.prompts());
921-
return allPromptsResult;
922-
});
913+
return this.listPrompts(McpSchema.FIRST_PAGE)
914+
.expand(result -> (result.nextCursor() != null) ? this.listPrompts(result.nextCursor()) : Mono.empty())
915+
.reduce(new ListPromptsResult(new ArrayList<>(), null), (allPromptsResult, result) -> {
916+
allPromptsResult.prompts().addAll(result.prompts());
917+
return allPromptsResult;
918+
})
919+
.map(result -> new McpSchema.ListPromptsResult(Collections.unmodifiableList(result.prompts()), null));
923920
}
924921

925922
/**

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
package io.modelcontextprotocol.server;
66

7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
710
import com.fasterxml.jackson.core.type.TypeReference;
811
import io.modelcontextprotocol.spec.McpError;
912
import io.modelcontextprotocol.spec.McpSchema;
@@ -142,7 +145,19 @@ public Mono<McpSchema.ElicitResult> createElicitation(McpSchema.ElicitRequest el
142145
* @return A Mono that emits the list of roots result.
143146
*/
144147
public Mono<McpSchema.ListRootsResult> listRoots() {
145-
return this.listRoots(null);
148+
149+
// @formatter:off
150+
return this.listRoots(McpSchema.FIRST_PAGE)
151+
.expand(result -> (result.nextCursor() != null) ?
152+
this.listRoots(result.nextCursor()) : Mono.empty())
153+
.reduce(new McpSchema.ListRootsResult(new ArrayList<>(), null),
154+
(allRootsResult, result) -> {
155+
allRootsResult.roots().addAll(result.roots());
156+
return allRootsResult;
157+
})
158+
.map(result -> new McpSchema.ListRootsResult(Collections.unmodifiableList(result.roots()),
159+
result.nextCursor()));
160+
// @formatter:on
146161
}
147162

148163
/**

0 commit comments

Comments
 (0)