关于 Spring MCP Server
Spring MCP Server 是面向 Java 开发者的 Spring Boot MCP 服务示例,展示如何将服务提供的信息作为工具公开,并配置 Claude Desktop 连接。
主要功能
- 01Spring Boot MCP 服务配置
- 02通过工具公开服务数据
- 03工具回调配置
- 04Maven 构建说明
适用场景
- 01原型开发 Java MCP 服务
- 02向 Claude Desktop 提供应用信息
MCP 相关
它实现 MCP 服务,通过工具向 MCP 客户端公开服务信息。README 展示了与 Claude Desktop 的连接方式。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
Spring MCP 服务
模型上下文协议是一项开放标准,开发者可以借此在数据源与 AI 工具之间建立安全的双向连接。 其架构十分简单:开发者既可以通过 MCP 服务公开数据,也可以构建连接到这些服务的 AI 应用(MCP 客户端)。
前置条件
- Java 21
- Maven 3.9.9
- Spring Boot 3.4.4
- Spring AI 0.4.0
- Spring Model Context Protocol Server
- Claude Desktop Application
开始使用
在此示例中,我们将创建一个简单的 Spring Boot 应用,将其用作 MCP 服务。
1. 创建 Spring Boot 应用
使用 Spring Initializr 或你偏好的方式创建一个新的 Spring Boot 应用。 你可以使用以下依赖:
- Spring Model Context Protocol Server
创建 record
创建一个 record 类,用于表示你想通过 MCP 服务公开的信息。
package org.sehn.spring_mcp;
public record Info(String title, String url) {
}
创建服务
创建一个服务类,用于提供你想通过 MCP 服务公开的信息。
package org.sehn.spring_mcp;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class InfoService {
private static final Logger logger = LoggerFactory.getLogger(InfoService.class);
private List<Info> information = new ArrayList<>();
@Tool(name = "dr_get_information", description = "Get a list of information about Deepanshu Rawat")
public List<Info> getInformation() {
return information;
}
@Tool(name = "dr_get_info", description = "Get a specific information about Deepanshu Rawat")
public Info getInfo(String title) {
return information.stream().filter(info -> info.title().equals(title)).findFirst().orElse(null);
}
@PostConstruct
public void init() {
information.addAll(List.of(
new Info("Portfolio", "https://bento.me/deepanshu-rawat6"),
new Info("GitHub", "https://github.com/deepanshu-rawat6"),
new Info("LinkedIn", "https://www.linkedin.com/in/deepanshu-rawat6/"),
new Info("Twitter", "https://twitter.com/deepanshuurawat")
));
}
}
创建工具回调
在 SpringMCPApplication 或你的主应用类中,创建一个工具回调,用于处理来自 MCP 服务的请求。
@Bean
public List<ToolCallback> deepanshuTools(InfoService infoService) {
return List.of(ToolCallbacks.from(infoService));
}
添加配置
创建一个配置类,用于配置 MCP 服务。
spring.application.name=spring_mcp
spring.main.web-application-type=none
spring.ai.mcp.server.name=spring_mcp
spring.ai.mcp.server.version=0.0.1
spring.main.banner-mode=off
logging.pattern.console=
2. 构建应用
运行以下命令即可构建应用:
mvn clean package -DskipTests
3. 进一步配置
现在,要使用 MCP 服务,你需要在 claude_desktop_config.json 中添加以下配置:
{
"mcpServers": {
"<name_of_the_mcp_server>": {
"command": "java",
"args": [
"-jar",
"<Path_of_the_jar_file_created_after_building>"
]
}
}
}
将 <name_of_the_mcp_server> 替换为你的 MCP 服务名称,并将 <Path_of_the_jar_file_created_after_building> 替换为应用构建后生成的 jar 文件路径。
例如,我的 MCP 服务名称是 spring_mcp。
4. 测试 MCP 服务
现在,启动 Claude Desktop,提示词区域下方会出现一些新内容:
已安装的 MCP 服务

可用的 MCP 工具

现在,输入提示词,例如:Get me the information about Deepanshu Rawat,你会看到一条关于正在使用第三方工具的警告,只需在当前聊天中接受该警告即可。
随后你会收到如下响应:

评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。