王炸!Spring AI+MCP 三步实现智能体开发

开发 前端
Model Context Protocol(MCP)模型上下文协议是一种标准化协议,它让大模型能够更容易地和外部的数据、工具连接起来。你可以把MCP想象成一个通用的插头或者接口,就像USB-C一样,不管是什么设备,只要插上这个接口,就能和电脑、充电器等连接起来。

环境:SpringBoot3.4.2

1. 简介

1.1 什么是MCP

Model Context Protocol(MCP)模型上下文协议是一种标准化协议,它让大模型能够更容易地和外部的数据、工具连接起来。你可以把MCP想象成一个通用的插头或者接口,就像USB-C一样,不管是什么设备,只要插上这个接口,就能和电脑、充电器等连接起来。

注意,它连接的不是物理设备,而是AI模型和外部的数据源、工具等。有了MCP,AI模型就能更方便地获取外部的信息,完成更多的任务。比如,通过MCP,AI模型可以操作电脑读写文件,或者模拟浏览器操作等。

1.2 为什么需要MCP

首先,MCP提供了一个标准化的接口,使得AI模型能够轻松地与各种外部工具和数据源进行交互,无需为每个工具或数据源单独开发集成代码。

其次,MCP还解决了数据孤岛问题,通过统一协议连接分散的数据源,使AI模型能够实时访问和利用最新的数据。

总的来说,MCP就像是一个桥梁,让AI模型与外部世界更好地连接起来,从而发挥出更大的价值和潜力。

1.3 Java与MCP架构

  • 客户端/服务器层:McpClient负责处理客户端操作,而McpServer则管理服务器端协议操作。两者都利用McpSession来进行通信管理。
  • 会话层(McpSession):通过DefaultMcpSession实现来管理通信模式和状态。
  • 传输层(McpTransport):处理JSON-RPC消息的序列化和反序列化,并支持多种传输实现。
     

MCP Client

MCP客户端是模型上下文协议(MCP)架构中的关键组件,负责建立和管理与MCP服务器的连接。它实现了协议的客户端部分,如下图所示:

Java MCP Client ArchitectureJava MCP Client Architecture

MCP Server

MCP服务器是模型上下文协议(MCP)架构中的基础组件,它为客户端提供工具、资源和功能。它实现了协议的服务器端部分,如下图所示:

Java MCP Server ArchitectureJava MCP Server Architecture

Spring AI 提供了相对应的Spring Boot staters来非常方便的进行 MCP 的集成。接下来,我们将详细的完成一个完整的MCP应用案例。

2. 实战案例

2.1 服务端开发

我们将在 MCP 服务端提供2个外部功能:查询天气预报、获取IP地址详细信息。

引入依赖

<dependency>
  <groupId>com.alibaba.cloud.ai</groupId>
  <artifactId>spring-ai-alibaba-starter</artifactId>
  <version>1.0.0-M6.1</version>
</dependency>
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

说明:

  • 引入alibaba-starter我们将使用阿里的大模型
  • 引入mcp-server-webflux,以支持基于 Spring WebFlux 的 SSE(服务器发送事件)服务器传输

配置文件

spring:
  ai:
    dashscope:
      api-key: sk-xxxooo
      base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
      chat:
        options:
          stream: true
          model: qwen-turbo
---
spring:
  ai:
    mcp:
      server:
        enabled: true
        name: ai_mcp_server
        version: 1.0.0
        type: ASYNC
        sse-message-endpoint: /mcp/message
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

以上我们就完成了基本的配置。接下来,我们需要提供2个外部工具。

工具编写

获取天气预报
public class CommonTool {
  
  @Tool(description = "获取当前天气预报")
  WeatherResponse getCurrentWeather(WeatherRequest request) {
    System.err.printf("准备查询【%s】天气预报%n", request.city()) ;
    RestClient client = RestClient.create(URI.create("https://api.vvhan.com")) ;
    Map<?, ?> result = client.get()
        .uri("/api/weather?city={0}", request.city())
        .retrieve()
        .body(Map.class) ;
    try {
      return new WeatherResponse(new ObjectMapper().writeValueAsString(result)) ;
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e) ;
    }
  }
  
  @Tool(description = "获取IP地址详细信息")
  String getIpAddressInfo(String ip) {
    System.err.printf("准备查询【%s】详细信息%n", ip) ;
    RestClient client = RestClient.create(URI.create("https://api.vvhan.com")) ;
    Map<?, ?> result = client.get()
        .uri("/api/ipInfo?ip={0}", ip)
        .retrieve()
        .body(Map.class) ;
    try {
      return new ObjectMapper().writeValueAsString(result) ;
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e) ;
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
注册工具
@Configuration
public class ToolsConfig {
  @Bean
  ToolCallbackProvider tools() {
    ToolCallback[] toolCallbacks = ToolCallbacks.from(new CommonTool()) ;
    return ToolCallbackProvider.from(toolCallbacks) ;
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

如上所述,我们就成功构建了一个仅包含两个外部工具的MCP服务器。

启动服务

图片图片

  • 默认开启了一个/see端点(其实,还有一个消息传输的端点)
  • 提示注册了2个工具(也不知道给个空格的)

接下来,我们进行客户端的开发。

2.2 客户端开发

引入依赖

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-mcp-client-spring-boot-starter</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>1.0.0-M6.1</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

注意,这里引入的是mcp-client包。

配置文件

spring:
  ai:
    mcp:
      client:
        enable: true
        name: ai-mcp-client
        initialized: true
        type: ASYNC
        sse:
          connections:
            server1:
              url: http://localhost:8888
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

我们配置了一个MCP服务端地址,你可以配置多个按照上面的方式。

完成以上的配置后,我们就可以配置ChatClient,然后进行接口的调用了。

@RestController
@RequestMapping("/tools")
public class ToolController {
  private final ChatClient chatClient ;
  public ToolController(ChatClient.Builder aiClientBuilder, ToolCallbackProvider mcpTools) {
    this.chatClient = aiClientBuilder
        .defaultTools(mcpTools)
        .build() ;
  }
  
  @GetMapping("/weather")
  public ResponseEntity<String> getCurrentWeather(String prompt) {
    System.err.println(prompt) ;
    String response = this.chatClient
        .prompt(prompt)
        .call().content() ;
    return ResponseEntity.ok(response) ;
  }
  
  @GetMapping("/ip")
  public ResponseEntity<String> getIpAddressInfo(String prompt) {
    System.err.println(prompt) ;
    String response = this.chatClient
        .prompt(prompt)
        .call().content() ;
    return ResponseEntity.ok(response) ;
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

在构造函数中,我们直接注入了ToolCallbackProvider,当我们系统启动时会自动的从配置的服务端进行查找有哪些可用的工具。

启动服务

控制台将输出如下从MCP Server获取的工具:

i.m.s.M Line:151 - Received JSON message: 
{
  "jsonrpc": "2.0",
  "id": "66d12dae-1",
  "result": {
    "tools": [
      {
        "name": "getCurrentWeather",
        "description": "获取当前天气预报",
         "inputSchema": {
           "type": "object",
           "properties": {
             "request": {
               "type": "object",
               "properties": {
                 "city": {
                   "type": "string",
                   "description": "城市"
                 }
               },
               "required": ["city"]
             }
           },
           "required": ["request"],
           "additionalProperties": false
         }
       },
       {
         "name": "getIpAddressInfo",
         "description": "获取IP地址详细信息",
         "inputSchema": {
           "type": "object",
           "properties": {
             "ip": {
               "type": "string"
             }
           },
           "required": ["ip"],
           "additionalProperties": false
         }
       }
    ]
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

测试结果

图片图片

图片图片

成功!!!

责任编辑:武晓燕 来源: Springboot实战案例源码
相关推荐

2023-05-06 08:23:36

ChatGPT自然语言技术

2024-06-28 07:42:18

2013-12-06 13:00:26

TechEd2013Office 365Visual Stud

2010-04-20 20:53:35

实现网络负载均衡

2010-02-26 16:16:15

2010-08-12 10:10:37

FlexMapABC

2009-11-10 12:55:26

VB.NET三维模型

2021-03-02 07:02:45

Linux操作系统

2009-11-16 13:04:04

PHP上传文件代码

2010-05-17 09:49:19

DataCore虚拟化

2009-02-04 09:45:05

Java SocketSocket APIJava编程

2011-07-13 09:54:22

VMware故障vSphere

2020-11-04 00:00:29

Kerberos协议身份

2012-08-08 17:05:36

App运营

2015-06-09 09:25:34

2024-03-29 08:56:47

2010-05-20 11:12:13

SVN插件安装

2025-04-01 08:05:00

智能体人工智能MCP

2011-04-11 16:37:21

2021-09-04 23:27:58

Axios源码流程
点赞
收藏

51CTO技术栈公众号