Skip to content

Commit c2843f1

Browse files
YunKuiLutzolov
authored andcommitted
Fix: Prevent exceptions when disabling Mcp server
- Prevent projects depending on `mcp-server-webmvc` or `mcp-server-webflux` from exceptions when `spring.ai.mcp.server.enabled` is set to `false`. - Add unit tests to verify. Signed-off-by: YunKui Lu <luyunkui95@gmail.com>
1 parent 04249e8 commit c2843f1

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.mcp.server.autoconfigure;
18+
19+
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
21+
22+
/**
23+
* This class defines a condition met when the MCP server is enabled and the STDIO
24+
* Transport is disabled.
25+
*
26+
* @since 1.0.0
27+
* @author YunKui Lu
28+
*/
29+
public class McpServerStdioDisabledCondition extends AllNestedConditions {
30+
31+
public McpServerStdioDisabledCondition() {
32+
super(ConfigurationPhase.PARSE_CONFIGURATION);
33+
}
34+
35+
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
36+
matchIfMissing = true)
37+
static class McpServerEnabledCondition {
38+
39+
}
40+
41+
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "stdio", havingValue = "false",
42+
matchIfMissing = true)
43+
static class StdioDisabledCondition {
44+
45+
}
46+
47+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.springframework.boot.autoconfigure.AutoConfiguration;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2827
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Conditional;
2929
import org.springframework.web.reactive.function.server.RouterFunction;
3030

3131
/**
@@ -68,8 +68,7 @@
6868
@AutoConfiguration
6969
@ConditionalOnClass({ WebFluxSseServerTransportProvider.class })
7070
@ConditionalOnMissingBean(McpServerTransportProvider.class)
71-
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "stdio", havingValue = "false",
72-
matchIfMissing = true)
71+
@Conditional(McpServerStdioDisabledCondition.class)
7372
public class McpWebFluxServerAutoConfiguration {
7473

7574
@Bean

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.springframework.boot.autoconfigure.AutoConfiguration;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2827
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Conditional;
2929
import org.springframework.web.servlet.function.RouterFunction;
3030
import org.springframework.web.servlet.function.ServerResponse;
3131

@@ -63,8 +63,7 @@
6363
@AutoConfiguration
6464
@ConditionalOnClass({ WebMvcSseServerTransportProvider.class })
6565
@ConditionalOnMissingBean(McpServerTransportProvider.class)
66-
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "stdio", havingValue = "false",
67-
matchIfMissing = true)
66+
@Conditional(McpServerStdioDisabledCondition.class)
6867
public class McpWebMvcServerAutoConfiguration {
6968

7069
@Bean

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ void stdioEnabledConfiguration() {
5353
.run(context -> assertThat(context).doesNotHaveBean(WebFluxSseServerTransportProvider.class));
5454
}
5555

56+
@Test
57+
void serverDisableConfiguration() {
58+
this.contextRunner.withPropertyValues("spring.ai.mcp.server.enabled=false").run(context -> {
59+
assertThat(context).doesNotHaveBean(WebFluxSseServerTransportProvider.class);
60+
assertThat(context).doesNotHaveBean(RouterFunction.class);
61+
});
62+
}
63+
5664
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828

29-
class McpWebMvcServerAutoConfigurationTest {
29+
class McpWebMvcServerAutoConfigurationIT {
3030

3131
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
3232
AutoConfigurations.of(McpWebMvcServerAutoConfiguration.class, McpServerAutoConfiguration.class));
@@ -53,4 +53,12 @@ void stdioEnabledConfiguration() {
5353
.run(context -> assertThat(context).doesNotHaveBean(WebMvcSseServerTransportProvider.class));
5454
}
5555

56+
@Test
57+
void serverDisableConfiguration() {
58+
this.contextRunner.withPropertyValues("spring.ai.mcp.server.enabled=false").run(context -> {
59+
assertThat(context).doesNotHaveBean(WebMvcSseServerTransportProvider.class);
60+
assertThat(context).doesNotHaveBean(RouterFunction.class);
61+
});
62+
}
63+
5664
}

0 commit comments

Comments
 (0)