Skip to content

Commit 2e49583

Browse files
committed
Polish and fixes
Signed-off-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>
1 parent 17804b6 commit 2e49583

File tree

11 files changed

+267
-271
lines changed

11 files changed

+267
-271
lines changed

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

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,13 @@ void testCreateMessageWithoutSamplingCapabilities(String clientType) {
112112
return Mono.just(mock(CallToolResult.class));
113113
});
114114

115-
//@formatter:off
116115
var server = McpServer.async(
117116
mcpServerTransportProvider)
118117
.serverInfo("test-server", "1.0.0")
119118
.tools(tool)
120119
.build();
121120

122-
try (
123-
var client = clientBuilder
121+
try (var client = clientBuilder
124122
.clientInfo(new McpSchema.Implementation("Sample " + "client", "0.0.0"))
125123
.build();) {
126124

@@ -133,7 +131,7 @@ void testCreateMessageWithoutSamplingCapabilities(String clientType) {
133131
assertThat(e).isInstanceOf(McpError.class)
134132
.hasMessage("Client must be configured with sampling capabilities");
135133
}
136-
} //@formatter:on
134+
}
137135
server.close();
138136
}
139137

@@ -180,7 +178,6 @@ void testCreateMessageSuccess(String clientType) throws InterruptedException {
180178
return Mono.just(callResponse);
181179
});
182180

183-
//@formatter:off
184181
var mcpServer = McpServer.async(mcpServerTransportProvider)
185182
.serverInfo("test-server", "1.0.0")
186183
.tools(tool)
@@ -190,7 +187,7 @@ void testCreateMessageSuccess(String clientType) throws InterruptedException {
190187
var mcpClient = clientBuilder.clientInfo(new McpSchema.Implementation("Sample client", "0.0.0"))
191188
.capabilities(ClientCapabilities.builder().sampling().build())
192189
.sampling(samplingHandler)
193-
.build()) {// @formatter:on
190+
.build()) {
194191

195192
InitializeResult initResult = mcpClient.initialize();
196193
assertThat(initResult).isNotNull();
@@ -215,14 +212,13 @@ void testRootsSuccess(String clientType) {
215212

216213
AtomicReference<List<Root>> rootsRef = new AtomicReference<>();
217214

218-
try (// @formatter:off
219-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
220-
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef.set(rootsUpdate))
221-
.build();
215+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
216+
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef.set(rootsUpdate))
217+
.build();
222218

223-
var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
219+
try (var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
224220
.roots(roots)
225-
.build()) { // @formatter:on
221+
.build()) {
226222

227223
InitializeResult initResult = mcpClient.initialize();
228224
assertThat(initResult).isNotNull();
@@ -250,6 +246,8 @@ void testRootsSuccess(String clientType) {
250246
assertThat(rootsRef.get()).containsAll(List.of(roots.get(1), root3));
251247
});
252248
}
249+
250+
mcpServer.close();
253251
}
254252

255253
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -266,16 +264,16 @@ void testRootsWithoutCapability(String clientType) {
266264
return mock(CallToolResult.class);
267265
});
268266

269-
try (// @formatter:off
270-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
271-
.rootsChangeHandler((exchange, rootsUpdate) -> {})
272-
.tools(tool)
273-
.build();
267+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
268+
.rootsChangeHandler((exchange, rootsUpdate) -> {})
269+
.tools(tool)
270+
.build();
274271

272+
try (
275273
// Create client without roots capability
276274
var mcpClient = clientBuilder
277275
.capabilities(ClientCapabilities.builder().build())
278-
.build()) { // @formatter:on}
276+
.build()) {
279277

280278
assertThat(mcpClient.initialize()).isNotNull();
281279

@@ -287,6 +285,8 @@ void testRootsWithoutCapability(String clientType) {
287285
assertThat(e).isInstanceOf(McpError.class).hasMessage("Roots not supported");
288286
}
289287
}
288+
289+
mcpServer.close();
290290
}
291291

292292
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -296,14 +296,13 @@ void testRootsNotifciationWithEmptyRootsList(String clientType) {
296296

297297
AtomicReference<List<Root>> rootsRef = new AtomicReference<>();
298298

299-
try ( //@formatter:off
300-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
301-
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef.set(rootsUpdate))
302-
.build();
299+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
300+
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef.set(rootsUpdate))
301+
.build();
303302

304-
var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
303+
try (var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
305304
.roots(List.of()) // Empty roots list
306-
.build()) { // @formatter:on
305+
.build()) {
307306

308307
assertThat(mcpClient.initialize()).isNotNull();
309308

@@ -313,6 +312,8 @@ void testRootsNotifciationWithEmptyRootsList(String clientType) {
313312
assertThat(rootsRef.get()).isEmpty();
314313
});
315314
}
315+
316+
mcpServer.close();
316317
}
317318

318319
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -326,15 +327,14 @@ void testRootsWithMultipleHandlers(String clientType) {
326327
AtomicReference<List<Root>> rootsRef1 = new AtomicReference<>();
327328
AtomicReference<List<Root>> rootsRef2 = new AtomicReference<>();
328329

329-
try ( //@formatter:off
330-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
331-
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef1.set(rootsUpdate))
332-
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef2.set(rootsUpdate))
333-
.build();
330+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
331+
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef1.set(rootsUpdate))
332+
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef2.set(rootsUpdate))
333+
.build();
334334

335-
var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
335+
try (var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
336336
.roots(roots)
337-
.build()) { // @formatter:on
337+
.build()) {
338338

339339
InitializeResult initResult = mcpClient.initialize();
340340
assertThat(initResult).isNotNull();
@@ -346,6 +346,8 @@ void testRootsWithMultipleHandlers(String clientType) {
346346
assertThat(rootsRef2.get()).containsAll(roots);
347347
});
348348
}
349+
350+
mcpServer.close();
349351
}
350352

351353
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -358,14 +360,13 @@ void testRootsServerCloseWithActiveSubscription(String clientType) {
358360

359361
AtomicReference<List<Root>> rootsRef = new AtomicReference<>();
360362

361-
try ( //@formatter:off
362-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
363-
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef.set(rootsUpdate))
364-
.build();
363+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
364+
.rootsChangeHandler((exchange, rootsUpdate) -> rootsRef.set(rootsUpdate))
365+
.build();
365366

366-
var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
367+
try (var mcpClient = clientBuilder.capabilities(ClientCapabilities.builder().roots(true).build())
367368
.roots(roots)
368-
.build()) { // @formatter:on
369+
.build()) {
369370

370371
InitializeResult initResult = mcpClient.initialize();
371372
assertThat(initResult).isNotNull();
@@ -376,6 +377,8 @@ void testRootsServerCloseWithActiveSubscription(String clientType) {
376377
assertThat(rootsRef.get()).containsAll(roots);
377378
});
378379
}
380+
381+
mcpServer.close();
379382
}
380383

381384
// ---------------------------------------
@@ -409,13 +412,12 @@ void testToolCallSuccess(String clientType) {
409412
return callResponse;
410413
});
411414

412-
try ( //@formatter:off
413-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
414-
.capabilities(ServerCapabilities.builder().tools(true).build())
415-
.tools(tool1)
416-
.build();
415+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
416+
.capabilities(ServerCapabilities.builder().tools(true).build())
417+
.tools(tool1)
418+
.build();
417419

418-
var mcpClient = clientBuilder.build()) { // @formatter:on
420+
try (var mcpClient = clientBuilder.build()) {
419421

420422
InitializeResult initResult = mcpClient.initialize();
421423
assertThat(initResult).isNotNull();
@@ -427,6 +429,8 @@ void testToolCallSuccess(String clientType) {
427429
assertThat(response).isNotNull();
428430
assertThat(response).isEqualTo(callResponse);
429431
}
432+
433+
mcpServer.close();
430434
}
431435

432436
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -450,12 +454,12 @@ void testToolListChangeHandlingSuccess(String clientType) {
450454

451455
AtomicReference<List<Tool>> rootsRef = new AtomicReference<>();
452456

453-
try ( //@formatter:off
454-
var mcpServer = McpServer.sync(mcpServerTransportProvider)
455-
.capabilities(ServerCapabilities.builder().tools(true).build())
456-
.tools(tool1)
457-
.build();
457+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
458+
.capabilities(ServerCapabilities.builder().tools(true).build())
459+
.tools(tool1)
460+
.build();
458461

462+
try (
459463
var mcpClient = clientBuilder.toolsChangeConsumer(toolsUpdate -> {
460464
// perform a blocking call to a remote service
461465
String response = RestClient.create()
@@ -465,7 +469,7 @@ void testToolListChangeHandlingSuccess(String clientType) {
465469
.body(String.class);
466470
assertThat(response).isNotBlank();
467471
rootsRef.set(toolsUpdate);
468-
}).build()) { // @formatter:on
472+
}).build()) {
469473

470474
InitializeResult initResult = mcpClient.initialize();
471475
assertThat(initResult).isNotNull();
@@ -498,6 +502,8 @@ void testToolListChangeHandlingSuccess(String clientType) {
498502
assertThat(rootsRef.get()).containsAll(List.of(tool2.tool()));
499503
});
500504
}
505+
506+
mcpServer.close();
501507
}
502508

503509
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -506,13 +512,14 @@ void testInitialize(String clientType) {
506512

507513
var clientBuilder = clientBuilders.get(clientType);
508514

509-
try (// @formatter:off
510-
var mcpServer = McpServer.sync(mcpServerTransportProvider).build();
511-
var mcpClient = clientBuilder.build()) { // @formatter:on
515+
var mcpServer = McpServer.sync(mcpServerTransportProvider).build();
512516

517+
try (var mcpClient = clientBuilder.build()) {
513518
InitializeResult initResult = mcpClient.initialize();
514519
assertThat(initResult).isNotNull();
515520
}
521+
522+
mcpServer.close();
516523
}
517524

518525
// ---------------------------------------
@@ -569,19 +576,17 @@ void testLoggingNotification(String clientType) {
569576
//@formatter:on
570577
});
571578

572-
//@formatter:off
573579
var mcpServer = McpServer.async(mcpServerTransportProvider)
574580
.serverInfo("test-server", "1.0.0")
575581
.capabilities(ServerCapabilities.builder().logging().tools(true).build())
576582
.tools(tool)
577583
.build();
578584

579585
try (
580-
581586
// Create client with logging notification handler
582587
var mcpClient = clientBuilder
583588
.loggingConsumer(notification -> { receivedNotifications.add(notification); })
584-
.build()) { // @formatter:on
589+
.build()) {
585590

586591
// Initialize client
587592
InitializeResult initResult = mcpClient.initialize();

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Disabled;
3333
import org.junit.jupiter.api.Test;
34+
import reactor.core.publisher.Flux;
3435
import reactor.core.publisher.Mono;
3536
import reactor.test.StepVerifier;
3637

@@ -453,15 +454,12 @@ void testLoggingLevelsWithoutInitialization() {
453454
@Test
454455
void testLoggingLevels() {
455456
withClient(createMcpTransport(), mcpAsyncClient -> {
456-
Mono<Object> testAllLevels = mcpAsyncClient.initialize().then(Mono.defer(() -> {
457-
Mono<Object> chain = Mono.empty();
458-
for (McpSchema.LoggingLevel level : McpSchema.LoggingLevel.values()) {
459-
chain = chain.then(mcpAsyncClient.setLoggingLevel(level));
460-
}
461-
return chain;
462-
}));
463-
464-
StepVerifier.create(testAllLevels).expectNextCount(1).verifyComplete();
457+
StepVerifier.create(mcpAsyncClient.initialize()
458+
.thenMany(Flux.fromArray(McpSchema.LoggingLevel.values())
459+
.flatMap(mcpAsyncClient::setLoggingLevel))
460+
.collectList())
461+
.assertNext(l -> assertThat(l).hasSize(McpSchema.LoggingLevel.values().length))
462+
.verifyComplete();
465463
});
466464
}
467465

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,15 +780,15 @@ private NotificationHandler asyncLoggingNotificationHandler(
780780
* @return A Mono that completes when the logging level is set.
781781
* @see McpSchema.LoggingLevel
782782
*/
783-
public Mono<Object> setLoggingLevel(LoggingLevel loggingLevel) {
783+
public Mono<Void> setLoggingLevel(LoggingLevel loggingLevel) {
784784
if (loggingLevel == null) {
785785
return Mono.error(new McpError("Logging level must not be null"));
786786
}
787787

788788
return this.withInitializationCheck("setting logging level", initializedResult -> {
789789
var params = new McpSchema.SetLevelRequest(loggingLevel);
790790
return this.mcpSession.sendRequest(McpSchema.METHOD_LOGGING_SET_LEVEL, params, new TypeReference<Object>() {
791-
});
791+
}).then();
792792
});
793793
}
794794

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,9 @@ public GetPromptResult getPrompt(GetPromptRequest getPromptRequest) {
320320
/**
321321
* Client can set the minimum logging level it wants to receive from the server.
322322
* @param loggingLevel the min logging level
323-
* @return empty response
324323
*/
325-
public Object setLoggingLevel(McpSchema.LoggingLevel loggingLevel) {
326-
return this.delegate.setLoggingLevel(loggingLevel).block();
324+
public void setLoggingLevel(McpSchema.LoggingLevel loggingLevel) {
325+
this.delegate.setLoggingLevel(loggingLevel).block();
327326
}
328327

329328
}

0 commit comments

Comments
 (0)