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

相关推荐
猿小羽6 天前
AI 2.0 时代全栈开发实战:从 Spring AI 到 MLOps 的进阶指南
ai·llm·mlops·rag·vector database·spring ai·prompt engineering
猿小羽6 天前
AI 学习与实战系列:Spring AI + MCP 深度实战——构建标准化、可扩展的智能 Agent 系统
java·spring boot·llm·agent·spring ai·mcp·model context protocol
猿小羽6 天前
深度实战:Spring AI 与 MCP(Model Context Protocol)构建下一代 AI Agent
java·大模型·llm·ai agent·spring ai·开发者工具·mcp
猿小羽6 天前
Spring AI + MCP 实战:构建企业级 Agent 生态的基石
java·spring boot·llm·agent·spring ai·mcp·artificial intelligence
猿小羽6 天前
Spring AI + MCP 实战:构建下一代智能 Agent 应用
java·spring boot·llm·ai agent·spring ai·mcp·model context protocol
猿小羽6 天前
Spring AI + MCP 实战:构建标准化、可扩展的 AI Agent 架构体系
java·spring boot·llm·架构设计·ai agent·spring ai·mcp
猿小羽6 天前
Spring AI + MCP 实战:构建标准化 AI 智能代理与上下文集成
java·spring boot·llm·ai agent·spring ai·anthropic·mcp
递归尽头是星辰7 天前
大模型与向量检索的融合:从核心原理到 Spring AI 落地
人工智能·大模型·向量检索·rag·spring ai·向量库
94甘蓝7 天前
第 5 篇 Spring AI - Tool Calling 全面解析:从基础到高级应用
java·人工智能·函数调用·工具调用·spring ai·tool calling
迦蓝叶8 天前
Javaluator 与 Spring AI 深度集成:构建智能表达式计算工具
人工智能·spring·ai·语言模型·tools·spring ai·mcp