注意:错误为spring ai mcp源码错误。由于github区别对待中国地区,所有不予以提交issue
当使用:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
</dependency>
构建应用的时候,使用自动注入的方式获取mcp的客户端:
private final List<McpAsyncClient> mcpAsyncClients;
会出现多条记录。
问题:
private final List<McpAsyncClient> mcpAsyncClients 包下的SseHttpClientTransportAutoConfiguration注解中,ConditionalOnMissingClass注解错误。
那如何修复这个问题呢:
1、在自己项目中定义目录
创建包:org.springframework.ai.autoconfigure.mcp.client
2、修改注解的信息
/*
* 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.autoconfigure.mcp.client;
import java.net.http.HttpClient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.ai.autoconfigure.mcp.client.properties.McpClientCommonProperties;
import org.springframework.ai.autoconfigure.mcp.client.properties.McpSseClientProperties;
import org.springframework.ai.autoconfigure.mcp.client.properties.McpSseClientProperties.SseParameters;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* Auto-configuration for Server-Sent Events (SSE) HTTP client transport in the Model
* Context Protocol (MCP).
*
* <p>
* This configuration class sets up the necessary beans for SSE-based HTTP client
* transport when WebFlux is not available. It provides HTTP client-based SSE transport
* implementation for MCP client communication.
*
* <p>
* The configuration is activated after the WebFlux SSE transport auto-configuration to
* ensure proper fallback behavior when WebFlux is not available.
*
* <p>
* Key features:
* <ul>
* <li>Creates HTTP client-based SSE transports for configured MCP server connections
* <li>Configures ObjectMapper for JSON serialization/deserialization
* <li>Supports multiple named server connections with different URLs
* </ul>
*
* @see HttpClientSseClientTransport
* @see McpSseClientProperties
*/
@AutoConfiguration(after = SseWebFluxTransportAutoConfiguration.class)
@ConditionalOnClass({ McpSchema.class, McpSyncClient.class })
@ConditionalOnMissingClass("io.modelcontextprotocol.client.transport.WebFluxSseClientTransport") // 注意,只是修改了这里
@EnableConfigurationProperties({ McpSseClientProperties.class, McpClientCommonProperties.class })
@ConditionalOnProperty(prefix = McpClientCommonProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
public class SseHttpClientTransportAutoConfiguration {
/**
* Creates a list of HTTP client-based SSE transports for MCP communication.
*
* <p>
* Each transport is configured with:
* <ul>
* <li>A new HttpClient instance
* <li>Server URL from properties
* <li>ObjectMapper for JSON processing
* </ul>
* @param sseProperties the SSE client properties containing server configurations
* @param objectMapper the ObjectMapper for JSON serialization/deserialization
* @return list of named MCP transports
*/
@Bean
public List<NamedClientMcpTransport> mcpHttpClientTransports(McpSseClientProperties sseProperties,
ObjectMapper objectMapper) {
List<NamedClientMcpTransport> sseTransports = new ArrayList<>();
for (Map.Entry<String, SseParameters> serverParameters : sseProperties.getConnections().entrySet()) {
var transport = new HttpClientSseClientTransport(HttpClient.newBuilder(), serverParameters.getValue().url(),
objectMapper);
sseTransports.add(new NamedClientMcpTransport(serverParameters.getKey(), transport));
}
return sseTransports;
}
/**
* Creates the default ObjectMapper if none is provided.
*
* <p>
* This ObjectMapper is used for JSON serialization and deserialization in the SSE
* transport implementation.
* @return the configured ObjectMapper instance
*/
@Bean
@ConditionalOnMissingBean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。