返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

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

[复制链接]
链载Ai 显示全部楼层 发表于 1 小时前 |阅读模式 打印 上一主题 下一主题

环境: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架构

MCP Stack Architecture

  • 客户端/服务器层:McpClient负责处理客户端操作,而McpServer则管理服务器端协议操作。两者都利用McpSession来进行通信管理。

  • 会话层(McpSession):通过DefaultMcpSession实现来管理通信模式和状态。

  • 传输层(McpTransport):处理JSON-RPC消息的序列化和反序列化,并支持多种传输实现。

MCP Client


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

Java MCP Client Architecture

MCP Server


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

Java 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>

说明:

  • 引入alibaba-starter我们将使用阿里的大模型

  • 引入mcp-server-webflux,以支持基于 Spring WebFlux 的 SSE(服务器发送事件)服务器传输

配置文件

spring:ai:dashscope:api-key:sk-xxxooobase-url:https://dashscope.aliyuncs.com/compatible-mode/v1chatptions:stream:truemodel:qwen-turbo---spring:ai:mcp:server:enabled:truename:ai_mcp_serverversion:1.0.0type:ASYNCsse-message-endpoint:/mcp/message

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

工具编写

获取天气预报

publicclassCommonTool{@Tool(description="获取当前天气预报")WeatherResponsegetCurrentWeather(WeatherRequestrequest){System.err.printf("准备查询【%s】天气预报%n",request.city());RestClientclient=RestClient.create(URI.create("https://api.vvhan.com"));Map<?,?>result=client.get().uri("/api/weather?city={0}",request.city()).retrieve().body(Map.class);try{returnnewWeatherResponse(newObjectMapper().writeValueAsString(result));}catch(JsonProcessingExceptione){thrownewRuntimeException(e);}}@Tool(description="获取IP地址详细信息")StringgetIpAddressInfo(Stringip){System.err.printf("准备查询【%s】详细信息%n",ip);RestClientclient=RestClient.create(URI.create("https://api.vvhan.com"));Map<?,?>result=client.get().uri("/api/ipInfo?ip={0}",ip).retrieve().body(Map.class);try{returnnewObjectMapper().writeValueAsString(result);}catch(JsonProcessingExceptione){thrownewRuntimeException(e);}}}

注册工具

@ConfigurationpublicclassToolsConfig{@BeanToolCallbackProvidertools(){ToolCallback[]toolCallbacks=ToolCallbacks.from(newCommonTool());returnToolCallbackProvider.from(toolCallbacks);}}

如上所述,我们就成功构建了一个仅包含两个外部工具的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>

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

配置文件

spring:ai:mcp:client:enable:truename:ai-mcp-clientinitialized:truetype:ASYNCsse:connections:server1:url:http://localhost:8888

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

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

@RestController@RequestMapping("/tools")publicclassToolController{privatefinalChatClientchatClient;publicToolController(ChatClient.BuilderaiClientBuilder,ToolCallbackProvidermcpTools){this.chatClient=aiClientBuilder.defaultTools(mcpTools).build();}@GetMapping("/weather")publicResponseEntity<String>getCurrentWeather(Stringprompt){System.err.println(prompt);Stringresponse=this.chatClient.prompt(prompt).call().content();returnResponseEntity.ok(response);}@GetMapping("/ip")publicResponseEntity<String>getIpAddressInfo(Stringprompt){System.err.println(prompt);Stringresponse=this.chatClient.prompt(prompt).call().content();returnResponseEntity.ok(response);}}

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

启动服务

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

i.m.s.MLine:151-ReceivedJSONmessage:{"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}}]}}

测试结果

成功!!!

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ