Skip to content

Commit 9eb1d26

Browse files
committed
KeepAliveScheduler only pings sessions with an open stream
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent f8380c9 commit 9eb1d26

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HttpServletStreamableServerTransportProvider.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.modelcontextprotocol.spec.HttpHeaders;
2121
import io.modelcontextprotocol.spec.McpError;
2222
import io.modelcontextprotocol.spec.McpSchema;
23+
import io.modelcontextprotocol.spec.McpSession;
2324
import io.modelcontextprotocol.spec.McpStreamableServerSession;
2425
import io.modelcontextprotocol.spec.McpStreamableServerTransport;
2526
import io.modelcontextprotocol.spec.McpStreamableServerTransportProvider;
@@ -165,8 +166,7 @@ private HttpServletStreamableServerTransportProvider(McpJsonMapper jsonMapper, S
165166

166167
if (keepAliveInterval != null) {
167168

168-
this.keepAliveScheduler = KeepAliveScheduler
169-
.builder(() -> (isClosing) ? Flux.empty() : Flux.fromIterable(sessions.values()))
169+
this.keepAliveScheduler = KeepAliveScheduler.builder(this::sessionsToPing)
170170
.initialDelay(keepAliveInterval)
171171
.interval(keepAliveInterval)
172172
.build();
@@ -176,6 +176,22 @@ private HttpServletStreamableServerTransportProvider(McpJsonMapper jsonMapper, S
176176

177177
}
178178

179+
/**
180+
* Returns the sessions a keep-alive ping can be sent to, that is the sessions having
181+
* a listening stream. A session without one, e.g. a client which only ever issues
182+
* POST requests, has nothing to write a ping to: pinging it would fail on every
183+
* interval without ever telling us anything about the client being alive.
184+
* @return the sessions to ping
185+
*/
186+
private Flux<McpSession> sessionsToPing() {
187+
if (this.isClosing) {
188+
return Flux.empty();
189+
}
190+
return Flux.fromIterable(this.sessions.values())
191+
.filter(McpStreamableServerSession::hasListeningStream)
192+
.cast(McpSession.class);
193+
}
194+
179195
@Override
180196
public void setSessionFactory(McpStreamableServerSession.Factory sessionFactory) {
181197
this.sessionFactory = sessionFactory;

‎mcp-core/src/main/java/io/modelcontextprotocol/spec/McpStreamableServerSession.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ public Mono<Void> delete() {
178178
}));
179179
}
180180

181+
/**
182+
* Whether the session currently has a listening stream, that is a stream the server
183+
* can send its own requests and notifications to. Sessions have none until the client
184+
* issues the GET request establishing one, and clients are not required to ever issue
185+
* it.
186+
* @return {@code true} if the session has a listening stream
187+
*/
188+
public boolean hasListeningStream() {
189+
return this.listeningStreamRef.get() instanceof McpStreamableServerSessionStream;
190+
}
191+
181192
/**
182193
* Create a listening stream (the generic HTTP GET request, with or without a
183194
* Last-Event-ID header). A session addresses a single listening stream at a time, so

‎mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletStreamableIntegrationTests.java‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.function.Function;
2121
import java.util.stream.Stream;
2222

23+
import ch.qos.logback.classic.Level;
24+
import ch.qos.logback.classic.spi.ILoggingEvent;
25+
import ch.qos.logback.core.read.ListAppender;
2326
import io.modelcontextprotocol.AbstractMcpClientServerIntegrationTests;
2427
import io.modelcontextprotocol.client.McpClient;
2528
import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport;
@@ -30,6 +33,7 @@
3033
import io.modelcontextprotocol.server.transport.TomcatTestUtil;
3134
import io.modelcontextprotocol.spec.HttpHeaders;
3235
import io.modelcontextprotocol.spec.McpSchema;
36+
import io.modelcontextprotocol.util.KeepAliveScheduler;
3337
import jakarta.servlet.http.HttpServletRequest;
3438
import jakarta.servlet.http.HttpServletResponse;
3539
import org.apache.catalina.LifecycleException;
@@ -42,6 +46,7 @@
4246
import org.junit.jupiter.api.Test;
4347
import org.junit.jupiter.api.Timeout;
4448
import org.junit.jupiter.params.provider.Arguments;
49+
import org.slf4j.LoggerFactory;
4550
import reactor.core.publisher.Mono;
4651
import reactor.test.StepVerifier;
4752

@@ -107,7 +112,7 @@ public void before() {
107112
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder()
108113
.contextExtractor(TEST_CONTEXT_EXTRACTOR)
109114
.mcpEndpoint(MESSAGE_ENDPOINT)
110-
.keepAliveInterval(Duration.ofSeconds(1))
115+
.keepAliveInterval(Duration.ofMillis(200))
111116
.maxRequestSize(MAX_REQUEST_SIZE)
112117
.build();
113118
MCP_SERVLET.setDelegate(mcpServerTransportProvider);
@@ -267,6 +272,46 @@ void replacedListeningStreamIsClosed() throws Exception {
267272
});
268273
}
269274

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+
270315
private String initializeSession(HttpClient httpClient) throws Exception {
271316
var initialize = HttpRequest.newBuilder()
272317
.uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT))

0 commit comments

Comments
 (0)