@@ -222,14 +222,13 @@ void testRootsSuccess() {
222222
223223 AtomicReference <List <Root >> rootsRef = new AtomicReference <>();
224224
225- //@formatter:off
226- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
227- .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef .set (rootsUpdate ))
228- .build ();
225+ var mcpServer = McpServer .sync (mcpServerTransportProvider )
226+ .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef .set (rootsUpdate ))
227+ .build ();
229228
230- var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
231- .roots (roots )
232- .build ()) {//@formatter:on
229+ try ( var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
230+ .roots (roots )
231+ .build ()) {
233232
234233 InitializeResult initResult = mcpClient .initialize ();
235234 assertThat (initResult ).isNotNull ();
@@ -257,6 +256,8 @@ void testRootsSuccess() {
257256 assertThat (rootsRef .get ()).containsAll (List .of (roots .get (1 ), root3 ));
258257 });
259258 }
259+
260+ mcpServer .close ();
260261 }
261262
262263 @ Test
@@ -270,17 +271,13 @@ void testRootsWithoutCapability() {
270271 return mock (CallToolResult .class );
271272 });
272273
273- //@formatter:off
274- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
275- .rootsChangeHandler ((exchange , rootsUpdate ) -> {})
276- .tools (tool )
277- .build ();
274+ var mcpServer = McpServer .sync (mcpServerTransportProvider ).rootsChangeHandler ((exchange , rootsUpdate ) -> {
275+ }).tools (tool ).build ();
278276
279- // Create client without roots capability
280- // No roots capability
281- var mcpClient = clientBuilder
282- .capabilities (ClientCapabilities .builder ().build ())
283- .build ()) {//@formatter:on
277+ try (
278+ // Create client without roots capability
279+ // No roots capability
280+ var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().build ()).build ()) {
284281
285282 assertThat (mcpClient .initialize ()).isNotNull ();
286283
@@ -292,20 +289,21 @@ void testRootsWithoutCapability() {
292289 assertThat (e ).isInstanceOf (McpError .class ).hasMessage ("Roots not supported" );
293290 }
294291 }
292+
293+ mcpServer .close ();
295294 }
296295
297296 @ Test
298297 void testRootsNotifciationWithEmptyRootsList () {
299298 AtomicReference <List <Root >> rootsRef = new AtomicReference <>();
300299
301- //@formatter:off
302- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
303- .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef .set (rootsUpdate ))
304- .build ();
300+ var mcpServer = McpServer .sync (mcpServerTransportProvider )
301+ .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef .set (rootsUpdate ))
302+ .build ();
305303
306- var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
307- .roots (List .of ()) // Empty roots list
308- .build ()) {//@formatter:on
304+ try ( var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
305+ .roots (List .of ()) // Empty roots list
306+ .build ()) {
309307
310308 InitializeResult initResult = mcpClient .initialize ();
311309 assertThat (initResult ).isNotNull ();
@@ -316,6 +314,8 @@ void testRootsNotifciationWithEmptyRootsList() {
316314 assertThat (rootsRef .get ()).isEmpty ();
317315 });
318316 }
317+
318+ mcpServer .close ();
319319 }
320320
321321 @ Test
@@ -325,15 +325,14 @@ void testRootsWithMultipleHandlers() {
325325 AtomicReference <List <Root >> rootsRef1 = new AtomicReference <>();
326326 AtomicReference <List <Root >> rootsRef2 = new AtomicReference <>();
327327
328- //@formatter:off
329- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
330- .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef1 .set (rootsUpdate ))
331- .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef2 .set (rootsUpdate ))
332- .build ();
328+ var mcpServer = McpServer .sync (mcpServerTransportProvider )
329+ .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef1 .set (rootsUpdate ))
330+ .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef2 .set (rootsUpdate ))
331+ .build ();
333332
334- var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
335- .roots (roots )
336- .build ()) {//@formatter:on
333+ try ( var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
334+ .roots (roots )
335+ .build ()) {
337336
338337 assertThat (mcpClient .initialize ()).isNotNull ();
339338
@@ -344,6 +343,8 @@ void testRootsWithMultipleHandlers() {
344343 assertThat (rootsRef2 .get ()).containsAll (roots );
345344 });
346345 }
346+
347+ mcpServer .close ();
347348 }
348349
349350 @ Test
@@ -352,14 +353,13 @@ void testRootsServerCloseWithActiveSubscription() {
352353
353354 AtomicReference <List <Root >> rootsRef = new AtomicReference <>();
354355
355- //@formatter:off
356- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
357- .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef .set (rootsUpdate ))
358- .build ();
356+ var mcpServer = McpServer .sync (mcpServerTransportProvider )
357+ .rootsChangeHandler ((exchange , rootsUpdate ) -> rootsRef .set (rootsUpdate ))
358+ .build ();
359359
360- var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
361- .roots (roots )
362- .build ()) {//@formatter:on
360+ try ( var mcpClient = clientBuilder .capabilities (ClientCapabilities .builder ().roots (true ).build ())
361+ .roots (roots )
362+ .build ()) {
363363
364364 InitializeResult initResult = mcpClient .initialize ();
365365 assertThat (initResult ).isNotNull ();
@@ -370,6 +370,8 @@ void testRootsServerCloseWithActiveSubscription() {
370370 assertThat (rootsRef .get ()).containsAll (roots );
371371 });
372372 }
373+
374+ mcpServer .close ();
373375 }
374376
375377 // ---------------------------------------
@@ -400,13 +402,12 @@ void testToolCallSuccess() {
400402 return callResponse ;
401403 });
402404
403- //@formatter:off
404- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
405- .capabilities (ServerCapabilities .builder ().tools (true ).build ())
406- .tools (tool1 )
407- .build ();
405+ var mcpServer = McpServer .sync (mcpServerTransportProvider )
406+ .capabilities (ServerCapabilities .builder ().tools (true ).build ())
407+ .tools (tool1 )
408+ .build ();
408409
409- var mcpClient = clientBuilder .build ()) {//@formatter:on
410+ try ( var mcpClient = clientBuilder .build ()) {
410411
411412 InitializeResult initResult = mcpClient .initialize ();
412413 assertThat (initResult ).isNotNull ();
@@ -417,6 +418,8 @@ void testToolCallSuccess() {
417418
418419 assertThat (response ).isNotNull ().isEqualTo (callResponse );
419420 }
421+
422+ mcpServer .close ();
420423 }
421424
422425 @ Test
@@ -437,22 +440,21 @@ void testToolListChangeHandlingSuccess() {
437440
438441 AtomicReference <List <Tool >> rootsRef = new AtomicReference <>();
439442
440- //@formatter:off
441- try (var mcpServer = McpServer .sync (mcpServerTransportProvider )
442- .capabilities (ServerCapabilities .builder ().tools (true ).build ())
443- .tools (tool1 )
444- .build ();
445-
446- var mcpClient = clientBuilder .toolsChangeConsumer (toolsUpdate -> {
447- // perform a blocking call to a remote service
448- String response = RestClient .create ()
449- .get ()
450- .uri ("https://raw.githubusercontent.com/modelcontextprotocol/java-sdk/refs/heads/main/README.md" )
451- .retrieve ()
452- .body (String .class );
453- assertThat (response ).isNotBlank ();
454- rootsRef .set (toolsUpdate );
455- }).build ()) {//@formatter:on
443+ var mcpServer = McpServer .sync (mcpServerTransportProvider )
444+ .capabilities (ServerCapabilities .builder ().tools (true ).build ())
445+ .tools (tool1 )
446+ .build ();
447+
448+ try (var mcpClient = clientBuilder .toolsChangeConsumer (toolsUpdate -> {
449+ // perform a blocking call to a remote service
450+ String response = RestClient .create ()
451+ .get ()
452+ .uri ("https://raw.githubusercontent.com/modelcontextprotocol/java-sdk/refs/heads/main/README.md" )
453+ .retrieve ()
454+ .body (String .class );
455+ assertThat (response ).isNotBlank ();
456+ rootsRef .set (toolsUpdate );
457+ }).build ()) {
456458
457459 InitializeResult initResult = mcpClient .initialize ();
458460 assertThat (initResult ).isNotNull ();
@@ -485,18 +487,22 @@ void testToolListChangeHandlingSuccess() {
485487 assertThat (rootsRef .get ()).containsAll (List .of (tool2 .tool ()));
486488 });
487489 }
490+
491+ mcpServer .close ();
488492 }
489493
490494 @ Test
491495 void testInitialize () {
492496
493- //@formatter:off
494- try ( var mcpServer = McpServer . sync ( mcpServerTransportProvider ). build ();
495- var mcpClient = clientBuilder .build ()) {//@formatter:on
497+ var mcpServer = McpServer . sync ( mcpServerTransportProvider ). build ();
498+
499+ try ( var mcpClient = clientBuilder .build ()) {
496500
497501 InitializeResult initResult = mcpClient .initialize ();
498502 assertThat (initResult ).isNotNull ();
499503 }
504+
505+ mcpServer .close ();
500506 }
501507
502508}
0 commit comments