@@ -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 ();
0 commit comments