基于Spring AI實現MCP客戶端 + 服務端,全網最保姆級的教程來了
我們在前文中介紹過,MCP是一種類似于USB-C接口一樣的標準化協議,能夠讓LLM以結構化的方式與外部工具及資源進行交互。
該協議支持多種傳輸機制,可在不同環境下提供靈活的適配能力。
而基于Spring AI實現的MCP協議,與Spring Boot進行了集成,提供客戶端和服務器端兩種啟動器Starter,可快速搭建具備 MCP 支持能力的 AI 應用。
Java MCP Client Architecture
在上圖中,MCP Client的核心作用在于與MCP Server建立并管理連接,并對其所提供的工具和能力進行發現、協商和執行,并與LLM的提示詞系統進行交互。
而MCP Server則是以提示詞模板匹配的方式,為MCP Client提供工具和能力。
接下來,我們基于Spring AI來實現MCP Client + Server的Demo,讓大家感受一下MCP的神奇之處。
準備工作
也可以通過這種方式來測試連通性,為Spring AI工程的引入做準備。

Spring AI構建MCP Client和MCP Server
接下來步入正題,我們開始進行Spring AI的工程構建,需要在工程中分為mcp-client和mcp-server兩個module。
圖片
1、mcp-server構建
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
</dependencies>我們在這里用的是1.0.0-M7版本,并引入 spring-ai-starter-mcp-server-webmvc jar包實現sse傳輸。
MCPService.java
package com.tony.mcpserver.service;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
@Service
public class MCPService {
@Tool(description = "中國最宜居的城市")
public String livingCity() {
return "最宜居的城市是天津,風景秀美,美食眾多,消費不貴。";
}
}在 Spring AI 中,@Tool 注解是實現 AI 模型與外部工具(如函數、服務、API 等)交互的核心機制。
它允許將普通 Java 方法標記為 “可被 AI 模型調用的工具”,從而讓LLM能夠根據用戶請求自動選擇并執行相應工具,擴展 AI 的能力邊界(數據庫查詢、API調用、文件處理等)。
ToolCallbackProviderConfig.java
package com.tony.mcpserver;
import com.tony.mcpserver.service.MCPService;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ToolCallbackProviderConfig {
@Bean
public ToolCallbackProvider livingCityTools(MCPService mcpService) {
return MethodToolCallbackProvider.builder().toolObjects(mcpService).build();
}
}將指定的工具對象( mcpService)注冊為可被 AI 模型調用的工具集。
MCPServerApplication.java
package com.tony.mcpserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MCPServerApplication {
public static void main(String[] args) {
SpringApplication.run(MCPServerApplication.class, args);
}
}application.properties
server.port=8080
spring.ai.mcp.server.name=city-mcp-server
spring.ai.mcp.server.sse-message-endpoint=/mcp/city至此,我們終于將MCP Server工程搭建完成,下面來訪問一下,看看效果。

我們可以在啟動日志中看到這條日志,代表已經將工具對象注冊成功。

通過訪問上述URL證明,SSE服務端已正確啟動并監聽端口。
2、mcp-client構建
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>
</dependencies>在這里,我們需要將Spring AI的版本與server端對應上,并引入mcp-client和ollama的依賴jar包。
MCPController.java
package com.tony.mcp.client;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MCPController {
@Autowired
private OllamaChatModel ollamaChatModel;
@Autowired
private ToolCallbackProvider toolCallbackProvider;
@GetMapping("/mcp")
public String MCPGenerate(@RequestParam(value = "message", defaultValue = "推薦宜居的城市") String message) {
ChatClient chatClient = ChatClient.builder(ollamaChatModel)
.defaultTools(toolCallbackProvider.getToolCallbacks())
.build();
return chatClient.prompt(message).call().content();
}
}該類用于接收用戶請求,與MCP Server建立連接并發現其所提供的帶@Tool注解的對象和方法,以及與LLM的提示詞系統進行交互。
MCPClientApplication.java
package com.tony.mcp.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MCPClientApplication {
public static void main(String[] args) {
SpringApplication.run(MCPClientApplication.class, args);
}
}application.properties
server.port=8081
spring.ai.ollama.base-url=http://127.0.0.1:11434/
spring.ai.ollama.chat.model=qwen3:8b
spring.ai.ollama.chat.options.temperature=0.7
spring.ai.mcp.client.name=mcp-client
spring.ai.mcp.client.sse.connections.server1.url=http://localhost:8080
spring.ai.mcp.client.toolcallback.enabled = true在該文件中,包括與MCP Server和Ollama的連接配置項。
至此,我們也將MCP Client工程搭建完成了,接下來我們把MCP Client和MCP Server的服務全部啟動起來,看看運行效果。

從輸出的這些內容可以看出來,這條請求確實到了MCP Server,并根據@Tool方法的內容返回了結果,實驗成功。
結語
我這段時間寫Spring AI代碼最直觀的感受是,LLM在講究因果的工程代碼中增加了智能因素。
























