From fc7716507ab899a226b1f886728016668e892537 Mon Sep 17 00:00:00 2001 From: koko <1429659362@qq.com> Date: Wed, 15 Oct 2025 16:19:41 +0800 Subject: [PATCH] mcp-system-fix --- mcp/system_tools.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mcp/system_tools.py b/mcp/system_tools.py index a9c6746..36f67a5 100644 --- a/mcp/system_tools.py +++ b/mcp/system_tools.py @@ -179,7 +179,7 @@ def create_system_mcp() -> FastMCP: @system_mcp.tool() async def write_file( - ctx: Context[ServerSession, dict], + ctx: Context[ServerSession, SharedAppContext], file_path: str, content: str, encoding: str = "utf-8", @@ -189,9 +189,9 @@ def create_system_mcp() -> FastMCP: 写入远程沙箱文件(默认按需创建父目录)。 """ try: - shared_ctx: SharedAppContext = ctx.request_context.lifespan_context["shared_context"] - conn = shared_ctx.ssh_connection - sandbox_root = shared_ctx.sandbox_path + app_ctx = ctx.request_context.lifespan_context # 类型化的 SharedAppContext + conn = app_ctx.ssh_connection + sandbox_root = app_ctx.sandbox_path target = _safe_join(sandbox_root, file_path) await ctx.info(f"写入文件:{target}") @@ -202,7 +202,6 @@ def create_system_mcp() -> FastMCP: await conn.run(f"mkdir -p {shell_quote(parent)}", check=True) data = content.encode(encoding) - async with conn.start_sftp_client() as sftp: async with sftp.open(target, "wb") as f: await f.write(data) @@ -261,14 +260,18 @@ def create_system_mcp() -> FastMCP: return f"错误: {e}" @system_mcp.tool() - async def delete_dir(ctx: Context[ServerSession, dict], dir_path: str, recursive: bool = False) -> str: + async def delete_dir( + ctx: Context[ServerSession, SharedAppContext], + dir_path: str, + recursive: bool = False, + ) -> str: """ 删除目录。 """ try: - shared_ctx: SharedAppContext = ctx.request_context.lifespan_context["shared_context"] - conn = shared_ctx.ssh_connection - sandbox_root = shared_ctx.sandbox_path + app_ctx = ctx.request_context.lifespan_context # 类型化的 SharedAppContext + conn = app_ctx.ssh_connection + sandbox_root = app_ctx.sandbox_path target = _safe_join(sandbox_root, dir_path) @@ -284,7 +287,6 @@ def create_system_mcp() -> FastMCP: msg = f"删除失败:目录不存在 '{dir_path}' 或非空" await ctx.error(msg) return msg - except Exception as e: await ctx.error(f"删除目录失败: {e}") return f"错误: {e}"