From 97e82e0c0eee18be064b4c21f14ca1a605cf8668 Mon Sep 17 00:00:00 2001 From: FuQuan233 Date: Mon, 29 Jun 2026 11:39:55 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E8=B0=83=E6=95=B4=20MCP=20HTTP?= =?UTF-8?q?=20=E8=87=AA=E5=8A=A8=E6=A3=80=E6=B5=8B=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E6=84=8F=E5=A4=96=E5=8D=A1=E4=BD=8F?= =?UTF-8?q?=20#28?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_llmchat/mcpclient.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/nonebot_plugin_llmchat/mcpclient.py b/nonebot_plugin_llmchat/mcpclient.py index 578ec77..40df705 100644 --- a/nonebot_plugin_llmchat/mcpclient.py +++ b/nonebot_plugin_llmchat/mcpclient.py @@ -106,26 +106,28 @@ class MCPClient: sse_client(url=config.url, headers=config.headers) ) else: - # 未指定协议,自动探测:先尝试 streamable_http,失败则回退到 sse + # 未指定协议,自动探测:先尝试 sse,失败则回退到 streamable_http + # (sse 服务器对 streamable_http 的 POST 请求会卡住,反之 sse 连 streamable_http 服务器会快速返回 405) logger.debug(f"服务器[{server_name}]未指定传输协议,开始自动探测") probe_stack = AsyncExitStack() try: - http_client = await probe_stack.enter_async_context( - httpx.AsyncClient(headers=config.headers or {}) - ) - read, write, _ = await probe_stack.enter_async_context( - streamable_http_client(url=config.url, http_client=http_client) + read, write = await probe_stack.enter_async_context( + sse_client(url=config.url, headers=config.headers) ) await session_stack.enter_async_context(probe_stack) transport = (read, write) - logger.debug(f"服务器[{server_name}]自动探测成功: 使用 streamable_http 传输协议") + logger.debug(f"服务器[{server_name}]自动探测成功: 使用 sse 传输协议") except Exception as e: await probe_stack.aclose() - logger.debug(f"服务器[{server_name}]streamable_http 探测失败({e}),回退到 sse") - transport = await session_stack.enter_async_context( - sse_client(url=config.url, headers=config.headers) + logger.debug(f"服务器[{server_name}]sse 探测失败({e}),回退到 streamable_http") + http_client = await session_stack.enter_async_context( + httpx.AsyncClient(headers=config.headers or {}) ) - logger.debug(f"服务器[{server_name}]自动探测成功: 使用 sse 传输协议") + read, write, _ = await session_stack.enter_async_context( + streamable_http_client(url=config.url, http_client=http_client) + ) + transport = (read, write) + logger.debug(f"服务器[{server_name}]自动探测成功: 使用 streamable_http 传输协议") elif config.command: stdio_params: dict[str, Any] = { "command": config.command,