最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?AI開發(fā)MCP?Server和MCP?Client的詳細(xì)過程

 更新時(shí)間:2025年06月25日 10:40:04   作者:大齡碼農(nóng)有夢想  
本文介紹基于SpringAI實(shí)現(xiàn)MCP服務(wù)端(STDIO/SSE協(xié)議)及客戶端,通過@Tool注解集成工具并調(diào)用阿里云Qwen大模型,構(gòu)建智能體與數(shù)據(jù)源的統(tǒng)一連接,適用于本地和分布式場景,提供示例代碼與測試方法,感興趣的朋友一起看看吧

本文介紹基于Spring AI如何實(shí)現(xiàn)MCP開發(fā)和調(diào)用。首先自定義了兩個(gè)MCP Server,其中:一個(gè)是算術(shù)計(jì)算器MCP Server,并通過sdtio傳輸協(xié)議發(fā)布,另一個(gè)是天氣預(yù)報(bào)MCP Server,通過sse傳輸協(xié)議發(fā)布。然后實(shí)現(xiàn)一個(gè)MCP Client,并調(diào)用阿里云qwen大模型完成整個(gè) MCP 調(diào)用流程,并給出來整個(gè)示例的Java代碼。

一、MCP是什么

模型上下文協(xié)議(即 Model Context Protocol,MCP)是一個(gè)開放協(xié)議,它規(guī)范了應(yīng)用程序如何向大型語言模型(LLM)提供上下文。MCP 提供了一種統(tǒng)一的方式將 AI 模型連接到不同的數(shù)據(jù)源和工具,它定義了統(tǒng)一的集成方式。在開發(fā)智能體(Agent)的過程中,我們經(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)建想要通過 MCP 訪問數(shù)據(jù)的生成式 AI 應(yīng)用程序。

2)Spring MCP 客戶端:MCP 協(xié)議的 Spring AI 實(shí)現(xiàn),與服務(wù)器保持 1:1 連接。

3)MCP 服務(wù)器:輕量級(jí)程序,每個(gè)程序都通過標(biāo)準(zhǔn)化的模型上下文協(xié)議公開特定的功能。

4)本地?cái)?shù)據(jù)源:MCP 服務(wù)器可以安全訪問的計(jì)算機(jī)文件、數(shù)據(jù)庫和服務(wù)。

5)遠(yuǎn)程服務(wù):MCP 服務(wù)器可以通過互聯(lián)網(wǎng)(例如,通過 API)連接到的外部系統(tǒng)。

Spring AI MCP實(shí)現(xiàn)遵循三層架構(gòu):

(1)STDIO傳輸協(xié)議:STDIO方式是基于進(jìn)程間通信,MCP Client和MCP Server運(yùn)行在同一主機(jī),主要用于本地集成、命令行工具等場景。

優(yōu)點(diǎn):

簡單可靠,無需網(wǎng)絡(luò)配置;適合本地部署場景;進(jìn)程隔離,安全性好。

缺點(diǎn):

僅支持單機(jī)部署;不支持跨網(wǎng)絡(luò)訪問;每個(gè)客戶端需要獨(dú)立啟動(dòng)服務(wù)器進(jìn)程。

2)SSE傳輸協(xié)議:SSE(Server-Sent Events)傳輸層是基于HTTP的單向通信機(jī)制,專門用于服務(wù)器向客戶端推送數(shù)據(jù)。MCP Client遠(yuǎn)程調(diào)用MCP Server提供的SSE服務(wù)。實(shí)現(xiàn)客戶端和服務(wù)端遠(yuǎn)程通信。

優(yōu)點(diǎn):

支持分布式部署;可跨網(wǎng)絡(luò)訪問;支持多客戶端連接;輕量級(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開發(fā)工具。本人使用的是IntelliJ IDEA開發(fā)工具,需要使用springboot3以上版本。

3、注冊阿里云大模型服務(wù),獲得api_key。本文采用了阿里巴巴的Qwen大模型進(jìn)行實(shí)驗(yàn)與驗(yàn)證,您也可以選擇使用DeepSeek等其它大模型作為替代方案。前提條件是該大模型要支持MCP工具回調(diào)。

三、代碼實(shí)現(xiàn)

1、開發(fā)基于sse協(xié)議的MCP Server

基于SSE的MCP服務(wù)端通過HTTP協(xié)議與客戶端通信,適用于作為獨(dú)立服務(wù)部署的場景,可以被多個(gè)客戶端遠(yuǎn)程調(diào)用。

通過Java開發(fā)工具創(chuàng)建一個(gè)springboot工程,模擬開發(fā)一個(gè)天氣預(yù)報(bào)服務(wù),通過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)開發(fā)天氣預(yù)報(bào)服務(wù)類

創(chuàng)建一個(gè)Server類,模擬天氣預(yù)報(bào)服務(wù),通過@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ù)城市名稱獲取天氣預(yù)報(bào)")
    public String getWeatherByCity(String city) {
        log.info("===============getWeatherByCity方法被調(diào)用:city="+city);
        Map<String, String> mockData = Map.of(
                "西安", "天氣炎熱",
                "北京", "晴空萬里",
                "上海", "陰雨綿綿"
        );
        return mockData.getOrDefault(city, "抱歉:未查詢到對(duì)應(yīng)城市!");
    }
}

(3)注冊為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的名稱、版本號(hào)、同步或異步。

server:
  port: 9090  # 服務(wù)器端口配置
spring:
  ai:
    mcp:
      server:
        name: spring-ai-mcp-server    # MCP服務(wù)器名稱
        version: 1.0.0                # 服務(wù)器版本號(hào)
        type: ASYNC #異步

到這里,一個(gè)通過Spring AI創(chuàng)建的MCP Server完成了,訪問http://localhost:9090/sse,能看到信息,即表示該服務(wù)通過sse發(fā)布成功了。

2、開發(fā)基于stdio協(xié)議的MCP Server

基于stdio的MCP服務(wù)端通過標(biāo)準(zhǔn)輸入輸出流與客戶端通信,適用于作為子進(jìn)程被客戶端啟動(dòng)和管理的場景,非常適合嵌入式應(yīng)用。

通過Java開發(fā)工具創(chuàng)建一個(gè)springboot工程,模擬開發(fā)一個(gè)算術(shù)計(jì)算器服務(wù),通過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)開發(fā)計(jì)算器服務(wù)類

創(chuàng)建一個(gè)Server類,模擬計(jì)算器服務(wù),通過@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)注冊為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的名稱、版本號(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è)通過Spring AI創(chuàng)建的MCP Server完成了。stdio方式是server和client通過進(jìn)程通信,所以需要把server打包成jar,以便client命令啟動(dòng)執(zhí)行。

通過maven工具打包即可,我打包后路徑為:D:\java\ai-project-web\spring-ai-mcp-stdio-server\target\spring-ai-mcp-stdio-server.jar。

測試該服務(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)成功。  

至此,我們通過spring ai框架開發(fā)完成了2個(gè)MCP Server服務(wù),一個(gè)通過sse協(xié)議發(fā)布,另一個(gè)通過stdio協(xié)議發(fā)布,接下來,開發(fā)一個(gè)MCP Client端,調(diào)用這兩個(gè)MCP Server服務(wù)。      

3、開發(fā)MCP Client調(diào)用MCP Server

Spring AI 提供了更簡便的方式來使用MCP,通過starter可以大大簡化MCP客戶端的配置和使用。Spring AI MCP支持兩種不同的傳輸層實(shí)現(xiàn):基于stdio的實(shí)現(xiàn)和基于SSE的實(shí)現(xiàn)。

通過Java開發(fā)工具,創(chuàng)建一個(gè)springboot工程,開發(fā)MCP Client。

(1)maven配置文件

引入spring-ai-mcp-client-webflux-spring-boot-starterspring-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)開發(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ì)象用于接收用戶輸入
            Scanner scanner = new Scanner(System.in);
            System.out.println(">>> 歡迎使用問答系統(tǒng)!輸入'exit'退出程序。");
            while (true) {
                // 提示用戶輸入問題
                System.out.print("\n>>> QUESTION: ");
                String userInput = scanner.nextLine();
                // 如果用戶輸入"exit",則退出循環(huán)
                if ("exit".equalsIgnoreCase(userInput)) {
                    System.out.println(">>> 已退出問答系統(tǒng)。");
                    break;
                }
                // 使用ChatClient與LLM交互
                try {
                    System.out.println("\n>>> ASSISTANT: " + chatClient.prompt(userInput).call().content());
                } catch (Exception e) {
                    System.out.println("\n>>> ERROR: 無法處理您的請(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 客戶端,調(diào)用了SSE 和 Stdio 兩種MCP服務(wù),server1采用SSE方式,連接指向http://localhost:9090,server1采用Stdio 方式,Stdio 通過 Java 命令啟動(dòng),指定 Jar 文件位置。同時(shí)集成了阿里巴巴大模型qwen-plus。讀者需要把a(bǔ)pi-key替換為自己的key進(jìn)行測試驗(yàn)證。

四、運(yùn)行測試

先啟動(dòng)MCP Server服務(wù),再運(yùn)行MCP Client,進(jìn)行AI對(duì)話,觀察日志輸出結(jié)果,確定是否理解了用戶的輸入信息,并分別調(diào)用了對(duì)應(yīng)的MCP Server服務(wù)。

觀察輸出結(jié)果:

(1)提問“你好”,大模型回答:“你好!今天天氣不錯(cuò),你想做點(diǎn)什么?如果你需要,我可以幫你查詢天氣、做加法或乘法運(yùn)算。”

說明大模型已經(jīng)發(fā)現(xiàn)了天氣預(yù)報(bào)、算術(shù)運(yùn)算兩個(gè)MCP服務(wù)。

(2)提問“今天北京天氣怎么樣”,大模型的回答調(diào)用了基于sse協(xié)議的MCP Server,可以到該服務(wù)后臺(tái)查看日志,確定是否被調(diào)用。

(3)提問“(55+45)*(1+99)等于多少”,大模型的回答調(diào)用了基于stdio協(xié)議的MCP Server,可以到mcp-server.log日志文件中查看日志,確定是否被調(diào)用。

通過驗(yàn)證結(jié)果表明大模型根據(jù)用戶的提問,選擇了合適的工具進(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)?;趕tdio的實(shí)現(xiàn)適用于嵌入式場景,而基于SSE的實(shí)現(xiàn)適用于獨(dú)立服務(wù)部署。

通過使用@Tool注解和@ToolParameter注解,可以輕松地將普通的Java方法轉(zhuǎn)換為MCP工具,使其可以被MCP客戶端發(fā)現(xiàn)和調(diào)用。Spring Boot的自動(dòng)配置機(jī)制使得MCP服務(wù)端的開發(fā)變得簡單高效。

智能體開發(fā)平臺(tái)在線體驗(yàn):http://www.yunchengxc.com

文章中示例工程源代碼:https://gitee.com/hibpm/spring-ai-mcp-demo

到此這篇關(guān)于Spring AI開發(fā)MCP Server和MCP Client的文章就介紹到這了,更多相關(guān)Spring AI MCP Server和MCP Client內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JSON在Java中的相互轉(zhuǎn)換示例詳解

    JSON在Java中的相互轉(zhuǎn)換示例詳解

    JSON (JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。這篇文章主要介紹了JSON在Java中的相互轉(zhuǎn)換,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java中FileWriter類的簡介說明

    Java中FileWriter類的簡介說明

    這篇文章主要介紹了Java中FileWriter類的簡介說明,FileWriter類提供了多種寫入字符的方法,包括寫入單個(gè)字符、寫入字符數(shù)組和寫入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下
    2023-10-10
  • 詳解Maven多模塊打包遇到的問題解決方法

    詳解Maven多模塊打包遇到的問題解決方法

    這篇文章主要介紹了詳解Maven多模塊打包遇到的問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • MyBatis-plus使用lambda條件構(gòu)造器報(bào)錯(cuò)問題及解決

    MyBatis-plus使用lambda條件構(gòu)造器報(bào)錯(cuò)問題及解決

    這篇文章主要介紹了MyBatis-plus使用lambda條件構(gòu)造器報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java NIO中的零拷貝原理

    Java NIO中的零拷貝原理

    這篇文章主要介紹了Java NIO中的零拷貝原理,零拷貝即Zero-Copy,顧名思義,零拷貝是指的一種非拷貝的方式來減少IO次數(shù)的工作方式,零拷貝的作用就是減少IO,提高IO效率,需要的朋友可以參考下
    2023-11-11
  • Java map.getOrDefault()方法的用法詳解

    Java map.getOrDefault()方法的用法詳解

    這篇文章主要介紹了Java map.getOrDefault()方法的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • MyBatis 注解操作與 XML 配置增刪改查全流程實(shí)現(xiàn)代碼

    MyBatis 注解操作與 XML 配置增刪改查全流程實(shí)現(xiàn)代碼

    本文介紹了MyBatis,它是一款輕量級(jí)的持久層框架,能簡化JDBC冗余代碼,支持自定義SQL與對(duì)象映射,并兼顧開發(fā)效率與靈活性,通過實(shí)例代碼介紹了MyBatis 注解操作與 XML 配置增刪改查全流程實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧
    2026-01-01
  • 淺談Spring學(xué)習(xí)之request,session與globalSession作用域

    淺談Spring學(xué)習(xí)之request,session與globalSession作用域

    這篇文章主要介紹了Spring學(xué)習(xí)之request,session與globalSession作用域的相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-09-09
  • Jar包沖突問題原理及解決方案

    Jar包沖突問題原理及解決方案

    這篇文章主要介紹了Jar包沖突問題原理及解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Scala中Array和List的區(qū)別說明

    Scala中Array和List的區(qū)別說明

    這篇文章主要介紹了Scala中Array和List的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論

体育| 阳城县| 建瓯市| 张家川| 远安县| 兴化市| 深圳市| 星子县| 喀喇| 綦江县| 通许县| 金湖县| 千阳县| 墨玉县| 东方市| 临湘市| 佳木斯市| 浦北县| 翁源县| 普洱| 晋州市| 禹城市| 金山区| 固原市| 固镇县| 东乌珠穆沁旗| 洞口县| 巴塘县| 桃园县| 高要市| 雷山县| 兴文县| 东明县| 五台县| 全州县| 云林县| 万全县| 通化县| 临猗县| 泽普县| 华安县|