作者简介:你好,我是影子,Spring Ai Alibaba开源社区 Committer,持续分享Spring Ai Alibaba最新进展 + 业界各类AI工程相关的方案

  • 最近有断时间没更了,熟悉我的朋友知道我刚结束完毕业旅行,最近也因为入职,参与公司为期一周的文化相关的培训(培训的东西真是太赞了,每一条都直击高效生活的本质,受益非浅,主动积极、双赢思维,个人成长->公众成长等等),实在抽不出时间来写文章了,这不趁着周末赶快写点东西

本文是官网更详细的上手例子:Spring AI Alibaba MCP Gateway 正式发布,零代码实现存量应用转换 MCP 工具!

Gateway 实现存量应用转 MCP 工具

[!TIP]
Spring AI Alibaba MCP Gateway 基于 Nacos 提供的 MCP server registry 实现,为普通应用建立一个中间代理层 Java MCP 应用。一方面将 Nacos 中注册的服务信息转换成 MCP 协议的服务器信息,以便 MCP 客户端可以无缝调用这些服务;另一方面可以实现协议转化,将 MCP 协议转换为对后端 HTTP、Dubbo 等服务的调用。基于 Spring AI Alibaba MCP Gateway,您无需对原有业务代码进行改造,新增或者删除 MCP 服务(在 Nacos 中)无需重启代理应用

实战代码可见:https://github.com/GTyingzi/spring-ai-tutorial 下的 other 目录下的 nacos-restful 模块、mcp 目录下的 server 目录下的 mcp-gateway 模块

restful 服务

pom 文件
<properties><nacos.version>3.0.2</nacos.version>
</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>${nacos.version}</version><scope>compile</scope></dependency>
</dependencies>
application.yml
server:port: ${SERVERPORT:18081}  # 默认端口为18081,可以通过命令行参数SERVERPORT覆盖spring:application:name: nacos-restfultcloud:# nacos注册中心配置nacos:discovery:username: nacospassword: nacosserver-addr: 127.0.0.1:8848namespace: 4ad3108b-4d44-43d0-9634-3c1ac4850c8c # nacos3.*版本
config
package com.spring.ai.tutorial.other.config;import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;/*** @author yingzi* @since 2025/7/7*/
@Configuration
public class NacosConfig {private static final Logger logger = LoggerFactory.getLogger(NacosConfig.class);@Value("${spring.application.name}")private String serviceName;@Value("${spring.cloud.nacos.discovery.server-addr}")private String serverAddr;@Value("${spring.cloud.nacos.discovery.namespace}")private String namespace;@Value("${spring.cloud.nacos.discovery.username}")private String username;@Value("${spring.cloud.nacos.discovery.password}")private String password;@Value("${server.port}")private int port;@Beanpublic NamingService namingService() throws NacosException, UnknownHostException {Properties properties = new Properties();properties.put(PropertyKeyConst.NAMESPACE, Objects.toString(this.namespace, ""));properties.put(PropertyKeyConst.SERVERADDR, Objects.toString(this.serverAddr, ""));properties.put(PropertyKeyConst.USERNAME, Objects.toString(this.username, ""));properties.put(PropertyKeyConst.PASSWORD, Objects.toString(this.password, ""));NamingService namingService = NamingFactory.createNamingService(properties);init(namingService);return namingService;}private void init(NamingService namingService) throws NacosException, UnknownHostException {Instance instance = new Instance();// 自动获取本机IPinstance.setIp(InetAddress.getLocalHost().getHostAddress());instance.setPort(port);instance.setMetadata(Map.of("register.timestamp", String.valueOf(System.currentTimeMillis())));logger.info("注册实例: {}:{}", instance.getIp(), port);namingService.registerInstance(serviceName, instance);}}
Controller
package com.spring.ai.tutorial.other.controller;import com.spring.ai.tutorial.other.utils.ZoneUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;/*** @author yingzi* @date 2025/4/6:12:56*/
@RestController
@RequestMapping("/time")
public class TimeController {private static final Logger logger = LoggerFactory.getLogger(TimeController.class);/*** 获取指定时区的时间*/@GetMapping("/city")public String getCiteTimeMethod(@RequestParam("timeZoneId") String timeZoneId) {logger.info("The current time zone is {}", timeZoneId);return String.format("The current time zone is %s and the current time is " + "%s", timeZoneId,ZoneUtils.getTimeByZoneId(timeZoneId));}
}
package com.spring.ai.tutorial.other.utils;import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;/*** @author yingzi* @date 2025/3/25:14:59*/
public class ZoneUtils {public static String getTimeByZoneId(String zoneId) {// Get the time zone using ZoneIdZoneId zid = ZoneId.of(zoneId);// Get the current time in this time zoneZonedDateTime zonedDateTime = ZonedDateTime.now(zid);// Defining a formatterDateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");// Format ZonedDateTime as a stringString formattedDateTime = zonedDateTime.format(formatter);return formattedDateTime;}public static void main(String[] args) {System.out.println(getTimeByZoneId("Asia/Shanghai"));}}
效果:注册至 Nacos 中

nacos 配置

Mcp server 连接 restful 配置

在 MCP 管理-MCP 列表处创建 MCP Server

  • 填写命名空间:4ad3108b-4d44-43d0-9634-3c1ac4850c8c

,不填写默认为 Default

  • MCP 服务名:mcp-nacos-restful
  • 协议类型:这里选择 sse
  • HTTP 转 MCP 服务:这里选择 http
  • 后端服务:选择已有服务(因为我们的后端服务已经注册在 Nacos 中了,故直接使用即可,也可以通过新建服务指定 ip+port 的形式)
  • 描述:该项服务的说明
  • 服务版本:指定版本

新增 Tool 配置

Tool 名称:getCiteTimeMethod

Tool 描述:获取指定时区的时间

启用:True

Tool 入参描述:

  • timeZoneId;string;time zone id, such as Asia/Shanghai

协议转换配置(注意,这里输出的转换逻辑具体可看 com.alibaba.cloud.ai.mcp.nacos.gateway.jsontemplate 包下的 ResponseTemplateParser 类)

{
"requestTemplate":{"method":"GET","url":"/time/city","argsToUrlParam":true
},
"responseTemplate":{"body":"{{.}}"
}
}

发布后,可到配置管理处看到相关配置信息

gateway 转接

pom 文件
<properties><!-- Spring AI Alibaba --><spring-ai-alibaba.version>1.0.0.3-SNAPSHOT</spring-ai-alibaba.version>
</properties><dependencies><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-nacos-mcp-gateway</artifactId><version>${spring-ai-alibaba.version}</version></dependency><!-- MCP Server WebFlux 支持(也可换成 WebMvc--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server-webflux</artifactId></dependency></dependencies>
application.yml
server:port: 19000spring:application:name: mcp-gateway-serverai:mcp:server:name: mcp-gateway-serverversion: 1.0.0alibaba:mcp:nacos:namespace: 4ad3108b-4d44-43d0-9634-3c1ac4850c8cserver-addr: 127.0.0.1:8848username: nacospassword: nacosgateway:enabled: trueservice-names: mcp-nacos-restful # 和nacos配置的mcp server保持一致# 调试日志
logging:level:io:modelcontextprotocol:client: DEBUGspec: DEBUGserver: DEBUG

验证

启动对应的两个模块

  • nacos-restful 模块
  • mcp-gateway 模块

在利用 MCP Inspector 进行测试

参考资料

Spring AI Alibaba MCP Gateway 正式发布,零代码实现存量应用转换 MCP 工具!

往期文章解读

第一章内容

SpringAI(GA)的chat:快速上手+自动注入源码解读

SpringAI(GA):ChatClient调用链路解读

第二章内容

SpringAI的Advisor:快速上手+源码解读

SpringAI(GA):Sqlite、Mysql、Redis消息存储快速上手

第三章内容

SpringAI(GA):Tool工具整合—快速上手

SpringAI(GA):Tool源码+工具触发链路解读

第四章内容

SpringAI(GA):结构化输出的快速上手+源码解读

第五章内容

SpringAI(GA):内存、Redis、ES的向量数据库存储—快速上手

SpringAI(GA):向量数据库理论源码解读+Redis、Es接入源码

第六章内容

SpringAI(GA):RAG快速上手+模块化解读

SpringAI(GA):RAG下的ETL快速上手

SpringAI(GA):RAG下的ETL源码解读

第七章内容

SpringAI(GA):Nacos2下的分布式MCP

SpringAI(GA):Nacos3下的分布式MCP

SpringAI(GA):MCP源码解读

SpringAI(GA): SpringAI下的MCP源码解读

进阶:MCP服务鉴权案例

第八章内容

SpringAI(GA): 多模型评估篇

第九章内容

SpringAI(GA):观测篇快速上手+源码解读

第十章内容

Spring AI Alibaba Graph:快速入门

Spring AI Alibaba Graph:多节点并行—快速上手

Spring AI Alibaba Graph:节点流式透传案例

Spring AI Alibaba Graph:分配MCP到指定节点

Spring AI Alibaba Graph:中断!人类反馈介入,流程丝滑走完~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/88771.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/88771.shtml
英文地址,请注明出处:http://en.pswp.cn/bicheng/88771.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTTP和HTTPS部分知识点

HTTP基本概念 超文本-传输-协议 协议 HTTP是一个用在计算机世界里的协议。它使用计算机可以理解的语言确立了一种计算机之间交流通信的规范(两个以上的参与者)&#xff0c;以及相关的各种控制和错误处理方式(行为约定和规范)。传输 HTTP协议是一个双向协议。是一个在计算机世界…

第10讲——一元函数积分学的几何应用

文章目录定积分计算平面图形的面积直角坐标系下参数方程下极坐标系下定积分计算旋转体的体积曲边梯形绕x轴旋转一周所得到的旋转体的体积曲边梯形绕y轴旋转一周所得到的旋转体的体积平面曲线绕定直线旋转定积分计算函数的平均值定积分计算平面光滑曲线的弧长曲线L绕x轴旋转一周…

Go从入门到精通(20)-一个简单web项目-服务搭建

Go从入门到精通&#xff08;15&#xff09;-包&#xff08;package&#xff09; Go从入门到精通&#xff08;9&#xff09;-函数 文章目录Go从入门到精通&#xff08;15&#xff09;-包&#xff08;package&#xff09;Go从入门到精通&#xff08;9&#xff09;-函数前言gogin…

Python爬虫实战:研究python-docx库相关技术

1. 引言 1.1 研究背景与意义 随着学术资源数字化程度的提高,科研工作者面临海量文献数据的筛选与分析挑战。传统人工调研方式效率低下,难以全面捕捉研究领域的动态趋势。自动化文献分析系统能够通过爬虫技术快速采集多源数据,并通过文本挖掘提取关键信息,为研究方向选择、…

Django中序列化与反序列化

1&#xff1a;序列化&#xff1a;将数据结构或对象状态转换为可以存储或传输的格式&#xff08;如JSON、XML&#xff09;的过程。在Web开发中&#xff0c;通常是将模型实例&#xff08;或查询集&#xff09;转换为JSON格式&#xff0c;以便通过HTTP响应发送给客户端。序列化&am…

【离线数仓项目】——电商域DWD层开发实战

摘要本文主要介绍了离线数仓项目中电商域DWD层的开发实战。DWD层是数据仓库架构中的明细数据层&#xff0c;对ODS层的原始数据进行清洗、规范、整合与业务建模。它具有数据清洗、标准化、业务建模、整合、维度挂载等作用&#xff0c;常见设计特征包括一致性、明细级建模、保留历…

爬虫-正则使用

1.模块选择用re模块导入&#xff0c;&#xff0c;最前面加个r&#xff0c;就不用怕转义了2.模块使用re.findall使用结果是数组方式呈现re.finditer把结果变成迭代器&#xff0c;从迭代器类中间取数re.searchre.search 只能匹配到第一个识别到的内容re.match3.推荐写法先预加载完…

python-range函数

文章目录基本用法重要特性与列表转换注意事项遍历回去列表的元素索引range()是Python中用于生成数字序列的内置函数&#xff0c;常用于循环和序列生成。基本用法 range(stop) # 生成0到stop-1的整数序列 range(start, stop) # 生成start到stop-1的整数序列 r…

汽车功能安全-软件集成和验证(Software Integration Verification)【目的、验证输入、集成验证要求】9

文章目录1 目的2 验证输入3 软件集成要求3.1 要求和建议3.2 汽车行业示例&#xff08;混合动力控制器软件&#xff09;4 验证要求1 目的 软件集成和验证阶段的核心目标是证明集成后的软件单元&#xff08;模块、组件&#xff09;已经正确地开发出来&#xff0c;满足了所有的功…

每天一个前端小知识 Day 27 - WebGL / WebGPU 数据可视化引擎设计与实践

WebGL / WebGPU 数据可视化引擎设计与实践&#x1f3af; 一、为什么前端需要 WebGL / WebGPU&#xff1f; 传统的图表库如 ECharts、Highcharts 基于 Canvas 或 SVG&#xff0c;适合 2D 渲染&#xff0c;但&#xff1a; 当数据量 > 1 万时&#xff0c;SVG 性能瓶颈明显&…

JavaScript代码段注入:动态抓取DOM元素的原理与实践

1.F12打开网页说明&#xff1a;以百度网站为例。通过插入代码块抓取当前网页dom元素。2.新代码段说明&#xff1a;点击源代码/来源菜单项下面的代码段。点击新代码段新增代码段。下面以脚本代码段#6为例。3.编写代码块说明&#xff1a;编写javascript代码&#xff0c;点击下面的…

Spring Easy

Spring Easy 用途 通过自动配置&#xff0c;实现了一些国内 Spring Boot 开发时需要在 Spring Boot 框架基础上完成的一些配置工作&#xff0c;可以提升基于 Spring Boot 开发 Web 应用的效率。 安装 使用 Maven 进行包管理&#xff0c;可以从中央仓库安装依赖&#xff1a;…

【Node.js】文本与 pdf 的相互转换

pdf 转文本 主要使用 pdf-parse 这个库&#xff0c;直接识别提取我们 pdf 文件中的文字。 const express require("express"); const fs require("fs"); const PDFParser require("pdf-parse"); const cors require("cors");const…

分布式ID方案

目录 &#x1f4ca; 分布式ID方案核心指标对比 &#x1f50d; 分方案深度解析 ⚙️ 1. UUID (Universally Unique Identifier) ❄️ 2. Snowflake (Twitter开源) ☘️ 3. 美团Leaf 号段模式 Snowflake模式 &#x1f504; 4. 百度UidGenerator &#x1f680; 5. CosId …

张量类型转换

一.前言本章节我们来讲解张量的类型转换&#xff0c;掌握张量的转换方法&#xff0c;张量的类型转换也是经常使⽤的⼀种操作&#xff0c;是必须掌握的知识点。在本⼩节&#xff0c;我们主要学习如何将 numpy 数组和 PyTorch Tensor 的转化⽅法.二.张量转换为 numpy 数组使⽤ Te…

JavaEE-初阶-多线程初阶

概念第一个多线程程序 可以通过查看jdk路径来找到jdk的控制可以通过jconsole来查看线程。创建线程这是实现多线程的其中一种方法&#xff0c;继承Thread类&#xff0c;实现run方法&#xff0c;之后实例化继承了Thread类的MyThread方法&#xff0c;调用start方法&#xff0c;就会…

解释全连接层的“参数数量”和“计算过程”,保证像看动画片一样直观~

假设场景输入图像&#xff1a;一张极小的 灰度图&#xff08;即 H2,W2&#xff0c;共4个像素&#xff09;&#xff0c;像素值如图所示&#xff1a;隐藏层&#xff1a;假设隐藏层也是 &#xff08;即 H2,W2&#xff0c;共4个神经元&#xff09;&#xff0c;每个神经元用 ( 表示…

DOM编程实例(不重要,可忽略)

文章目录 简介 表格增加删除&#xff0c;效果如下图 样式属性案例 简介 DOM---表格添加删除&#xff0c;样式属性案例 表格增加删除&#xff0c;效果如下图 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><met…

​Windows API 介绍及核心函数分类表

Windows API 介绍​ Windows API&#xff08;Application Programming Interface&#xff09;&#xff0c;也称为WinAPI&#xff0c;是微软Windows操作系统的核心编程接口。它提供了一系列函数、消息、数据结构、宏和系统服务&#xff0c;允许开发者创建运行在Windows平台上的应…

Kubernetes Dashboard UI 部署安装

K8S 集群环境&#xff1a; Ubuntu 24 / K8S 1.28.21. 推荐使用helm 安装Kubernetes Dashboardsudo snap install helm --classic2. 部署Kubernetes Dashboard# Add kubernetes-dashboard repository helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboar…