Skip to content

Commit 0064869

Browse files
committed
McpSyncServer: set immediate execution when in a servlet context
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent aa590e8 commit 0064869

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

auto-configurations/mcp/spring-ai-autoconfigure-mcp-server/src/main/java/org/springframework/ai/mcp/server/autoconfigure/McpServerAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5656
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5757
import org.springframework.context.annotation.Bean;
58+
import org.springframework.core.env.Environment;
5859
import org.springframework.core.log.LogAccessor;
5960
import org.springframework.util.CollectionUtils;
6061
import org.springframework.util.MimeType;
62+
import org.springframework.web.context.support.StandardServletEnvironment;
6163

6264
/**
6365
* {@link EnableAutoConfiguration Auto-configuration} for the Model Context Protocol (MCP)
@@ -177,7 +179,7 @@ public McpSyncServer mcpSyncServer(McpServerTransportProvider transportProvider,
177179
ObjectProvider<List<SyncPromptSpecification>> prompts,
178180
ObjectProvider<List<SyncCompletionSpecification>> completions,
179181
ObjectProvider<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers,
180-
List<ToolCallbackProvider> toolCallbackProvider) {
182+
List<ToolCallbackProvider> toolCallbackProvider, Environment environment) {
181183

182184
McpSchema.Implementation serverInfo = new Implementation(serverProperties.getName(),
183185
serverProperties.getVersion());
@@ -257,6 +259,9 @@ public McpSyncServer mcpSyncServer(McpServerTransportProvider transportProvider,
257259
serverBuilder.instructions(serverProperties.getInstructions());
258260

259261
serverBuilder.requestTimeout(serverProperties.getRequestTimeout());
262+
if (environment instanceof StandardServletEnvironment) {
263+
serverBuilder.immediateExecution(true);
264+
}
260265

261266
return serverBuilder.build();
262267
}

auto-configurations/mcp/spring-ai-autoconfigure-mcp-server/src/test/java/org/springframework/ai/mcp/server/autoconfigure/McpWebMvcServerAutoConfigurationIT.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
package org.springframework.ai.mcp.server.autoconfigure;
1818

1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import io.modelcontextprotocol.server.McpSyncServer;
2021
import io.modelcontextprotocol.server.transport.WebMvcSseServerTransportProvider;
2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.boot.autoconfigure.AutoConfigurations;
2425
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.core.env.ConfigurableEnvironment;
28+
import org.springframework.util.ReflectionUtils;
29+
import org.springframework.web.context.support.StandardServletEnvironment;
2530
import org.springframework.web.servlet.function.RouterFunction;
2631

2732
import static org.assertj.core.api.Assertions.assertThat;
@@ -68,4 +73,21 @@ void serverBaseUrlConfiguration() {
6873
.isEqualTo("/test"));
6974
}
7075

76+
@Test
77+
void servletEnvironmentConfiguration() {
78+
new ApplicationContextRunner(() -> new AnnotationConfigApplicationContext() {
79+
@Override
80+
public ConfigurableEnvironment getEnvironment() {
81+
return new StandardServletEnvironment();
82+
}
83+
}).withConfiguration(
84+
AutoConfigurations.of(McpWebMvcServerAutoConfiguration.class, McpServerAutoConfiguration.class))
85+
.run(context -> {
86+
var mcpSyncServer = context.getBean(McpSyncServer.class);
87+
var field = ReflectionUtils.findField(McpSyncServer.class, "immediateExecution");
88+
field.setAccessible(true);
89+
assertThat(field.getBoolean(mcpSyncServer)).isTrue();
90+
});
91+
}
92+
7193
}

0 commit comments

Comments
 (0)