Build a Spring Boot MCP Server to Let AI Safely Call Your App
This guide walks you through creating a lightweight Spring Boot MCP service that securely exposes CRM data as AI‑callable tools, covering project setup, data model definition, tool implementation, registration, and both STDIO and SSE client connections for seamless AI integration.
What is Spring AI MCP?
Spring AI MCP framework exposes data and services as standardized tools for AI models, communicating via STDIO instead of traditional Web APIs, providing a secure and efficient way for native AI clients to interact with code.
In this guide we build a lightweight Spring Boot application that lets AI directly invoke business logic to query CRM user information.
Step 1: Create the Spring Boot project
Generate a project at start.spring.io with the dependency Spring AI MCP Server (STDIO transport) or Spring AI MCP Server WebMVC (SSE transport) and download the zip.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server</artifactId>
</dependency>Step 2: Understand the project structure
Customer.java : data model for customer records.
CustomerService.java : service class where AI‑exposed methods are defined.
TestMcpServerApplication.java : main application entry point.
application.properties : server configuration.
Step 3: Define the data model
package com.example.testmcpserver;
public record Customer(String id, String name, String email, String phone, String company) {}Step 4: Implement MCP tools
In CustomerService.java annotate methods with @Tool to expose them to the MCP framework.
@Service
public class CustomerService {
private static final Logger log = LoggerFactory.getLogger(CustomerService.class);
private List<Customer> customers = new ArrayList<>();
@Tool(name = "get_customers", description = "Get a list of customers from the CRM system")
public List<Customer> getCustomers() {
return customers;
}
@Tool(name = "get_customer_by_name", description = "Get a single customer from the CRM system by name")
public Customer getCustomerByName(String name) {
return customers.stream()
.filter(customer -> customer.name().equals(name))
.findFirst()
.orElse(null);
}
@PostConstruct
public void init() {
customers.addAll(List.of(
new Customer("1","张三","[email protected]","13800138001","阿里巴巴"),
new Customer("2","李四","[email protected]","13800138002","腾讯科技"),
new Customer("3","王五","[email protected]","13800138003","字节跳动"),
new Customer("4","赵六","[email protected]","13800138004","华为技术"),
new Customer("5","钱七","[email protected]","13800138005","百度在线")
));
}
}Step 5: Register the tools
In TestMcpServerApplication.java create a bean that registers all @Tool methods.
@SpringBootApplication
public class TestMcpServerApplication {
public static void main(String[] args) {
SpringApplication.run(TestMcpServerApplication.class, args);
}
@Bean
public List<ToolCallback> crmTools(CustomerService customerService) {
return List.of(ToolCallbacks.from(customerService));
}
}Running the MCP client
Build the executable JAR with mvn clean package, locate it in the target/ directory, and run it.
Option 1: Connect via Cursor IDE
Configure a new MCP server in Cursor with a JSON like:
{
"name":"crm-demo-mcp",
"command":"java",
"args":["-jar","path/to/crm-mcp-server-0.0.1-SNAPSHOT.jar"]
}Then issue commands such as “Get all CRM customer information” or “Find customer named ‘张三’”.
Option 2: Use SSE transport
Add the WebMVC dependency:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>Running the Spring Boot application will expose an /sse MCP endpoint. Configure the client with:
{
"mcpServers": {
"crm-demo-mcp": {
"url": "http://localhost:8080/sse"
}
}
}Conclusion
The Spring Boot MCP service enables AI to safely and efficiently access application data, providing a foundation for building smarter, AI‑driven applications.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
