modelcontextprotocol / modelcontextprotocol/python-sdk

Lazy HTTP connections seem to make error handling difficult.

Aberta
#1,393 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

enhancement needs decision P2
Linguagem predominante
Python
Estrelas
24.3k
Forks
4k
Merge médio
1d 1h
PRs com merge (30d)
31

Descrição

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)

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pelo ponto de entrada streamablehttp_client e pela configuração de anyio.create_task_group; reproduza os exemplos de domínios inacessíveis com asyncio.timeout e timeout=10. Rastreie quando httpx.ConnectTimeout é apresentado em relação a session.initialize() e ao gerenciador de contexto ao redor, e defina a conclusão como o momento do erro corrigido ou documentado para os casos mostrados.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
networking
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Precisa de esclarecimento
Facilidade para iniciantes
30/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.