用模型上下文協(xié)議(MCP)和 Spring AI 構(gòu)建智能應(yīng)用
1. MCP 架構(gòu)介紹
MCP(模型上下文協(xié)議)標(biāo)準(zhǔn)化了人工智能應(yīng)用與外部數(shù)據(jù)源之間的交互,使得像數(shù)據(jù)庫、API 和搜索引擎等工具能夠無縫集成。其客戶端 -
服務(wù)器架構(gòu)包括:
MCP 主機(jī):用戶與之交互的人工智能應(yīng)用層(例如 Claude 聊天機(jī)器人)。
-
MCP 客戶端:處理主機(jī)與服務(wù)器之間的通信,將請求格式化為外部系統(tǒng)可識別的格式。
client.png -
MCP 服務(wù)器:連接到外部資源(例如 PostgreSQL、Google Drive)并執(zhí)行操作的中間件。
server.png
2. 安裝 MySQL MCP 服務(wù)器
**第一步:您可以查看這個 GitHub 倉庫:
GitHub 手動安裝
pip install mysql-mcp-server
第二步:由于我們將使用 uv 工具,因此需要安裝它
請參考這篇文章:UV Installation
安裝 uv 工具
3. 使用 Spring AI 進(jìn)行項(xiàng)目設(shè)置
第一步:添加依賴項(xiàng)
在 build.gradle 中包含 Spring AI MCP 庫:
implementation 'org.springframework.ai:spring-ai-mcp-client-spring-boot-starter'
implementation 'org.springframework.ai:spring-ai-mcp-client-webflux-spring-boot-starter' // 用于 SSE 傳輸
4. 客戶端集成
第一步:在 application.yml 中配置 Spring AI 配置
spring:
ai:
mcp:
client:
enabled: true
name: mysqlMCP # MCP 服務(wù)器名稱
version: 1.0.0
type: SYNC
request-timeout: 20s
stdio:
root-change-notification: true
servers-configuration: classpath:mcp-servers-config.json # 與 Claude 桌面配置相同的 MCP 服務(wù)器配置。
第二步:添加 mcp-servers-config.json
{
"mcpServers": {
"mysql": {
"command": "C:\\Users\\xxx\\.local\\bin\\uv.exe",
"args": [
"--directory",
"C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\mysql_mcp_server",
"run",
"mysql_mcp_server"
],
"env": {
"MYSQL_HOST": "localhost",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "root",
"MYSQL_DATABASE": "test"
}
}
}
}
需要檢查 uv.exe 和 mysql_mcp_server 的目錄,并檢查所有 MySQL 配置。
5. 簡單示例
示例將使用 MCP 與 MySQL 數(shù)據(jù)庫進(jìn)行交互。
@SpringBootApplication(scanBasePackages = "org.openwes")
@EnableDiscoveryClient
public class AiApplication {
public static void main(String[] args) {
SpringApplication.run(AiApplication.class, args);
}
private String userInput = "show all tables";
@Bean
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools,
ConfigurableApplicationContext context) {
return args -> {
var chatClient = chatClientBuilder
.defaultTools(tools)
.build();
System.out.println(">>> 問題: " + userInput);
System.out.println(">>> 回答: " + chatClient.prompt(userInput).call().content());
context.close();
};
}
}
然后我們會看到日志顯示它將自然語言“show all tables”轉(zhuǎn)換為 SQL“show all tables”:
received: 2025-03-27 09:21:19,799 - mysql_mcp_server - INFO - Listing tools...
>>> 問題:show all tables
received: 2025-03-27 09:21:20,602 - mysql_mcp_server - INFO - Calling tool: execute_sql with arguments: {'query': 'show all tables'}
>>> 回答:以下是在 MySQL 服務(wù)器上執(zhí)行 `SHOW TABLES` 命令后返回的所有表名:
- a_api
- a_api_config
- a_api_key
- a_api_log
- d_domain_event
- e_container_task
- e_container_task_and_business_task_relation
- e_ems_location_config
- l_change_log
- l_change_log_lock
...
6. Spring AI MCP 的優(yōu)勢
- 聲明式工具注冊:通過注解而非手動 SDK 配置簡化集成。
- 統(tǒng)一協(xié)議:通過標(biāo)準(zhǔn)化的 MCP 通信消除數(shù)據(jù)源碎片化。
- 可擴(kuò)展性:添加新工具(例如 Meilisearch、Git)而不會干擾現(xiàn)有工作流。
7. 結(jié)論
通過結(jié)合 Spring AI 的依賴管理與 MCP 的協(xié)議標(biāo)準(zhǔn)化,開發(fā)人員可以快速構(gòu)建企業(yè)級人工智能應(yīng)用。對于高級用例,可以探索混合架構(gòu),其中
MCP 服務(wù)器同時處理實(shí)時數(shù)據(jù)和批處理。
本文綜合了最新的 MCP 進(jìn)展與 Spring AI 的相關(guān)內(nèi)容。完整的代碼示例請參考鏈接中的資源。
代碼可在 GitHub 上找到:GitHub - jingsewu/open-wes

