agentscope-ai / agentscope-ai/agentscope-java
[Bug]:When a SubAgent uses the MCP tools from the MainAgent, there is a certain probability of errors occurring.
- Vorherrschende Sprache
- Java
- Sterne
- 5.6k
- Forks
- 1.3k
- Ø Merge
- 4 T. 12 Std.
- Gemergte PRs (30 T.)
- 77
Beschreibung
**AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.**
When a SubAgent uses the MCP tools from the MainAgent, there is a certain probability of errors occurring.
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. You code:
import cn.hutool.core.util.StrUtil;
import io.agentscope.core.ReActAgent;
import io.agentscope.core.message.Msg;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.model.GenerateOptions;
import io.agentscope.core.session.InMemorySession;
import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import io.agentscope.core.tool.Toolkit;
import io.agentscope.core.tool.ToolkitConfig;
import io.agentscope.core.tool.mcp.McpClientBuilder;
import io.agentscope.core.tool.mcp.McpClientWrapper;
import io.agentscope.core.tool.subagent.SubAgentConfig;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
public class TestErrorAgent {
public static ReActAgent createPlanReActAgent() {
DashScopeChatModel model = DashScopeChatModel.builder()
.apiKey(ApiKeyCommon.ALIYUN_API_KEY)
.modelName("qwen3-max")
.defaultOptions(GenerateOptions.builder().additionalBodyParam("parallel_tool_calls", true)
.build())
.build();
// 创建工具集
Toolkit toolkit = new Toolkit(ToolkitConfig.builder().parallel(true).build());
// 高德MCP
McpClientWrapper gaodeMcpClient = McpClientBuilder.create("remote-mcp")
.sseTransport("https://mcp.amap.com/sse")
.queryParam("key", System.getProperty("gaode-mcp-key"))
.timeout(Duration.ofSeconds(60))
.buildAsync()
.block();
toolkit.registration().mcpClient(gaodeMcpClient)
.enableTools(List.of("maps_geo"))
.apply();
toolkit.registration().subAgent(() -> buildHotelSubAgent(model), SubAgentConfig.builder()
.toolName("hotelSubAgent")
.description("用来查询并推荐酒店工具")
.forwardEvents(true)
.session(new InMemorySession())
.build()).apply();
Map dateMap = Map.of("currentDate", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)
, "week", LocalDate.now().getDayOfWeek().name());
// 创建智能体
String systemPrompt = StrUtil.format("""
你是'Ai助手'。你可以帮助用户的目的推荐酒店
当前日期:{currentDate},周:{week}
使用'currentDate'推荐酒店
# 输出格式要求
**最终规划结果必须以markdown表格的形式,枚举所有推荐的最终结果进行展示**
""", dateMap);
return ReActAgent.builder()
.name("Ai助手")
.sysPrompt(systemPrompt)
.model(model)
.toolkit(toolkit)
.maxIters(Integer.MAX_VALUE)
.build();
}
private static class HotelTool {
@Tool(name = "hotelQueryTool", description = "查询酒店,以帮助用户规划酒店行程")
public String hotelQueryTool(
@ToolParam(name = "lon", description = "经度") String lon,
@ToolParam(name = "lat", description = "维度") String lat
) {
return "已按用户要求推荐酒店.推荐酒店为:希尔顿酒店,汉庭酒店,如家酒店";
}
}
/**
* 创建酒店智能体
*/
private static ReActAgent buildHotelSubAgent(DashScopeChatModel model) {
Toolkit toolkit = new Toolkit(ToolkitConfig.builder().parallel(true).build());
toolkit.registerTool(new HotelTool());
return ReActAgent.builder()
.name("hotelSubAgent")
.sysPrompt(String.format("""
你是一个酒店推荐专家,可以根据用户的需求,提取各行程对于酒店的需求,调用酒店工具查询并推荐最佳酒店,并按照指定的格式输出推荐的酒店.
当前日期:%s,周:%s
# Rule
- 所有的工具必须以并行的形式进行调用
- 所有经纬度信息必须通过地图工具获取
- 根据用户在入住城市的活动地点,通过地图工具获取经纬度,优先推荐最方便用户附近的酒店(这时地图工具可能非常有用)
- 酒店推荐工具推荐的都是基于用户历史入住画像所推荐的酒店,直接从推荐工具的结果中选择最适合的酒店即可
- 酒店推荐工具的调用时间较长(10s以上),为了节约时间,**必须使用并行的形式进行调用**
## 工具使用说明
- 酒店查询使用hotelQueryTool工具,工具支持并行调用,为节省时间,尽量并行调用。
- 地图工具'maps_geo'在当需要获取地址信息的时候非常有用
## 输出
- **以markdown表格形式输出各行程段的推荐结果,并简单说明理由,理由保持30字以内**
- 在输出最后,加上一个标签,以便识别结束
""",
LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE),
LocalDate.now().getDayOfWeek().name()))
.model(model)
.toolkit(toolkit)
.build();
}
public static void main(String[] args) {
// stream并行提交10次
for (int i = 0; i < 10; i++) {
ReActAgent planAgentDemo2 = TestErrorAgent.createPlanReActAgent();
// 发送消息
Msg msg = Msg.builder()
.textContent("帮我推荐南京新街口和上海外滩还有北京潘家园的酒店")
.build();
Msg call = planAgentDemo2.call(msg).block();
System.out.println(call.getTextContent());
}
}
}
2. How to execute
execute the main()
4. See error
in console
**Expected behavior**
A clear and concise description of what you expected to happen.
**Error messages**
Detailed error messages.
**Environment (please complete the following information):**
- AgentScope-Java Version: [e.g. 1.0.8, check your pom.xml dependency version or run `mvn dependency:tree | grep agentscope-parent:pom`(only mac/linux)]
- Java Version: [e.g. 17]
- OS: [e.g. macos, windows]
**Additional context**
Add any other context about the problem here.
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.