ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Java SpringBoot 实现 MCP Server SSE:TaoToken 统一 Key 接入配置与联调验证

Java SpringBoot 实现 MCP Server SSE:TaoToken 统一 Key 接入配置与联调验证 1. 为什么要在 SpringBoot 里自建 MCP Server SSEMCP Server 是让大模型调用你本地业务能力的一种标准协议SSE 则是它最常用的传输方式之一。简单说你写一个 Java 方法打上Tool注解模型就能通过 SSE 长连接发现并调用它。适合谁适合手里已经有 SpringBoot 项目、想让 CRUD 系统或内部工具被 AI 直接调用的后端开发者。我这次的目标很明确用 SpringBoot 3.4 Spring AI 1.0.0-M8 搭一个 MCP Server通过/sse端点对外暴露工具同时用 TaoToken 的统一 Key 作为模型侧通道把「模型对话 → 工具调用 → 业务返回」整条链路跑通。整个过程不依赖任何特殊网络环境本地localhost:8000就能验证。踩过的坑先提前说M8 版本的 starter 名字和 M6 不一样网上很多老教程还在用spring-ai-mcp-server-webmvc-spring-boot-starter直接抄会拉不到依赖。另外 SSE 端点的路径、工具注册方式、以及模型侧 Key 的配置位置这三处最容易卡住。下面按可复制的顺序一步步来。2. TaoToken 前置统一 Key 与通道准备TaoToken 在这里扮演的是「模型侧统一入口」的角色。你的 MCP Server 负责暴露工具但真正发起对话、决定调用哪个工具的是模型。TaoToken 提供一个统一的 API Key 和兼容通道让你不用在代码里散落多家厂商的 Key。你需要先拿到两样东西一个 API Key以及确认接入地址。Key 在控制台的 API Keys 页面创建地址是https://taotoken.net/api注意 API 调用不加 UTM 参数。控制台入口https://taotoken.net/console?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content API Keys 管理页https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。注意Key 只放在服务端配置文件或环境变量里不要提交到 Git也不要写进前端。本文所有示例用占位符sk-xxxx。如果你只是想先验证模型通道是否通可以直接用模型对话页试一条消息https://taotoken.net/chat?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。确认能正常返回后再回到 SpringBoot 里配置。3. 可复制配置pom、application.yml 与 MCP Server 骨架3.1 pom.xml 关键依赖JDK 用 17SpringBoot 用 3.4.3MCP starter 用spring-ai-starter-mcp-server-webmvc的1.0.0-M8。注意 M8 的 artifactId 已经简化不再是 M6 那串长名字。parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.3/version /parent properties java.version17/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-mcp-server-webmvc/artifactId version1.0.0-M8/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency /dependencies3.2 application.ymlMCP SSE 与 TaoToken KeyMCP Server 的 SSE 端点、名称、版本在这里声明TaoToken 的 Key 和 base-url 放在自定义节点下供模型客户端读取。server: port: 8000 spring: ai: mcp: server: name: springboot-mcp-demo version: 1.0.0 protocol: SSE sse-endpoint: /sse taotoken: api-key: sk-xxxx base-url: https://taotoken.net/api model: claude-3-5-sonnetprotocol: SSE和sse-endpoint: /sse决定了最终访问地址是http://localhost:8000/sse。端口我固定成 8000避免和常见 8080 冲突。3.3 工具类用 Tool 暴露业务能力工具方法写在 Service 层Tool的 description 会被模型用来判断何时调用所以描述要写清楚。Service public class NumService { Tool(description 判断一个整数是否为双数) public String judgeIfEven(ToolParam(description 待判断的整数) Integer num) { return num (num % 2 0 ? 是双数 : 不是双数); } }3.4 注册 ToolCallbackProviderM8 里工具通过MethodToolCallbackProvider注册成 BeanSpring AI 会自动把它挂到 MCP Server 上。Configuration public class ToolCallbackProviderRegister { Bean public ToolCallbackProvider numTools(NumService numService) { return MethodToolCallbackProvider.builder() .toolObjects(numService) .build(); } }3.5 启动类与 RESTful 共存同一个应用里MCP 工具接口和普通 REST 接口可以并存共享 Service 层逻辑。SpringBootApplication public class McpApplication { public static void main(String[] args) { SpringApplication.run(McpApplication.class, args); } }RestController RequestMapping(/api/num) public class NumController { Autowired private NumService numService; GetMapping(/judge/{num}) public String judge(PathVariable Integer num) { return numService.judgeIfEven(num); } }这样设计的好处是AI 侧走 MCP 的/sse传统 Web 侧走/api/num/judge/{num}业务逻辑只有一份。4. 验证请求curl 测 SSE 连接与工具调用4.1 启动并确认 SSE 端点mvn spring-boot:run启动后先确认 SSE 端点活着。SSE 是长连接curl 会持续输出事件流加-N关闭缓冲。curl -N http://localhost:8000/sse正常会看到类似event: endpoint和data: /mcp/message?sessionIdxxx的输出说明 SSE 通道已建立并返回了后续消息投递用的 sessionId。4.2 用 REST 接口快速验证业务逻辑在测 MCP 之前先用普通接口确认 Service 没问题curl http://localhost:8000/api/num/judge/8返回8 是双数说明工具方法本身逻辑正确。这一步能帮你把「业务 bug」和「MCP 配置 bug」分开。4.3 通过 MCP 客户端触发工具调用SSE 本身只是通道真正触发工具调用需要 MCP 客户端。你可以用支持 MCP 的客户端如 Cursor 或 Cherry Studio配置 SSE 地址http://localhost:8000/sse然后在对话里问「8 是不是双数」。模型会通过 TaoToken 通道发起对话识别到需要调用工具再经 SSE 调用你的judgeIfEven。模型侧配置时把 base-url 指向https://taotoken.net/apiKey 填你的sk-xxxx。这样模型对话走 TaoToken工具执行走你本地 MCP Server链路清晰。4.4 成功结果长什么样客户端里会看到模型先输出一段「我来调用工具判断」然后返回8 是双数。同时你的 SpringBoot 控制台会打印工具被调用的日志。两条都出现说明整条链路通了。5. 本篇常见错排查依赖拉不到M8 的 artifactId 是spring-ai-starter-mcp-server-webmvc不是 M6 的spring-ai-mcp-server-webmvc-spring-boot-starter。版本号写1.0.0-M8别写1.0.0。SSE 连上但工具不触发先确认ToolCallbackProviderBean 被扫描到包路径要在启动类的同级或子包下。再看Tool的 description 是否足够明确描述太模糊模型不会选它。端口冲突默认 8080 常被占用本文固定 8000。如果 8000 也被占改server.port后记得同步改客户端里的 SSE 地址。Key 报 401检查taotoken.api-key是否有多余空格base-url 是否为https://taotoken.net/api。API 地址不要带 UTM 参数带了可能被当成非法路径。curl 看不到事件流SSE 是流式响应必须加-N否则 curl 会缓冲导致看起来没输出。另外别用浏览器直接开/sse浏览器会一直转圈。M8 与 M6 行为差异M8 对工具注册和协议字段做了调整如果你从 M6 升级重点检查protocol和sse-endpoint两个配置项以及 starter 名称。6. 接入与后续把 Key 和文档用起来链路跑通后下一步通常是把模型侧配置固化下来。如果你要长期做编码或 Agent 类应用建议用 Coding Plan 统一管理额度与 Keyhttps://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。接入细节和参数说明看官方文档https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。排障时优先回到 API Keys 页面确认 Key 状态https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。想快速验证模型通道是否正常用模型对话页发一条消息即可https://taotoken.net/chat?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 。最后给个实用建议把application.yml里的 Key 换成环境变量注入比如taotoken.api-key: ${TAOTOKEN_API_KEY}本地用 IDE 配置线上用容器环境变量。这样既安全也方便多环境切换。工具方法尽量保持单一职责一个Tool只做一件事模型选择准确率会明显更高。
返回列表