将 Nacos AI Registry 中的 Skill 包作为 AgentScope 的技能来源。通过 NacosSkillRepository,Agent 可以直接从 Nacos 下载 Skill ZIP 并加载为 AgentSkill 使用。
前提条件
已开通阿里云 MSE AI 治理中心实例,并完成工作空间创建。
已准备好阿里云 AccessKey / SecretKey,且 RAM 子账号已授予 Skill 相关读权限,参考 AI 治理中心 RAM 权限配置指南。
已根据 Agent 运行环境完成网络接入配置:
本地开发、IDC 或未部署在阿里云 VPC 内的应用,请参考 AI 治理中心公网访问配置指南 配置公网接入点和 IP 白名单。
部署在阿里云 VPC 内的应用,请参考 AI 治理中心 VPC 私网访问配置指南 配置 VPC 私网访问。
AgentScope 1.0.12+,JDK 17+,Maven 3.8+。
模块信息
Maven Artifact:
io.agentscope:agentscope-extensions-nacos-skillAgentScope 版本要求:1.0.12+
主要依赖:
io.agentscope:agentscope-core、com.alibaba.nacos:nacos-clientJDK 要求:JDK 17+
注意:Nacos 导出的
SKILL.md中 YAML frontmatter 可能存在多行缩进,NacosSkillRepository加载时自动处理为扁平格式,无需手动干预。
完整示例
以下是一个端到端的可运行示例,展示从初始化 Nacos 连接到 Agent 对话的完整链路。请将 SERVER_ADDR 替换为公网接入点域名或 VPC 私网域名:
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.ai.AiFactory;
import com.alibaba.nacos.api.ai.AiService;
import io.agentscope.core.ReActAgent;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.nacos.skill.NacosSkillRepository;
import io.agentscope.core.skill.AgentSkill;
import io.agentscope.core.skill.SkillBox;
import java.util.Properties;
public class NacosSkillDemo {
public static void main(String[] args) throws Exception {
// 1. 创建 AiService
Properties props = new Properties();
props.put(PropertyKeyConst.SERVER_ADDR, "airegistry.cn-hangzhou.mse.aliyuncs.com:80");
props.put(PropertyKeyConst.NAMESPACE, "your-namespace-id");
props.put(PropertyKeyConst.ACCESS_KEY, "your-access-key");
props.put(PropertyKeyConst.SECRET_KEY, "your-secret-key");
AiService aiService = AiFactory.createAiService(props);
// 2. 创建 NacosSkillRepository,加载 Skill
try (NacosSkillRepository repo = new NacosSkillRepository(aiService, "public")) {
AgentSkill skill = repo.getSkill("safe-commit-helper");
System.out.println("Loaded: " + skill.getName());
System.out.println("Description: " + skill.getDescription());
// 3. 绑定到 Agent
SkillBox skillBox = new SkillBox(repo);
var model = DashScopeChatModel.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.modelName("qwen-max")
.build();
var agent = ReActAgent.builder()
.name("DevAssistant")
.systemPrompt("你是一个编程助手。")
.model(model)
.skillBox(skillBox)
.build();
// 4. 使用 Agent
var response = agent.call(io.agentscope.core.message.Msg.of("帮我生成一个commit message"));
System.out.println(response.getText());
} finally {
aiService.shutdown();
}
}
}分步详解
1. 添加依赖
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-extensions-nacos-skill</artifactId>
</dependency>推荐通过 agentscope-bom 统一管理版本:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-bom</artifactId>
<version>1.0.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>2. 初始化 AiService
创建 Nacos AiService 实例,配置 AI 治理中心访问地址和凭证。SERVER_ADDR 可填写公网接入点域名或 VPC 私网域名:
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.ai.AiFactory;
import com.alibaba.nacos.api.ai.AiService;
import java.util.Properties;
Properties props = new Properties();
props.put(PropertyKeyConst.SERVER_ADDR, "airegistry.cn-hangzhou.mse.aliyuncs.com:80");
props.put(PropertyKeyConst.NAMESPACE, "your-namespace-id");
props.put(PropertyKeyConst.ACCESS_KEY, "LTAI5tXXXXXXXXXXXXXX");
props.put(PropertyKeyConst.SECRET_KEY, "XXXXXXXXXXXXXXXXXXXXxx");
AiService aiService = AiFactory.createAiService(props);一个AiService实例对应一个命名空间。应用内尽量复用实例,退出时调用aiService.shutdown()释放资源。
3. 创建 NacosSkillRepository 并加载 Skill
使用 try-with-resources 方式创建 NacosSkillRepository:
import io.agentscope.core.nacos.skill.NacosSkillRepository;
import io.agentscope.core.skill.AgentSkill;
try (NacosSkillRepository repo = new NacosSkillRepository(aiService, "public")) {
String name = "safe-commit-helper";
if (repo.skillExists(name)) {
AgentSkill skill = repo.getSkill(name);
System.out.println(skill.getName() + " - " + skill.getDescription());
} else {
System.out.println("Skill not found: " + name);
}
}如果 namespaceId 为空或 null,自动使用 "public"。
4. 将 Skill 绑定到 Agent
通过 SkillBox 将 Skill 仓库关联到 Agent,Agent 在需要时按需从仓库加载 Skill:
import io.agentscope.core.skill.SkillBox;
import io.agentscope.core.ReActAgent;
SkillBox skillBox = new SkillBox(repo);
var agent = ReActAgent.builder()
.name("MyAgent")
.systemPrompt("你是一个智能助手。")
.model(model)
.skillBox(skillBox)
.build();版本与标签配置
NacosSkillRepository 根据 skillVersion 和 skillLabel 决定从 Nacos 下载哪个版本的 Skill ZIP,两者各自按以下优先级解析,首个非空值生效:
优先级 | 来源 | 配置 Key |
1(最高) | 构造函数传入的 |
|
2 | JVM 系统属性 | 同上 |
3(最低) | 环境变量 |
|
当 Properties、JVM 属性、环境变量同时设置 version 时,Properties 中的值优先生效;当 version 和 label 同时配置时,version 优先,label 被忽略。
配置方式
通过构造函数 Properties:
按版本加载:
Properties appProps = new Properties();
appProps.setProperty(NacosSkillRepository.SKILL_VERSION_PATH, "v1.0.0");
try (NacosSkillRepository repo =
new NacosSkillRepository(aiService, "public", appProps)) {
AgentSkill skill = repo.getSkill("safe-commit-helper");
}按标签加载:
Properties appProps = new Properties();
appProps.setProperty(NacosSkillRepository.SKILL_LABEL_PATH, "stable");
try (NacosSkillRepository repo =
new NacosSkillRepository(aiService, "public", appProps)) {
AgentSkill skill = repo.getSkill("safe-commit-helper");
}通过环境变量:
export AGENTSCOPE_NACOS_SKILL_VERSION=v1.0.0
# 或
export AGENTSCOPE_NACOS_SKILL_LABEL=stable通过 JVM 系统属性:
java -Dagentscope.nacos.skill.version=v1.0.0 -jar myapp.jar优先级示例:
// JVM: -Dagentscope.nacos.skill.version=jvm-ver
// ENV: AGENTSCOPE_NACOS_SKILL_VERSION=env-ver
Properties props = new Properties();
props.setProperty(NacosSkillRepository.SKILL_VERSION_PATH, "app-ver");
// 此处 "app-ver" 生效
NacosSkillRepository repo = new NacosSkillRepository(aiService, "public", props);
repo.getSkill("my-skill"); // 实际调用 downloadSkillZipByVersion("my-skill", "app-ver")下载 API 选择逻辑
以下为 NacosSkillRepository 内部实现说明,供理解调用逻辑参考。
NacosSkillRepository 根据 version / label 自动选择下载方式(version 优先):
若
skillVersion非空 →AiService.downloadSkillZipByVersion(name, version)否则若
skillLabel非空 →AiService.downloadSkillZipByLabel(name, label)否则 →
AiService.downloadSkillZip(name)
NacosSkillRepository 能力矩阵
核心类:io.agentscope.core.nacos.skill.NacosSkillRepository,实现了 AgentSkillRepository 接口。
能力 | 方法 | 状态 | 说明 |
加载单个 Skill |
| 支持 | 下载 ZIP → 适配 frontmatter → 构建 |
判断 Skill 是否存在 |
| 支持 | 依据下载结果 / |
获取仓库元信息 |
| 支持 | 类型固定 |
获取来源标识 |
| 支持 | 格式: |
只读声明 |
| 恒为 |
|
列举全部 Skill |
| 暂未实现 | 返回空集合,打印 warn |
写操作 |
| 暂未实现 | 返回 |
环境变量参考
以下为 Nacos 通用环境变量参考,本文代码示例使用 PropertyKeyConst 常量方式进行配置,与下表中的环境变量无关。
变量 | 说明 |
| 指定 Skill 版本(可选) |
| 指定 Skill 标签(可选) |
更多参考
AI Registry Skill 快速上手指南:Skill 的创建、审核、发布流程
Nacos Java SDK 接入 AI 治理中心:Nacos Java SDK 的接入配置与能力概览
Nacos CLI 接入 AI 治理中心:通过 Nacos CLI 接入 AI Registry
AI 治理中心公网访问配置指南:公网接入点和 IP 白名单的配置步骤
AI 治理中心工作空间管理指南:工作空间的创建、查询、编辑
AI 治理中心 Skill 管理:控制台侧 Skill 的版本管理与发布
AI 治理中心 VPC 私网访问配置指南:VPC 私网访问的配置步骤
AI 治理中心 RAM 权限配置指南:RAM 子账号授权配置