mcp-system-fix

This commit is contained in:
2025-10-15 16:19:41 +08:00
parent b11bf2417b
commit fc7716507a

View File

@@ -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}"