Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
<artifactId>agentscope-runtime-web</artifactId>
<version>1.0.0</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# AgentScope Usage Example For Client

This directory contains example demonstrating the Agent-to-Agent (A2A) protocol implementation with RocketMQ integration in AgentScope.

## Prerequisites

- Java 17+
- Maven 3.6+

## Basic preliminary preparations

### Deploy Apache RocketMQ

Deploy the LiteTopic version of [Apache RocketMQ](http://rocketmq.apache.org/) (the open-source version is expected to be released by the end of December), or purchase a commercial RocketMQ instance that supports LiteTopic, and create the following resources:
- **1.1** Create a LiteTopic: `WorkerAgentResponse`
- **1.2** Create a bound Lite consumer group ID for WorkerAgentResponse: `CID_HOST_AGENT_LITE`

## Quick Start

| Parameter Name | Description | Required |
|-------|------------------|------|
| rocketMQNamespace | RocketMQ namespace | No |
| rocketMQAK | RocketMQ access key | No |
| rocketMQSK | RocketMQ secret key | No |
| workAgentResponseTopic | LiteTopic | Yes |
| workAgentResponseGroupID | LiteConsumer CID | Yes |

### Build the Project

```bash
mvn clean compile
```

### Run the Client Example

```bash
mvn compile exec:java -Dexec.mainClass=io.agentscope.A2aAgentCallerExample -DrocketMQNamespace= -DworkAgentResponseTopic=WorkerAgentResponse -DworkAgentResponseGroupID=CID_HOST_AGENT_LITE -DrocketMQAK= -DrocketMQSK=
```

## Contributing

If you'd like to contribute to these examples or report issues, please submit a pull request or open an issue in the repository.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-runtime</artifactId>
<version>1.0.0</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>agentscope_use_rocketmq_client_example</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<revision>1.0.3</revision>
<agentscope.version>1.0.3</agentscope.version>
<agentscope-extensions.version>${revision}</agentscope-extensions.version>

<logback.version>1.5.20</logback.version>
</properties>

<dependencies>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-extensions-a2a-nacos</artifactId>
<version>${agentscope-extensions.version}</version>
</dependency>

<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-core</artifactId>
<version>${agentscope.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-a2a</artifactId>
<version>1.0.7</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 1999-2025 Alibaba Group Holding Ltd.
*
* 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 io.agentscope;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.alibaba.nacos.api.exception.NacosException;
import io.a2a.client.http.JdkA2AHttpClient;
import io.agentscope.core.a2a.agent.A2aAgent;
import io.agentscope.core.a2a.agent.A2aAgentConfig;
import io.agentscope.core.a2a.agent.A2aAgentConfig.A2aAgentConfigBuilder;
import io.agentscope.core.a2a.agent.card.WellKnownAgentCardResolver;
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.TextBlock;
import org.apache.rocketmq.a2a.transport.RocketMQTransport;
import org.apache.rocketmq.a2a.transport.RocketMQTransportConfig;
import reactor.core.publisher.Flux;

public class A2aAgentCallerExample {
private static final String USER_INPUT_PREFIX = "\u001B[34mYou>\u001B[0m ";
private static final String AGENT_RESPONSE_PREFIX = "\u001B[32mAgent>\u001B[0m ";
private static final String ACCESS_KEY = System.getProperty("rocketMQAK");
private static final String SECRET_KEY = System.getProperty("rocketMQSK");
private static final String WORK_AGENT_RESPONSE_TOPIC = System.getProperty("workAgentResponseTopic");
private static final String WORK_AGENT_RESPONSE_GROUP_ID = System.getProperty("workAgentResponseGroupID");
private static final String ROCKETMQ_NAMESPACE = System.getProperty("rocketMQNamespace");
private static final String AGENT_NAME = "agentscope-a2a-rocketmq-example-agent";

// Can change this to false disable streaming.
static boolean streaming = true;

public static void main(String[] args) throws NacosException {
RocketMQTransportConfig rocketMQTransportConfig = new RocketMQTransportConfig();
rocketMQTransportConfig.setAccessKey(ACCESS_KEY);
rocketMQTransportConfig.setSecretKey(SECRET_KEY);
rocketMQTransportConfig.setWorkAgentResponseTopic(WORK_AGENT_RESPONSE_TOPIC);
rocketMQTransportConfig.setWorkAgentResponseGroupID(WORK_AGENT_RESPONSE_GROUP_ID);
rocketMQTransportConfig.setNamespace(ROCKETMQ_NAMESPACE);
rocketMQTransportConfig.setHttpClient(new JdkA2AHttpClient());
A2aAgentConfig a2aAgentConfig = new A2aAgentConfigBuilder().withTransport(RocketMQTransport.class, rocketMQTransportConfig).build();
A2aAgent agent = A2aAgent.builder().a2aAgentConfig(a2aAgentConfig).name(AGENT_NAME).agentCardResolver(WellKnownAgentCardResolver.builder().baseUrl("http://localhost:10001").build()).build();
startExample(agent);
}

private static void startExample(A2aAgent agent) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
while (true) {
// 用户输入提示
System.out.print(USER_INPUT_PREFIX);
String input = reader.readLine();
// 退出条件检测
if (input == null || input.trim().equalsIgnoreCase("exit") || input.trim().equalsIgnoreCase("quit")) {
System.out.println(AGENT_RESPONSE_PREFIX + "Bye!");
break;
}
System.out.println(AGENT_RESPONSE_PREFIX + "I have received your question: " + input);
// 流式输出带前缀
System.out.print(AGENT_RESPONSE_PREFIX);
// 处理输入并获取响应
processInput(agent, input).doOnNext(System.out::print).then().block();
// 换行分隔
System.out.println();
}
} catch (IOException e) {
System.err.println("input error: " + e.getMessage());
}
}

private static Flux<String> processInput(A2aAgent agent, String input) {
Msg msg = Msg.builder().role(MsgRole.USER).content(TextBlock.builder().text(input).build()).build();
return agent.stream(msg).map(event -> {
if (streaming && event.isLast()) {
// The last message is whole artifact message result, which has been solved and print in before event handle.
// Weather need to handle the last message, depends on the use case.
return "";
}
Msg message = event.getMessage();
StringBuilder partText = new StringBuilder();
message.getContent().stream().filter(block -> block instanceof TextBlock).map(block -> (TextBlock) block)
.forEach(block -> partText.append(block.getText()));
return partText.toString();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
~ Copyright 1999-2025 Alibaba Group Holding Ltd.
~
~ 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.
-->

<configuration>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# AgentScope Usage Example For Server

A minimal, end-to-end example showing how to build and deploy an intelligent agent with AgentScope Runtime Java using a ReAct-style agent, sandboxed tools, and local A2A deployment.

## Features

- Build agents using AgentScope's `ReActAgent`
- Integrate DashScope (Qwen) large language models
- Use RocketMQ for asynchronous communication between agents
- Call tools: Python execution, Shell command execution, browser navigation
- Deploy locally as an A2A application
- Stream responses and show thinking process
- One-click Docker image packaging

## Prerequisites

- Java 17+
- Maven 3.6+

## Basic preliminary preparations

### 1. Deploy Apache RocketMQ

Deploy the LiteTopic version of [Apache RocketMQ](http://rocketmq.apache.org/) (the open-source version is expected to be released by the end of December), or purchase a commercial RocketMQ instance that supports LiteTopic, and create the following resources:
- **1.1** Create a standard topic for the AI assistant: `AgentTask`
- **1.2** Create a standard consumer group ID for the AI assistant: `AgentTaskConsumerGroup`

### 2. Get Qwen API key
- Go to the Bailian platform to obtain the corresponding Qwen API key.

## Quick Start

| Parameter Name | Description | Required |
|-------|------------------|------|
| rocketMQEndpoint | RocketMQ service endpoint | Yes |
| rocketMQNamespace | RocketMQ namespace | No |
| bizTopic | Standard topic | Yes |
| bizConsumerGroup | Standard consumer group ID (CID) | Yes |
| rocketMQAK | RocketMQ access key | No |
| rocketMQSK | RocketMQ secret key | No |
| apiKey | API key for calling Bailian platform | Yes |

### 1.Build the Project

```bash
mvn clean compile
```

### 2.Run the Example

```bash
mvn exec:java -Dexec.mainClass=io.agentscope.AgentScopeDeployRocketMQExample -DrocketMQEndpoint= -DrocketMQNamespace= -DbizTopic=AgentTask -DbizConsumerGroup=AgentTaskConsumerGroup -DrocketMQAK= -DrocketMQSK= -DapiKey=
```

### 3.Test the Deployed Agent
After deployment, the agent listens on `http://localhost:10001`.
Query AgentCard Info, `http://localhost:10001/.well-known/agent-card.json`

## Notes

1. Default deployment port is `10001` (change in code if needed)
2. The sandbox manager uses default settings; customize via `ManagerConfig` as required
3. This example uses in-memory storage; use persistent storage for production

## Extension Ideas

- Add custom tools
- Configure persistent storage (Redis, OSS, etc.)
- Deploy sandboxes with Kubernetes
- Integrate additional MCP tools

## Related Documentation

- [AgentScope Runtime Java (root)](../../README.md)
- [Examples Overview](../README.md)
Loading