agentscope-ai / agentscope-ai/agentscope

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

未關閉 適合新手
#1,985 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
state: needs clarification
主要語言
Python
星號
31.6k
分支
3.5k
平均合併
1 天 16 小時
30 天內合併 PR
103

描述

## 问题描述

当 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` 子句

貢獻指南

開啟貢獻指南

研究方向

Inspect `src/agentscope/agent/_react_agent.py` at `._acting()` (around line 657) where `toolkit.call_tool_function(tool_call)` is inside a `try`/`finally`. Read the surrounding code to confirm how `tool_res_msg` and the `ToolResultBlock` are created and why exceptions currently bubble out of `_acting()`. Reproduce by triggering a tool failure (for example MCP returning 401) and verify the loop no longer crashes and can continue handling messages. The issue is done when failed tool calls are handled and the agent responds without deadlocking.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
冷清
描述清晰度
描述清楚
新手友好度
84/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。