Spring Web to MCP Converter 🚀

Spring Web to MCP Converter 🚀

Okay, here's a translation of "Convert Spring REST API to MCP Server with OpenRewrite" into Chinese, along with some considerations for different nuances: **Option 1 (More Literal, General):** * **中文:** 使用 OpenRewrite 将 Spring REST API 转换为 MCP 服务器 * **Pinyin:** Shǐyòng OpenRewrite jiāng Spring REST API zhuǎnhuàn wèi MCP fúwùqì * **Explanation:** This is a straightforward translation. It's suitable if you're talking about a general process or concept. **Option 2 (More Technical, Emphasizing the Transformation):** * **中文:** 使用 OpenRewrite 重构 Spring REST API 为 MCP 服务器 * **Pinyin:** Shǐyòng OpenRewrite chónggòu Spring REST API wèi MCP fúwùqì * **Explanation:** "重构 (chónggòu)" means "refactor." This is a better choice if you're emphasizing the code transformation aspect of using OpenRewrite. It implies that you're changing the structure of the code to fit the MCP server architecture. **Option 3 (More Specific, Implying Migration):** * **中文:** 使用 OpenRewrite 将 Spring REST API 迁移到 MCP 服务器 * **Pinyin:** Shǐyòng OpenRewrite jiāng Spring REST API qiānyí dào MCP fúwùqì * **Explanation:** "迁移 (qiānyí)" means "migrate." This is suitable if you're talking about moving the API from one platform (Spring) to another (MCP). **Option 4 (Most Concise, Assumes Context):** * **中文:** OpenRewrite 实现 Spring REST API 到 MCP 服务器的转换 * **Pinyin:** OpenRewrite shíxiàn Spring REST API dào MCP fúwùqì de zhuǎnhuàn * **Explanation:** This is the most concise. It assumes the reader already knows you're using OpenRewrite to *do* the conversion. "实现 (shíxiàn)" means "implement" or "achieve." **Which one to choose depends on the context:** * If you're introducing the idea for the first time, Option 1 or 2 is good. * If you're talking to someone familiar with the process, Option 4 is fine. * If you're specifically talking about moving the API to a new platform, Option 3 is best. **Important Considerations:** * **MCP Server:** Make sure the reader understands what "MCP Server" refers to. If it's a specific product or technology, you might need to provide more context or a brief explanation elsewhere. * **OpenRewrite:** Assuming the audience knows what OpenRewrite is. If not, a brief explanation might be needed. I would probably lean towards **Option 2 (重构)** if I were talking to a technical audience about the code changes involved. If it's a more general audience, **Option 1 (转换)** is safer.

addozhang

开发者工具
访问服务器

README

Spring Web 到 MCP 转换器 🚀

License

一个 OpenRewrite 配方集合,可自动将 Spring Web REST API 转换为 Spring AI 模型上下文协议 (MCP) 服务器工具。

📋 简介

本项目提供了一组 OpenRewrite 配方,可帮助您将传统的 Spring Web REST API 迁移到 Spring AI 的模型上下文协议 (MCP) 服务器工具。 转换包括:

  1. 🔄 将 Spring Web 注解转换为 Spring AI MCP @Tool 注解
  2. 🔧 添加必要的 MCP 配置和组件
  3. 📦 更新 Maven 依赖项以包含 Spring AI MCP 服务器组件

这些配方会自动从您现有的 REST 控制器中提取文档,以创建正确记录的 MCP 工具,从而使您的 API 可以通过 模型上下文协议 供 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 或更高版本)添加到您的项目中。

构建步骤

  1. 克隆此存储库:

    git clone https://github.com/yourusername/web-to-mcp.git
    cd web-to-mcp
    
  2. 构建项目:

    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

重要提示:此命令需要执行两次:

  1. 第一次执行将更新您的 pom.xml 以添加必要的存储库和依赖项
  2. 第二次执行将执行 Spring Web 控制器到 MCP 工具的实际代码转换

✨ 功能

该配方执行多个转换,这些转换被组织成三个主要组件:

1. POM 更新 (UpdatePom)

  • 添加 Spring Snapshots 存储库 (https://repo.spring.io/snapshot)
  • 添加 Central Portal Snapshots 存储库 (https://central.sonatype.com/repository/maven-snapshots/)
  • 添加 Spring AI MCP 服务器 WebMVC 依赖项 (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 注解的控制器
    • 如果已存在,则智能地更新现有的 provider bean
  • AddSpringAIMcpProperties: 配置 MCP 服务器属性

    • 将所需的 MCP 服务器配置添加到 application.propertiesapplication.yml
    • 设置服务器名称、版本、类型和消息端点
    • 支持 YAML 和 Properties 文件格式

🧪 示例

之前 (Spring Web Controller)

@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 Tool)

@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();
}

🌟 演示

您可以尝试使用一个已准备好转换的示例 Spring Boot 3 REST API 项目来使用此转换工具。

示例项目设置

  1. 克隆示例项目:

    git clone https://github.com/addozhang/spring-boot-3-rest-api-sample.git
    cd spring-boot-3-rest-api-sample
    
  2. 查看示例项目结构:

    • 这是一个标准的 Spring Boot 3 应用程序,带有 REST 控制器
    • 包括具有各种 HTTP 方法(GET、POST、PUT、DELETE)的典型 REST 端点
    • 包含正确的 JavaDoc 注释,这些注释将被转换为 MCP 工具描述

转换过程

  1. 首先,运行 Maven 命令以使用所需的依赖项更新 POM 文件:

    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
    
  2. 然后,再次运行相同的命令以执行实际的代码转换:

    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
    
  3. 验证更改:

    • 检查您的控制器类中是否添加了 @Tool@ToolParam 注解
    • 在您的主应用程序类中查找新的 ToolCallbackProvider bean
    • 检查 application.propertiesapplication.yml 是否具有 MCP 服务器配置
  4. 运行应用程序:

    mvn spring-boot:run
    
  5. 使用官方 MCP Inspector 测试您的 MCP 服务器:

    • 克隆 MCP Inspector 存储库:
      git clone https://github.com/modelcontextprotocol/inspector.git
      cd inspector
      
    • 安装依赖项并启动 inspector:
      npm install
      npm run dev
      
    • 在您的浏览器中访问 inspector:http://localhost:5173/
    • 在左侧面板中,使用以下内容配置您的 MCP 服务器:
      • 类型:SSE
      • 地址:http://localhost:8080/mcp/messages
    • 连接后,您可以:
      • 在主面板中查看所有可用的工具
      • 交互式地测试每个工具
      • 查看来自您的 MCP 服务器的响应

预期结果

转换后,您的 Spring Boot 应用程序将同时作为传统的 REST API 和 MCP 服务器运行。 这意味着:

  • 您所有现有的端点都像以前一样继续工作
  • 支持 MCP 协议的应用程序可以发现并与您的 API 交互
  • AI 助手可以通过 MCP 协议的标准化格式了解如何使用您的工具

使用您的 MCP 服务器的应用程序可以配置为使用如下配置连接到它:

{
  "mcpServers": {
    "spring-ai-mcp-sample": {
      "autoApprove": [],
      "disabled": false,
      "timeout": 60,
      "url": "http://localhost:8080/mcp/messages",
      "transportType": "sse"
    }
  }
}

这允许客户端应用程序无缝地发现和利用您的转换后的 API 提供的工具。

📄 许可证

本项目根据 Apache License 2.0 获得许可 - 有关详细信息,请参阅 LICENSE 文件。

👥 贡献

欢迎贡献! 请随时提交 Pull Request。

📞 支持

如果您有任何问题或需要帮助,请在 GitHub 上打开一个 issue。

推荐服务器

Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
MCP Package Docs Server

MCP Package Docs Server

促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。

精选
本地
TypeScript
Claude Code MCP

Claude Code MCP

一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。

精选
本地
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。

精选
本地
JavaScript
mermaid-mcp-server

mermaid-mcp-server

一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。

精选
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

精选
TypeScript
Linear MCP Server

Linear MCP Server

一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

精选
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Curri MCP Server

Curri MCP Server

通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。

官方
本地
JavaScript