modelcontextprotocol / modelcontextprotocol/python-sdk
Lazy HTTP connections seem to make error handling difficult.
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
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)
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 streamablehttp_client 入口點及其 anyio.create_task_group 設定開始;使用 asyncio.timeout 和 timeout=10 重現無法連線網域的範例。追蹤 httpx.ConnectTimeout 相對於 session.initialize() 和周圍的內容管理器何時顯現,然後將完成定義為所示案例中錯誤已修正或已記錄文件的時間點。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- networking
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 30/100