[Python] Autogen agent performs only the first step when run with playwright mcp

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    Problem:

    I have an autogen agent which needs to perform basic website navigation task with several steps. The agent uses playwright MCP. The agent when run, performs the first step, and then it goes to the second but it just logs out displaying a weird warning message. When I try this agent in roundrobinchat it works as it should. When I run it on its own it stops right after the first step. I am new to autogen, I don't understand why does it stop right after the first step when run as an agent and why it doesn't stop when used in roundrobin.


    Warning message displayed:

    ---------- 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 Agricultural 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
    <sys>:0: RuntimeWarning: Cannot safely stop actor at [McpWorkbench.__del__]: loop is closed or not running



    My code:

    import asyncio, sys
    from autogen_agentchat.agents import AssistantAgent
    import os
    from dotenv import load_dotenv, dotenv_values
    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 credentials
    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")

    # load yaml system prompt
    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)


    #await Console(webassistant.run_stream(task=""))



    asyncio.run(main())



    My prompt:

    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 ouptut.

    Continue reading...

Compartilhe esta Página