Spring AI 开发 MCP Server 与 MCP Client(SSE 方式)
基于 Spring AI 框架开发支持 SSE 协议的 MCP Server 和 MCP Client 的完整流程。内容涵盖初始化 Spring Boot 工程、配置 Maven 依赖、定义工具类并通过配置类注入容器。详细说明了服务端与应用配置文件的编写及启动方法,并演示了两种连接方式:使用 Chatbox 客户端连接及开发 Java 客户端调用接口。集成智谱 AI 模型实现工具调用功能。

基于 Spring AI 框架开发支持 SSE 协议的 MCP Server 和 MCP Client 的完整流程。内容涵盖初始化 Spring Boot 工程、配置 Maven 依赖、定义工具类并通过配置类注入容器。详细说明了服务端与应用配置文件的编写及启动方法,并演示了两种连接方式:使用 Chatbox 客户端连接及开发 Java 客户端调用接口。集成智谱 AI 模型实现工具调用功能。



访问网址:https://start.spring.io/ 填写基本信息→添加依赖项。
选择 Server,点击生成,解压后用 IDEA 打开。
把 spring-ai-starter-mcp-server 依赖改为 spring-ai-starter-mcp-server-webmvc。
完整 pom.xml 内容:
<?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.5.8</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>test-mcp-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-mcp-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.1.2</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.tool;
import org.springframework.ai.tool.annotation.Tool;
public class MathTool {
@Tool(description = "两个数字相加")
public static int addNumbers(int a, int b) {
return a + b;
}
@Tool(description = "两个数字相减")
public static int subtractNumbers(int a, int b) {
return a - b;
}
}
创建 config 包,在下面创建 McpConfig 类。
package com.example.test_mcp_server.config;
import com.example.tool.MathTool;
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 McpConfig {
@Bean
public ToolCallbackProvider mathTool() {
return MethodToolCallbackProvider.builder()
.toolObjects(new MathTool())
.build();
}
}
配置内容为:
server:
port: 8080
spring:
application:
name: math_mcp_server
ai:
mcp:
server:
enabled: true
name: test_mcp_server
version: 1.0.0
sse-endpoint: /api/v1/sse
sse-message-endpoint: /api/v1/mcp
capabilities:
tool: true
logging:
level:
# 查看 MCP 详细日志
io.modelcontextprotocol: TRACE
org.springframework.ai.mcp: TRACE
启动成功,并提示注册了两个 Tool。
选择智谱的 ChatGLM6B,API 密钥去智谱官网申请,点击检查确认。
点击左侧的 MCP,添加服务器。配置信息后点击测试,编写的两个工具成功显示。
PS:必须点测试,这样才能完成服务端的初始化连接。 服务端日志会显示连接状态。点击保存。
按 ESC 键退出设置,点击新对话,可以看到刚才添加的 MCP 服务。 输入'运维小兵的计算服务有哪些功能',AI 给的回复很好的总结了工具的功能。 问:8 加 7 等于几,工具成功调用。 服务端日志内容显示调用过程,测试完毕,符合预期。
跟创建 MCP Server 类似,改下 Artifact 名字,添加 Client 的依赖。 保存工程文件,解压后用 IDEA 打开。
<properties>
<java.version>17</java.version>
<spring-ai.version>1.1.2</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--调用智谱系列大模型的依赖-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<>
org.springframework.ai
spring-ai-bom
${spring-ai.version}
pom
import
server:
port: 8081
spring:
application:
name: test-mcp-client
ai:
zhipuai:
# 智谱官网创建 API Key,配置到机器的环境变量 ZHIPU_KEY
api-key: ${ZHIPU_KEY}
base-url: "https://open.bigmodel.cn/api/paas"
chat:
options:
model: glm-4-flash
mcp:
client:
sse:
connections:
server1:
# 填写 MCP Server 的地址
url: http://localhost:8080
sse-endpoint: /api/v1/sse
ConnectMcpServer.java:
package com.example.test_mcp_client.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/mcp")
public class ConnectMcpServer {
private final ChatClient chatClient;
public ConnectMcpServer(ChatClient.Builder builder, ToolCallbackProvider toolCallbackProvider) {
this.chatClient = builder
.defaultToolCallbacks(toolCallbackProvider.getToolCallbacks())
.build();
}
@GetMapping("/test")
public String test(@RequestParam(name = "query") String query) {
return chatClient.prompt()
.system("你是一个有用的 AI 助手")
.user(query)
.call()
.content();
}
}
服务启动成功,SERVER 端完成初始化。
GET http://localhost:8081/mcp/test?query=8 加 6 等于几 查看 SERVER 日志,完美。
Spring AI 官网:https://docs.spring.io/spring-ai/reference/index.html

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online