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

相关推荐
MateCloud微服务16 分钟前
MateClaw:基于 Spring AI Alibaba 的开源多智能体 AI 操作系统(附架构详解)
人工智能·多智能体·spring ai·aiagent·openclaw·小龙虾·mateclaw
QC·Rex4 天前
Spring Boot + Spring AI 实战:从零构建企业级 AI 应用
spring boot·大模型·向量数据库·rag·spring ai·tool calling
最初的↘那颗心5 天前
Agent 实战:构建第一个 Agent 与记忆系统设计
java·大模型·agent·spring ai·记忆系统
最初的↘那颗心5 天前
Agent 核心原理:本质、ReAct 框架与工具设计最佳实践
大模型·agent·react·spring ai·工具设计
LucaJu6 天前
Agent Skill 踩坑记录 | SpringBoot 打包后 Skill 加载失败问题排查与解决
agent·skill·spring ai·spring ai alibaba
LucaJu7 天前
一、先了解:MCP 公开服务市场
agent·智能体·spring ai·mcp·spring ai alibaba
凤山老林7 天前
Java 开发者零成本构建 RAG 知识库:Spring AI Alibaba + Ollama 搭建本地 RAG 知识库
java·人工智能·知识库·rag·spring ai
最初的↘那颗心7 天前
Prompt安全实战:注入攻击防御与越狱防护全攻略
大模型·spring ai·注入攻击·prompt安全·越狱防护
最初的↘那颗心7 天前
结构化Prompt与Meta Prompt实战——让AI输出你想要的格式
大模型·prompt·spring ai·结构化输出·meta prompt
最初的↘那颗心7 天前
Prompt基础功:角色分工与样本策略——System Prompt与Few-Shot实战
大模型·llm·prompt·few-shot·spring ai