Spring AI 从 1.1.0-M1(里程碑版本) 开始正式支持 MCP 的 Streamable HTTP 模式。该版本解决了此前 1.0.x 版本仅支持 SSE 协议的局限,新增断线重连、会话恢复等核心能力,同时提供 WebFlux/WebMVC 两种编程模型的 Starter 支持。

为什么 MCP 规范要转向 Streamable HTTP 模式?

1. 解决连接不可恢复问题

  • HTTP + SSE 缺陷:SSE(服务器发送事件)连接一旦中断,客户端无法从中断点恢复,只能重新建立连接,导致上下文丢失和用户体验下降
  • Streamable HTTP 改进:支持会话状态管理,允许客户端在断线后重新连接并恢复之前的会话,保障了通信的连续性

2. 降低服务器资源压力

  • HTTP + SSE 缺陷:每个客户端都需要维持一个长期的 SSE 长连接,高并发场景下导致 TCP 连接数激增,服务器资源消耗巨大,且难以横向扩展
  • Streamable HTTP 改进:采用按需流式传输,无需为每个客户端维持长连接,连接可复用,显著降低了服务器的负载和资源占用

3. 简化架构与提升兼容性

  • HTTP + SSE 缺陷:需要维护 /sse 专用端点,增加了系统复杂性,且部分网络基础设施(如防火墙)可能干扰长期 SSE 连接
  • Streamable HTTP 改进:移除了专用端点,所有通信整合到统一端点(如 /message),架构更简洁,并且能更好地兼容现有网络基础设施

4. 支持更灵活的通信模式

  • Streamable HTTP 允许服务器在需要时将响应升级为 SSE 流,实现流式传输,同时保留标准 HTTP 通信能力,满足了“既是传统 API 又能流式推送”的混合需求

MCP Server 实战

父项目中引入spring-ai 1.1.0-M3依赖

首先在 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>

extra-mcp-server 项目中引入依赖

<!-- 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类

这里只是简单模拟 通过城市名字获取温度的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();  
    }  
}

mcp Server yml配置

# 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

启动服务器后,使用 Cherry Studio 客户端测试

MCP Client实现

子项目extra-mcp-client引入依赖

<!-- 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>

配置yml

# 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

编写测试用例调用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…

学习资源推荐

如果你想更深入地学习大模型,以下是一些非常有价值的学习资源,这些资源将帮助你从不同角度学习大模型,提升你的实践能力。

本站提供的所有下载资源均来自互联网,仅提供学习交流使用,版权归原作者所有。如需商业使用,请联系原作者获得授权。 如您发现有涉嫌侵权的内容,请联系我们 邮箱:[email protected]