Skip to content

Lazy HTTP connections seem to make error handling difficult. #1393

Description

@caffeinism

Question

When using streamablehttp_client, since HTTP connections are initialized with anyio.create_task_group, if the server is unreachable, an error will occur unexpectedly at some point later. Is this the intended behavior? If the connection is impossible, I want to wait and handle the error. However, the current behavior executes the code after the initialization and then throws an error at some point. If the error had occurred earlier, the code below it wouldn't have needed to execute. I consider this excessive optimization. Is there a reason for this design?

Additional Context

importasynciofromlangchain_mcp_adapters.toolsimportload_mcp_toolsfrommcpimportClientSessionfrommcp.client.streamable_httpimportstreamablehttp_clientasyncdef_init_and_load(session: ClientSession):
try:
asyncwithasyncio.timeout(10): # << I cannot access the server and will time out.awaitsession.initialize()
returnawaitload_mcp_tools(session)
exceptExceptionase:
return []
asyncdefmain():
asyncwithstreamablehttp_client(url="https://some.unreachable.domain") asclient:
asyncwithClientSession(client[0], client[1]) assession:
tools=await_init_and_load(session)
awaitasyncio.sleep(100) # << A httpx.ConnectTimeout exception occurred while performing the following operation!!
importasynciofromlangchain_mcp_adapters.toolsimportload_mcp_toolsfrommcpimportClientSessionfrommcp.client.streamable_httpimportstreamablehttp_clientasyncdef_init_and_load(session: ClientSession):
try:
awaitsession.initialize()
returnawaitload_mcp_tools(session)
exceptExceptionase: # << Since the error is caused by tg, this cannot handle the error too!!return []
asyncdefmain():
asyncwithstreamablehttp_client(url="https://some.unreachable.domain", timeout=10) asclient:
asyncwithClientSession(client[0], client[1]) assession:
tools=await_init_and_load(session)
awaitasyncio.sleep(100)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featureenhancementRequest for a new feature that's not currently supportedneeds decisionIssue is actionable, needs maintainer decision on whether to implement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions