这是一个 OpenRewrite 配方集合,自动将 Spring Web REST API 转换为 Spring AI 模型上下文协议(MCP)服务器工具。
该项目提供了一组 OpenRewrite 配方,帮助您将传统的 Spring Web REST API 迁移到 Spring AI 的模型上下文协议(MCP)服务器工具。转换包括:
@Tool 注解这些配方会自动从您的现有 REST 控制器中提取文档,创建适当记录的 MCP 工具,使您的 API 可通过 模型上下文协议 访问到 AI 代理。
有关 Spring AI 实现 MCP 的更多详情,请参阅 Spring AI MCP 文档。
要成功将您的 Spring Web REST API 迁移到 MCP,您的项目应满足以下条件:
该配方会自动向您的项目添加 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
重要:此命令需要执行两次:
该配方执行多个转换,组织成三个主要组件:
UpdatePom)https://repo.spring.io/snapshot)https://central.sonatype.com/repository/maven-snapshots/)spring-ai-starter-mcp-server-webmvc)AddToolAnnotationToMappingMethod:自动将 Spring Web 控制器方法转换为 MCP 工具
@GetMapping,@PostMapping 等)添加 @Tool 注解description 属性@ToolParam 注解,保留其来自 JavaDoc 的描述AddToolCallbackProviderBean:创建或更新一个 Bean 来注册 MCP 工具
ToolCallbackProvider Bean 来注册所有带有 @Tool 注解的控制器AddSpringAIMcpProperties:配置 MCP 服务器属性
application.properties 或 application.yml@RestController
@RequestMapping("/api/users")
public class UserController {
/**
* 根据ID获取用户
* @param id 用户标识符
* @return 用户详细信息
*/
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
// 实现
}
}
@RestController
@RequestMapping("/api/users")
public class UserController {
/**
* 根据ID获取用户
* @param id 用户标识符
* @return 用户详细信息
*/
@GetMapping("/{id}")
@Tool(description = "根据ID获取用户")
public User getUserById(@ToolParam(description = "用户标识符") @PathVariable Long id) {
// 实现
}
}
该配方还将自动添加 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();
}
您可以尝试使用一个准备转换的样本 Spring Boot 3 REST API 项目来体验这个转换工具。
克隆样本项目:
git clone https://github.com/addozhang/spring-boot-3-rest-api-sample.git
cd spring-boot-3-rest-api-sample
查看样本项目结构:
首先,运行 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 Beanapplication.properties 或 application.yml 是否有 MCP 服务器配置运行应用程序:
mvn spring-boot:run
使用官方 MCP Inspector 测试您的 MCP 服务器:
git clone https://github.com/modelcontextprotocol/inspector.git
cd inspector
npm install
npm run dev
转换后,您的 Spring Boot 应用程序将同时作为传统 REST API 和 MCP 服务器工作。这意味着:
消费您的 MCP 服务器的应用程序可以配置如下连接:
{
"mcpServers": {
"spring-ai-mcp-sample": {
"autoApprove": [],
"disabled": false,
"timeout": 60,
"url": "http://localhost:8080/sse",
"transportType": "sse"
}
}
}
这允许客户端应用程序无缝地发现并利用您转换后的 API 提供的工具。
本项目在 Apache 许可证 2.0 下发布 - 详情见 LICENSE 文件。
欢迎贡献!请随时提交拉取请求。
如果您有任何问题或需要帮助,请在 GitHub 上打开一个问题。