The Model Context Protocol (MCP) has become the standard way to connect AI models to external tools and data. But nearly every tutorial is Python or TypeScript — leaving Java shops out. The good news: with Spring AI, exposing your existing Spring services as MCP tools is straightforward, and any MCP client (Claude Desktop, IDE assistants, your own agent) can then call them.
Why an MCP server?
Without MCP, every AI integration is bespoke: you wire each tool into each app by hand. MCP flips that — you build one server that exposes capabilities in a standard shape, and any MCP-aware client can discover and use them. For an enterprise with existing Java services, an MCP server is the cleanest bridge between your systems and AI assistants.
1. Add the MCP server starter
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server</artifactId>
</dependency>
spring.ai.mcp.server.name=order-tools
spring.ai.mcp.server.version=1.0.0
2. Expose services as tools
A tool is a Spring bean method annotated with @Tool — exactly like local tool calling. The MCP server publishes these so remote clients can call them. The description is the contract the client's model reads to decide when to call it.
@Service
class OrderTools {
private final OrderRepository orders;
OrderTools(OrderRepository orders) {
this.orders = orders;
}
@Tool(description = "Get the current status and ETA for a customer order by ID")
OrderStatus orderStatus(
@ToolParam(description = "Order ID, e.g. ORD-1234") String orderId) {
return orders.findStatus(orderId);
}
}
record OrderStatus(String orderId, String state, String eta) {}
3. Register the tools with the server
@Configuration
class McpConfig {
@Bean
ToolCallbackProvider orderToolCallbacks(OrderTools orderTools) {
return MethodToolCallbackProvider.builder()
.toolObjects(orderTools)
.build();
}
}
That's it — start the app and the server advertises orderStatus over MCP. Local clients connect over stdio; networked clients over SSE/HTTP, configurable via properties.