Skip to content

Instantly share code, notes, and snippets.

@vguhesan
Created February 16, 2026 23:11
Show Gist options
  • Select an option

  • Save vguhesan/57a7675d944958348c46c0ba534611d1 to your computer and use it in GitHub Desktop.

Select an option

Save vguhesan/57a7675d944958348c46c0ba534611d1 to your computer and use it in GitHub Desktop.

Example of taking existing REST APIs in Java to create a MCP Server/Client

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

  1. Build and run: Use Maven/Gradle with spring-ai-starter-mcp-server dependency.
<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.

  1. The @Tool descriptions are derived from OpenAPI/Swagger docs.
  2. For automation, use OpenRewrite recipes (from GitHub link) to scan controllers and generate @Tool methods.
// Filename: McpServerApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import org.springframework.ai.mcp.Tool;
import org.springframework.ai.mcp.ToolCallbacks;
@SpringBootApplication
public class McpServerApp {
public static void main(String[] args) {
SpringApplication.run(McpServerApp.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public UserService userService(RestTemplate restTemplate) {
return new UserService(restTemplate);
}
@Bean
public List<ToolCallbacks> toolCallbacks(UserService userService) {
return List.of(ToolCallbacks.from(userService));
}
}
// Filename: User.java (Data model from OpenAPI)
public record User(String id, String name, String email) {}
// Filename: UserService.java (Wraps REST API calls)
import org.springframework.web.client.RestTemplate;
public class UserService {
private final RestTemplate restTemplate;
private final String baseUrl = "https://api.example.com"; // Existing REST API base URL
public UserService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Tool(name = "get_user", description = "Get user by ID from existing REST API (from OpenAPI: Retrieves user details)") // From Swagger docs
public User getUser(String id) {
return restTemplate.getForObject(baseUrl + "/users/" + id, User.class);
}
@Tool(name = "create_user", description = "Create a new user via existing REST API (from OpenAPI: Posts new user data)") // From Swagger
public User createUser(User user) {
return restTemplate.postForObject(baseUrl + "/users", user, User.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment