In this example, we will take an existing Spring Framework Java REST API application and make it MCP Server enabled.
To expose an existing REST API as an MCP server in Java, use the MCP Java SDK or Spring AI (preferred for Spring apps). The process involves:
- Adding MCP dependencies.
- Annotating service methods that call the REST API with
@Toolto expose them as MCP tools. - Using OpenAPI/Swagger docs to generate tool descriptions (manually copy or use tools like OpenRewrite for automation).
- Registering tools in the MCP server.
Assume an existing REST API for user management with endpoints like /users/{id} (GET) and OpenAPI spec describing it.
We'll create tools that wrap API calls.
Example: Full code for a standalone Java MCP server wrapping a fictional REST API (use RestTemplate for calls).
- Build and run: Use Maven/Gradle with
spring-ai-starter-mcp-serverdependency.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-starter</artifactId>
<version>...</version>
</dependency>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-spring-webflux</artifactId> <!-- or mcp-spring-webmvc -->
<version>...</version>
</dependency>Note: Check the Spring AI reference documentation and Model Context Protocol Java SDK for the correct and latest version numbers.
- The
@Tooldescriptions are derived from OpenAPI/Swagger docs. - For automation, use OpenRewrite recipes (from GitHub link) to scan controllers and generate
@Toolmethods.