【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》

相关推荐
心之伊始5 小时前
Spring Boot 接入 MCP 实战:用 Spring AI 调用本地工具的最小闭环
java·spring boot·agent·spring ai·mcp
亦暖筑序3 天前
GraphRAG vs 传统向量RAG:Spring AI实战对比
知识图谱·neo4j·向量数据库·rag·spring ai·graphrag
a23121214 天前
从零搭建Spring Ai多智能体后端应用
java·运维·微服务·多智能体·后端开发·spring ai
中间件XL4 天前
ai-agent框架spring ai/alibaba(四) RAG
rag·ai agent·智能体·spring ai
java1234_小锋6 天前
利用Cursor AI编程 两小时实现 基于Spring AI 2.0的带智能客服的商城系统(带在线支付功能)
人工智能·spring·ai编程·智能客服·spring ai·ai智能客服
java1234_小锋7 天前
Spring AI 2.0 开发Java Agent智能体 - MCP(模型上下文协议)
java·人工智能·spring·spring ai
中间件XL7 天前
ai-agent框架spring ai alibaba (三)外部调用II-1 MCP
ai agent·spring ai·mcp·ai agent框架
TechMoeTiger9 天前
Java + Spring实现Hermes Agent之龙虾、Skills、Mcp和沙箱代码执行环境思路
spring ai·openclaw·龙虾·hermes agent·spring ai skills·服务端龙虾·spring ai 沙箱·code interpreter
爱编程的小新☆9 天前
JAVA实现Manus智能体
java·react·cot·智能体·spring ai·manus·agent loop
海兰11 天前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai