王炸!Spring AI+MCP 三步實(shí)現(xiàn)智能體開(kāi)發(fā)
環(huán)境:SpringBoot3.4.2
1. 簡(jiǎn)介
1.1 什么是MCP
Model Context Protocol(MCP)模型上下文協(xié)議是一種標(biāo)準(zhǔn)化協(xié)議,它讓大模型能夠更容易地和外部的數(shù)據(jù)、工具連接起來(lái)。你可以把MCP想象成一個(gè)通用的插頭或者接口,就像USB-C一樣,不管是什么設(shè)備,只要插上這個(gè)接口,就能和電腦、充電器等連接起來(lái)。
注意,它連接的不是物理設(shè)備,而是AI模型和外部的數(shù)據(jù)源、工具等。有了MCP,AI模型就能更方便地獲取外部的信息,完成更多的任務(wù)。比如,通過(guò)MCP,AI模型可以操作電腦讀寫(xiě)文件,或者模擬瀏覽器操作等。
1.2 為什么需要MCP
首先,MCP提供了一個(gè)標(biāo)準(zhǔn)化的接口,使得AI模型能夠輕松地與各種外部工具和數(shù)據(jù)源進(jìn)行交互,無(wú)需為每個(gè)工具或數(shù)據(jù)源單獨(dú)開(kāi)發(fā)集成代碼。
其次,MCP還解決了數(shù)據(jù)孤島問(wèn)題,通過(guò)統(tǒng)一協(xié)議連接分散的數(shù)據(jù)源,使AI模型能夠?qū)崟r(shí)訪問(wèn)和利用最新的數(shù)據(jù)。
總的來(lái)說(shuō),MCP就像是一個(gè)橋梁,讓AI模型與外部世界更好地連接起來(lái),從而發(fā)揮出更大的價(jià)值和潛力。
1.3 Java與MCP架構(gòu)
- 客戶端/服務(wù)器層:McpClient負(fù)責(zé)處理客戶端操作,而McpServer則管理服務(wù)器端協(xié)議操作。兩者都利用McpSession來(lái)進(jìn)行通信管理。
- 會(huì)話層(McpSession):通過(guò)DefaultMcpSession實(shí)現(xiàn)來(lái)管理通信模式和狀態(tài)。
- 傳輸層(McpTransport):處理JSON-RPC消息的序列化和反序列化,并支持多種傳輸實(shí)現(xiàn)。
MCP Client
MCP客戶端是模型上下文協(xié)議(MCP)架構(gòu)中的關(guān)鍵組件,負(fù)責(zé)建立和管理與MCP服務(wù)器的連接。它實(shí)現(xiàn)了協(xié)議的客戶端部分,如下圖所示:
Java MCP Client Architecture
MCP Server
MCP服務(wù)器是模型上下文協(xié)議(MCP)架構(gòu)中的基礎(chǔ)組件,它為客戶端提供工具、資源和功能。它實(shí)現(xiàn)了協(xié)議的服務(wù)器端部分,如下圖所示:
Java MCP Server Architecture
Spring AI 提供了相對(duì)應(yīng)的Spring Boot staters來(lái)非常方便的進(jìn)行 MCP 的集成。接下來(lái),我們將詳細(xì)的完成一個(gè)完整的MCP應(yīng)用案例。
2. 實(shí)戰(zhàn)案例
2.1 服務(wù)端開(kāi)發(fā)
我們將在 MCP 服務(wù)端提供2個(gè)外部功能:查詢天氣預(yù)報(bào)、獲取IP地址詳細(xì)信息。
引入依賴
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
說(shuō)明:
- 引入alibaba-starter我們將使用阿里的大模型
- 引入mcp-server-webflux,以支持基于 Spring WebFlux 的 SSE(服務(wù)器發(fā)送事件)服務(wù)器傳輸
配置文件
spring:
ai:
dashscope:
api-key: sk-xxxooo
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
stream: true
model: qwen-turbo
---
spring:
ai:
mcp:
server:
enabled: true
name: ai_mcp_server
version: 1.0.0
type: ASYNC
sse-message-endpoint: /mcp/message
以上我們就完成了基本的配置。接下來(lái),我們需要提供2個(gè)外部工具。
工具編寫(xiě)
獲取天氣預(yù)報(bào)
public class CommonTool {
@Tool(description = "獲取當(dāng)前天氣預(yù)報(bào)")
WeatherResponse getCurrentWeather(WeatherRequest request) {
System.err.printf("準(zhǔn)備查詢【%s】天氣預(yù)報(bào)%n", request.city()) ;
RestClient client = RestClient.create(URI.create("https://api.vvhan.com")) ;
Map<?, ?> result = client.get()
.uri("/api/weather?city={0}", request.city())
.retrieve()
.body(Map.class) ;
try {
return new WeatherResponse(new ObjectMapper().writeValueAsString(result)) ;
} catch (JsonProcessingException e) {
throw new RuntimeException(e) ;
}
}
@Tool(description = "獲取IP地址詳細(xì)信息")
String getIpAddressInfo(String ip) {
System.err.printf("準(zhǔn)備查詢【%s】詳細(xì)信息%n", ip) ;
RestClient client = RestClient.create(URI.create("https://api.vvhan.com")) ;
Map<?, ?> result = client.get()
.uri("/api/ipInfo?ip={0}", ip)
.retrieve()
.body(Map.class) ;
try {
return new ObjectMapper().writeValueAsString(result) ;
} catch (JsonProcessingException e) {
throw new RuntimeException(e) ;
}
}
}
注冊(cè)工具
@Configuration
public class ToolsConfig {
@Bean
ToolCallbackProvider tools() {
ToolCallback[] toolCallbacks = ToolCallbacks.from(new CommonTool()) ;
return ToolCallbackProvider.from(toolCallbacks) ;
}
}
如上所述,我們就成功構(gòu)建了一個(gè)僅包含兩個(gè)外部工具的MCP服務(wù)器。
啟動(dòng)服務(wù)
圖片
- 默認(rèn)開(kāi)啟了一個(gè)/see端點(diǎn)(其實(shí),還有一個(gè)消息傳輸?shù)亩它c(diǎn))
- 提示注冊(cè)了2個(gè)工具(也不知道給個(gè)空格的)
接下來(lái),我們進(jìn)行客戶端的開(kāi)發(fā)。
2.2 客戶端開(kāi)發(fā)
引入依賴
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.1</version>
</dependency>
注意,這里引入的是mcp-client包。
配置文件
spring:
ai:
mcp:
client:
enable: true
name: ai-mcp-client
initialized: true
type: ASYNC
sse:
connections:
server1:
url: http://localhost:8888
我們配置了一個(gè)MCP服務(wù)端地址,你可以配置多個(gè)按照上面的方式。
完成以上的配置后,我們就可以配置ChatClient,然后進(jìn)行接口的調(diào)用了。
@RestController
@RequestMapping("/tools")
public class ToolController {
private final ChatClient chatClient ;
public ToolController(ChatClient.Builder aiClientBuilder, ToolCallbackProvider mcpTools) {
this.chatClient = aiClientBuilder
.defaultTools(mcpTools)
.build() ;
}
@GetMapping("/weather")
public ResponseEntity<String> getCurrentWeather(String prompt) {
System.err.println(prompt) ;
String response = this.chatClient
.prompt(prompt)
.call().content() ;
return ResponseEntity.ok(response) ;
}
@GetMapping("/ip")
public ResponseEntity<String> getIpAddressInfo(String prompt) {
System.err.println(prompt) ;
String response = this.chatClient
.prompt(prompt)
.call().content() ;
return ResponseEntity.ok(response) ;
}
}
在構(gòu)造函數(shù)中,我們直接注入了ToolCallbackProvider,當(dāng)我們系統(tǒng)啟動(dòng)時(shí)會(huì)自動(dòng)的從配置的服務(wù)端進(jìn)行查找有哪些可用的工具。
啟動(dòng)服務(wù)
控制臺(tái)將輸出如下從MCP Server獲取的工具:
i.m.s.M Line:151 - Received JSON message:
{
"jsonrpc": "2.0",
"id": "66d12dae-1",
"result": {
"tools": [
{
"name": "getCurrentWeather",
"description": "獲取當(dāng)前天氣預(yù)報(bào)",
"inputSchema": {
"type": "object",
"properties": {
"request": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市"
}
},
"required": ["city"]
}
},
"required": ["request"],
"additionalProperties": false
}
},
{
"name": "getIpAddressInfo",
"description": "獲取IP地址詳細(xì)信息",
"inputSchema": {
"type": "object",
"properties": {
"ip": {
"type": "string"
}
},
"required": ["ip"],
"additionalProperties": false
}
}
]
}
}
測(cè)試結(jié)果
圖片
圖片
成功?。?!