mcp-system-fix
This commit is contained in:
@@ -179,7 +179,7 @@ def create_system_mcp() -> FastMCP:
|
|||||||
|
|
||||||
@system_mcp.tool()
|
@system_mcp.tool()
|
||||||
async def write_file(
|
async def write_file(
|
||||||
ctx: Context[ServerSession, dict],
|
ctx: Context[ServerSession, SharedAppContext],
|
||||||
file_path: str,
|
file_path: str,
|
||||||
content: str,
|
content: str,
|
||||||
encoding: str = "utf-8",
|
encoding: str = "utf-8",
|
||||||
@@ -189,9 +189,9 @@ def create_system_mcp() -> FastMCP:
|
|||||||
写入远程沙箱文件(默认按需创建父目录)。
|
写入远程沙箱文件(默认按需创建父目录)。
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
shared_ctx: SharedAppContext = ctx.request_context.lifespan_context["shared_context"]
|
app_ctx = ctx.request_context.lifespan_context # 类型化的 SharedAppContext
|
||||||
conn = shared_ctx.ssh_connection
|
conn = app_ctx.ssh_connection
|
||||||
sandbox_root = shared_ctx.sandbox_path
|
sandbox_root = app_ctx.sandbox_path
|
||||||
|
|
||||||
target = _safe_join(sandbox_root, file_path)
|
target = _safe_join(sandbox_root, file_path)
|
||||||
await ctx.info(f"写入文件:{target}")
|
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)
|
await conn.run(f"mkdir -p {shell_quote(parent)}", check=True)
|
||||||
|
|
||||||
data = content.encode(encoding)
|
data = content.encode(encoding)
|
||||||
|
|
||||||
async with conn.start_sftp_client() as sftp:
|
async with conn.start_sftp_client() as sftp:
|
||||||
async with sftp.open(target, "wb") as f:
|
async with sftp.open(target, "wb") as f:
|
||||||
await f.write(data)
|
await f.write(data)
|
||||||
@@ -261,14 +260,18 @@ def create_system_mcp() -> FastMCP:
|
|||||||
return f"错误: {e}"
|
return f"错误: {e}"
|
||||||
|
|
||||||
@system_mcp.tool()
|
@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:
|
try:
|
||||||
shared_ctx: SharedAppContext = ctx.request_context.lifespan_context["shared_context"]
|
app_ctx = ctx.request_context.lifespan_context # 类型化的 SharedAppContext
|
||||||
conn = shared_ctx.ssh_connection
|
conn = app_ctx.ssh_connection
|
||||||
sandbox_root = shared_ctx.sandbox_path
|
sandbox_root = app_ctx.sandbox_path
|
||||||
|
|
||||||
target = _safe_join(sandbox_root, dir_path)
|
target = _safe_join(sandbox_root, dir_path)
|
||||||
|
|
||||||
@@ -284,7 +287,6 @@ def create_system_mcp() -> FastMCP:
|
|||||||
msg = f"删除失败:目录不存在 '{dir_path}' 或非空"
|
msg = f"删除失败:目录不存在 '{dir_path}' 或非空"
|
||||||
await ctx.error(msg)
|
await ctx.error(msg)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await ctx.error(f"删除目录失败: {e}")
|
await ctx.error(f"删除目录失败: {e}")
|
||||||
return f"错误: {e}"
|
return f"错误: {e}"
|
||||||
|
|||||||
Reference in New Issue
Block a user