spring-rest-to-mcp
Convert Spring REST API to MCP Server with OpenRewrite
Documentation
Spring Web to MCP Converter π
An OpenRewrite recipe collection that automatically converts Spring Web REST APIs to Spring AI Model Context Protocol (MCP) server tools.
π Introduction
This project provides a set of OpenRewrite recipes that help you migrate traditional Spring Web REST APIs to Spring AI's Model Context Protocol (MCP) server tools. The transformation includes:
1. π Converting Spring Web annotations to Spring AI MCP `@Tool` annotations
2. π§ Adding necessary MCP configuration and components
3. π¦ Updating Maven dependencies to include Spring AI MCP server components
The recipes automatically extract documentation from your existing REST controllers to create properly documented MCP tools, making your APIs accessible to AI agents through the Model Context Protocol.
For more details about Spring AI's implementation of MCP, see the Spring AI MCP documentation.
π οΈ How to Build and Install
Prerequisites
- Java 17 or higher
- Maven 3.6+
Prerequisites for Target REST API Projects
To successfully migrate your Spring Web REST API to MCP, your project should:
- Use Spring Boot 3.2+ (3.2.0 or newer)
- Use Spring Web MVC for REST controllers
- Use Maven build tool
The recipe adds Spring AI MCP dependencies (version 1.0.0-SNAPSHOT or newer) to your project automatically.
Build Steps
1. Clone this repository:
git clone https://github.com/yourusername/web-to-mcp.git
cd web-to-mcp2. Build the project:
mvn clean installThis will compile the code and install the artifact to your local Maven repository.
π₯ How to Use
To apply the recipes to your Spring Web project, run the following Maven command:
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=trueImportant: This command needs to be executed twice:
1. First execution will update your pom.xml to add necessary repositories and dependencies
2. Second execution will perform the actual code conversion of your Spring Web controllers to MCP tools
β¨ Features
The recipe performs several transformations that are organized into three main components:
1. POM Updates (`UpdatePom`)
- Adds Spring Snapshots repository (`https://repo.spring.io/snapshot`)
- Adds Central Portal Snapshots repository (`https://central.sonatype.com/repository/maven-snapshots/`)
- Adds Spring AI MCP server WebMVC dependency (`spring-ai-starter-mcp-server-webmvc`)
2. Code Transformations
- `AddToolAnnotationToMappingMethod`: Automatically converts Spring Web controller methods to MCP tools
- Adds `@Tool` annotations to methods with Spring Web mapping annotations (`@GetMapping`, `@PostMapping`, etc.)
- Extracts method descriptions from JavaDoc comments to populate the `description` attribute
- Adds `@ToolParam` annotations to method parameters, preserving their descriptions from JavaDoc
- `AddToolCallbackProviderBean`: Creates or updates a bean to register MCP tools
- Identifies Spring Boot application entry point class
- Creates a `ToolCallbackProvider` bean to register all controllers with `@Tool` annotations
- Intelligently updates existing provider beans if they already exist
- `AddSpringAIMcpProperties`: Configures MCP server properties
- Adds required MCP server configuration to `application.properties` or `application.yml`
- Sets server name, version, type, and message endpoints
- Supports both YAML and Properties file formats
π§ͺ Example
Before (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
}
}After (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
}
}Generated MCP Configuration
The recipe will also automatically add MCP server configuration to your application properties:
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.0And automatically register your tools by adding a `ToolCallbackProvider` bean to your Spring Boot application class:
@Bean
ToolCallbackProvider toolCallbackProvider(UserController userController) {
return MethodToolCallbackProvider.builder()
.toolObjects(userController)
.build();
}π Demonstration
You can try out this conversion tool with a sample Spring Boot 3 REST API project that's ready for conversion.
Sample Project Setup
1. Clone the sample project:
git clone https://github.com/addozhang/spring-boot-3-rest-api-sample.git
cd spring-boot-3-rest-api-sample2. Review the sample project structure:
Conversion Process
1. First, run the Maven command to update the POM file with required dependencies:
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=true2. Then, run the same command again to perform the actual code conversion:
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=true3. Verify the changes:
4. Run the application:
mvn spring-boot:run5. Test your MCP server using the official MCP Inspector:
git clone https://github.com/modelcontextprotocol/inspector.git
cd inspectornpm install
npm run devWhat to Expect
After conversion, your Spring Boot application will function both as a traditional REST API and as an MCP server. This means:
- All your existing endpoints continue to work as before
- Applications that support the MCP protocol can discover and interact with your API
- AI assistants can understand how to use your tools through the MCP protocol's standardized format
Applications consuming your MCP server can be configured to connect to it with configuration like:
{
"mcpServers": {
"spring-ai-mcp-sample": {
"autoApprove": [],
"disabled": false,
"timeout": 60,
"url": "http://localhost:8080/sse",
"transportType": "sse"
}
}
}This allows client applications to seamlessly discover and utilize the tools provided by your converted API.
π License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
π₯ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
π Support
If you have any questions or need help, please open an issue on GitHub.
Frequently asked questions
What is spring-rest-to-mcp?
spring-rest-to-mcp is Convert Spring REST API to MCP Server with OpenRewrite
How do I install spring-rest-to-mcp?
Open the GitHub repository and follow its README. Most MCP servers are added to your client's MCP config, then called by your agent.
Is spring-rest-to-mcp open source?
Yes β it is hosted on GitHub at https://github.com/addozhang/spring-rest-to-mcp and has 35 stars.
Related MCP tools
A library for communication with a Minecraft client/server. Built for the Model Context Protocol to enhance AI capabilities. Java-based implementation.
MCP Server for Ghidra Java-based implementation. Trusted by 6400+ developers. Trusted by 6400+ developers. Trusted by 6400+ developers.
The official Java SDK for Model Context Protocol servers and clients. Maintained in collaboration with Spring AI Trusted by 2800+ developers.
Plugin for JADX to integrate MCP server Java-based implementation. Trusted by 600+ developers. Trusted by 600+ developers.
π€ The Semantic Engine for Model Context Protocol(MCP) Clients and AIΒ Agents π₯ Java-based implementation.
The official TypeScript SDK for Model Context Protocol servers and clients Trusted by 10500+ developers. Trusted by 10500+ developers.
Run your own MCP server? See who uses it and what to fix.
Measure it with TrackMCP