Skip to content

Commit c8a8746

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

File tree

15 files changed

+25
-101
lines changed

15 files changed

+25
-101
lines changed

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

-6
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,6 @@ public <T> T unmarshalFrom(Object data, TypeReference<T> typeRef) {
369369
public Mono<Void> closeGracefully() {
370370
return Mono.fromRunnable(sink::complete);
371371
}
372-
373-
@Override
374-
public void close() {
375-
sink.complete();
376-
}
377-
378372
}
379373

380374
public static Builder builder() {

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/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

-5
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,6 @@ public Mono<Void> closeGracefully() {
390390
return this.mcpTransportProvider.closeGracefully();
391391
}
392392

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

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

-16
Original file line numberDiff line numberDiff line change
@@ -432,22 +432,6 @@ public Mono<Void> closeGracefully() {
432432
}
433433
});
434434
}
435-
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-
451435
}
452436

453437
/**

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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.modelcontextprotocol.spec;
2+
3+
import reactor.core.publisher.Mono;
4+
5+
/**
6+
* Interface for close operations that are asynchronous.
7+
*/
8+
public interface AsyncCloseable {
9+
10+
/**
11+
* Begins the process of closing the resource gracefully.
12+
* @return A {@link Mono} that completes when the resource has been closed.
13+
*/
14+
Mono<Void> closeGracefully();
15+
}

Diff for: mcp/src/main/java/io/modelcontextprotocol/spec/McpClientSession.java

-10
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,4 @@ public Mono<Void> closeGracefully() {
276276
return transport.closeGracefully();
277277
});
278278
}
279-
280-
/**
281-
* Closes the session immediately, potentially interrupting pending operations.
282-
*/
283-
@Override
284-
public void close() {
285-
this.connection.dispose();
286-
transport.close();
287-
}
288-
289279
}

Diff for: mcp/src/main/java/io/modelcontextprotocol/spec/McpServerSession.java

-5
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,6 @@ public Mono<Void> closeGracefully() {
272272
return this.transport.closeGracefully();
273273
}
274274

275-
@Override
276-
public void close() {
277-
this.transport.close();
278-
}
279-
280275
/**
281276
* Request handler for the initialization request.
282277
*/

Diff for: mcp/src/main/java/io/modelcontextprotocol/spec/McpServerTransportProvider.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Dariusz Jędrzejczyk
3131
*/
32-
public interface McpServerTransportProvider {
32+
public interface McpServerTransportProvider extends AsyncCloseable {
3333

3434
/**
3535
* Sets the session factory that will be used to create sessions for new clients. An
@@ -48,19 +48,12 @@ public interface McpServerTransportProvider {
4848
*/
4949
Mono<Void> notifyClients(String method, Object params);
5050

51-
/**
52-
* Immediately closes all the transports with connected clients and releases any
53-
* associated resources.
54-
*/
55-
default void close() {
56-
this.closeGracefully().subscribe();
57-
}
58-
5951
/**
6052
* Gracefully closes all the transports with connected clients and releases any
6153
* associated resources asynchronously.
6254
* @return a {@link Mono<Void>} that completes when the connections have been closed.
6355
*/
56+
@Override
6457
Mono<Void> closeGracefully();
6558

6659
}

Diff for: mcp/src/main/java/io/modelcontextprotocol/spec/McpSession.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Christian Tzolov
2424
* @author Dariusz Jędrzejczyk
2525
*/
26-
public interface McpSession {
26+
public interface McpSession extends AsyncCloseable {
2727

2828
/**
2929
* Sends a request to the model counterparty and expects a response of type T.
@@ -72,11 +72,7 @@ default Mono<Void> sendNotification(String method) {
7272
* Closes the session and releases any associated resources asynchronously.
7373
* @return a {@link Mono<Void>} that completes when the session has been closed.
7474
*/
75+
@Override
7576
Mono<Void> closeGracefully();
7677

77-
/**
78-
* Closes the session and releases any associated resources.
79-
*/
80-
void close();
81-
8278
}

Diff for: mcp/src/main/java/io/modelcontextprotocol/spec/McpTransport.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,14 @@
3535
* @author Christian Tzolov
3636
* @author Dariusz Jędrzejczyk
3737
*/
38-
public interface McpTransport {
39-
40-
/**
41-
* Closes the transport connection and releases any associated resources.
42-
*
43-
* <p>
44-
* This method ensures proper cleanup of resources when the transport is no longer
45-
* needed. It should handle the graceful shutdown of any active connections.
46-
* </p>
47-
*/
48-
default void close() {
49-
this.closeGracefully().subscribe();
50-
}
38+
public interface McpTransport extends AsyncCloseable {
5139

5240
/**
5341
* Closes the transport connection and releases any associated resources
5442
* asynchronously.
5543
* @return a {@link Mono<Void>} that completes when the connection has been closed.
5644
*/
45+
@Override
5746
Mono<Void> closeGracefully();
5847

5948
/**

Diff for: mcp/src/test/java/io/modelcontextprotocol/server/transport/StdioServerTransportProviderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void shouldHandleSessionClose() throws Exception {
218218
transportProvider.setSessionFactory(sessionFactory);
219219

220220
// Close the transport provider
221-
transportProvider.close();
221+
transportProvider.closeGracefully().block();
222222

223223
// Verify session was closed
224224
verify(mockSession).closeGracefully();

Diff for: mcp/src/test/java/io/modelcontextprotocol/spec/McpClientSessionTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void setUp() {
5353
@AfterEach
5454
void tearDown() {
5555
if (session != null) {
56-
session.close();
56+
session.closeGracefully().block();
5757
}
5858
}
5959

0 commit comments

Comments
 (0)