Description
Problem Description
I'm encountering a persistent timeout issue when using MCPToolset
(which leverages @browsermcp/mcp
) within a SequentialAgent
that is, in turn, nested inside a LoopAgent
. The MCPToolset
works as expected and successfully browses the internet when the agent is run independently. However, as soon as the SequentialAgent
is executed as a sub-agent within a LoopAgent
, it consistently throws a Timeout
exception.
This suggests a potential interaction or resource management issue specific to how LoopAgent handles the execution of sub-agents that involve external processes or long-running operations like web browsing via MCPToolset
.
Steps to Reproduce
- Define an LlmAgent that uses MCPToolset for web browsing.
- Embed this LlmAgent within a SequentialAgent.
- Embed this SequentialAgent within a LoopAgent.
- Run the LoopAgent.
Expected Behavior
The MCPToolset within the search_term_browser_agent should successfully execute web browsing operations for each iteration of the accomodation_browser_loop_agent without timing out, similar to its behavior when run outside the LoopAgent context.
Actual Behavior
When the accomodation_browser_loop_agent attempts to execute the search_term_process_agent (which contains the MCPToolset), the MCPToolset operation times out after 5.0 seconds, leading to the following error:
025-06-01 12:02:04,658 - ERROR - fast_api.py:712 - Error in event_generator: Timed out while waiting for response to ClientRequest. Waited 5.0 seconds.
Traceback (most recent call last):
File "/home/niko/workspace/adk-accommodation-search_agent/.venv/lib/python3.11/site-packages/anyio/streams/memory.py", line 111, in receive
return self.receive_nowait()
^^^^^^^^^^^^^^^^^^^^^
File "/home/niko/workspace/adk-accommodation-search_agent/.venv/lib/python3.11/site-packages/anyio/streams/memory.py", line 106, in receive_nowait
raise WouldBlock
anyio.WouldBlock
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/niko/workspace/adk-accommodation-search_agent/.venv/lib/python3.11/site-packages/anyio/_core/_tasks.py", line 115, in fail_after
yield cancel_scope
File "/home/niko/workspace/adk-accommodation-search_agent/.venv/lib/python3.11/site-packages/mcp/shared/session.py", line 280, in send_request
response_or_error = await response_stream_reader.receive()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/niko/workspace/adk-accommodation-search_agent/.venv/lib/python3.11/site-packages/anyio/streams/memory.py", line 119, in receive
await receive_event.wait()
File "/home/niko/anaconda3/lib/python3.11/asyncio/locks.py", line 213, in wait
await fut
asyncio.exceptions.CancelledError: Cancelled by cancel scope 7fb0902c02d0
Relevant Code Snippets
# Assuming WEB_PROMPT, before_search_term_browser_agent_callback are defined elsewhere
# and search_term_selection_agent, browsing_results_agent are defined but not shown.
from agency_swarm.agents import LlmAgent, SequentialAgent, LoopAgent
from agency_swarm.tools import MCPToolset
from mcp.server.stdio import StdioServerParameters
args_browsermcp = [
"-y", # Argument for npx to auto-confirm install
"@browsermcp/mcp@latest"
]
search_term_browser_agent = LlmAgent(
model='gemini-2.0-flash',
name='search_term_browser_agent',
instruction="""You are a web browsing agent. Your task is to use the MCPToolset to browse the internet for the given search term and return the search results.
""", # Added a placeholder instruction for clarity
tools=[
MCPToolset(
connection_params=StdioServerParameters(
command='npx',
args=args_browsermcp,
),
)
],
output_key="search_results",
# Assuming before_agent_callback is correctly defined and imported
# before_agent_callback=before_search_term_browser_agent_callback,
)
search_term_process_agent = SequentialAgent(
sub_agents=[search_term_selection_agent, search_term_browser_agent, browsing_results_agent], # Commented out due to missing agents
name="search_term_process_agent",
description="""Processes the search term one at a time by:
1. Selecting the next search term to process
2. Browsing the web for the search term
3. Saving the results
"""
)
accomodation_browser_loop_agent = LoopAgent(
name="accomodation_browser_loop_agent",
description="""Iteratively processes search terms one at a time by:
1. Selecting the next search term to process
2. Browsing the web for the search term
3. Saving the results
""",
sub_agents=[search_term_process_agent],
max_iterations=10
)
Question
How can I prevent this timeout exception from occurring when MCPToolset
is used within a LoopAgent? Is there a specific configuration or a known pattern for handling long-running tool executions within LoopAgent to avoid anyio.WouldBlock and asyncio.exceptions.CancelledError?