Skip to content

Commit 7190726

Browse files
refactor: Clean up McpWebFluxServerAutoConfiguration and add tests
- Reorganize imports and improve code formatting for better readability. - Update `webFluxTransport` method to accept a custom `ObjectMapper`. - Introduce `McpWebFluxServerAutoConfigurationTests` to validate configuration and ensure proper handling of unknown JSON properties.
1 parent bc375ab commit 7190726

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@
6666
@AutoConfiguration
6767
@ConditionalOnClass({ WebFluxSseServerTransport.class })
6868
@ConditionalOnMissingBean(ServerMcpTransport.class)
69-
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "stdio", havingValue = "false",
70-
matchIfMissing = true)
69+
@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "stdio", havingValue = "false", matchIfMissing = true)
7170
public class McpWebFluxServerAutoConfiguration {
7271

7372
@Bean
7473
@ConditionalOnMissingBean
75-
public WebFluxSseServerTransport webFluxTransport(McpServerProperties serverProperties) {
76-
return new WebFluxSseServerTransport(new ObjectMapper(), serverProperties.getSseMessageEndpoint());
74+
public WebFluxSseServerTransport webFluxTransport(McpServerProperties serverProperties, ObjectMapper objectMapper) {
75+
return new WebFluxSseServerTransport(objectMapper, serverProperties.getSseMessageEndpoint());
7776
}
7877

7978
// Router function for SSE transport used by Spring WebFlux to start an HTTP server.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.springframework.ai.autoconfigure.mcp.server;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.boot.autoconfigure.AutoConfigurations;
7+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.web.reactive.function.server.RouterFunction;
12+
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
15+
import io.modelcontextprotocol.server.transport.WebFluxSseServerTransport;
16+
17+
class McpWebFluxServerAutoConfigurationTests {
18+
19+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
20+
.withConfiguration(AutoConfigurations.of(
21+
McpWebFluxServerAutoConfiguration.class,
22+
JacksonAutoConfiguration.class,
23+
TestConfiguration.class)); // Add test configuration
24+
25+
@Test
26+
void shouldConfigureWebFluxTransportWithCustomObjectMapper() {
27+
this.contextRunner.run((context) -> {
28+
assertThat(context).hasSingleBean(WebFluxSseServerTransport.class);
29+
assertThat(context).hasSingleBean(RouterFunction.class);
30+
assertThat(context).hasSingleBean(McpServerProperties.class);
31+
32+
WebFluxSseServerTransport transport = context.getBean(WebFluxSseServerTransport.class);
33+
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
34+
35+
// Verify that the ObjectMapper is configured to ignore unknown properties
36+
assertThat(objectMapper.getDeserializationConfig().isEnabled(
37+
com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES))
38+
.isFalse();
39+
40+
// Test with a JSON payload containing unknown fields
41+
String jsonWithUnknownField = """
42+
{
43+
"tools": ["tool1", "tool2"],
44+
"name": "test",
45+
"unknownField": "value"
46+
}
47+
""";
48+
49+
// This should not throw an exception
50+
TestMessage message = objectMapper.readValue(jsonWithUnknownField, TestMessage.class);
51+
assertThat(message.getName()).isEqualTo("test");
52+
});
53+
}
54+
55+
// Test configuration to enable McpServerProperties
56+
@Configuration
57+
@EnableConfigurationProperties(McpServerProperties.class)
58+
static class TestConfiguration {
59+
}
60+
61+
// Test class to simulate the actual message structure
62+
static class TestMessage {
63+
private String name;
64+
65+
public String getName() {
66+
return name;
67+
}
68+
69+
public void setName(String name) {
70+
this.name = name;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)