【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 天前
业务智能体实战笔记:分层消除不确定性(五·终)|完全脱离 LLM 的确定性判定 + 评测闭环
ai agent·spring ai·业务智能体
码上解惑1 天前
2026 年智能体开发平台怎么选:从开源产品、云厂商到私有化平台
人工智能·ai·开源·智能体·spring ai
码上解惑2 天前
从模型接入到应用运行:智能体开发平台的整体架构设计
人工智能·agent·智能体·spring ai
荣--2 天前
业务智能体实战笔记:兜底修复——LLM 错了怎么救
ai agent·spring ai·业务智能体
荣--3 天前
业务智能体实战笔记(三):收窄 LLM 决策空间
工具调用·spring ai·agent工程化·reactagent·业务智能体·llm调优·准确率优化
行者-全栈开发3 天前
【码动四季】Spring AI + RAG 电商知识库:AtomCode 如何让 Embedding 对齐从 3 天缩短到 4 小时
embedding·向量检索·ollama·spring ai·pgvector·atomcode·rag电商知识库
小沈同学呀4 天前
【Agent开发第一期】LLM+Agent-从概念到第一次模型调用
ai agent·工具调用·ai助手·spring ai·实战演示·agent入门
中间件XL5 天前
ai-agent框架spring ai/alibaba 原理源码分析(五)graph III 图执行
graph·ai agent·spring ai·springaialibaba
闲猫7 天前
Ollama 本地部署,Python、SpringAI对接Ollama
人工智能·python·ollama·spring ai
早春的树长在理想三旬8 天前
RAG的三次进化:从朴素检索到Agentic RAG
人工智能·agent·spring ai