Skip to content

Commit 75ec813

Browse files
committed
chore: Add AsyncCloseable interface.
Signed-off-by: He-Pin <[email protected]>
1 parent 734d173 commit 75ec813

24 files changed

+70
-156
lines changed

Diff for: mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/server/transport/WebFluxSseServerTransportProvider.java

-5
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,6 @@ public Mono<Void> closeGracefully() {
370370
return Mono.fromRunnable(sink::complete);
371371
}
372372

373-
@Override
374-
public void close() {
375-
sink.complete();
376-
}
377-
378373
}
379374

380375
public static Builder builder() {

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void testCreateMessageWithoutSamplingCapabilities(String clientType) {
121121
.hasMessage("Client must be configured with sampling capabilities");
122122
}
123123
}
124-
server.close();
124+
server.closeGracefully().block();
125125
}
126126

127127
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -189,7 +189,7 @@ void testCreateMessageSuccess(String clientType) {
189189
assertThat(result.stopReason()).isEqualTo(CreateMessageResult.StopReason.STOP_SEQUENCE);
190190
});
191191
}
192-
mcpServer.closeGracefully().block();
192+
mcpServer.closeGracefully();
193193
}
194194

195195
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -267,7 +267,7 @@ void testCreateMessageWithRequestTimeoutSuccess(String clientType) throws Interr
267267
});
268268
}
269269

270-
mcpServer.closeGracefully().block();
270+
mcpServer.closeGracefully();
271271
}
272272

273273
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -326,7 +326,7 @@ void testCreateMessageWithRequestTimeoutFail(String clientType) throws Interrupt
326326

327327
}
328328

329-
mcpServer.closeGracefully().block();
329+
mcpServer.closeGracefully();
330330
}
331331

332332
// ---------------------------------------
@@ -376,7 +376,7 @@ void testRootsSuccess(String clientType) {
376376
});
377377
}
378378

379-
mcpServer.close();
379+
mcpServer.closeGracefully();
380380
}
381381

382382
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -410,7 +410,7 @@ void testRootsWithoutCapability(String clientType) {
410410
}
411411
}
412412

413-
mcpServer.close();
413+
mcpServer.closeGracefully();
414414
}
415415

416416
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -437,7 +437,7 @@ void testRootsNotificationWithEmptyRootsList(String clientType) {
437437
});
438438
}
439439

440-
mcpServer.close();
440+
mcpServer.closeGracefully();
441441
}
442442

443443
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -471,7 +471,7 @@ void testRootsWithMultipleHandlers(String clientType) {
471471
});
472472
}
473473

474-
mcpServer.close();
474+
mcpServer.closeGracefully();
475475
}
476476

477477
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -502,7 +502,7 @@ void testRootsServerCloseWithActiveSubscription(String clientType) {
502502
});
503503
}
504504

505-
mcpServer.close();
505+
mcpServer.closeGracefully();
506506
}
507507

508508
// ---------------------------------------
@@ -554,7 +554,7 @@ void testToolCallSuccess(String clientType) {
554554
assertThat(response).isEqualTo(callResponse);
555555
}
556556

557-
mcpServer.close();
557+
mcpServer.closeGracefully();
558558
}
559559

560560
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -626,7 +626,7 @@ void testToolListChangeHandlingSuccess(String clientType) {
626626
});
627627
}
628628

629-
mcpServer.close();
629+
mcpServer.closeGracefully();
630630
}
631631

632632
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -642,7 +642,7 @@ void testInitialize(String clientType) {
642642
assertThat(initResult).isNotNull();
643643
}
644644

645-
mcpServer.close();
645+
mcpServer.closeGracefully();
646646
}
647647

648648
// ---------------------------------------
@@ -750,7 +750,7 @@ void testLoggingNotification(String clientType) {
750750
assertThat(notificationMap.get("Another error message").data()).isEqualTo("Another error message");
751751
});
752752
}
753-
mcpServer.close();
753+
mcpServer.closeGracefully();
754754
}
755755

756756
// ---------------------------------------
@@ -799,7 +799,7 @@ void testCompletionShouldReturnExpectedSuggestions(String clientType) {
799799
assertThat(samplingRequest.get().ref().type()).isEqualTo("ref/prompt");
800800
}
801801

802-
mcpServer.close();
802+
mcpServer.closeGracefully();
803803
}
804804

805805
}

Diff for: mcp-spring/mcp-spring-webmvc/src/main/java/io/modelcontextprotocol/server/transport/WebMvcSseServerTransportProvider.java

-14
Original file line numberDiff line numberDiff line change
@@ -401,20 +401,6 @@ public Mono<Void> closeGracefully() {
401401
});
402402
}
403403

404-
/**
405-
* Closes the transport immediately.
406-
*/
407-
@Override
408-
public void close() {
409-
try {
410-
sseBuilder.complete();
411-
logger.debug("Successfully completed SSE builder for session {}", sessionId);
412-
}
413-
catch (Exception e) {
414-
logger.warn("Failed to complete SSE builder for session {}: {}", sessionId, e.getMessage());
415-
}
416-
}
417-
418404
}
419405

420406
}

Diff for: mcp-spring/mcp-spring-webmvc/src/test/java/io/modelcontextprotocol/server/WebMvcSseIntegrationTests.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void testCreateMessageWithoutSamplingCapabilities() {
149149
.hasMessage("Client must be configured with sampling capabilities");
150150
}
151151
}
152-
server.close();
152+
server.closeGracefully().block();
153153
}
154154

155155
@Test
@@ -211,7 +211,7 @@ void testCreateMessageSuccess() {
211211

212212
assertThat(response).isNotNull().isEqualTo(callResponse);
213213
}
214-
mcpServer.close();
214+
mcpServer.closeGracefully();
215215
}
216216

217217
@Test
@@ -283,7 +283,7 @@ void testCreateMessageWithRequestTimeoutSuccess() throws InterruptedException {
283283
assertThat(response).isEqualTo(callResponse);
284284

285285
mcpClient.close();
286-
mcpServer.close();
286+
mcpServer.closeGracefully();
287287
}
288288

289289
@Test
@@ -354,7 +354,7 @@ void testCreateMessageWithRequestTimeoutFail() throws InterruptedException {
354354
}).withMessageContaining("Timeout");
355355

356356
mcpClient.close();
357-
mcpServer.close();
357+
mcpServer.closeGracefully();
358358
}
359359

360360
// ---------------------------------------
@@ -401,7 +401,7 @@ void testRootsSuccess() {
401401
});
402402
}
403403

404-
mcpServer.close();
404+
mcpServer.closeGracefully();
405405
}
406406

407407
@Test
@@ -434,7 +434,7 @@ void testRootsWithoutCapability() {
434434
}
435435
}
436436

437-
mcpServer.close();
437+
mcpServer.closeGracefully();
438438
}
439439

440440
@Test
@@ -459,7 +459,7 @@ void testRootsNotificationWithEmptyRootsList() {
459459
});
460460
}
461461

462-
mcpServer.close();
462+
mcpServer.closeGracefully();
463463
}
464464

465465
@Test
@@ -488,7 +488,7 @@ void testRootsWithMultipleHandlers() {
488488
});
489489
}
490490

491-
mcpServer.close();
491+
mcpServer.closeGracefully();
492492
}
493493

494494
@Test
@@ -515,7 +515,7 @@ void testRootsServerCloseWithActiveSubscription() {
515515
});
516516
}
517517

518-
mcpServer.close();
518+
mcpServer.closeGracefully();
519519
}
520520

521521
// ---------------------------------------
@@ -563,7 +563,7 @@ void testToolCallSuccess() {
563563
assertThat(response).isNotNull().isEqualTo(callResponse);
564564
}
565565

566-
mcpServer.close();
566+
mcpServer.closeGracefully();
567567
}
568568

569569
@Test
@@ -632,7 +632,7 @@ void testToolListChangeHandlingSuccess() {
632632
});
633633
}
634634

635-
mcpServer.close();
635+
mcpServer.closeGracefully();
636636
}
637637

638638
@Test
@@ -646,7 +646,7 @@ void testInitialize() {
646646
assertThat(initResult).isNotNull();
647647
}
648648

649-
mcpServer.close();
649+
mcpServer.closeGracefully();
650650
}
651651

652652
}

Diff for: mcp-test/src/main/java/io/modelcontextprotocol/server/AbstractMcpAsyncServerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void testGracefulShutdown() {
8787
void testImmediateClose() {
8888
var mcpAsyncServer = McpServer.async(createMcpTransportProvider()).serverInfo("test-server", "1.0.0").build();
8989

90-
assertThatCode(() -> mcpAsyncServer.close()).doesNotThrowAnyException();
90+
assertThatCode(() -> mcpAsyncServer.closeGracefully().block()).doesNotThrowAnyException();
9191
}
9292

9393
// ---------------------------------------

Diff for: mcp-test/src/main/java/io/modelcontextprotocol/server/AbstractMcpSyncServerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void testGracefulShutdown() {
8484
void testImmediateClose() {
8585
var mcpSyncServer = McpServer.sync(createMcpTransportProvider()).serverInfo("test-server", "1.0.0").build();
8686

87-
assertThatCode(() -> mcpSyncServer.close()).doesNotThrowAnyException();
87+
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
8888
}
8989

9090
@Test

Diff for: mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

-7
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,6 @@ public McpSchema.Implementation getClientInfo() {
287287
return this.clientInfo;
288288
}
289289

290-
/**
291-
* Closes the client connection immediately.
292-
*/
293-
public void close() {
294-
this.mcpSession.close();
295-
}
296-
297290
/**
298291
* Gracefully closes the client connection.
299292
* @return A Mono that completes when the connection is closed

Diff for: mcp/src/main/java/io/modelcontextprotocol/client/McpSyncClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public McpSchema.Implementation getClientInfo() {
115115

116116
@Override
117117
public void close() {
118-
this.delegate.close();
118+
this.closeGracefully();
119119
}
120120

121121
public boolean closeGracefully() {

Diff for: mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java

-12
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,6 @@ public Mono<Void> closeGracefully() {
120120
return this.delegate.closeGracefully();
121121
}
122122

123-
/**
124-
* Close the server immediately.
125-
*/
126-
public void close() {
127-
this.delegate.close();
128-
}
129-
130123
// ---------------------------------------
131124
// Tool Management
132125
// ---------------------------------------
@@ -390,11 +383,6 @@ public Mono<Void> closeGracefully() {
390383
return this.mcpTransportProvider.closeGracefully();
391384
}
392385

393-
@Override
394-
public void close() {
395-
this.mcpTransportProvider.close();
396-
}
397-
398386
private McpServerSession.NotificationHandler asyncRootsListChangedNotificationHandler(
399387
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers) {
400388
return (exchange, params) -> exchange.listRoots()

Diff for: mcp/src/main/java/io/modelcontextprotocol/server/McpSyncServer.java

-7
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,6 @@ public void closeGracefully() {
170170
this.asyncServer.closeGracefully().block();
171171
}
172172

173-
/**
174-
* Close the server immediately.
175-
*/
176-
public void close() {
177-
this.asyncServer.close();
178-
}
179-
180173
/**
181174
* Get the underlying async server instance.
182175
* @return The wrapped async server

Diff for: mcp/src/main/java/io/modelcontextprotocol/server/transport/HttpServletSseServerTransportProvider.java

-15
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,6 @@ public Mono<Void> closeGracefully() {
433433
});
434434
}
435435

436-
/**
437-
* Closes the transport immediately.
438-
*/
439-
@Override
440-
public void close() {
441-
try {
442-
sessions.remove(sessionId);
443-
asyncContext.complete();
444-
logger.debug("Successfully completed async context for session {}", sessionId);
445-
}
446-
catch (Exception e) {
447-
logger.warn("Failed to complete async context for session {}: {}", sessionId, e.getMessage());
448-
}
449-
}
450-
451436
}
452437

453438
/**

Diff for: mcp/src/main/java/io/modelcontextprotocol/server/transport/StdioServerTransportProvider.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ public Mono<Void> closeGracefully() {
173173
});
174174
}
175175

176-
@Override
177-
public void close() {
178-
isClosing.set(true);
179-
logger.debug("Session transport closed");
180-
}
181-
182176
private void initProcessing() {
183177
handleIncomingMessages();
184178
startInboundProcessing();
@@ -239,7 +233,7 @@ private void startInboundProcessing() {
239233
finally {
240234
isClosing.set(true);
241235
if (session != null) {
242-
session.close();
236+
session.closeGracefully().block();
243237
}
244238
inboundSink.tryEmitComplete();
245239
}

0 commit comments

Comments
 (0)