【Spring AI】基于 sse 实现 MCP Server

一、maven 依赖

在 pom.xml 文件中增加 spring-boot-starter-web 和 spring-ai-mcp-server-webmvc-spring-boot-starter 依赖,如下:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>3.2.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
    <version>1.0.0-M6</version>
</dependency>

二、application.yml

在 application.yml 中增加如下配置:

yaml 复制代码
spring:
  ai:
    mcp:
      server:
        name: my-mcp-server # MCP Server 的名称
        version: 0.0.1

三、代码

  • 定义 MyMcpService,代码如下:
java 复制代码
@Service
public class MyMcpService {
    @Tool(description = "打招呼")
    public String hello(@ToolParam(description = "name") String name) {
        return name + ",您好!欢迎使用 MCP Server";
    }
}
  • 将 MyMcpService 注册到 MCP Tool 中,代码如下:
java 复制代码
@SpringBootApplication
public class Application {
    @Bean
    ToolCallbackProvider whatcellTools(MyMcpService myMcpService) {
        return MethodToolCallbackProvider.builder()
                .toolObjects(myMcpService)
                .build();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

四、使用 MCP Inspector 测试连接

假设 MCP Server 是在本机上的 9000 端口上运行,那么 MCP Inspector 如下:

5、如何将已有的 MCP Server 从 sse 升级到 streamableHttp

可参考 《【Spring AI】MCP Server 从 sse 升级到 streamableHttp》

相关推荐
神云瑟瑟1 天前
spring ai对接deepseek
spring ai·deepseek
callJJ2 天前
Spring AI 文本聊天模型完全指南:ChatModel 与 ChatClient
java·大数据·人工智能·spring·spring ai·聊天模型
hay_lee3 天前
Spring AI实现对话聊天-流式输出
java·人工智能·ollama·spring ai
要开心吖ZSH6 天前
Spring AI Alibaba 个人学习笔记
人工智能·学习·spring·spring ai·springaialibaba
库里不会投三分6 天前
谢飞机面试记:从JVM到Spring AI的3轮灵魂拷问(音视频+AI招聘双场景)
spring cloud·java面试·rag·spring ai·ai招聘·音视频架构
猿小羽7 天前
基于 Spring AI 与 Streamable HTTP 构建 MCP Server 实践
java·llm·spring ai·mcp·streamable http
没有腰的嘟嘟嘟7 天前
[特殊字符] 本地部署 Qwen3:4B 大模型并使用 Spring Boot 对接实践指南
spring·ai·spring ai
猿小羽14 天前
AI 2.0 时代全栈开发实战:从 Spring AI 到 MLOps 的进阶指南
ai·llm·mlops·rag·vector database·spring ai·prompt engineering
猿小羽14 天前
AI 学习与实战系列:Spring AI + MCP 深度实战——构建标准化、可扩展的智能 Agent 系统
java·spring boot·llm·agent·spring ai·mcp·model context protocol
猿小羽14 天前
深度实战:Spring AI 与 MCP(Model Context Protocol)构建下一代 AI Agent
java·大模型·llm·ai agent·spring ai·开发者工具·mcp