Skip to content

Python: [AG-UI] Failure when connecting multiple MCP tools #3238

@TsukasaIshimura

Description

@TsukasaIshimura

I am building a web app using Microsoft Agent Framework AG-UI and CopilotKit.
When I connect multiple MCP tools to the agent, the first MCP tool call succeeds, but the second MCP tool call fails.
We would appreciate any advice.

Source code

from __future__ import annotations

import logging
import os
from typing import Any

import uvicorn
from agent_framework import ChatAgent, MCPStreamableHTTPTool, MCPWebsocketTool
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework_ag_ui import (AgentFrameworkAgent,
                                   add_agent_framework_fastapi_endpoint)
from app.config import settings
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware


chat_client = AzureOpenAIChatClient(credential=AzureCliCredential())

app = FastAPI(title="CopilotKit + Microsoft Agent Framework (Python)")
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

get_datetime_mcp = MCPStreamableHTTPTool(
    name="get-datetime-mcp",
    url="http://localhost:8001/mcp",
)
get_weather_mcp = MCPStreamableHTTPTool(
    name="get-weather-mcp",
    url="http://localhost:8002/mcp",
)

base_agent = ChatAgent(
    name="MyAgent",
    instructions="You are a helpful AI Agent.",
    chat_client=chat_client,
    tools=[
        get_datetime_mcp,
        get_weather_mcp,
    ],
)

add_agent_framework_fastapi_endpoint(
    app=app,
    agent=AgentFrameworkAgent(
        agent=base_agent,
        name="MyAgent",
        description="Helpful AI Agent",
    ),
    path="/api/v1/agents/my-agent",
)

if __name__ == "__main__":
    host = os.getenv("AGENT_HOST", "0.0.0.0")
    port = int(os.getenv("AGENT_PORT", "8000"))
    uvicorn.run("sample:app", host=host, port=port, reload=True)

Log

INFO:agent_framework_ag_ui._orchestrators:[STREAM] Received update #3 from agent
INFO:agent_framework_ag_ui._events:Processing AgentRunUpdate with 0 content items
INFO:agent_framework_ag_ui._orchestrators:[STREAM] Update #3 produced 0 events
INFO:agent_framework_ag_ui._orchestrators:[STREAM] Received update #4 from agent
INFO:agent_framework_ag_ui._events:Processing AgentRunUpdate with 1 content items
INFO:agent_framework_ag_ui._events:  Content 0: type=UsageContent
INFO:agent_framework_ag_ui._orchestrators:[STREAM] Update #4 produced 0 events
DEBUG:httpcore.http11:response_closed.started
DEBUG:httpcore.http11:response_closed.complete
INFO:agent_framework:Function name: get_weather
DEBUG:agent_framework:Function arguments: {'thread': <agent_framework._threads.AgentThread object at 0x75f9251d9090>}
INFO:agent_framework._mcp:MCP connection closed unexpectedly. Reconnecting...
DEBUG:httpcore.connection:connect_tcp.started host='localhost' port=8002 local_address=None timeout=30.0 socket_options=None
DEBUG:httpcore.http11:receive_response_body.failed exception=GeneratorExit()
DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x75f925ed8830>
DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'DELETE']>
DEBUG:httpcore.http11:send_request_headers.complete
DEBUG:httpcore.http11:send_request_body.started request=<Request [b'DELETE']>
DEBUG:httpcore.http11:send_request_body.complete
DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'DELETE']>
DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'date', b'Thu, 15 Jan 2026 15:48:35 GMT'), (b'server', b'uvicorn'), (b'content-type', b'application/json'), (b'mcp-session-id', b'f9fea0adf57b4c43bbbe612e384abaa4'), (b'content-length', b'0')])
INFO:httpx:HTTP Request: DELETE http://localhost:8002/mcp "HTTP/1.1 200 OK"
DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'DELETE']>
DEBUG:httpcore.http11:receive_response_body.complete
DEBUG:httpcore.http11:response_closed.started
DEBUG:httpcore.http11:response_closed.complete
DEBUG:httpcore.connection:close.started
DEBUG:httpcore.connection:close.complete
DEBUG:agent_framework:Failed to reconnect to MCP server.
  + Exception Group Traceback (most recent call last):
  |   File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 783, in __aexit__
  |     raise BaseExceptionGroup(
  |         "unhandled errors in a TaskGroup", self._exceptions
  |     ) from None
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.13/site-packages/mcp/client/streamable_http.py", line 670, in streamable_http_client
    |     yield (
    |     ...<3 lines>...
    |     )
    |   File "/usr/local/lib/python3.13/contextlib.py", line 751, in __aexit__
    |     cb_suppress = await cb(*exc_details)
    |                   ^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/mcp/shared/session.py", line 238, in __aexit__
    |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 794, in __aexit__
    |     return self.cancel_scope.__exit__(exc_type, exc_val, exc_tb)
    |            ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 461, in __exit__
    |     raise RuntimeError(
    |     ...<2 lines>...
    |     )
    | RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
    +------------------------------------

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/agent_framework/_mcp.py", line 750, in call_tool
    await self.connect(reset=True)
  File "/usr/local/lib/python3.13/site-packages/agent_framework/_mcp.py", line 392, in connect
    await self._exit_stack.aclose()
  File "/usr/local/lib/python3.13/contextlib.py", line 705, in aclose
    await self.__aexit__(None, None, None)
  File "/usr/local/lib/python3.13/contextlib.py", line 768, in __aexit__
    raise exc
  File "/usr/local/lib/python3.13/contextlib.py", line 751, in __aexit__
    cb_suppress = await cb(*exc_details)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/contextlib.py", line 235, in __aexit__
    await self.gen.athrow(value)
  File "/usr/local/lib/python3.13/site-packages/mcp/client/streamable_http.py", line 647, in streamable_http_client
    async with anyio.create_task_group() as tg:
               ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 789, in __aexit__
    if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
       ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 461, in __exit__
    raise RuntimeError(
    ...<2 lines>...
    )
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
INFO:agent_framework_ag_ui._orchestrators:[STREAM] Received update #5 from agent
INFO:agent_framework_ag_ui._events:Processing AgentRunUpdate with 1 content items
INFO:agent_framework_ag_ui._events:  Content 0: type=FunctionResultContent
INFO:agent_framework_ag_ui._events:Emitting ToolCallEndEvent for completed tool call 'call_Ou7M7GySQIaE5sXUjwuHMNMG'
INFO:agent_framework_ag_ui._orchestrators:[STREAM] Update #5 produced 2 events
INFO:agent_framework_ag_ui._orchestrators:[STREAM] Yielding event: ToolCallEndEvent
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Event 5: ToolCallEndEvent
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Event payload: {'type': <EventType.TOOL_CALL_END: 'TOOL_CALL_END'>, 'tool_call_id': 'call_Ou7M7GySQIaE5sXUjwuHMNMG'}
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Encoded as: data: {"type":"TOOL_CALL_END","toolCallId":"call_Ou7M7GySQIaE5sXUjwuHMNMG"}


INFO:agent_framework_ag_ui._orchestrators:[STREAM] Yielding event: ToolCallResultEvent
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Event 6: ToolCallResultEvent
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Event payload: {'type': <EventType.TOOL_CALL_RESULT: 'TOOL_CALL_RESULT'>, 'message_id': '9312609e-c03f-468d-b2cd-a081615f9d39', 'tool_call_id': 'call_Ou7M7GySQIaE5sXUjwuHMNMG', 'content': 'Error: Function failed.', 'role': 'tool'}
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Encoded as: data: {"type":"TOOL_CALL_RESULT","messageId":"9312609e-c03f-468d-b2cd-a081615f9d39","toolCallId":"call_Ou7M7GySQIaE5sXUjwuHMNMG","content":"Error: Function failed.","role":"tool"}


INFO:agent_framework_ag_ui._orchestrators:[STREAM] Yielding event: MessagesSnapshotEvent
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Event 7: MessagesSnapshotEvent
DEBUG:agent_framework_ag_ui._endpoint:[/api/v1/agents/my-agent] Event payload: {'type': <EventType.MESSAGES_SNAPSHOT: 'MESSAGES_SNAPSHOT'>, 'messages': [{'id': '169459aa-4fe5-4a8c-8878-a2e7cde1c4ad', 'role': 'user', 'content': 'Please tell me the current time.'}, {'id': '56eea2ea-c3a3-4f4e-bb14-086c3933c06a', 'role': 'assistant', 'content': '', 'tool_calls': [{'id': 'call_TkE0DaHk9z9dDWmH6vd9GR5v', 'type': 'function', 'function': {'name': 'get_datetime', 'arguments': '{}'}}]}, {'id': '80275ed4-d631-40ca-a644-3a9d8226ab34', 'role': 'tool', 'content': '[{"type": "text", "text": "2025/12/01 12:00:00"}]', 'tool_call_id': 'call_TkE0DaHk9z9dDWmH6vd9GR5v'}, {'id': '92e185ea-7386-4db4-8a24-2cca17eb2981', 'role': 'assistant', 'content': 'The current time is 2025-12-01 12:00:00 (no timezone was provided). Would you like this converted to a specific timezone or to your local time?'}, {'id': '7c9d2365-9558-41fc-a54c-18ff47d2f827', 'role': 'user', 'content': 'Please tell me the weather.'}, {'id': '8f4affc7-6def-490b-a961-a1fff20ee657', 'role': 'assistant', 'tool_calls': [{'id': 'call_Ou7M7GySQIaE5sXUjwuHMNMG', 'type': 'function', 'function': {'name': 'get_weather', 'arguments': '{}'}}]}, {'id': '9312609e-c03f-468d-b2cd-a081615f9d39', 'role': 'tool', 'content': 'Error: Function failed.', 'tool_call_id': 'call_Ou7M7GySQIaE5sXUjwuHMNMG'}]}

Screenshots
Image

Version information
Python 3.13.10
agent-framework-ag-ui==1.0.0b260114
agent-framework-core==1.0.0b260114
fastmcp==2.14.3

Thank you in advance.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions