Spring Web到MCP转换器🚀

一 OpenRewrite 配方集合,自动将Spring Web REST API转换为Spring AI模型上下文协议(MCP)服务器工具。
📋 引言
该项目提供了一组OpenRewrite配方,可帮助您将传统的Spring Web REST API迁移到Spring AI的模型上下文协议(MCP)服务器工具。转型包括:
- 🔄 将Spring Web注释转换为Spring AI MCP
@Tool注释 - 🔧 添加必要的MCP配置和组件
- 📦 更新Maven依赖项以包含Spring AI MCP服务器组件
这些配方会自动从现有的REST控制器中提取文档,以创建正确记录的MCP工具,使AI代理可以通过 模型上下文协议.
有关Spring AI实现MCP的更多详细信息,请参阅 Spring AI MCP文档.
🛠️ 如何构建和安装
先决条件
- Java 17或更高版本
- Maven 3.6+
目标REST API项目的先决条件
为了成功地将您的Spring Web REST API迁移到MCP,您的项目应该:
- 使用Spring Boot 3.2+(3.2.0或更高版本)
- 将Spring Web MVC用于REST控制器
- 使用Maven构建工具
该配方会自动将Spring AI MCP依赖项(1.0.0-SNAPSHOT或更高版本)添加到您的项目中。
构建步骤
- 克隆此存储库:
git clone https://github.com/yourusername/web-to-mcp.git
cd web-to-mcp- 构建项目:
mvn clean install这将编译代码并将工件安装到本地Maven存储库中。
🔥 如何使用
要将配方应用于Spring Web项目,请运行以下Maven命令:
mvn org.openrewrite.maven:rewrite-maven-plugin:6.4.0:run \
-Drewrite.activeRecipes=RewriteWebToMCP \
-Drewrite.recipeArtifactCoordinates=com.atbug.rewrite:web-to-mcp:1.0-SNAPSHOT \
-Drewrite.exportDatatables=true重要:此命令需要执行两次:
- 第一次执行将更新pom.xml以添加必要的存储库和依赖项
- 第二次执行将执行Spring Web控制器到MCP工具的实际代码转换
✨ 特性
该配方执行了几个转换,这些转换分为三个主要部分:
1.POM更新(UpdatePom)
- 添加Spring快照存储库(
https://repo.spring.io/snapshot) - 添加中央门户快照存储库(
https://central.sonatype.com/repository/maven-snapshots/) - 添加Spring AI MCP服务器WebVC依赖项(
spring-ai-starter-mcp-server-webmvc)
2.代码转换
AddToolAnnotationToMappingMethod:自动将Spring Web控制器方法转换为MCP工具
- 增加 @Tool 对带有Spring Web映射注释的方法的注释(@GetMapping, @PostMapping等等) - 从JavaDoc注释中提取方法描述以填充 description 属性 - 增加 @ToolParam 方法参数的注释,保留JavaDoc中的描述
AddToolCallbackProviderBean:创建或更新bean以注册MCP工具
- 标识Spring Boot应用程序入口点类 - 创建一个 ToolCallbackProvider bean用于注册所有控制器 @Tool 注释 - 如果现有的提供程序bean已经存在,则智能地更新它们
AddSpringAIMcpProperties:配置MCP服务器属性
- 将所需的MCP服务器配置添加到 application.properties 或 application.yml - 设置服务器名称、版本、类型和消息端点 - 支持YAML和Properties文件格式
🧪 示例
之前(Spring Web控制器)
@RestController
@RequestMapping("/api/users")
public class UserController {
/**
* Get a user by ID
* @param id The user identifier
* @return The user details
*/
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
// Implementation
}
}之后(MCP工具)
@RestController
@RequestMapping("/api/users")
public class UserController {
/**
* Get a user by ID
* @param id The user identifier
* @return The user details
*/
@GetMapping("/{id}")
@Tool(description = "Get a user by ID")
public User getUserById(@ToolParam(description = "The user identifier") @PathVariable Long id) {
// Implementation
}
}生成的MCP配置
该配方还将自动将MCP服务器配置添加到您的应用程序属性中:
spring.ai.mcp.server.name=webmvc-mcp-server
spring.ai.mcp.server.sse-message-endpoint=/mcp/messages
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.version=1.0.0并通过添加 ToolCallbackProvider bean到Spring Boot应用程序类:
@Bean
ToolCallbackProvider toolCallbackProvider(UserController userController) {
return MethodToolCallbackProvider.builder()
.toolObjects(userController)
.build();
}🌟 演示
您可以在准备好进行转换的SpringBoot3RESTneneneba API项目中试用这个转换工具。
示例项目设置
- 克隆示例项目:
git clone https://github.com/addozhang/spring-boot-3-rest-api-sample.git
cd spring-boot-3-rest-api-sample- 查看示例项目结构:
- 这是一个带有REST控制器的标准Spring Boot 3应用程序 - 包括具有各种HTTP方法(GET、POST、PUT、DELETE)的典型REST端点 - 包含将转换为MCP工具描述的正确JavaDoc注释
转换过程
- 首先,运行Maven命令,使用所需的依赖项更新POM文件:
mvn org.openrewrite.maven:rewrite-maven-plugin:6.4.0:run \
-Drewrite.activeRecipes=RewriteWebToMCP \
-Drewrite.recipeArtifactCoordinates=com.atbug.rewrite:spring-rest-to-mcp:1.0-SNAPSHOT \
-Drewrite.exportDatatables=true- 然后,再次运行相同的命令以执行实际的代码转换:
mvn org.openrewrite.maven:rewrite-maven-plugin:6.4.0:run \
-Drewrite.activeRecipes=RewriteWebToMCP \
-Drewrite.recipeArtifactCoordinates=com.atbug.rewrite:spring-rest-to-mcp:1.0-SNAPSHOT \
-Drewrite.exportDatatables=true- 验证更改:
- 检查您的控制器类是否已添加 @Tool 和 @ToolParam 注释 - 寻找新的 ToolCallbackProvider bean在主应用程序类中 - 检查一下 application.properties 或 application.yml 具有MCP服务器配置
- 运行应用程序:
mvn spring-boot:run- 使用官方MCP检查器测试您的MCP服务器:
- 克隆MCP检查器存储库:
git clone https://github.com/modelcontextprotocol/inspector.git
cd inspector- 安装依赖项并启动检查器:
npm install
npm run dev- 在浏览器中访问检查器:http://localhost:5173/ - 在左侧面板中,使用以下配置MCP服务器: - 类型:SSE - 地址:http://localhost:8080/sse - 连接后,您可以: - 在主面板中查看所有可用工具 - 交互式测试每个工具 - 查看MCP服务器的响应
期待什么
转换后,SpringBoot应用程序将同时作为传统的RESTneneneba API和MCP服务器运行。这意味着:
- 您现有的所有端点继续像以前一样工作
- 支持MCP协议的应用程序可以发现API并与之交互
- AI助手可以通过MCP协议的标准化格式了解如何使用您的工具
使用MCP服务器的应用程序可以配置为使用以下配置连接到它:
{
"mcpServers": {
"spring-ai-mcp-sample": {
"autoApprove": [],
"disabled": false,
"timeout": 60,
"url": "http://localhost:8080/sse",
"transportType": "sse"
}
}
}这允许客户端应用程序无缝地发现和利用转换后的API提供的工具。
📄 许可证
此项目在Apache License 2.0下获得许可-有关详细信息,请参阅License文件。
👥 贡献
欢迎投稿!请随时提交拉取请求。
📞 支持
如果您有任何问题或需要帮助,请在GitHub上打开问题。
