Skip to content

Fix UnrecognizedPropertyException in MCP WebFlux Transport #2484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public class McpWebFluxServerAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public WebFluxSseServerTransport webFluxTransport(McpServerProperties serverProperties) {
return new WebFluxSseServerTransport(new ObjectMapper(), serverProperties.getSseMessageEndpoint());
public WebFluxSseServerTransport webFluxTransport(McpServerProperties serverProperties, ObjectMapper objectMapper) {
return new WebFluxSseServerTransport(objectMapper, serverProperties.getSseMessageEndpoint());
}

// Router function for SSE transport used by Spring WebFlux to start an HTTP server.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.springframework.ai.autoconfigure.mcp.server;

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.WebFluxSseServerTransport;

class McpWebFluxServerAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
McpWebFluxServerAutoConfiguration.class,
JacksonAutoConfiguration.class,
TestConfiguration.class)); // Add test configuration

@Test
void shouldConfigureWebFluxTransportWithCustomObjectMapper() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(WebFluxSseServerTransport.class);
assertThat(context).hasSingleBean(RouterFunction.class);
assertThat(context).hasSingleBean(McpServerProperties.class);

WebFluxSseServerTransport transport = context.getBean(WebFluxSseServerTransport.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;
}
}
}