Skip to content

Commit e880736

Browse files
committed
feat: implement AutoCloseable for MCP client and server classes
- Implement AutoCloseable interface for McpAsyncClient, McpAsyncServer, and McpSyncServer - Refactor test methods to use try-with-resources for automatic resource cleanup - Add missing Javadoc for McpSyncServerExchange.setMinLoggingLevel - Change visibility of McpAsyncServerExchange.setMinLoggingLevel to package-private Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent 2805809 commit e880736

File tree

9 files changed

+643
-642
lines changed

9 files changed

+643
-642
lines changed

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

Lines changed: 224 additions & 226 deletions
Large diffs are not rendered by default.

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

Lines changed: 176 additions & 178 deletions
Large diffs are not rendered by default.

mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
* @see McpSchema
7676
* @see McpClientSession
7777
*/
78-
public class McpAsyncClient {
78+
public class McpAsyncClient implements AutoCloseable {
7979

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

@@ -275,6 +275,7 @@ public McpSchema.Implementation getClientInfo() {
275275
/**
276276
* Closes the client connection immediately.
277277
*/
278+
@Override
278279
public void close() {
279280
this.mcpSession.close();
280281
}

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
* @see McpSchema
7373
* @see McpClientSession
7474
*/
75-
public class McpAsyncServer {
75+
public class McpAsyncServer implements AutoCloseable {
7676

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

@@ -121,6 +121,7 @@ public Mono<Void> closeGracefully() {
121121
/**
122122
* Close the server immediately.
123123
*/
124+
@Override
124125
public void close() {
125126
this.delegate.close();
126127
}

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public Mono<Void> loggingNotification(LoggingMessageNotification loggingMessageN
135135
* filtered out.
136136
* @param minLoggingLevel The minimum logging level
137137
*/
138-
public void setMinLoggingLevel(LoggingLevel minLoggingLevel) {
138+
void setMinLoggingLevel(LoggingLevel minLoggingLevel) {
139139
Assert.notNull(minLoggingLevel, "minLoggingLevel must not be null");
140140
this.session.setMinLoggingLevel(minLoggingLevel);
141141
}

mcp/src/main/java/io/modelcontextprotocol/server/McpSyncServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* @see McpAsyncServer
4747
* @see McpSchema
4848
*/
49-
public class McpSyncServer {
49+
public class McpSyncServer implements AutoCloseable {
5050

5151
/**
5252
* The async server to wrap.
@@ -157,6 +157,7 @@ public void closeGracefully() {
157157
/**
158158
* Close the server immediately.
159159
*/
160+
@Override
160161
public void close() {
161162
this.asyncServer.close();
162163
}

mcp/src/main/java/io/modelcontextprotocol/server/McpSyncServerExchange.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public Mono<Void> loggingNotification(LoggingMessageNotification loggingMessageN
9292
return this.exchange.loggingNotification(loggingMessageNotification);
9393
}
9494

95+
/**
96+
* Set the minimum logging level for the client. Messages below this level will be
97+
* filtered out.
98+
* @param minLoggingLevel The minimum logging level
99+
*/
95100
void setMinLoggingLevel(LoggingLevel minLoggingLevel) {
96101
this.exchange.setMinLoggingLevel(minLoggingLevel);
97102
}

mcp/src/test/java/io/modelcontextprotocol/server/transport/HttpServletSseServerCustomContextPathTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
99
import io.modelcontextprotocol.server.McpServer;
1010
import io.modelcontextprotocol.spec.McpSchema;
11-
import org.apache.catalina.Context;
1211
import org.apache.catalina.LifecycleException;
1312
import org.apache.catalina.LifecycleState;
1413
import org.apache.catalina.startup.Tomcat;
@@ -78,9 +77,12 @@ public void after() {
7877

7978
@Test
8079
void testCustomContextPath() {
81-
McpServer.async(mcpServerTransportProvider).serverInfo("test-server", "1.0.0").build();
82-
var client = clientBuilder.clientInfo(new McpSchema.Implementation("Sample " + "client", "0.0.0")).build();
83-
assertThat(client.initialize()).isNotNull();
80+
try (//@formatter:off
81+
var server = McpServer.async(mcpServerTransportProvider).serverInfo("test-server", "1.0.0").build();
82+
var client = clientBuilder.clientInfo(new McpSchema.Implementation("Sample " + "client", "0.0.0")) .build()) { //@formatter:on
83+
84+
assertThat(client.initialize()).isNotNull();
85+
}
8486
}
8587

8688
}

0 commit comments

Comments
 (0)