> ## Documentation Index
> Fetch the complete documentation index at: https://blog.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Spring Web转MCP转换器：自动化API迁移工具

> 使用OpenRewrite自动将Spring Web REST API转换为Spring AI模型上下文协议(MCP)服务器工具的完整指南

Rest2MCP 帮助您将传统的 Spring Web REST API迁移到Spring AI的模型上下文协议(MCP)服务器工具。插件会自动从现有的REST控制器中提取文档，创建具有完整文档的MCP工具，使您的API能够通过[模型上下文协议](https://modelcontextprotocol.io/)为AI代理提供服务。

## ✨ 核心特性

* 🔄 **零侵入式转换**：不需要修改原有代码，保持现有业务逻辑完整性
* 🤖 **无需MCP基础**：不需要了解MCP开发基础知识，插件自动处理所有技术细节
* 🧠 **AI模型无关**：无需了解大模型支持细节，插件提供通用的MCP协议转换
* ⚡ **全自动化**：从依赖管理到代码转换，全部由插件自动完成
* 📖 **文档保留**：自动提取和转换现有的JavaDoc注释为MCP工具描述
* 🔧 **智能配置**：自动添加必要的Spring AI MCP配置和组件

![8eXTmD](https://minio.pigx.vip/oss/202506/8eXTmD.png)

## 🔥 使用方法

要将插件应用到您的Spring Web项目，请运行以下Maven命令：

```bash theme={null}
mvn org.openrewrite.maven:rewrite-maven-plugin:6.4.0:run \
  -Drewrite.activeRecipes=RewriteWebToMCP \
  -Drewrite.recipeArtifactCoordinates=com.pig4cloud.plugin:rest2mcp:1.0.0 \
  -Drewrite.exportDatatables=true
```

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

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

## ✨ 功能特性

该插件执行多个转换，这些转换被组织为三个主要组件：

### 1. POM更新（`UpdatePom`）

* 添加Spring AI MCP服务器WebMVC依赖（`spring-ai-starter-mcp-server-webmvc`）

### 2. 代码转换

* **`AddToolAnnotationToMappingMethod`**：自动将Spring Web控制器方法转换为MCP工具
  * 为带有Spring Web映射注解（`@GetMapping`、`@PostMapping`等）的方法添加`@Tool`注解
  * 从JavaDoc注释中提取方法描述以填充`description`属性
  * 为方法参数添加`@ToolParam`注解，保留JavaDoc中的参数描述
* **`AddToolCallbackProviderBean`**：创建或更新Bean以注册MCP工具
  * 识别Spring Boot应用程序入口点类
  * 创建`ToolCallbackProvider` Bean以注册所有带有`@Tool`注解的控制器
  * 如果已存在提供者Bean，则智能更新现有Bean
* **`AddSpringAIMcpProperties`**：配置MCP服务器属性
  * 向`application.properties`或`application.yml`添加必需的MCP服务器配置
  * 设置服务器名称、版本、类型和消息端点
  * 支持YAML和Properties文件格式

## 🧪 示例

![9oImF7](https://minio.pigx.vip/oss/202506/9oImF7.png)

### 转换前（Spring Web控制器）

```java theme={null}
@RestController
@RequestMapping("/api/customers")
public class CustomerController {
    
    private final CustomerService customerService;

    /**
     * 根据ID获取客户
     */
    @GetMapping("/{id}")
    public ResponseEntity<CustomerDTO> getCustomerById(@PathVariable Long id) {
        CustomerDTO customer = customerService.getCustomerById(id);
        return ResponseEntity.ok(customer);
    }
}
```

### 转换后（MCP工具）

```java theme={null}
@RestController
@RequestMapping("/api/customers")
public class CustomerController {
    
    private final CustomerService customerService;

    /**
     * 根据ID获取客户
     */
    @GetMapping("/{id}")
    @Tool(description = "根据ID获取客户")
    public ResponseEntity<CustomerDTO> getCustomerById(@PathVariable @ToolParam(description = "id") Long id) {
        CustomerDTO customer = customerService.getCustomerById(id);
        return ResponseEntity.ok(customer);
    }
}
```

### 生成的MCP配置

该插件还会自动向您的应用程序属性添加MCP服务器配置：

```properties theme={null}
spring.ai.mcp.server.name=webmvc-mcp-server
spring.ai.mcp.server.sse-message-endpoint=/sse
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.version=1.0.0
```

并通过向您的Spring Boot应用程序类添加`ToolCallbackProvider` Bean来自动注册您的工具：

```java theme={null}
@Bean
ToolCallbackProvider toolCallbackProvider(CustomerController customerController) {
    return MethodToolCallbackProvider.builder()
            .toolObjects(customerController)
            .build();
}
```

## 🧪 MCP 客户端调用测试

### 1. 配置MCP服务器端点

在MCP客户端中添加rest2mcp插件自动发布的SSE端点，即可建立与您的Spring Web应用的连接。以下以PIG AI为例进行演示：

![vxvwYe](https://minio.pigx.vip/oss/202506/vxvwYe.png)

### 2. 自然语言API调用

配置完成后，您可以使用自然语言直接查询和调用原有的REST接口功能，AI模型会自动将您的请求转换为相应的API调用：

![ONqCs9](https://minio.pigx.vip/oss/202506/ONqCs9.png)

通过MCP协议，您的Spring Web API现在可以无缝地为AI代理提供服务，实现真正的智能化API交互体验。
