Skip to content
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

chore: Add AsyncCloseable interface. #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions mcp/src/main/java/io/modelcontextprotocol/spec/AsyncCloseable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.modelcontextprotocol.spec;

import reactor.core.publisher.Mono;

/**
* Interface for close operations that are asynchronous.
*/
public interface AsyncCloseable {

/**
* Begins the process of closing the resource gracefully, if there is one in progress,
* return the existing one.
* @return A {@link Mono} that completes when the resource has been closed.
*/
Mono<Void> closeGracefully();

/**
* Immediately closes the resource gracefully.
*/
default void close() {
this.closeGracefully().subscribe();
}

}
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 AsyncCloseable {

/**
* 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 All @@ -61,6 +62,7 @@ default void close() {
* associated resources asynchronously.
* @return a {@link Mono<Void>} that completes when the connections have been closed.
*/
@Override
Mono<Void> closeGracefully();

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

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

/**
* 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 AsyncCloseable {

/**
* 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 All @@ -54,6 +55,7 @@ default void close() {
* asynchronously.
* @return a {@link Mono<Void>} that completes when the connection has been closed.
*/
@Override
Mono<Void> closeGracefully();

/**
Expand Down