springboot改造mcp服务器(streamablehttp)
1 项目说明
mcp(model context protocol)协议是一个用于 ai 模型和工具之间通信的标准协议。随着 ai 应用变得越来越复杂并被广泛部署,原有的通信机制面临着一系列挑战。
近期 mcp 仓库的 pr #206【1】 引入了一个全新的 streamable http 传输层替代原有的 http+sse 传输层
- streamable http 相比 http + sse 具有更好的稳定性,在高并发场景下表现更优。
- streamable http 在性能方面相比 http + sse 具有明显优势,响应时间更短且更稳定。
- streamable http 客户端实现相比 http + sse 更简单,代码量更少,维护成本更低。
本文介绍了springboot如何实现mcp streamablehttp 服务器,并且使用 cherry studio 客户端测试mcp服务器
streamable http 支持无状态的服务和有状态的服务,目前的大部分场景无状态的 streamable http 的可以解决,通过对比两种传输方案的客户端实现代码,可以直观地看到无状态的 streamable http 的客户端实现简洁性。
由于官方的 mcp-java-sdk 还未支持 streamablehttp 模式,同时需要研究以原有项目提供mcp服务,故设计了该项目
项目已经封装为 springboot 的 starter 组件,仅需添加几个注解就能实现mcp服务器方法
项目地址: https://gitee.com/kylewka/smart-ai
2 使用说明
2.1 安装教程
- 确保您的开发环境已安装 jdk 8 或更高版本以及 maven
- 克隆本仓库到本地:
git clone https://gitee.com/kylewka/smart-ai.git
- 进入项目根目录,执行 maven 构建命令:
mvn clean install
2.2 添加依赖
在您的 spring boot 项目的 pom.xml
文件中添加以下依赖
该依赖暂未发布到 maven 仓库,需要手动安装至本地仓库
<dependency> <groupid>com.github.kylewka</groupid> <artifactid>smart-ai-mcp-spring-boot-starter</artifactid> <version>1.0.0</version> </dependency>
2.3 创建 mcp 服务端点
使用 @mcpserverendpoint 注解标注您的服务类,并使用 @mcpfunction 注解标注服务方法
@mcpserverendpoint(value = "/mcp", version = "1.0.0", name = "测试mcp服务") public class mcpservertool { @mcpfunction(name = "getweather", description = "获取天气信息") public string getweather(@mcpparam(name = "city", description = "城市名称", required = true) string city) { return string.format("%s: 晴天,温度25℃", city); } @mcpfunction(name = "getspeciality", description = "获取城市特产") public string getspeciality(@mcpparam(name = "city", description = "城市名称", required = true) string city) { return string.format("%s特产是小笼包", city); } }
2.4 启动应用
直接启动 spring boot 启动,即可启动mcp服务
- mcp服务受项目本身的鉴权系统影响,请根据实际情况进行配置
客户端使用
使用新版mcp协议的客户端软件 cherry studio,建议使用 cherry studio >= 1.2.0
客户端设置中添加mcp服务器
配置模型并选择mcp服务器
对话测试效果
到此这篇关于springboot改造mcp服务器的详细说明(streamablehttp 类型)的文章就介绍到这了,更多相关springboot mcp服务器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论