Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

/**
* @author Christian Tzolov
* @author Armen Petrosyan
*/
@ConfigurationProperties(McpServerStreamableHttpProperties.CONFIG_PREFIX)
public class McpServerStreamableHttpProperties {
Expand All @@ -42,6 +43,19 @@ public class McpServerStreamableHttpProperties {

private boolean disallowDelete;

/**
* Maximum number of concurrent sessions. Requests to initialize a new session are
* rejected once this limit is reached.
*/
private long maxSessions = 100_000;

/**
* Duration of inactivity after which a session is evicted and closed. Must be greater
* than the keep-alive interval when both are set. When not set, idle sessions are
* never evicted.
*/
private @Nullable Duration sessionIdleTimeout;

public String getMcpEndpoint() {
return this.mcpEndpoint;
}
Expand All @@ -68,4 +82,32 @@ public void setDisallowDelete(boolean disallowDelete) {
this.disallowDelete = disallowDelete;
}

/**
* @since 2.1.1
*/
public long getMaxSessions() {
return this.maxSessions;
}

/**
* @since 2.1.1
*/
public void setMaxSessions(long maxSessions) {
this.maxSessions = maxSessions;
}

/**
* @since 2.1.1
*/
public @Nullable Duration getSessionIdleTimeout() {
return this.sessionIdleTimeout;
}

/**
* @since 2.1.1
*/
public void setSessionIdleTimeout(@Nullable Duration sessionIdleTimeout) {
this.sessionIdleTimeout = sessionIdleTimeout;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
/**
* @author Christian Tzolov
* @author Yanming Zhou
* @author Armen Petrosyan
*/
// before: McpServerAutoConfiguration defines a low priority
// McpServerTransportProviderBase bean and this conf should have priority
Expand All @@ -59,7 +60,9 @@ public WebFluxStreamableServerTransportProvider webFluxStreamableServerTransport
.jsonMapper(new JacksonMcpJsonMapper(jsonMapper))
.messageEndpoint(serverProperties.getMcpEndpoint())
.keepAliveInterval(serverProperties.getKeepAliveInterval())
.disallowDelete(serverProperties.isDisallowDelete());
.disallowDelete(serverProperties.isDisallowDelete())
.maxSessions(serverProperties.getMaxSessions())
.sessionIdleTimeout(serverProperties.getSessionIdleTimeout());
customizers.orderedStream().forEach(customizer -> customizer.customize(builder));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.ai.mcp.server.webflux.autoconfigure;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -103,6 +104,40 @@ void disallowDeleteFalseConfiguration() {
});
}

@Test
void sessionLimitsDefaultConfiguration() {
this.contextRunner.run(context -> {
WebFluxStreamableServerTransportProvider provider = context
.getBean(WebFluxStreamableServerTransportProvider.class);
assertThat(provider).extracting("maxSessions").isEqualTo(100_000L);
assertThat(provider).extracting("sessionIdleTimeout").isNull();
});
}

@Test
void sessionLimitsConfiguration() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.server.streamable-http.max-sessions=50",
"spring.ai.mcp.server.streamable-http.session-idle-timeout=PT10M")
.run(context -> {
WebFluxStreamableServerTransportProvider provider = context
.getBean(WebFluxStreamableServerTransportProvider.class);
assertThat(provider).extracting("maxSessions").isEqualTo(50L);
assertThat(provider).extracting("sessionIdleTimeout").isEqualTo(Duration.ofMinutes(10));
});
}

@Test
void sessionIdleTimeoutNotGreaterThanKeepAliveIntervalFails() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.server.streamable-http.keep-alive-interval=PT30S",
"spring.ai.mcp.server.streamable-http.session-idle-timeout=PT30S")
.run(context -> assertThat(context).hasFailed()
.getFailure()
.rootCause()
.hasMessageContaining("sessionIdleTimeout must be greater than keepAliveInterval"));
}

@Test
void customJsonMapperIsUsed() {
JsonMapper customJsonMapper = new JsonMapper();
Expand Down Expand Up @@ -172,11 +207,15 @@ void allPropertiesConfiguration() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.server.streamable-http.mcp-endpoint=/custom-endpoint",
"spring.ai.mcp.server.streamable-http.keep-alive-interval=PT45S",
"spring.ai.mcp.server.streamable-http.disallow-delete=true")
"spring.ai.mcp.server.streamable-http.disallow-delete=true",
"spring.ai.mcp.server.streamable-http.max-sessions=50",
"spring.ai.mcp.server.streamable-http.session-idle-timeout=PT10M")
.run(context -> {
WebFluxStreamableServerTransportProvider provider = context
.getBean(WebFluxStreamableServerTransportProvider.class);
assertThat(provider).extracting("mcpEndpoint").isEqualTo("/custom-endpoint");
assertThat(provider).extracting("maxSessions").isEqualTo(50L);
assertThat(provider).extracting("sessionIdleTimeout").isEqualTo(Duration.ofMinutes(10));
// Verify beans are created successfully with all properties
assertThat(context).hasSingleBean(WebFluxStreamableServerTransportProvider.class);
assertThat(context).hasSingleBean(RouterFunction.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
/**
* @author Christian Tzolov
* @author Yanming Zhou
* @author Armen Petrosyan
*/
// before: McpServerAutoConfiguration defines a low priority
// McpServerTransportProviderBase bean and this conf should have priority
Expand All @@ -60,7 +61,9 @@ public WebMvcStreamableServerTransportProvider webMvcStreamableServerTransportPr
.jsonMapper(new JacksonMcpJsonMapper(jsonMapper))
.mcpEndpoint(serverProperties.getMcpEndpoint())
.keepAliveInterval(serverProperties.getKeepAliveInterval())
.disallowDelete(serverProperties.isDisallowDelete());
.disallowDelete(serverProperties.isDisallowDelete())
.maxSessions(serverProperties.getMaxSessions())
.sessionIdleTimeout(serverProperties.getSessionIdleTimeout());
customizers.orderedStream().forEach(customizer -> customizer.customize(builder));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.ai.mcp.server.webmvc.autoconfigure;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -103,6 +104,40 @@ void disallowDeleteFalseConfiguration() {
});
}

@Test
void sessionLimitsDefaultConfiguration() {
this.contextRunner.run(context -> {
WebMvcStreamableServerTransportProvider provider = context
.getBean(WebMvcStreamableServerTransportProvider.class);
assertThat(provider).extracting("maxSessions").isEqualTo(100_000L);
assertThat(provider).extracting("sessionIdleTimeout").isNull();
});
}

@Test
void sessionLimitsConfiguration() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.server.streamable-http.max-sessions=50",
"spring.ai.mcp.server.streamable-http.session-idle-timeout=PT10M")
.run(context -> {
WebMvcStreamableServerTransportProvider provider = context
.getBean(WebMvcStreamableServerTransportProvider.class);
assertThat(provider).extracting("maxSessions").isEqualTo(50L);
assertThat(provider).extracting("sessionIdleTimeout").isEqualTo(Duration.ofMinutes(10));
});
}

@Test
void sessionIdleTimeoutNotGreaterThanKeepAliveIntervalFails() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.server.streamable-http.keep-alive-interval=PT30S",
"spring.ai.mcp.server.streamable-http.session-idle-timeout=PT30S")
.run(context -> assertThat(context).hasFailed()
.getFailure()
.rootCause()
.hasMessageContaining("sessionIdleTimeout must be greater than keepAliveInterval"));
}

@Test
void customJsonMapperIsUsed() {
JsonMapper customJsonMapper = new JsonMapper();
Expand Down Expand Up @@ -172,11 +207,15 @@ void allPropertiesConfiguration() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.server.streamable-http.mcp-endpoint=/custom-endpoint",
"spring.ai.mcp.server.streamable-http.keep-alive-interval=PT45S",
"spring.ai.mcp.server.streamable-http.disallow-delete=true")
"spring.ai.mcp.server.streamable-http.disallow-delete=true",
"spring.ai.mcp.server.streamable-http.max-sessions=50",
"spring.ai.mcp.server.streamable-http.session-idle-timeout=PT10M")
.run(context -> {
WebMvcStreamableServerTransportProvider provider = context
.getBean(WebMvcStreamableServerTransportProvider.class);
assertThat(provider).extracting("mcpEndpoint").isEqualTo("/custom-endpoint");
assertThat(provider).extracting("maxSessions").isEqualTo(50L);
assertThat(provider).extracting("sessionIdleTimeout").isEqualTo(Duration.ofMinutes(10));
// Verify beans are created successfully with all properties
assertThat(context).hasSingleBean(WebMvcStreamableServerTransportProvider.class);
assertThat(context).hasSingleBean(RouterFunction.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ All streamable-HTTP properties are prefixed with `spring.ai.mcp.server.streamabl
|`mcp-endpoint` |Custom MCP endpoint path |`/mcp`
|`keep-alive-interval` |Connection keep-alive interval |`null` (disabled)
|`disallow-delete` |Disallow delete operations |`false`
|`max-sessions` |Maximum number of concurrent sessions |`100000`
|`session-idle-timeout` |Duration of inactivity after which a session is evicted |`null` (disabled)
|===

== Features and Capabilities
Expand Down Expand Up @@ -341,6 +343,27 @@ spring:

NOTE: Currently, for streamable-http servers, the keep-alive mechanism is available only for the link:https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#listening-for-messages-from-the-server[Listening for Messages from the Server (SSE)] connection.

=== Session Limits

By default, the server accepts up to 100,000 concurrent sessions and does not evict idle sessions.
Set `session-idle-timeout` to close sessions that have had no client requests for the given duration, for example sessions of clients that went away without sending a `DELETE` request.
Use `max-sessions` to limit the number of concurrent sessions; once the limit is reached, requests to initialize a new session are rejected.

[source,yaml]
----
spring:
ai:
mcp:
server:
streamable-http:
max-sessions: 1000
session-idle-timeout: 30m
----

Only client requests count as activity, so a client that keeps its stream open without sending requests for that long is evicted as well.
Enable keep-alive too if your clients can stay idle that long: their responses to the keep-alive pings count as activity.
When keep-alive is enabled, `session-idle-timeout` must be greater than `keep-alive-interval`.


=== Session Limits

Expand Down