参考:
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 {
@Bean
public 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 {
@Autowired
private 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层),减少代码重复