Team with MCP Enabled Agents with Chainlit as Frontend

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

Hi @ksksrkr, 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.If it’s urgent, please let us know. We appreciate your patience!

1 Like

Hi @Monali - Thank you so much for personalized response. I definitely am not demanding an out of turn answer, but I am struggling with this apparently simple task for couple of days. So, if I get any direction w.r.t. async handling, it would be highly appreciated.

Thanks and Regards,

Kaushik

Hey @kskskr

The problem is a mismatch between the lifetimes of the MCPTools, handling the connection to the MCP server, and the ones of your Agents when running in the CL context. The general rule of thumb when using async context managers like this one is to try and do everything inside the context manager. You can try that - although I understand that sometimes that’s difficult to handle.

But good news: we’re just now working on an improved DX that simplifies this, by removing the need for the async context manager. We haven’t released it yet (we’re aiming to do so in a few days), but you could check it out here: feat: revamp MCP integration by manuhortet · Pull Request #3955 · agno-agi/agno · GitHub . You can see usage examples in the cookbook files in that PR.

I’ll ping this thread again when we release it, and be around to help you test it out. Thanks for using Agno!

1 Like

Thank you so much @manu for such a prompt and helpful response. I love Agno and not going anywhere else, I will wait for the release and some examples.
Thanks and Regards,
Kaushik

Great to hear that @ksksrkr !

I’m happy to announce we released our new MCP integration already. It’s available in Agno version 1.7.7 - You can check the new syntax in our docs: Model Context Protocol (MCP) - Agno

Maybe it helps with your project! In any case, let me know if I can help further.

Thanks for using Agno!

1 Like