Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<module>spring-ai-alibaba-studio</module>
<module>spring-ai-alibaba-jmanus</module>
<module>spring-ai-alibaba-deepresearch</module>
<module>spring-ai-alibaba-agent-nacos</module>

<!-- Spring AI Alibaba Tool Call Plugins -->
<module>community/tool-calls/spring-ai-alibaba-starter-tool-calling-common</module>
Expand Down Expand Up @@ -411,6 +412,9 @@
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring-javaformat-maven-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<goals>
Expand Down
144 changes: 144 additions & 0 deletions spring-ai-alibaba-agent-nacos/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-alibaba-agent-nacos</artifactId>
<name>Spring AI Alibaba Agent Nacos</name>
<description>spring-ai-alibaba-agent-nacos</description>
<url></url>
<licenses>
<license></license>
</licenses>
<developers>
<developer></developer>
</developers>
<scm>
<connection></connection>
<developerConnection></developerConnection>
<tag></tag>
<url></url>
</scm>
<properties>
<java.version>17</java.version>
<lombok.version>1.18.30</lombok.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-maintainer-client</artifactId>
<version>${nacos3.version}</version>

<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-commons</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-retry</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-retry</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-core</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-mcp-registry</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-graph-core</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-mcp-router</artifactId>
<version>${revision}</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos3.version}</version>
<exclusions>

<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.ai.agent.nacos;

import com.alibaba.cloud.ai.graph.agent.Builder;
import com.alibaba.cloud.ai.graph.agent.factory.AgentBuilderFactory;

public class NacosAgentBuilderFactory implements AgentBuilderFactory {

private NacosOptions nacosOptions;

public NacosAgentBuilderFactory(NacosOptions nacosOptions) {
this.nacosOptions = nacosOptions;
}

@Override
public Builder builder() {
return new NacosReactAgentBuilder().nacosOptions(this.nacosOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.ai.agent.nacos;

import com.alibaba.cloud.ai.agent.nacos.vo.AgentVO;
import com.alibaba.cloud.ai.agent.nacos.vo.ModelVO;
import com.alibaba.cloud.ai.agent.nacos.vo.PromptVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.NacosConfigService;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;

public class NacosAgentInjector {

public static void injectPrompt(NacosConfigService nacosConfigService, ChatClient chatClient, String promptKey) {

try {
PromptVO promptVO = NacosPromptInjector.getPromptByKey(nacosConfigService, promptKey);
if (promptVO != null) {
NacosPromptInjector.replacePrompt(chatClient, promptVO);
}
NacosPromptInjector.registerPromptListener(nacosConfigService, chatClient, promptKey);
}

catch (Exception e) {
throw new RuntimeException(e);
}

}

/**
* load prompt by agent id.
*
* @param nacosConfigService
* @param agentName
* @return
*/
public static AgentVO loadAgentVO(NacosConfigService nacosConfigService, String agentName) {
try {
String config = nacosConfigService.getConfig("agent-base.json", "ai-agent-" + agentName,
3000L);
return JSON.parseObject(config, AgentVO.class);
}
catch (NacosException e) {
throw new RuntimeException(e);
}
}

public static void injectPromptByAgentName(NacosConfigService nacosConfigService, ChatClient chatClient,String agentName,AgentVO agentVO) {

try {
if (agentVO == null) {
return;
}
PromptVO promptVO = NacosPromptInjector.loadPromptByAgentId(nacosConfigService, agentVO);
if (promptVO != null) {
NacosPromptInjector.replacePrompt(chatClient, promptVO);
}
NacosPromptInjector.registryPromptByAgentId(chatClient, nacosConfigService, agentName, promptVO);
}

catch (Exception e) {
throw new RuntimeException(e);
}

}

public static void injectModel(NacosOptions nacosOptions, ChatClient chatClient, String agentId) {
ModelVO modelVO = NacosModelInjector.getModelByAgentId(nacosOptions, agentId);
if (modelVO != null) {
try {
ChatModel chatModel = NacosModelInjector.initModel(nacosOptions, modelVO);
NacosModelInjector.replaceModel(chatClient, chatModel);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
NacosModelInjector.registerModelListener(chatClient, nacosOptions, agentId);
}


public static ChatModel initModel(NacosOptions nacosOptions, String agentId) {
ModelVO modelVo = NacosModelInjector.getModelByAgentId(nacosOptions, agentId);
if (modelVo == null) {
return null;
}

return NacosModelInjector.initModel(nacosOptions, modelVo);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.ai.agent.nacos;

import java.util.List;

import com.alibaba.cloud.ai.agent.nacos.tools.NacosMcpGatewayToolsInitializer;
import com.alibaba.cloud.ai.agent.nacos.vo.McpServersVO;
import com.alibaba.cloud.ai.graph.node.LlmNode;
import com.alibaba.cloud.ai.graph.node.ToolNode;
import com.alibaba.cloud.ai.mcp.gateway.nacos.properties.NacosMcpGatewayProperties;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ai.tool.ToolCallback;

public class NacosMcpToolsInjector {

private static final Logger logger = LoggerFactory.getLogger(NacosMcpToolsInjector.class);

public static List<ToolCallback> loadMcpTools(NacosOptions nacosOptions, String agentId) {
McpServersVO mcpServersVO = getMcpServersVO(nacosOptions, agentId);
if (mcpServersVO != null) {
return convert(nacosOptions, mcpServersVO);
}
return null;
}

public static void registry(LlmNode llmNode, ToolNode toolNode, NacosOptions nacosOptions, String agentName) {

try {
nacosOptions.getNacosConfigService()
.addListener("mcp-servers.json", "ai-agent-" + agentName, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
McpServersVO mcpServersVO = JSON.parseObject(configInfo, McpServersVO.class);
List<ToolCallback> toolCallbacks = convert(nacosOptions, mcpServersVO);
if (toolCallbacks != null) {
toolNode.setToolCallbacks(toolCallbacks);
llmNode.setToolCallbacks(toolCallbacks);
}

}
});
}
catch (NacosException e) {
throw new RuntimeException(e);
}

}

public static McpServersVO getMcpServersVO(NacosOptions nacosOptions, String agentName) {
try {
String config = nacosOptions.getNacosConfigService()
.getConfig("mcp-servers.json", "ai-agent-" + agentName, 3000L);
return JSON.parseObject(config, McpServersVO.class);
}
catch (NacosException e) {
throw new RuntimeException(e);
}
}

public static List<ToolCallback> convert(NacosOptions nacosOptions, McpServersVO mcpServersVO) {

NacosMcpGatewayProperties nacosMcpGatewayProperties = new NacosMcpGatewayProperties();
nacosMcpGatewayProperties.setServiceNames(mcpServersVO.getMcpServers().stream()
.map(McpServersVO.McpServerVO::getMcpServerName).toList());
NacosMcpGatewayToolsInitializer nacosMcpGatewayToolsInitializer = new NacosMcpGatewayToolsInitializer(
nacosOptions.mcpOperationService, nacosMcpGatewayProperties, mcpServersVO.getMcpServers());
List<ToolCallback> toolCallbacks = nacosMcpGatewayToolsInitializer.initializeTools();

return toolCallbacks;
}

}
Loading
Loading