Skip to content

feat: Added AutoCloseable interface #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* the initialized notification</li>
* </ul>
*/
class LifecycleInitializer {
class LifecycleInitializer implements AutoCloseable {

private static final Logger logger = LoggerFactory.getLogger(LifecycleInitializer.class);

Expand Down Expand Up @@ -326,6 +326,7 @@ private Mono<McpSchema.InitializeResult> doInitialize(DefaultInitialization init
/**
* Closes the current initialization if it exists.
*/
@Override
public void close() {
DefaultInitialization current = this.initializationRef.getAndSet(null);
if (current != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
* @see McpClientSession
* @see McpClientTransport
*/
public class McpAsyncClient {
public class McpAsyncClient implements AutoCloseable {

private static final Logger logger = LoggerFactory.getLogger(McpAsyncClient.class);

Expand Down Expand Up @@ -315,6 +315,7 @@ public McpSchema.Implementation getClientInfo() {
/**
* Closes the client connection immediately.
*/
@Override
public void close() {
this.initializer.close();
this.transport.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
* @see McpSchema
* @see McpClientSession
*/
public class McpAsyncServer {
public class McpAsyncServer implements AutoCloseable {

private static final Logger logger = LoggerFactory.getLogger(McpAsyncServer.class);

Expand Down Expand Up @@ -254,6 +254,7 @@ public Mono<Void> closeGracefully() {
/**
* Close the server immediately.
*/
@Override
public void close() {
this.mcpTransportProvider.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* @see McpAsyncServer
* @see McpSchema
*/
public class McpSyncServer {
public class McpSyncServer implements AutoCloseable {

/**
* The async server to wrap.
Expand Down Expand Up @@ -203,6 +203,7 @@ public void closeGracefully() {
/**
* Close the server immediately.
*/
@Override
public void close() {
this.asyncServer.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author Dariusz Jędrzejczyk
*/
public interface McpServerTransportProvider {
public interface McpServerTransportProvider extends AutoCloseable {

/**
* Sets the session factory that will be used to create sessions for new clients. An
Expand All @@ -52,6 +52,7 @@ public interface McpServerTransportProvider {
* Immediately closes all the transports with connected clients and releases any
* associated resources.
*/
@Override
default void close() {
this.closeGracefully().subscribe();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author Christian Tzolov
* @author Dariusz Jędrzejczyk
*/
public interface McpSession {
public interface McpSession extends AutoCloseable {

/**
* Sends a request to the model counterparty and expects a response of type T.
Expand Down Expand Up @@ -75,6 +75,7 @@ default Mono<Void> sendNotification(String method) {
/**
* Closes the session and releases any associated resources.
*/
@Override
void close();

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author Christian Tzolov
* @author Dariusz Jędrzejczyk
*/
public interface McpTransport {
public interface McpTransport extends AutoCloseable {

/**
* Closes the transport connection and releases any associated resources.
Expand All @@ -45,6 +45,7 @@ public interface McpTransport {
* needed. It should handle the graceful shutdown of any active connections.
* </p>
*/
@Override
default void close() {
this.closeGracefully().subscribe();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* manages.
* @author Dariusz Jędrzejczyk
*/
public interface McpTransportSession<CONNECTION> {
public interface McpTransportSession<CONNECTION> extends AutoCloseable {

/**
* In case of stateful MCP servers, the value is present and contains the String
Expand Down Expand Up @@ -49,6 +49,7 @@ public interface McpTransportSession<CONNECTION> {
/**
* Close and clear the monitored resources. Potentially asynchronous.
*/
@Override
void close();

/**
Expand Down