modelcontextprotocol / modelcontextprotocol/python-sdk

Lazy HTTP connections seem to make error handling difficult.

Aperta
#1,393 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

enhancement needs decision P2
Lingua principale
Python
Stelle
24.3k
Fork
4k
Merge medio
1g 1h
PR unite (30g)
31

Descrizione

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
import asyncio
from langchain_mcp_adapters.tools import load_mcp_tools
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

async def _init_and_load(session: ClientSession):
    try:
        async with asyncio.timeout(10): # << I cannot access the server and will time out.
            await session.initialize()
            return await load_mcp_tools(session)
    except Exception as e:
        return []

async def main():
    async with streamablehttp_client(url="https://some.unreachable.domain") as client:
        async with ClientSession(client[0], client[1]) as session:
            tools = await _init_and_load(session)
            await asyncio.sleep(100) # << A httpx.ConnectTimeout exception occurred while performing the following operation!!
import asyncio
from langchain_mcp_adapters.tools import load_mcp_tools
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

async def _init_and_load(session: ClientSession):
    try:
        await session.initialize()
        return await load_mcp_tools(session)
    except Exception as e: # << Since the error is caused by tg, this cannot handle the error too!!
        return []

async def main():
    async with streamablehttp_client(url="https://some.unreachable.domain", timeout=10) as client:
        async with ClientSession(client[0], client[1]) as session:
            tools = await _init_and_load(session)
            await asyncio.sleep(100)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal punto di ingresso streamablehttp_client e dalla sua configurazione di anyio.create_task_group; riproduci gli esempi di domini irraggiungibili con asyncio.timeout e timeout=10. Traccia quando httpx.ConnectTimeout viene esposto rispetto a session.initialize() e al context manager circostante, quindi definisci il completamento come il momento dell’errore corretto o documentato per i casi mostrati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
networking
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.