|
20 | 20 | import java.util.function.Function; |
21 | 21 | import java.util.stream.Stream; |
22 | 22 |
|
| 23 | +import ch.qos.logback.classic.Level; |
| 24 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 25 | +import ch.qos.logback.core.read.ListAppender; |
23 | 26 | import io.modelcontextprotocol.AbstractMcpClientServerIntegrationTests; |
24 | 27 | import io.modelcontextprotocol.client.McpClient; |
25 | 28 | import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport; |
|
30 | 33 | import io.modelcontextprotocol.server.transport.TomcatTestUtil; |
31 | 34 | import io.modelcontextprotocol.spec.HttpHeaders; |
32 | 35 | import io.modelcontextprotocol.spec.McpSchema; |
| 36 | +import io.modelcontextprotocol.util.KeepAliveScheduler; |
33 | 37 | import jakarta.servlet.http.HttpServletRequest; |
34 | 38 | import jakarta.servlet.http.HttpServletResponse; |
35 | 39 | import org.apache.catalina.LifecycleException; |
|
42 | 46 | import org.junit.jupiter.api.Test; |
43 | 47 | import org.junit.jupiter.api.Timeout; |
44 | 48 | import org.junit.jupiter.params.provider.Arguments; |
| 49 | +import org.slf4j.LoggerFactory; |
45 | 50 | import reactor.core.publisher.Mono; |
46 | 51 | import reactor.test.StepVerifier; |
47 | 52 |
|
@@ -107,7 +112,7 @@ public void before() { |
107 | 112 | mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder() |
108 | 113 | .contextExtractor(TEST_CONTEXT_EXTRACTOR) |
109 | 114 | .mcpEndpoint(MESSAGE_ENDPOINT) |
110 | | - .keepAliveInterval(Duration.ofSeconds(1)) |
| 115 | + .keepAliveInterval(Duration.ofMillis(200)) |
111 | 116 | .maxRequestSize(MAX_REQUEST_SIZE) |
112 | 117 | .build(); |
113 | 118 | MCP_SERVLET.setDelegate(mcpServerTransportProvider); |
@@ -267,6 +272,46 @@ void replacedListeningStreamIsClosed() throws Exception { |
267 | 272 | }); |
268 | 273 | } |
269 | 274 |
|
| 275 | + @Test |
| 276 | + void keepAliveSkipsSessionsWithoutListeningStream() throws Exception { |
| 277 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); |
| 278 | + var httpClient = HttpClient.newHttpClient(); |
| 279 | + var keepAliveLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(KeepAliveScheduler.class); |
| 280 | + ListAppender<ILoggingEvent> logAppender = new ListAppender<>(); |
| 281 | + logAppender.start(); |
| 282 | + keepAliveLogger.addAppender(logAppender); |
| 283 | + |
| 284 | + try { |
| 285 | + // A client is free to never issue the GET request establishing a listening |
| 286 | + // stream. Such a session has nothing to write a ping to, so it must not be |
| 287 | + // pinged on every keep-alive interval. |
| 288 | + initializeSession(httpClient); |
| 289 | + |
| 290 | + // The keep-alive interval is 200ms, so this spans several intervals |
| 291 | + Thread.sleep(1_000); |
| 292 | + |
| 293 | + assertThat(logAppender.list).noneMatch(event -> event.getLevel() == Level.WARN); |
| 294 | + } |
| 295 | + finally { |
| 296 | + keepAliveLogger.detachAppender(logAppender); |
| 297 | + logAppender.stop(); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + void keepAlivePingsSessionsWithListeningStream() throws Exception { |
| 303 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); |
| 304 | + var httpClient = HttpClient.newHttpClient(); |
| 305 | + |
| 306 | + var sessionId = initializeSession(httpClient); |
| 307 | + var stream = openListeningStream(httpClient, sessionId, null); |
| 308 | + awaitStreamOpen(stream); |
| 309 | + |
| 310 | + // Sessions with a listening stream are still pinged |
| 311 | + await().atMost(Duration.ofSeconds(1)) |
| 312 | + .untilAsserted(() -> assertThat(stream.events()).anyMatch(line -> line.contains("\"method\":\"ping\""))); |
| 313 | + } |
| 314 | + |
270 | 315 | private String initializeSession(HttpClient httpClient) throws Exception { |
271 | 316 | var initialize = HttpRequest.newBuilder() |
272 | 317 | .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) |
|
0 commit comments