04 - 开发 MCP Client 简易示例
约 841 字大约 3 分钟
2025-04-10
目标 开发一个 MCP Client,实现模型对话时,调用MCP Server,自动编写Sql 查询指定MySQL数据库
1、新建Spring-Boot 工程
1.1、使用start.spring.io 创建
关键信息如下: Maven构建、JDK21、SpringBoot3.4.4
<groupId>cn.spads</groupId>
<artifactId>ai-mcp-client-sse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ai-mcp-client-sse</name>
<description>HTTP with SSE 版MCP Client实现</description>
1.2 生成后下载解压并使用Idea打开工程
略
1.3 修改pom,新增依赖管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.4 修改pom,新增仓库配置
<repositories>
<repository>
<name>Central Portal Snapshots</name>
<id>central-portal-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
1.4 修改pom,新增依赖
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MCP Client -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
1.5 修改后完整pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.spads</groupId>
<artifactId>ai-mcp-client-sse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ai-mcp-client-sse</name>
<description>HTTP with SSE 版MCP Client实现</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<name>Central Portal Snapshots</name>
<id>central-portal-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>
2 开发服务和配置
2.1、配置
application.yaml
配置服务端口 9091, 对话模型使用硅基流动的Qwen2.5-72B-Instruct, MCPserver 配置 http://localhost:9090
spring:
ai:
name: ai-mcp-client-sse
openai:
api-key: sk-ggcvpuywyptvqnamrkroxzuooodgfwmumocfggolsXXXXXX
base-url: https://api.siliconflow.cn
chat:
options:
model: Qwen/Qwen2.5-72B-Instruct
mcp:
client:
sse:
connections:
server1:
url: http://localhost:9090
toolcallback:
enabled: true
server:
port: 9091
2.2 启动类配置默认对话
cn.spads.ai.mcp.client.sse.AiMcpClientSseApplication
package cn.spads.ai.mcp.client.sse;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AiMcpClientSseApplication implements CommandLineRunner {
@Resource
private ToolCallbackProvider tools;
@Resource
ChatClient.Builder chatClientBuilder;
public static void main(String[] args) {
SpringApplication.run(AiMcpClientSseApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
var chatClient = chatClientBuilder
.defaultTools(tools)
.build();
String content = chatClient.prompt("查询数据库daily,看下总共有多少张表").call().content();
System.out.println(content);
String content1 = chatClient.prompt("查询数据库daily,看下sys_log表中有多少条记录").call().content();
System.out.println(content1);
}
}
启动服务验证
2025-04-10T09:33:33.849+08:00 INFO 21384 --- [ient-1-Worker-0] i.m.client.McpAsyncClient : Server response with Protocol: 2024-11-05, Capabilities: ServerCapabilities[experimental=null, logging=LoggingCapabilities[], prompts=null, resources=null, tools=ToolCapabilities[listChanged=true]], Info: Implementation[name=ai-mcp-server-sse, version=1.0.0] and Instructions null
2025-04-10T09:33:34.231+08:00 INFO 21384 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 9091 (http) with context path '/'
2025-04-10T09:33:34.237+08:00 INFO 21384 --- [ main] c.s.a.m.c.sse.AiMcpClientSseApplication : Started AiMcpClientSseApplication in 1.994 seconds (process running for 2.48)
数据库 `daily` 中总共有 80 张表。
`sys_log`表中共有91,645条记录。