Building and Using a Model Context Protocol (MCP) Server with Spring AI
The article explains Anthropic’s Model Context Protocol, outlines its architecture, and provides a step‑by‑step guide to creating a Spring AI‑based MCP server in Java—including adding the starter, defining @Tool‑annotated services, packaging the jar, configuring the Cline plugin, and demonstrating advanced tools such as Elasticsearch queries.
Model Context Protocol (MCP) is an open protocol released by Anthropic in Nov 2024 to standardize how applications provide context to large language models, similar to a USB‑C interface for AI.
The article first introduces the basic architecture of MCP, describing the roles of MCP Host, MCP Server, local data sources and remote services.
It then walks through a hands‑on development of an MCP Server using Spring AI in Java. The steps include:
Adding the Spring AI MCP Server starter dependency to pom.xml .
Defining a tool class ( LarkService ) that sends Feishu messages, annotated with @Tool .
Creating the Spring Boot main class ( McpServerApplication ) and exposing the tool via a ToolCallbackProvider bean.
Packaging the application with mvn clean package to obtain an executable jar.
Configuration of the MCP Server in the Cline plugin is shown as a JSON snippet, specifying the server identifier, command, arguments, environment variables, auto‑approved tools and transport type.
{
"mcpServers": {
"lark": {
"disabled": false,
"timeout": 60,
"command": "/Users/admin/Documents/jdk-17.jdk/Contents/Home/bin/java",
"args": [
"-Dspring.ai.mcp.server.stdio=true",
"-Dspring.main.web-application-type=none",
"-Dlogging.pattern.console=",
"-jar",
"/Users/admin/Documents/git/open-source/spring-ai-mcp-server-demo/target/spring-ai-mcp-server-demo-1.0-SNAPSHOT.jar"
],
"env": {
"larkAppId": "xxx",
"larkAppSecret": "xxx"
},
"autoApprove": ["sendLarkCardMessage"],
"transportType": "stdio"
}
}
}After configuring, the Cline plugin can invoke the MCP Server’s tool to send a Feishu card message, and the article demonstrates the interaction flow with screenshots.
For advanced usage, an example of an Elasticsearch query tool is provided, showing how to annotate a method with @Tool and call it from the model.
@Tool(description = """
通用ES查询工具,参数示例:
path: 请求路径
method: HTTP请求方法 GET 或 POST
queryJson: 具体请求体
""")
public String searchByQuery(String path, String method, String queryJson) {
String url = String.format("%s/%s", System.getenv("esBaseUrl"), path);
HttpEntity
request = buildEsRequest(queryJson);
ResponseEntity
response = restTemplate.exchange(
url, HttpMethod.valueOf(method), request, String.class);
return response.getBody();
}The article concludes that MCP provides a unified way to connect LLMs with various services, and encourages developers to explore and combine multiple MCP Servers for complex workflows.
DeWu Technology
A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.
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.