From 8047f82bfde5cf9eefeaf918d2873cd2fdf04907 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 24 Mar 2025 23:07:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88perf:=20=E4=BC=98=E5=8C=96=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=8F=92=E4=BB=B6=E7=9B=AE=E5=BD=95=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8=E7=BB=86?= =?UTF-8?q?=E8=8A=82=EF=BC=9B=E5=AE=8C=E5=96=84=20mcp=20=E6=9C=AA=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E6=97=B6=E7=9A=84=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/provider/func_tool_manager.py | 10 ++++++---- astrbot/core/star/star_manager.py | 6 ++++-- astrbot/core/utils/io.py | 10 +++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/astrbot/core/provider/func_tool_manager.py b/astrbot/core/provider/func_tool_manager.py index 52f31c363..55c53eb3a 100644 --- a/astrbot/core/provider/func_tool_manager.py +++ b/astrbot/core/provider/func_tool_manager.py @@ -3,17 +3,19 @@ import json import textwrap import os import asyncio -import mcp import copy from typing import Dict, List, Awaitable, Literal, Any from dataclasses import dataclass from typing import Optional from contextlib import AsyncExitStack - -from mcp.client.stdio import stdio_client from astrbot import logger +try: + import mcp +except (ModuleNotFoundError, ImportError): + logger.warning("警告: 缺少依赖库 'mcp',将无法使用 MCP 服务。") + DEFAULT_MCP_CONFIG = {"mcpServers": {}} SUPPORTED_TYPES = [ @@ -99,7 +101,7 @@ class MCPClient: ) stdio_transport = await self.exit_stack.enter_async_context( - stdio_client(server_params) + mcp.stdio_client(server_params) ) self.stdio, self.write = stdio_transport self.session = await self.exit_stack.enter_async_context( diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 347bc13ef..c49027268 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -471,9 +471,11 @@ class PluginManager: # 从 star_registry 和 star_map 中删除 await self._unbind_plugin(plugin_name, plugin.module_path) - if not remove_dir(os.path.join(ppath, root_dir_name)): + try: + remove_dir(os.path.join(ppath, root_dir_name)) + except Exception as e: raise Exception( - "移除插件成功,但是删除插件文件夹失败。您可以手动删除该文件夹,位于 addons/plugins/ 下。" + f"移除插件成功,但是删除插件文件夹失败: {str(e)}。您可以手动删除该文件夹,位于 addons/plugins/ 下。" ) async def _unbind_plugin(self, plugin_name: str, plugin_module_path: str): diff --git a/astrbot/core/utils/io.py b/astrbot/core/utils/io.py index 7393cb424..c996cadbd 100644 --- a/astrbot/core/utils/io.py +++ b/astrbot/core/utils/io.py @@ -20,24 +20,20 @@ def on_error(func, path, exc_info): """ a callback of the rmtree function. """ - print(f"remove {path} failed.") import stat if not os.access(path, os.W_OK): os.chmod(path, stat.S_IWUSR) func(path) else: - raise + raise exc_info[1] def remove_dir(file_path) -> bool: if not os.path.exists(file_path): return True - try: - shutil.rmtree(file_path, onerror=on_error) - return True - except BaseException: - return False + shutil.rmtree(file_path, onerror=on_error) + return True def port_checker(port: int, host: str = "localhost"):