參考:
https://juejin.cn/post/7491881721278529570
SpringAI 實現 SSE MCP Server項目 - Auler - 博客園
springboot-MCPserver-JUnit: 使用springboot支持mcp項目搭建,同時有著便捷的單元測試來進行敏捷開發對話即服務:Spring Boot+MCP讓你的CRUD系統秒變AI助手 - 雨夢山人 - 博客園
java的jdk使用17,可參考:Java 17 Windows 安裝教程--保姆級安裝_java17-CSDN博客
java的mcp-server依賴使用1.0.0-M8,可參考:
MCP Server Boot Starter :: Spring AI Reference
Spring AI版本1.0.0-M6和M8效果比較-CSDN博客
本文使用了兩種mcp client:cherry studio和cursor
源代碼參考:我的/springboot-mcpserver-demo
代碼片段
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>top.dreamcenter</groupId><artifactId>mcp</artifactId><version>1.0.0-SNAPSHOT</version><name>mcp</name><description>Template project for SpringBoot + MCP</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- <dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId><version>1.0.0-M6</version></dependency> --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server-webmvc</artifactId><version>1.0.0-M8</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>top.dreamcenter.mcp.McpApplication</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
McpApplication.java
package top.dreamcenter.mcp;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class McpApplication {public static void main(String[] args) {SpringApplication.run(McpApplication.class, args);}}
NumService.java
package top.dreamcenter.mcp.service;import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;@Service
public class NumService {@Tool(description = "判斷是否是雙數")public String judgeIfOddJava(@ToolParam(description = "待判斷的數") Integer num) {return num + (num%2 == 0 ? "是雙數" : "不是雙數");}}
ToolCallbackProviderRegister.java
package top.dreamcenter.mcp.config;import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.dreamcenter.mcp.service.NumService;@Configuration
public class ToolCallbackProviderRegister {@Beanpublic ToolCallbackProvider numTools(NumService numService) {return MethodToolCallbackProvider.builder().toolObjects(numService).build();}}
NumController.java
package top.dreamcenter.mcp.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.dreamcenter.mcp.service.NumService;@RestController
@RequestMapping("/api/num")
public class NumController {@Autowiredprivate NumService numService;@GetMapping("/judge/{num}")public String judgeIfOddJava(@PathVariable Integer num) {return numService.judgeIfOddJava(num);}}
cherry studio效果
?
cursor效果
單獨通過url請求http://localhost:8000/sse端口的響應內容如下
MCP工具接口和RESTful接口
我們可以在同一個應用程序中同時提供MCP工具接口和RESTful接口。
MCP工具接口 - 通過@Tool注解在Service層提供AI工具能力
RESTful API接口 - 通過Controller層提供常規Web API訪問
?
這樣設計的優勢:
對于AI應用(如Claude這樣的大模型),可以通過MCP接口直接調用您的服務功能
對于傳統Web應用,可以通過RESTful API接口訪問相同的功能
共享相同的業務邏輯層(Service層),減少代碼重復