Skip to content

Commit 199c27f

Browse files
Update McpWebFluxServerAutoConfiguration.java
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. Signed-off-by: Hari Ohm Prasath <[email protected]> Signed-off-by: Hari Ohm Prasth Rajagopal <[email protected]>
1 parent bc375ab commit 199c27f

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public class McpWebFluxServerAutoConfiguration {
7272

7373
@Bean
7474
@ConditionalOnMissingBean
75-
public WebFluxSseServerTransport webFluxTransport(McpServerProperties serverProperties) {
76-
return new WebFluxSseServerTransport(new ObjectMapper(), serverProperties.getSseMessageEndpoint());
75+
public WebFluxSseServerTransport webFluxTransport(McpServerProperties serverProperties, ObjectMapper objectMapper) {
76+
return new WebFluxSseServerTransport(objectMapper, serverProperties.getSseMessageEndpoint());
7777
}
7878

7979
// 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)