Hello Team - I started using AGNO and I found it great so far. An week ago I was able to build a version of a demo application using AGNO as the backend and Chainlit as frontend. Now I am refactoring this to incorporate Streamable HTTP MCP Server Calls, Workflow V2 and enhanced resource management. I am stuck with a basic issue. Here is a simplified code that I am trying with but unable to make it work. My idea is when a chat session starts in front end, a agent-team will be assigned and on every message it will be called to answer.
import os
import chainlit as cl
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.mcp import MCPTools
import asyncio
async def init_agent_team():
async with (
MCPTools(url="http://localhost:8084/mcp/", transport="streamable-http") as npi_tool,
MCPTools(url="http://localhost:8082/mcp/", transport="streamable-http") as jira_tool,
):
npi_agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
tools=[npi_tool]
)
jira_agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
tools=[jira_tool]
)
agent_team = Agent(
team=[npi_agent, jira_agent],
model=OpenAIChat(id="gpt-4o-mini"),
instructions=[
"Always choose the right agent for the user's intent.",
"Show your work with markdown and tables if relevant."
]
)
return agent_team
# Store agent in user session at chat start
@cl.on_chat_start
async def on_chat_start():
agent_team = await init_agent_team()
cl.user_session.set("agent_team", agent_team)
# Handle messages
@cl.on_message
async def on_message(message: cl.Message):
agent_team = cl.user_session.get("agent_team")
msg = cl.Message(content="")
async for chunk in await agent_team.arun(message.content, stream=True):
await msg.stream_token(chunk.get_content_as_string())
await msg.send()
"""for chunk in await cl.make_async(agent_team.run)(message.content, stream=True):
await msg.stream_token(chunk.get_content_as_string())
await msg.send()"""
This is giving me multiple errors such as RuntimeError: Attempted to exit cancel scope in a different task than it was entered in; ‘ToolCallStartedEvent’ object has no attribute ‘get_content_as_string’ … Can you please help me with this, how to properly use async context for MCP Tools in such an use case (UI interaction using MCP enabled AGNO team)?
Thanks and Regards,
Kaushik