|
| 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 | +package org.springframework.ai.mcp.server.autoconfigure; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 22 | +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
| 23 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 24 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 27 | + |
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | +import io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider; |
| 30 | + |
| 31 | +class McpWebFluxServerAutoConfigurationTests { |
| 32 | + |
| 33 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 34 | + .withConfiguration(AutoConfigurations.of(McpWebFluxServerAutoConfiguration.class, |
| 35 | + JacksonAutoConfiguration.class, TestConfiguration.class)); |
| 36 | + |
| 37 | + @Test |
| 38 | + void shouldConfigureWebFluxTransportWithCustomObjectMapper() { |
| 39 | + this.contextRunner.run((context) -> { |
| 40 | + assertThat(context).hasSingleBean(WebFluxSseServerTransportProvider.class); |
| 41 | + assertThat(context).hasSingleBean(RouterFunction.class); |
| 42 | + assertThat(context).hasSingleBean(McpServerProperties.class); |
| 43 | + |
| 44 | + ObjectMapper objectMapper = context.getBean(ObjectMapper.class); |
| 45 | + |
| 46 | + // Verify that the ObjectMapper is configured to ignore unknown properties |
| 47 | + assertThat(objectMapper.getDeserializationConfig() |
| 48 | + .isEnabled(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse(); |
| 49 | + |
| 50 | + // Test with a JSON payload containing unknown fields |
| 51 | + String jsonWithUnknownField = """ |
| 52 | + { |
| 53 | + "tools": ["tool1", "tool2"], |
| 54 | + "name": "test", |
| 55 | + "unknownField": "value" |
| 56 | + } |
| 57 | + """; |
| 58 | + |
| 59 | + // This should not throw an exception |
| 60 | + TestMessage message = objectMapper.readValue(jsonWithUnknownField, TestMessage.class); |
| 61 | + assertThat(message.getName()).isEqualTo("test"); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + // Test configuration to enable McpServerProperties |
| 66 | + @Configuration |
| 67 | + @EnableConfigurationProperties(McpServerProperties.class) |
| 68 | + static class TestConfiguration { |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + // Test class to simulate the actual message structure |
| 73 | + static class TestMessage { |
| 74 | + |
| 75 | + private String name; |
| 76 | + |
| 77 | + public String getName() { |
| 78 | + return name; |
| 79 | + } |
| 80 | + |
| 81 | + public void setName(String name) { |
| 82 | + this.name = name; |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments