Build a Java MCP Server with Spring AI in Minutes
This tutorial shows how to use Spring AI MCP to create a Java MCP server, covering environment setup, business logic implementation, service registration, and client configuration, enabling seamless AI service integration with minimal effort.
Model Context Protocol (MCP) is a next‑generation AI service interaction protocol, and its Java SDK 0.8 adds powerful session management and tool integration, but using the raw SDK requires manual dependency injection, complex tool registration, and lacks deep Spring integration.
Spring AI MCP Extension
To address these issues, the Spring team released Spring AI MCP , a Spring Boot starter that provides one‑click integration and dramatically lowers the barrier for enterprise‑grade AI service development.
Demo Overview
This article guides you to build a custom Java MCP server with Spring AI MCP, allowing any client to call enterprise AI services directly and embed business logic and tools into the AI interaction flow.
Development Steps
1. Environment Preparation
<code><dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency></code>2. Business Implementation
<code>@Service
public class MeilisearchService {
@Tool(description = "PIG ISSUE knowledge base search to solve user technical problems")
public String queryQuestion(@ToolParam(description = "User's technical problem description") String question) {
Client client = new Client(new Config());
SearchResult results = client.index("pigx-doc")
.search(new SearchRequest(question)
.setShowMatchesPosition(true)
.setSort(new String[]{"lvl2:desc"})
.setLimit(1));
return results.getHits().stream()
.map(hit -> "【" + hit.get("lvl0") + "】" + hit.get("text"))
.collect(Collectors.joining("\n\n"));
}
}</code>3. Service Registration
<code>@Configuration
public class McpConfig {
@Bean
public ToolCallbackProvider documentTools(MeilisearchService searchService) {
return MethodToolCallbackProvider.builder()
.toolObjects(searchService)
.build();
}
}</code>Client Configuration Guide
General JSON Configuration
<code>{
"mcpServers": {
"pig-issue": {
"isActive": true,
"command": "java",
"args": [
"-Dspring.ai.mcp.server.stdio=true",
"-jar",
"/Users/lengleng/env/repository/io/github/pig-mesh/pig-issue-query-mcp/0.0.1-SNAPSHOT/pig-issue-query-mcp-0.0.1-SNAPSHOT.jar"
]
}
}
}</code>Graphical Configuration
Cherry client configuration interface:
PIG AI client configuration interface:
Conclusion
Model Context Protocol (MCP) has become a standard for large‑model applications, and the new Java SDK accelerates its adoption in the Java ecosystem. Spring AI MCP simplifies development with a declarative programming model using the
@Toolannotation to expose business capabilities as AI tools.
Through the Meilisearch integration example, the article demonstrates that only three steps are needed to wrap a traditional service with AI capabilities: develop tool methods, register the service, and configure the client. This low‑intrusion approach preserves existing technology stacks while adding intelligent interaction, making MCP the best practice for building enterprise‑grade AI applications.
Reference: Java SDK – https://github.com/modelcontextprotocol/java-sdk
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.