Skip to content

Commit 0d45959

Browse files
committed
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 <[email protected]>
1 parent 867bc2e commit 0d45959

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.springframework.ai.mcp.server.autoconfigure;
2+
3+
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
4+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5+
6+
/**
7+
* This class defines a condition met when the MCP server is enabled and the STDIO
8+
* Transport is disabled.
9+
*
10+
* @since 1.0.0
11+
* @author YunKui Lu
12+
*/
13+
public class McpServerStdioDisabledCondition extends AllNestedConditions {
14+
15+
public McpServerStdioDisabledCondition() {
16+
super(ConfigurationPhase.PARSE_CONFIGURATION);
17+
}
18+
19+
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
20+
matchIfMissing = true)
21+
static class McpServerEnabledCondition {
22+
23+
}
24+
25+
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "stdio", havingValue = "false",
26+
matchIfMissing = true)
27+
static class StdioDisabledCondition {
28+
29+
}
30+
31+
}

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)