觅讯会议
167.22MB · 2025-10-22
Spring AI 从 1.1.0-M1(里程碑版本) 开始正式支持 MCP 的 Streamable HTTP 模式。该版本解决了此前 1.0.x 版本仅支持 SSE 协议的局限,新增断线重连、会话恢复等核心能力,同时提供 WebFlux/WebMVC 两种编程模型的 Starter 支持。
首先在 extra01
父项目中引入 spring-ai 1.1.0-M3
版本.
<!-- extra01/pom.xml -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.1.0-M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- extra01/extra-mcp-server/pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
这里只是简单模拟 通过城市名字获取温度的Service
// extra01/extra-mcp-server/src/main/java/com/kaifamiao/extra01/service/WeatherService.java
@Service
public class WeatherService {
@Tool(description = "通过城市名字获取温度")
public String getWeatherByCity(@ToolParam(description = "城市名称") String cityName) {
return cityName + "今天的温度是" + (new java.util.Random().nextInt(9) + 1) * 6;
}
}
// extra01/extra-mcp-server/src/main/java/com/kaifamiao/extra01/comfiguration/McpServerConfig.java
@Configuration
public class McpServerConfig {
@Bean
@Primary // 添加此注解指定优先使用此Bean
public ToolCallbackProvider toolProvider(WeatherService weatherService) {
// 注册工具类实例
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
}
# extra01/extra-mcp-server/src/main/resources/application.yml
spring:
ai:
mcp:
server:
name: streamable-weather-server
version: 0.0.1
protocol: streamable
type: sync
streamable-http:
mcp-endpoint: /mcp
keep-alive-interval: 30s
server:
port: 8081
<!-- extra01/extra-mcp-client/pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 使用测试用例进行测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
# extra01/extra-mcp-client/src/main/resources/application.yml
spring:
ai:
dashscope:
api-key: ${AI_BAI_LIAN_API_KEY}
chat:
options:
model: qwen-plus
temperature: 0.7
mcp:
client:
streamable-http:
connections:
server1: # mcp 服务URL
url: http://127.0.0.1:8081/mcp
// extra01/extra-mcp-client/src/test/java/com/kaifamiao/extra01/StreamableWeatherServerTest.java
@SpringBootTest
@Slf4j
public class StreamableWeatherServerTest {
@Test
void testMcpServer(@Autowired ChatClient.Builder chatClientBuilder,
@Autowired SyncMcpToolCallbackProvider syncMcpToolCallbackProvider) {
ChatClient chatClient = chatClientBuilder
.build();
String response = chatClient.prompt()
.toolCallbacks(syncMcpToolCallbackProvider)
.user("北京现在的天气如何").call().content();
log.info("response: {}", response);
}
}
控制台输出:
北京今天的温度是36℃。请注意防暑降温!
之前,Spring AI 最受诟病的一点是不支持可流式传输的 HTTP(Streamable HTTP),这使得在实现自定义 MCP 服务器端时非常麻烦,开发者不得不手动实现断线自动重连等功能。不过,随着新版本的发布,这一问题得到了解决。现在,Spring AI 已经支持 Streamable HTTP,我们能够更加便捷地实现 MCP 服务,感兴趣的开发者不妨动手尝试一下。
源代码地址:github.com/kaiwill/kai…
如果你想更深入地学习大模型,以下是一些非常有价值的学习资源,这些资源将帮助你从不同角度学习大模型,提升你的实践能力。