Skip to content

Python: [Bug]: Incorrect exception raised while MCP server is unreachable #5876

Description

Description

In the description of #5687, it mentioned that the error is wrapped in a ToolException after the fix. However, it raised the following error instead in my test: Error: ('Failed to enter context manager.', ExceptionGroup('unhandled errors in a TaskGroup', [ConnectError('All connection attempts failed')]))”.

Code Sample

This is the sample code example as #5667.

import asyncio
import os

import dotenv
from agent_framework import Agent, MCPStreamableHTTPTool
from agent_framework.openai import OpenAIChatCompletionClient
from httpx import AsyncClient

dotenv.load_dotenv()


async def http_mcp_example():
    """Example using an HTTP-based MCP server."""
    client = OpenAIChatCompletionClient(
        azure_endpoint=os.environ["AI_PLATFORM_ENDPOINT"],
        api_key=os.environ["AI_PLATFORM_API_KEY"],
        model="gpt-5.4-2026-03-05",
    )
    try:
        async with (
            MCPStreamableHTTPTool(
                name="Microsoft Learn MCP",
                url="https://127.0.0.1:12345/mcp",  # Simulate downed MCP server.
                http_client=AsyncClient(verify=False),
            ) as mcp_server,
            Agent(
                client=client,
                name="DocsAgent",
                instructions="You help with Microsoft documentation questions.",
            ) as agent,
        ):
            result = await agent.run(
                "How to create an Azure storage account using az cli?", tools=mcp_server
            )
            print(result)
    except Exception as e: # The error is not handled due the bug.
        print(f"Error: {e}")


if __name__ == "__main__":
    asyncio.run(http_mcp_example())

Error Messages / Stack Traces

Error: ('Failed to enter context manager.', ExceptionGroup('unhandled errors in a TaskGroup', [ConnectError('All connection attempts failed')]))

Package Versions

agent-framework: 1.3.0

Python Version

Python: 3.14

Additional Context

No response

Metadata

Metadata

Labels

bugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)pythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions