Hello Guys. I try to build my agent server with agent os. and when i use MCPtools,"Failed to close MCP connection: Attempted to exit cancel scope in a different task than it was entered in"

image
my code:

async def get_team(user_id: str, session_id: str):
    jdsk_agent = Agent(
        name="jdsk_mcp_tools",
        model=llm_model,
        tools=[MCPTools(url="http://localhost:9298/mcp", transport="streamable-http")],
        tool_hooks=[format_hook],
        user_id=user_id,
        session_id=session_id,
        db=storage,
        markdown=True,
        enable_user_memories=True,
        add_history_to_context=True,
        add_datetime_to_context=True,
        read_chat_history=True,
        add_location_to_context=True,
        # reasoning=True,
        # read_tool_call_history=True,
    )

    lst_agent = Agent(
        name="lst_mcp_tools",
        model=llm_model,
        tools=[MCPTools(url="http://localhost:9299/mcp", transport="streamable-http")],
        tool_hooks=[format_hook],
        user_id=user_id,
        session_id=session_id,
        db=storage,
        markdown=True,
        enable_user_memories=True,
        add_history_to_context=True,
        add_datetime_to_context=True,
        read_chat_history=True,
        add_location_to_context=True,
        # reasoning=True,
        # read_tool_call_history=True,
    )

    team = Team(
        name="agent system",
        model=llm_model,
        members=[jdsk_agent, lst_agent],
        user_id=user_id,
        session_id=session_id,
        delegate_task_to_all_members=True,
        add_datetime_to_context=True,
        add_team_history_to_members=True,
        search_session_history=True,
        markdown=True,
        add_location_to_context=True,
        db=storage,
        add_history_to_context=True,
    )

    return team


@router.post("/analysis", status_code=status.HTTP_200_OK)
async def team_run(body: RunRequest):
    team = await get_team(user_id=body.user_id, session_id=body.session_id)
    agent_output_event = team.arun(input=body.query, stream=True, stream_events=True,)
    async def sse():
        
        answer = ''
        async for output_event in agent_output_event:
            if output_event.event in [TeamRunEvent.run_started, TeamRunEvent.run_completed]:
                pass
    
            if output_event.event in [TeamRunEvent.tool_call_started]:
                answer += f"\nstart call {output_event.tool.tool_name}\n"
                yield f"start call {output_event.tool.tool_name}"
    
            if output_event.event in [TeamRunEvent.tool_call_completed]:
                pass
            
            if output_event.event in [RunEvent.tool_call_started]:
                answer += f"\nstart call {output_event.tool.tool_name}\n"
                yield f"start call {output_event.tool.tool_name}"
    
            if output_event.event in [RunEvent.tool_call_completed]:
                pass
    
            if output_event.event in [TeamRunEvent.run_content]:
                answer += f"{output_event.content}"
                yield output_event.content
            await asyncio.sleep(0.001)
        
        logger.info(f"Final answer: {answer}")

    if body.stream:
        return EventSourceResponse(sse())
    else:
        response = await team.arun(input=body.query, stream=False)
        return response.content

How to modify the code to ensure it executes correctly.

Thanks.

Hi @longfellow, thanks for reaching out and supporting Agno. I’ve shared this with the team, we’re working through all requests one by one and will get back to you soon. We appreciate your patience!

thanks, talk to you soon.