当前位置: 首页 > news >正文

Spring Ai Alibaba Gateway 实现存量应用转 MCP 工具

作者简介:你好,我是影子,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.lryc.cn/news/585806.html

相关文章:

  • 如何查看服务器当前用户的权限
  • [es自动化更新] Updatecli编排配置.yaml | dockerfilePath值文件.yml
  • QT中设计qss字体样式但是没有用【已解决】
  • 进制转换小题
  • Django母婴商城项目实践(三)
  • 【Luogu】每日一题——Day1. P3385 【模板】负环
  • Redis概念和基础
  • [论文阅读] 人工智能 + 软件工程 | LLM辅助软件开发:需求如何转化为代码?
  • 端口到底是个什么鬼?回答我!
  • Java大厂面试故事:谢飞机的互联网医疗系统技术面试(Spring Boot、MyBatis、Kafka、Spring Security、AI等)
  • Umi-OCR 的 Docker安装(win制作镜像,Linux(Ubuntu Server 22.04)离线部署)
  • TensorFlow2 study notes[1]
  • 【每日算法】专题八_分治_归并排序
  • The Practice of Programming
  • 【2025/07/11】GitHub 今日热门项目
  • 2025十大免费销售管理软件推荐
  • AIGC(生成式AI)试用 33 -- 用AI学AI名词
  • [spring6: @EnableLoadTimeWeaving]-使用案例
  • 人脸图像生成(DCGAN)
  • Linux入门篇学习——Linux 编写第一个自己的命令,make 工具和 makefile 文件
  • C++编程基础
  • 大模型在卵巢癌预测及诊疗方案制定中的应用研究
  • Linux驱动基本概念(内核态、用户态、模块、加载、卸载、设备注册、字符设备)
  • Allegro 17.4操作记录
  • 【理念●体系】从零打造 Windows + WSL + Docker + Anaconda + PyCharm 的 AI 全链路开发体系
  • 数据库系统的基础知识(三)
  • uniapp---入门、基本配置了解
  • spring-ai RAG(Retrieval-Augmented Generation)
  • ESP32_启动日志分析
  • 力扣 hot100 Day41