From f4d4dddf9d87f25b9af721872534a3abce76ffb5 Mon Sep 17 00:00:00 2001 From: Hari Ohm Prasth Rajagopal Date: Sun, 16 Mar 2025 00:14:36 -0700 Subject: [PATCH] Add tests for McpWebFluxServerAutoConfiguration Add unit tests to verify that the WebFlux server transport is properly configured with a custom ObjectMapper that ignores unknown properties. This ensures that the MCP server can handle JSON payloads containing fields not defined in the model classes, which is important for forward compatibility. Signed-off-by: Hari Ohm Prasath Signed-off-by: Hari Ohm Prasth Rajagopal --- ...cpWebFluxServerAutoConfigurationTests.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 auto-configurations/mcp/spring-ai-autoconfigure-mcp-server/src/test/java/org/springframework/ai/mcp/server/autoconfigure/McpWebFluxServerAutoConfigurationTests.java diff --git a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-server/src/test/java/org/springframework/ai/mcp/server/autoconfigure/McpWebFluxServerAutoConfigurationTests.java b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-server/src/test/java/org/springframework/ai/mcp/server/autoconfigure/McpWebFluxServerAutoConfigurationTests.java new file mode 100644 index 000000000..c89f2e337 --- /dev/null +++ b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-server/src/test/java/org/springframework/ai/mcp/server/autoconfigure/McpWebFluxServerAutoConfigurationTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ai.mcp.server.autoconfigure; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.server.RouterFunction; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider; + +class McpWebFluxServerAutoConfigurationTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(McpWebFluxServerAutoConfiguration.class, + JacksonAutoConfiguration.class, TestConfiguration.class)); + + @Test + void shouldConfigureWebFluxTransportWithCustomObjectMapper() { + this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(WebFluxSseServerTransportProvider.class); + assertThat(context).hasSingleBean(RouterFunction.class); + assertThat(context).hasSingleBean(McpServerProperties.class); + + ObjectMapper objectMapper = context.getBean(ObjectMapper.class); + + // Verify that the ObjectMapper is configured to ignore unknown properties + assertThat(objectMapper.getDeserializationConfig() + .isEnabled(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse(); + + // Test with a JSON payload containing unknown fields + String jsonWithUnknownField = """ + { + "tools": ["tool1", "tool2"], + "name": "test", + "unknownField": "value" + } + """; + + // This should not throw an exception + TestMessage message = objectMapper.readValue(jsonWithUnknownField, TestMessage.class); + assertThat(message.getName()).isEqualTo("test"); + }); + } + + // Test configuration to enable McpServerProperties + @Configuration + @EnableConfigurationProperties(McpServerProperties.class) + static class TestConfiguration { + + } + + // Test class to simulate the actual message structure + static class TestMessage { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + } + +}