Autogen代理结合Playwright MCP单独运行仅执行第一步即终止,RoundRobinGroupChat中正常的问题求助
我最近在开发一个基于Autogen的网页导航代理,用Playwright MCP来执行多步骤的网页操作,结果遇到了一个非常诡异的问题:把代理放到RoundRobinGroupChat团队里跑时,能完整走完所有12步任务;但单独运行这个AssistantAgent时,它只执行完第一步就停了,还弹出一个RuntimeWarning。我刚上手Autogen,完全摸不着头脑,求各位大佬帮忙分析下原因!
错误信息与运行日志
运行时弹出的警告:
<sys>:0: RuntimeWarning: Cannot safely stop actor at [McpWorkbench.__del__]: loop is closed or not running
Agent停步前的最后输出日志:
---------- webassistant ----------
I see the 'Reports' menu and its submenu options on the left side of the page. Next, I will open the submenu and click on the 'World xxxx Production Reports' link.
[Prompt tokens: 7045, Completion tokens: 39]
---------- Summary ----------
Number of inner messages: 2
Total prompt tokens: 8595
Total completion tokens: 63
Duration: 4.73 seconds
我的代码实现
import asyncio import sys from autogen_agentchat.agents import AssistantAgent import os from dotenv import load_dotenv from autogen_core.models import UserMessage from autogen_ext.models.openai import AzureOpenAIChatCompletionClient from autogen_ext.tools.mcp import StdioServerParams, McpWorkbench from autogen_agentchat.conditions import TextMentionTermination from autogen_agentchat.ui import Console import yaml from autogen_agentchat.teams import RoundRobinGroupChat from autogen_agentchat.messages import TextMessage from autogen_core import CancellationToken # 加载凭据 load_dotenv(override=True) azure_openai_api_key = os.getenv("AZURE_OPENAI_API_KEY") azure_openai_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") azure_openai_api_version = os.getenv("AZURE_OPENAI_API_VERSION") azure_deployment = os.getenv("AZURE_DEPLOYMENT") # 加载系统提示词 with open("prompt_basic.yml", "r") as f: yaml_data = yaml.safe_load(f) system_prompt = yaml_data['webassistant_prompt'] webnavigation_server_params = StdioServerParams( command="npx", args=["@playwright/mcp@latest"], read_timeout_seconds=60 ) fs_workbench = McpWorkbench(webnavigation_server_params) async def main(): az_model_client = AzureOpenAIChatCompletionClient( azure_deployment=azure_deployment, model=azure_deployment, api_version=azure_openai_api_version, azure_endpoint=azure_openai_endpoint, ) async with fs_workbench as fs_wb: webassistant = AssistantAgent( name="webassistant", workbench=fs_wb, model_client=az_model_client, system_message=system_prompt, reflect_on_tool_use=True ) # 单独运行时用这个,目前只执行第一步就终止 await Console( webassistant.on_messages_stream( messages=[TextMessage(source="User", content="")], cancellation_token=CancellationToken() ), output_stats=True ) # 团队模式下用这个,能正常完成所有任务 # group_chat = RoundRobinGroupChat(agents=[webassistant], messages=[], max_rounds=50) # await Console(group_chat.run_stream(task="", cancellation_token=CancellationToken()), output_stats=True) if __name__ == "__main__": asyncio.run(main())
系统提示词内容(prompt_basic.yml)
webassistant_prompt: | You are a webpage surfer. You have the following list of tasks. You need to perform ALL the tasks and only then hand over your output: 1. Navigate to the following website: https://xxxx/xxxx 2. Look for the 'Reports' menu, open it and in the submenu click on the 'World Production Reports' link 3. Look for the latest 'World Production' monthly report and click on it 4. Wait for the page to load, once loaded click on the current report 5. Wait till the pdf gets loaded. 6. Get and store the raw URL in a python dictionary. Example: 'production_data':'https://url.address.com/pdf_link' 7. Navigate to the following website: https://xxxx/xxxx 8. Look for the 'Reports' menu, open it and in the submenu click on the 'World Markets and Trade Reports' link 9. Wait for the page to load, and look particularly for latest xxxx related monthly report and click on it. It must be xxxx , all other xxxx are not in scope, so make sure it starts with "xxxx:". You have to scroll down to see the reports for all xxxx. If you cannot find xxxx report you need to navigate to previous reports using the navigation pages at the bottom of the page. 10. Once the page is loaded click on the current report 11. Get and store the raw URL in the previously createad python dictionary. Example: 'market_data':'https://url.address.com/pdf_link' You have two items in your python dictionary now. 12. Print the python dictionary and return it as your ONLY output.
我自己琢磨的可能原因
- 终止条件不明确:单独运行时,Agent是不是默认生成一个思考消息就判定任务完成了?而团队模式下有更宽松的终止逻辑?
- 事件循环资源问题:那个RuntimeWarning提到
loop is closed,会不会是McpWorkbench的资源清理时机和单独Agent的异步上下文不兼容? - 任务触发方式错误:单独运行时我传的是空消息,Agent是不是没正确接收到“执行全量12步任务”的指令?
有没有遇到过类似问题的朋友,或者懂Autogen的大佬,帮我看看怎么调整才能让单独运行的Agent也能走完所有任务步骤,同时消除那个RuntimeWarning?
内容来源于stack exchange

