Skip to content

Commit 0b5da6b

Browse files
committed
refactor: replace direct dependencies with ObjectProvider in MCP transports
Replace direct dependencies with ObjectProvider pattern in SSE transport auto-configurations. - Uses ObjectProvider for ObjectMapper and WebClient.Builder dependencies - Removes redundant @ConditionalOnMissingBean methods - Provides fallback instantiation when dependencies are not available Resolves #2440 Signed-off-by: Christian Tzolov <[email protected]>
1 parent 3f7f2f1 commit 0b5da6b

File tree

2 files changed

+15
-49
lines changed

2 files changed

+15
-49
lines changed

auto-configurations/spring-ai-mcp-client/src/main/java/org/springframework/ai/autoconfigure/mcp/client/SseHttpClientTransportAutoConfiguration.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.ai.autoconfigure.mcp.client.properties.McpClientCommonProperties;
3030
import org.springframework.ai.autoconfigure.mcp.client.properties.McpSseClientProperties;
3131
import org.springframework.ai.autoconfigure.mcp.client.properties.McpSseClientProperties.SseParameters;
32+
import org.springframework.beans.factory.ObjectProvider;
3233
import org.springframework.boot.autoconfigure.AutoConfiguration;
3334
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3435
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -80,12 +81,15 @@ public class SseHttpClientTransportAutoConfiguration {
8081
* <li>ObjectMapper for JSON processing
8182
* </ul>
8283
* @param sseProperties the SSE client properties containing server configurations
83-
* @param objectMapper the ObjectMapper for JSON serialization/deserialization
84+
* @param objectMapperProvider the provider for ObjectMapper or a new instance if not
85+
* available
8486
* @return list of named MCP transports
8587
*/
8688
@Bean
8789
public List<NamedClientMcpTransport> mcpHttpClientTransports(McpSseClientProperties sseProperties,
88-
ObjectMapper objectMapper) {
90+
ObjectProvider<ObjectMapper> objectMapperProvider) {
91+
92+
ObjectMapper objectMapper = objectMapperProvider.getIfAvailable(ObjectMapper::new);
8993

9094
List<NamedClientMcpTransport> sseTransports = new ArrayList<>();
9195

@@ -99,18 +103,4 @@ public List<NamedClientMcpTransport> mcpHttpClientTransports(McpSseClientPropert
99103
return sseTransports;
100104
}
101105

102-
/**
103-
* Creates the default ObjectMapper if none is provided.
104-
*
105-
* <p>
106-
* This ObjectMapper is used for JSON serialization and deserialization in the SSE
107-
* transport implementation.
108-
* @return the configured ObjectMapper instance
109-
*/
110-
@Bean
111-
@ConditionalOnMissingBean
112-
public ObjectMapper objectMapper() {
113-
return new ObjectMapper();
114-
}
115-
116106
}

auto-configurations/spring-ai-mcp-client/src/main/java/org/springframework/ai/autoconfigure/mcp/client/SseWebFluxTransportAutoConfiguration.java

+9-33
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import org.springframework.ai.autoconfigure.mcp.client.properties.McpClientCommonProperties;
2727
import org.springframework.ai.autoconfigure.mcp.client.properties.McpSseClientProperties;
2828
import org.springframework.ai.autoconfigure.mcp.client.properties.McpSseClientProperties.SseParameters;
29+
import org.springframework.beans.factory.ObjectProvider;
2930
import org.springframework.boot.autoconfigure.AutoConfiguration;
3031
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3333
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3434
import org.springframework.context.annotation.Bean;
@@ -73,17 +73,21 @@ public class SseWebFluxTransportAutoConfiguration {
7373
* <li>Server connection parameters from properties
7474
* </ul>
7575
* @param sseProperties the SSE client properties containing server configurations
76-
* @param webClientBuilderTemplate the template WebClient.Builder to clone for each
77-
* connection
78-
* @param objectMapper the ObjectMapper for JSON serialization/deserialization
76+
* @param webClientBuilderProvider the provider for WebClient.Builder
77+
* @param objectMapperProvider the provider for ObjectMapper or a new instance if not
78+
* available
7979
* @return list of named MCP transports
8080
*/
8181
@Bean
8282
public List<NamedClientMcpTransport> webFluxClientTransports(McpSseClientProperties sseProperties,
83-
WebClient.Builder webClientBuilderTemplate, ObjectMapper objectMapper) {
83+
ObjectProvider<WebClient.Builder> webClientBuilderProvider,
84+
ObjectProvider<ObjectMapper> objectMapperProvider) {
8485

8586
List<NamedClientMcpTransport> sseTransports = new ArrayList<>();
8687

88+
var webClientBuilderTemplate = webClientBuilderProvider.getIfAvailable(WebClient::builder);
89+
var objectMapper = objectMapperProvider.getIfAvailable(ObjectMapper::new);
90+
8791
for (Map.Entry<String, SseParameters> serverParameters : sseProperties.getConnections().entrySet()) {
8892
var webClientBuilder = webClientBuilderTemplate.clone().baseUrl(serverParameters.getValue().url());
8993
var transport = new WebFluxSseClientTransport(webClientBuilder, objectMapper);
@@ -93,32 +97,4 @@ public List<NamedClientMcpTransport> webFluxClientTransports(McpSseClientPropert
9397
return sseTransports;
9498
}
9599

96-
/**
97-
* Creates the default WebClient.Builder if none is provided.
98-
*
99-
* <p>
100-
* This builder serves as a template for creating server-specific WebClient instances
101-
* used in SSE transport implementation.
102-
* @return the configured WebClient.Builder instance
103-
*/
104-
@Bean
105-
@ConditionalOnMissingBean
106-
public WebClient.Builder webClientBuilder() {
107-
return WebClient.builder();
108-
}
109-
110-
/**
111-
* Creates the default ObjectMapper if none is provided.
112-
*
113-
* <p>
114-
* This ObjectMapper is used for JSON serialization and deserialization in the SSE
115-
* transport implementation.
116-
* @return the configured ObjectMapper instance
117-
*/
118-
@Bean
119-
@ConditionalOnMissingBean
120-
public ObjectMapper objectMapper() {
121-
return new ObjectMapper();
122-
}
123-
124100
}

0 commit comments

Comments
 (0)