modelcontextprotocol / modelcontextprotocol/python-sdk

Lazy HTTP connections seem to make error handling difficult.

Đang mở
#1,393 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

enhancement needs decision P2
Ngôn ngữ chính
Python
Star
24.3k
Fork
4k
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
31

Mô tả

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)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ entry point streamablehttp_client và phần thiết lập anyio.create_task_group; tái hiện các ví dụ về miền không thể truy cập bằng asyncio.timeout và timeout=10. Theo dõi thời điểm httpx.ConnectTimeout được phát sinh so với session.initialize() và context manager bao quanh, sau đó xác định việc hoàn tất là thời điểm lỗi được sửa hoặc được ghi lại tài liệu cho các trường hợp đã nêu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
networking
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.