-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Describe the bug
An API endpoint built with FastAPI (ASGI framework) hangs indefinitely after an ADK agent, which uses a MCPToolset (sooperset mcp-atlassian), has successfully completed its execution and returned the final result.
The core logic (async for event in event_generator:) finishes, the final response is collected and logged successfully (confirming agent execution finished), but the ASGI worker process stalls before or while attempting to return the final HTTP response payload to the client.
This behavior occurs despite:
Setting StreamingMode.NONE explicitly.
To Reproduce
The problem is isolated to the interaction between the Runner and the MCPToolset cleanup phase.
- Agent Service Execution (execute_agent):
config = RunConfig(streaming_mode=StreamingMode.NONE, max_llm_calls=100)
async def execute_agent(self, query: str, user_id: str, ticket_id: str) -> str:
# Session setup logic... (using InMemorySessionService)
content = types.Content(role="user", parts=[types.Part(text=query)])
final_text = None
try:
event_generator = self.runner.run_async(
new_message=content, user_id=user_id, session_id=session_id
)
async for event in event_generator:
print(f"Event: {event}")
if event.is_final_response():
final_text = event.content.parts[0].text.strip()
print(f"Final text captured: {final_text}")
# Control is returned to the FastAPI endpoint.
except Exception as e:
logger.error(f"Error executing agent: {e}")
final_text = "No response received."
return final_text
- FastAPI Endpoint Invocation:
@app.post("/query")
@limiter.limit(settings.rate_limit_requests)
async def query(request: Request, payload: JiraIssuePayload, ...):
try:
# ... Message and ID preparation ...
result = await agent_service.execute_agent(message, user_id, ticket_id)
print(f"Result: {result}") # <-- THIS IS PRINTED SUCCESSFULLY
# HANG OCCURS HERE: The HTTP response is not returned to the client.
return {"success": True, "result": result, "timestamp": datetime.now().isoformat()}
except Exception as e:
# ... error handling ...
pass
Steps to reproduce the behavior:
Set up an ADK project using an MCPToolset (Atlassian) with StreamableHTTPConnectionParams.
Deploy the agent via a FastAPI server.
Send a query that triggers the agent execution.
Observe that the server console prints the final Result: {result}
Expected behavior
The Event Loop should successfully finalize the run_async operation and return control immediately to the FastAPI endpoint to complete the HTTP response (return {"success": True, ...}) without any delay or hang.
Desktop:
OS: Windows
Python version: 3.13.5
ADK version: 1.15.1
Model Information: gemini-2.0-flash
Additional context - Important
The principal agent has a quite large prompt which contains a complex workflow that results in many calls to the MCP Atlassian tools. The agent's workflow itself is executed correctly, and the endpoint successfully receives and prints the complete response from the agent execution. However, the endpoint fails to return a 200 OK success response.
Failure Case (High Load): When the agent's execution involves many tool calls to the MCPToolset, the endpoint hangs.
Success Case (Low Load): If the agent's prompt is modified to only require few tool calls, the endpoint works correctly and returns a 200 OK HTTP response.