agentscope-ai / agentscope-ai/agentscope

[Bug]: Agent crashes when tool call fails due to missing exception handling in _acting()

Đang mở
#1,985 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
state: needs clarification
Ngôn ngữ chính
Python
Star
31.5k
Fork
3.5k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
95

Mô tả

## 问题描述

当 Agent 调用 MCP 工具失败时(如 MCP Server 返回 HTTP 401),`ReActAgent._acting()` 方法中的工具调用异常未被捕获,直接向上传播导致整个 Agent 循环崩溃。表现为:

- 微信渠道:typing 指示器永久旋转,用户收不到任何回复
- 消费者线程死锁,后续消息被丢弃

## 根因分析

`_acting()` 方法(`_react_agent.py:657`)的 `try` 块只包含 `finally` 子句来记录工具结果消息,但缺少 `except` 子句。当 `toolkit.call_tool_function(tool_call)` 抛出异常时,由于没有异常处理:

1. 异常直接向上传播到调用方
2. Agent 的推理循环被中断
3. 消费者永远等待不到响应

```python
try:
tool_res = await self.toolkit.call_tool_function(tool_call)
# ... process chunks ...
finally:
await self.memory.add(tool_res_msg) # 只记录,不处理异常
```

## 修复方案

在 `try` 和 `finally` 之间添加 `except Exception` 块,将异常信息作为工具返回结果写入 `ToolResultBlock`,使 Agent 能够看到错误并继续推理循环,而非直接崩溃:

```python
except Exception as e:
error_msg = f"Tool call failed: {e}"
tool_res_msg.content[0]["output"] = error_msg
await self.print(tool_res_msg, True)
return None
```

## 修改文件

- `src/agentscope/agent/_react_agent.py`:在 `_acting()` 方法的 `try` 块中添加 `except Exception` 子句

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.