Spring?AI開(kāi)發(fā)MCP?Server和MCP?Client的詳細(xì)過(guò)程
本文介紹基于Spring AI如何實(shí)現(xiàn)MCP開(kāi)發(fā)和調(diào)用。首先自定義了兩個(gè)MCP Server,其中:一個(gè)是算術(shù)計(jì)算器MCP Server,并通過(guò)sdtio傳輸協(xié)議發(fā)布,另一個(gè)是天氣預(yù)報(bào)MCP Server,通過(guò)sse傳輸協(xié)議發(fā)布。然后實(shí)現(xiàn)一個(gè)MCP Client,并調(diào)用阿里云qwen大模型完成整個(gè) MCP 調(diào)用流程,并給出來(lái)整個(gè)示例的Java代碼。
一、MCP是什么
模型上下文協(xié)議(即 Model Context Protocol,MCP)是一個(gè)開(kāi)放協(xié)議,它規(guī)范了應(yīng)用程序如何向大型語(yǔ)言模型(LLM)提供上下文。MCP 提供了一種統(tǒng)一的方式將 AI 模型連接到不同的數(shù)據(jù)源和工具,它定義了統(tǒng)一的集成方式。在開(kāi)發(fā)智能體(Agent)的過(guò)程中,我們經(jīng)常需要將將智能體與數(shù)據(jù)和工具集成,MCP 以標(biāo)準(zhǔn)的方式規(guī)范了智能體與數(shù)據(jù)及工具的集成方式,可以幫助您在 LLM 之上構(gòu)建智能體(Agent)和復(fù)雜的工作流。目前已經(jīng)有大量的服務(wù)接入并提供了 MCP server 實(shí)現(xiàn),當(dāng)前這個(gè)生態(tài)正在以非??斓乃俣炔粩嗟呢S富中。

Spring AI MCP 采用模塊化架構(gòu),包括以下組件:
(1)Spring AI 應(yīng)用程序:使用 Spring AI 框架構(gòu)建想要通過(guò) MCP 訪問(wèn)數(shù)據(jù)的生成式 AI 應(yīng)用程序。
(2)Spring MCP 客戶(hù)端:MCP 協(xié)議的 Spring AI 實(shí)現(xiàn),與服務(wù)器保持 1:1 連接。
(3)MCP 服務(wù)器:輕量級(jí)程序,每個(gè)程序都通過(guò)標(biāo)準(zhǔn)化的模型上下文協(xié)議公開(kāi)特定的功能。
(4)本地?cái)?shù)據(jù)源:MCP 服務(wù)器可以安全訪問(wèn)的計(jì)算機(jī)文件、數(shù)據(jù)庫(kù)和服務(wù)。
(5)遠(yuǎn)程服務(wù):MCP 服務(wù)器可以通過(guò)互聯(lián)網(wǎng)(例如,通過(guò) API)連接到的外部系統(tǒng)。
Spring AI MCP實(shí)現(xiàn)遵循三層架構(gòu):

(1)STDIO傳輸協(xié)議:STDIO方式是基于進(jìn)程間通信,MCP Client和MCP Server運(yùn)行在同一主機(jī),主要用于本地集成、命令行工具等場(chǎng)景。
優(yōu)點(diǎn):
簡(jiǎn)單可靠,無(wú)需網(wǎng)絡(luò)配置;適合本地部署場(chǎng)景;進(jìn)程隔離,安全性好。
缺點(diǎn):
僅支持單機(jī)部署;不支持跨網(wǎng)絡(luò)訪問(wèn);每個(gè)客戶(hù)端需要獨(dú)立啟動(dòng)服務(wù)器進(jìn)程。
(2)SSE傳輸協(xié)議:SSE(Server-Sent Events)傳輸層是基于HTTP的單向通信機(jī)制,專(zhuān)門(mén)用于服務(wù)器向客戶(hù)端推送數(shù)據(jù)。MCP Client遠(yuǎn)程調(diào)用MCP Server提供的SSE服務(wù)。實(shí)現(xiàn)客戶(hù)端和服務(wù)端遠(yuǎn)程通信。
優(yōu)點(diǎn):
支持分布式部署;可跨網(wǎng)絡(luò)訪問(wèn);支持多客戶(hù)端連接;輕量級(jí),使用標(biāo)準(zhǔn)HTTP協(xié)議。
缺點(diǎn):
需要額外的網(wǎng)絡(luò)配置;相比stdio實(shí)現(xiàn)略微復(fù)雜;需要考慮網(wǎng)絡(luò)安全性。
二、前提條件
執(zhí)行以下示例代碼的前提條件如下:
1、Java運(yùn)行環(huán)境。要求JDK版本17+,本人使用的jdk21版本;
2、Java開(kāi)發(fā)工具。本人使用的是IntelliJ IDEA開(kāi)發(fā)工具,需要使用springboot3以上版本。
3、注冊(cè)阿里云大模型服務(wù),獲得api_key。本文采用了阿里巴巴的Qwen大模型進(jìn)行實(shí)驗(yàn)與驗(yàn)證,您也可以選擇使用DeepSeek等其它大模型作為替代方案。前提條件是該大模型要支持MCP工具回調(diào)。
三、代碼實(shí)現(xiàn)
1、開(kāi)發(fā)基于sse協(xié)議的MCP Server
基于SSE的MCP服務(wù)端通過(guò)HTTP協(xié)議與客戶(hù)端通信,適用于作為獨(dú)立服務(wù)部署的場(chǎng)景,可以被多個(gè)客戶(hù)端遠(yuǎn)程調(diào)用。
通過(guò)Java開(kāi)發(fā)工具創(chuàng)建一個(gè)springboot工程,模擬開(kāi)發(fā)一個(gè)天氣預(yù)報(bào)服務(wù),通過(guò)sse傳輸協(xié)議發(fā)布為MCP Server。
(1)maven配置文件
引入spring-ai-mcp-server-webflux-spring-boot-starter,完整pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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.3.3</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>spring-ai-mcp-sse-server</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-ai.version>1.0.0-M6</spring-ai.version>
<spring-ai-alibaba.version>1.0.0-M5.1</spring-ai-alibaba.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<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>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>(2)開(kāi)發(fā)天氣預(yù)報(bào)服務(wù)類(lèi)
創(chuàng)建一個(gè)Server類(lèi),模擬天氣預(yù)報(bào)服務(wù),通過(guò)@Tool注解把方法標(biāo)注為MCP服務(wù)接口。
package com.yuncheng.mcp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class WeatherService {
private static final Logger log = LoggerFactory.getLogger(WeatherService.class);
@Tool(description = "根據(jù)城市名稱(chēng)獲取天氣預(yù)報(bào)")
public String getWeatherByCity(String city) {
log.info("===============getWeatherByCity方法被調(diào)用:city="+city);
Map<String, String> mockData = Map.of(
"西安", "天氣炎熱",
"北京", "晴空萬(wàn)里",
"上海", "陰雨綿綿"
);
return mockData.getOrDefault(city, "抱歉:未查詢(xún)到對(duì)應(yīng)城市!");
}
}(3)注冊(cè)為MCP工具
將 WeatherService 封裝為工具回調(diào)提供者(ToolCallbackProvider),便于被MCP Client端發(fā)現(xiàn)和調(diào)用。
package com.yuncheng.mcp;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class McpServerApplication {
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder()
.toolObjects(weatherService)
.build();
}
}(4)配置yml文件
定義MCP Server的名稱(chēng)、版本號(hào)、同步或異步。
server:
port: 9090 # 服務(wù)器端口配置
spring:
ai:
mcp:
server:
name: spring-ai-mcp-server # MCP服務(wù)器名稱(chēng)
version: 1.0.0 # 服務(wù)器版本號(hào)
type: ASYNC #異步到這里,一個(gè)通過(guò)Spring AI創(chuàng)建的MCP Server完成了,訪問(wèn)http://localhost:9090/sse,能看到信息,即表示該服務(wù)通過(guò)sse發(fā)布成功了。
2、開(kāi)發(fā)基于stdio協(xié)議的MCP Server
基于stdio的MCP服務(wù)端通過(guò)標(biāo)準(zhǔn)輸入輸出流與客戶(hù)端通信,適用于作為子進(jìn)程被客戶(hù)端啟動(dòng)和管理的場(chǎng)景,非常適合嵌入式應(yīng)用。
通過(guò)Java開(kāi)發(fā)工具創(chuàng)建一個(gè)springboot工程,模擬開(kāi)發(fā)一個(gè)算術(shù)計(jì)算器服務(wù),通過(guò)stdio傳輸協(xié)議發(fā)布為MCP Server。
(1)maven配置文件
引入spring-ai-mcp-server-webflux-spring-boot-starter
,完整pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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.3.3</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>spring-ai-mcp-stdio-server</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-ai.version>1.0.0-M6</spring-ai.version>
<spring-ai-alibaba.version>1.0.0-M5.1</spring-ai-alibaba.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<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>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>(2)開(kāi)發(fā)計(jì)算器服務(wù)類(lèi)
創(chuàng)建一個(gè)Server類(lèi),模擬計(jì)算器服務(wù),通過(guò)@Tool注解把方法標(biāo)注為MCP服務(wù)接口。
package com.yuncheng.mcp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
@Service
public class MathService {
private static final Logger log = LoggerFactory.getLogger(MathService.class);
@Tool(description = "加法方法")
public Integer add(Integer a, Integer b) {
log.info("===============add方法被調(diào)用: a={}, b={}", a, b);
return a + b;
}
@Tool(description = "乘法方法")
public Integer multiply(Integer a, Integer b) {
log.info("===============multiply方法被調(diào)用: a={}, b={}", a, b);
return a * b;
}
}(3)注冊(cè)為MCP工具
將 MathService封裝為工具回調(diào)提供者(ToolCallbackProvider),便于被MCP Client端發(fā)現(xiàn)和調(diào)用。
package com.yuncheng.mcp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class McpServerApplication {
private static final Logger log = LoggerFactory.getLogger(McpServerApplication.class);
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
log.info("===============McpServerApplication服務(wù)啟動(dòng)成功");
}
@Bean
public ToolCallbackProvider mathTools(MathService mathService) {
return MethodToolCallbackProvider.builder()
.toolObjects(mathService).build();
}
}(4)配置yml文件
定義MCP Server的名稱(chēng)、版本號(hào)。
spring:
ai:
mcp:
server:
name: spring-ai-mcp-stdio-server
version: 1.0.0(5)配置log日志
在resources目錄下,新建一個(gè)log日志配置文件logback-spring.xml,目的是記錄MCP Server端的接口是否被調(diào)用。
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>mcp-server.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>到這里,一個(gè)通過(guò)Spring AI創(chuàng)建的MCP Server完成了。stdio方式是server和client通過(guò)進(jìn)程通信,所以需要把server打包成jar,以便client命令啟動(dòng)執(zhí)行。
通過(guò)maven工具打包即可,我打包后路徑為:D:\java\ai-project-web\spring-ai-mcp-stdio-server\target\spring-ai-mcp-stdio-server.jar。
測(cè)試該服務(wù)是否發(fā)布成功,在cmd命令行窗口里輸入如下命令:
java -Dspring.ai.mcp.server.stdio=true -Dspring.main.web-application-type=none -Dspring.main.banner-mode=off -jar D:/java/ai-project-web/spring-ai-mcp-stdio-server/target/spring-ai-mcp-stdio-server.jar
驗(yàn)證服務(wù)是否啟動(dòng)成功。
至此,我們通過(guò)spring ai框架開(kāi)發(fā)完成了2個(gè)MCP Server服務(wù),一個(gè)通過(guò)sse協(xié)議發(fā)布,另一個(gè)通過(guò)stdio協(xié)議發(fā)布,接下來(lái),開(kāi)發(fā)一個(gè)MCP Client端,調(diào)用這兩個(gè)MCP Server服務(wù)。
3、開(kāi)發(fā)MCP Client調(diào)用MCP Server
Spring AI 提供了更簡(jiǎn)便的方式來(lái)使用MCP,通過(guò)starter可以大大簡(jiǎn)化MCP客戶(hù)端的配置和使用。Spring AI MCP支持兩種不同的傳輸層實(shí)現(xiàn):基于stdio的實(shí)現(xiàn)和基于SSE的實(shí)現(xiàn)。
通過(guò)Java開(kāi)發(fā)工具,創(chuàng)建一個(gè)springboot工程,開(kāi)發(fā)MCP Client。
(1)maven配置文件
引入spring-ai-mcp-client-webflux-spring-boot-starter和spring-ai-alibaba-starter,完整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 http://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.3.3</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>spring-ai-mcp-client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-ai.version>1.0.0-M6</spring-ai.version>
<spring-ai-alibaba.version>1.0.0-M6.1</spring-ai-alibaba.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>${spring-ai-alibaba.version}</version>
</dependency>
</dependencies>
<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>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>(2)開(kāi)發(fā)MCP Client
package com.yuncheng.mcp;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.ai.tool.ToolCallbackProvider;
import java.util.Scanner;
@SpringBootApplication
public class McpClientApplication {
public static void main(String[] args) {
SpringApplication.run(McpClientApplication.class, args);
}
@Bean
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder,
ToolCallbackProvider tools,
ConfigurableApplicationContext context) {
return args -> {
// 構(gòu)建ChatClient并注入MCP工具
var chatClient = chatClientBuilder
.defaultTools(tools)
.build();
// 創(chuàng)建Scanner對(duì)象用于接收用戶(hù)輸入
Scanner scanner = new Scanner(System.in);
System.out.println(">>> 歡迎使用問(wèn)答系統(tǒng)!輸入'exit'退出程序。");
while (true) {
// 提示用戶(hù)輸入問(wèn)題
System.out.print("\n>>> QUESTION: ");
String userInput = scanner.nextLine();
// 如果用戶(hù)輸入"exit",則退出循環(huán)
if ("exit".equalsIgnoreCase(userInput)) {
System.out.println(">>> 已退出問(wèn)答系統(tǒng)。");
break;
}
// 使用ChatClient與LLM交互
try {
System.out.println("\n>>> ASSISTANT: " + chatClient.prompt(userInput).call().content());
} catch (Exception e) {
System.out.println("\n>>> ERROR: 無(wú)法處理您的請(qǐng)求,請(qǐng)稍后再試。");
e.printStackTrace();
}
}
// 關(guān)閉Spring上下文
context.close();
scanner.close();
};
}
}(3)配置yml文件
server:
port: 8080
spring:
ai:
mcp:
client:
type: ASYNC
sse:
connections:
server1:
url: http://localhost:9090
stdio:
connections:
server2:
command: java
args:
- -Dspring.ai.mcp.server.stdio=true
- -Dspring.main.web-application-type=none
- -Dspring.main.banner-mode=off
- -jar
- D:/java/ai-project-web/spring-ai-mcp-stdio-server/target/spring-ai-mcp-stdio-server.jar
dashscope:
api-key: sk-b90ad31bb3eb4a158524928354f31dc5
chat:
options:
model: qwen-plus本配置文件定義了一個(gè)基于 Spring AI 的異步 MCP 客戶(hù)端,調(diào)用了SSE 和 Stdio 兩種MCP服務(wù),server1采用SSE方式,連接指向http://localhost:9090,server1采用Stdio 方式,Stdio 通過(guò) Java 命令啟動(dòng),指定 Jar 文件位置。同時(shí)集成了阿里巴巴大模型qwen-plus。讀者需要把a(bǔ)pi-key替換為自己的key進(jìn)行測(cè)試驗(yàn)證。
四、運(yùn)行測(cè)試
先啟動(dòng)MCP Server服務(wù),再運(yùn)行MCP Client,進(jìn)行AI對(duì)話,觀察日志輸出結(jié)果,確定是否理解了用戶(hù)的輸入信息,并分別調(diào)用了對(duì)應(yīng)的MCP Server服務(wù)。

觀察輸出結(jié)果:
(1)提問(wèn)“你好”,大模型回答:“你好!今天天氣不錯(cuò),你想做點(diǎn)什么?如果你需要,我可以幫你查詢(xún)天氣、做加法或乘法運(yùn)算。”
說(shuō)明大模型已經(jīng)發(fā)現(xiàn)了天氣預(yù)報(bào)、算術(shù)運(yùn)算兩個(gè)MCP服務(wù)。
(2)提問(wèn)“今天北京天氣怎么樣”,大模型的回答調(diào)用了基于sse協(xié)議的MCP Server,可以到該服務(wù)后臺(tái)查看日志,確定是否被調(diào)用。
(3)提問(wèn)“(55+45)*(1+99)等于多少”,大模型的回答調(diào)用了基于stdio協(xié)議的MCP Server,可以到mcp-server.log日志文件中查看日志,確定是否被調(diào)用。
通過(guò)驗(yàn)證結(jié)果表明:大模型根據(jù)用戶(hù)的提問(wèn),選擇了合適的工具進(jìn)行回答,分別調(diào)用了對(duì)應(yīng)的MCP Server服務(wù)。
那么,AI大模型是如何實(shí)現(xiàn)Tool Call的呢?以下是Spring AI實(shí)現(xiàn)Tool Call的原理圖:

在最近發(fā)布的 Spring AI 1.0.0.M6 版本中,其中一個(gè)重大變化是 Function Calling 被廢棄,被 Tool Calling 取代。
五、總結(jié)
Spring AI MCP Server Starter提供了兩種實(shí)現(xiàn)MCP服務(wù)端的方式:基于stdio的實(shí)現(xiàn)和基于SSE的實(shí)現(xiàn)。基于stdio的實(shí)現(xiàn)適用于嵌入式場(chǎng)景,而基于SSE的實(shí)現(xiàn)適用于獨(dú)立服務(wù)部署。
通過(guò)使用@Tool注解和@ToolParameter注解,可以輕松地將普通的Java方法轉(zhuǎn)換為MCP工具,使其可以被MCP客戶(hù)端發(fā)現(xiàn)和調(diào)用。Spring Boot的自動(dòng)配置機(jī)制使得MCP服務(wù)端的開(kāi)發(fā)變得簡(jiǎn)單高效。
智能體開(kāi)發(fā)平臺(tái)在線體驗(yàn):http://www.yunchengxc.com
文章中示例工程源代碼:https://gitee.com/hibpm/spring-ai-mcp-demo
到此這篇關(guān)于Spring AI開(kāi)發(fā)MCP Server和MCP Client的文章就介紹到這了,更多相關(guān)Spring AI MCP Server和MCP Client內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)類(lèi)型轉(zhuǎn)換的示例詳解
Java程序中要求參與的計(jì)算的數(shù)據(jù),必須要保證數(shù)據(jù)類(lèi)型的一致性,如果數(shù)據(jù)類(lèi)型不一致將發(fā)生類(lèi)型的轉(zhuǎn)換。本文將通過(guò)示例詳細(xì)說(shuō)說(shuō)Java中數(shù)據(jù)類(lèi)型的轉(zhuǎn)換,感興趣的可以了解一下2022-10-10
Java語(yǔ)言基于無(wú)向有權(quán)圖實(shí)現(xiàn)克魯斯卡爾算法代碼示例
這篇文章主要介紹了Java語(yǔ)言基于無(wú)向有權(quán)圖實(shí)現(xiàn)克魯斯卡爾算法代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Java多線程之readwritelock讀寫(xiě)分離的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java多線程之readwritelock讀寫(xiě)分離的相關(guān)內(nèi)容,文中涉及具體實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
SpringBoot熔斷機(jī)制之CircuitBreaker詳解
這篇文章主要介紹了SpringBoot熔斷機(jī)制之CircuitBreaker詳解,SpringBoot的熔斷機(jī)制在微服務(wù)架構(gòu)中扮演著重要角色,其中CircuitBreaker是其核心機(jī)制之一,用于防止服務(wù)的異常狀態(tài)影響到整個(gè)系統(tǒng)的運(yùn)作,需要的朋友可以參考下2023-10-10
微信公眾號(hào)獲取access_token的方法實(shí)例分析
這篇文章主要介紹了微信公眾號(hào)獲取access_token的方法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)微信公眾號(hào)獲取access_token的相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2019-10-10
java如何根據(jù)HttpServletRequest獲取IP地址
文章介紹了幾種代理服務(wù)器轉(zhuǎn)發(fā)服務(wù)請(qǐng)求頭的方法,這些請(qǐng)求頭可能包含真實(shí)IP地址,但并不是所有的代理都會(huì)包括這些請(qǐng)求頭,而且這些IP地址可能被偽造2025-03-03

