From 5014d3014bb38d36df7851d091a74111b9ee837c Mon Sep 17 00:00:00 2001 From: FuQuan233 Date: Wed, 20 Aug 2025 11:40:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8Dmcp=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E5=8D=A1=E4=BD=8F=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E5=8D=A1=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_llmchat/__init__.py | 2 +- nonebot_plugin_llmchat/mcpclient.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/nonebot_plugin_llmchat/__init__.py b/nonebot_plugin_llmchat/__init__.py index c9dda84..d0d5857 100755 --- a/nonebot_plugin_llmchat/__init__.py +++ b/nonebot_plugin_llmchat/__init__.py @@ -380,7 +380,7 @@ async def process_messages(group_id: int): new_messages.append({ "role": "tool", "tool_call_id": tool_call.id, - "content": str(result.content) + "content": str(result) }) # 将工具调用的结果交给 LLM diff --git a/nonebot_plugin_llmchat/mcpclient.py b/nonebot_plugin_llmchat/mcpclient.py index 7031d34..9ed9fe0 100644 --- a/nonebot_plugin_llmchat/mcpclient.py +++ b/nonebot_plugin_llmchat/mcpclient.py @@ -1,4 +1,5 @@ from contextlib import AsyncExitStack +import asyncio from mcp import ClientSession, StdioServerParameters from mcp.client.sse import sse_client @@ -64,9 +65,15 @@ class MCPClient: server_name, real_tool_name = tool_name.split("___") logger.info(f"正在服务器[{server_name}]上调用工具[{real_tool_name}]") session = self.sessions[server_name] - response = await session.call_tool(real_tool_name, tool_args) + try: + response = await asyncio.wait_for( + session.call_tool(real_tool_name, tool_args), timeout=10 + ) + except asyncio.TimeoutError: + logger.error(f"调用工具[{real_tool_name}]超时") + return f"调用工具[{real_tool_name}]超时" logger.debug(f"工具[{real_tool_name}]调用完成,响应: {response}") - return response + return response.content def get_friendly_name(self, tool_name: str): server_name, real_tool_name = tool_name.split("___")