From 61607b828c207d38113f65458dcf297f81587a19 Mon Sep 17 00:00:00 2001 From: cui-gaoleng <562344211@qq.com> Date: Wed, 17 Dec 2025 11:13:12 +0800 Subject: [PATCH] =?UTF-8?q?file=5Ftool=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=B9=B6=E5=86=99=E5=85=A5=E5=86=85=E5=AE=B9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mcp_tools/base_tools/file_tool/base.py | 30 ++++++++++++++----- .../base_tools/file_tool/config.json | 4 +-- .../mcp_tools/base_tools/file_tool/tool.py | 15 ++++++++-- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/base.py b/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/base.py index ce5e1aa05..2cc858ff3 100644 --- a/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/base.py +++ b/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/base.py @@ -2,7 +2,7 @@ import logging import os import shutil from enum import Enum -from typing import Dict, List +from typing import Dict, List, Optional from pydantic import Field from config.public.base_config_loader import BaseConfig, LanguageEnum @@ -124,24 +124,38 @@ class FileManager: def add(self, file_path: str, - overwrite: bool = Field(False, description="是否覆盖已存在文件")) -> None: + overwrite: bool = Field(False, description="是否覆盖已存在文件"), + content: Optional[str] = Field(None, description="创建文件时写入的内容,为None则创建空文件"), + encoding: str = Field(FileEncodingEnum.UTF8.value, description="文件编码") + ) -> None: """ - 新建空文件(Python实现touch功能) - :param file_path: 文件路径(必填) + 新建文件(支持创建空文件或写入指定内容,Python实现touch功能) + :param file_path: 文件路径(必填,绝对路径) :param overwrite: 已存在时是否覆盖(默认False) + :param content: 创建文件时写入的内容,为None则创建空文件(新增参数) + :param encoding: 文件编码(默认utf-8,新增参数用于内容写入) """ + # 1. 检查文件是否存在,且不允许覆盖则跳过 if os.path.exists(file_path) and not overwrite: logger.info(self._get_error_msg(f"文件已存在,跳过创建:{file_path}", f"File exists, skip creation: {file_path}")) return - # 确保父目录存在 + # 2. 确保父目录存在 parent_dir = os.path.dirname(file_path) if parent_dir and not os.path.exists(parent_dir): os.makedirs(parent_dir, exist_ok=True) - with open(file_path, "w", encoding=FileEncodingEnum.UTF8.value) as f: - pass - logger.info(self._get_error_msg(f"文件创建成功:{file_path}", f"File created successfully: {file_path}")) + # 3. 根据content参数决定写入内容还是创建空文件 + with open(file_path, "w", encoding=encoding) as f: + if content is not None: + f.write(content) # 写入指定内容 + # content为None时,仅打开文件后关闭(创建空文件) + + # 4. 日志提示(区分空文件和带内容文件) + if content is None: + logger.info(self._get_error_msg(f"空文件创建成功:{file_path}", f"Empty file created successfully: {file_path}")) + else: + logger.info(self._get_error_msg(f"文件创建并写入内容成功:{file_path}", f"File created and content written successfully: {file_path}")) def append(self, file_path: str, diff --git a/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/config.json b/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/config.json index 93845a4db..d9688dac7 100644 --- a/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/config.json +++ b/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/config.json @@ -1,8 +1,8 @@ { "tools": { "file_tool": { - "zh": "【统一文件管理工具】\n功能:支持本地文件/目录的查、增、改、删全操作(纯Python实现,无shell依赖)\n\n【核心提示】\n1. 操作类型(action)必须从以下枚举值中选择:ls/cat/add/append/edit/rename/chmod/delete,不可传入其他值;\n2. 文件编码(encoding)必须从以下枚举值中选择:utf-8/gbk/gb2312/ascii,默认值为utf-8;\n3. 不同操作类型(action)对应不同必填参数,未满足则执行失败,请严格遵守:\n - ls(查看列表)/cat(读取内容)/delete(删除):仅需传入 file_path(文件/目录路径);\n - add(新建文件):需传入 file_path,可选传入 overwrite(是否覆盖已存在文件,默认False);\n - append(追加内容)/edit(覆盖内容):必须传入 file_path + content(写入/追加的内容);\n - rename(重命名):必须传入 file_path(原路径) + new_path(新路径);\n - chmod(修改权限):必须传入 file_path + mode(权限模式,如755/644,需为8进制格式);\n4. 非必填参数默认值:detail(ls是否显示详细信息)=False,recursive(删除目录是否递归)=False;\n5. 路径格式要求:file_path/new_path需传入绝对路径(如\"/tmp/test.txt\"),避免相对路径导致的找不到文件问题。\n\n【枚举类定义(必须遵守)】\n- FileActionEnum(操作类型枚举):ls / cat / add / append / edit / rename / chmod / delete\n- FileEncodingEnum(文件编码枚举):utf-8 / gbk / gb2312 / ascii\n\n【参数详情】\n- action:操作类型(必填,枚举值见上方)\n- file_path:目标文件/目录路径(必填,绝对路径)\n- content:写入/追加内容(add/edit/append操作必填)\n- new_path:重命名后的新路径(rename操作必填,绝对路径)\n- mode:权限模式(chmod操作必填,如755)\n- detail:ls操作是否显示详细信息(默认False)\n- overwrite:add操作是否覆盖已存在文件(默认False)\n- recursive:delete操作是否递归删除目录(默认False)\n- encoding:文件编码(枚举值见上方,默认utf-8)\n【返回值说明】\n- success:执行结果(True=成功,False=失败)\n- message:执行信息/错误提示(多语言)\n- result:操作结果(ls/cat返回结构化列表,其他操作返回空列表)\n- file_path:操作的文件路径(rename后自动更新为新路径)\n- target:执行目标(固定为127.0.0.1,本地执行)", - "en": "【Unified File Management Tool】\nFunction: Supports query/add/edit/delete operations for local files/directories (Python native implementation, no shell dependency)\n\n【Core Guidelines】\n1. Operation type (action) must be selected from the following enum values: ls/cat/add/append/edit/rename/chmod/delete, other values are not allowed;\n2. File encoding (encoding) must be selected from the following enum values: utf-8/gbk/gb2312/ascii, default value is utf-8;\n3. Different action types correspond to different required parameters, execution will fail if not met, please strictly follow:\n - ls (list files)/cat (read content)/delete (delete): Only need to pass file_path (file/directory path);\n - add (create empty file): Need to pass file_path, optionally pass overwrite (whether to overwrite existing file, default False);\n - append (append content)/edit (overwrite content): Must pass file_path + content (content to write/append);\n - rename (rename file/directory): Must pass file_path (old path) + new_path (new path);\n - chmod (modify permission): Must pass file_path + mode (permission mode, e.g.755/644, must be octal format);\n4. Default values for optional parameters: detail (whether to show detailed info for ls)=False, recursive (whether to delete directory recursively)=False;\n5. Path format requirement: file_path/new_path must be absolute path (e.g.\"/tmp/test.txt\"), avoid file not found due to relative path.\n\n【Enum Class Definition (Must Follow)】\n- FileActionEnum (Operation Type Enum): ls / cat / add / append / edit / rename / chmod / delete\n- FileEncodingEnum (File Encoding Enum): utf-8 / gbk / gb2312 / ascii\n\n【Parameter Details】\n- action: Operation type (required, enum values see above)\n- file_path: Target file/directory path (required, absolute path)\n- content: Content to write/append (required for add/edit/append)\n- new_path: New path after rename (required for rename, absolute path)\n- mode: Permission mode (required for chmod, e.g.755)\n- detail: Whether to show detailed info for ls (default False)\n- overwrite: Whether to overwrite existing file for add (default False)\n- recursive: Whether to delete directory recursively for delete (default False)\n- encoding: File encoding (enum values see above, default utf-8)\n- lang: Language (enum values: ZH/EN, default ZH)\n\n【Return Value Explanation】\n- success: Execution result (True=success, False=failure)\n- message: Execution info/error prompt (multilingual)\n- result: Operation result (structured list for ls/cat, empty list for others)\n- file_path: Operated file path (automatically updated to new path after rename)\n- target: Execution target (fixed as 127.0.0.1, local execution)" + "zh": "【统一文件管理工具】\n功能:支持本地文件/目录的查、增、改、删全操作(纯Python实现,无shell依赖),add操作支持创建空文件或写入指定内容\n\n【核心提示】\n1. 操作类型(action)必须从以下枚举值中选择:ls/cat/add/append/edit/rename/chmod/delete,不可传入其他值;\n2. 文件编码(encoding)必须从以下枚举值中选择:utf-8/gbk/gb2312/ascii,默认值为utf-8;\n3. 不同操作类型(action)对应不同必填参数,未满足则执行失败,请严格遵守:\n - ls(查看列表)/cat(读取内容)/delete(删除):仅需传入 file_path(文件/目录路径);\n - add(新建文件):需传入 file_path,可选传入 overwrite(是否覆盖已存在文件,默认False)、content(创建文件时写入的内容,默认None)、encoding(文件编码,默认utf-8);\n - append(追加内容)/edit(覆盖内容):必须传入 file_path + content(写入/追加的内容,不能为空);\n - rename(重命名):必须传入 file_path(原路径) + new_path(新路径);\n - chmod(修改权限):必须传入 file_path + mode(权限模式,如755/644,需为8进制格式);\n4. 非必填参数默认值:detail(ls是否显示详细信息)=False,recursive(删除目录是否递归)=False;\n5. 路径格式要求:file_path/new_path需传入绝对路径(如\"/tmp/test.txt\"),避免相对路径导致的找不到文件问题。\n\n【枚举类定义(必须遵守)】\n- FileActionEnum(操作类型枚举):ls / cat / add / append / edit / rename / chmod / delete\n- FileEncodingEnum(文件编码枚举):utf-8 / gbk / gb2312 / ascii\n\n【参数详情】\n- action:操作类型(必填,枚举值见上方)\n- file_path:目标文件/目录路径(必填,绝对路径)\n- content:写入/追加内容(edit/append操作必填,add操作可选,默认None)\n- new_path:重命名后的新路径(rename操作必填,绝对路径)\n- mode:权限模式(chmod操作必填,如755)\n- detail:ls操作是否显示详细信息(默认False)\n- overwrite:add操作是否覆盖已存在文件(默认False)\n- recursive:delete操作是否递归删除目录(默认False)\n- encoding:文件编码(枚举值见上方,默认utf-8)\n【返回值说明】\n- success:执行结果(True=成功,False=失败)\n- message:执行信息/错误提示(多语言,add操作区分空文件和带内容文件创建)\n- result:操作结果(ls/cat返回结构化列表,其他操作返回空列表)\n- file_path:操作的文件路径(rename后自动更新为新路径)\n- target:执行目标(固定为127.0.0.1,本地执行)", + "en": "【Unified File Management Tool】\nFunction: Supports query/add/edit/delete operations for local files/directories (Python native implementation, no shell dependency), the add operation supports creating empty files or writing specified content\n\n【Core Guidelines】\n1. Operation type (action) must be selected from the following enum values: ls/cat/add/append/edit/rename/chmod/delete, other values are not allowed;\n2. File encoding (encoding) must be selected from the following enum values: utf-8/gbk/gb2312/ascii, default value is utf-8;\n3. Different action types correspond to different required parameters, execution will fail if not met, please strictly follow:\n - ls (list files)/cat (read content)/delete (delete): Only need to pass file_path (file/directory path);\n - add (create file): Need to pass file_path, optionally pass overwrite (whether to overwrite existing file, default False), content (content to write when creating file, default None), encoding (file encoding, default utf-8);\n - append (append content)/edit (overwrite content): Must pass file_path + content (content to write/append, cannot be empty);\n - rename (rename file/directory): Must pass file_path (old path) + new_path (new path);\n - chmod (modify permission): Must pass file_path + mode (permission mode, e.g.755/644, must be octal format);\n4. Default values for optional parameters: detail (whether to show detailed info for ls)=False, recursive (whether to delete directory recursively)=False;\n5. Path format requirement: file_path/new_path must be absolute path (e.g.\"/tmp/test.txt\"), avoid file not found due to relative path.\n\n【Enum Class Definition (Must Follow)】\n- FileActionEnum (Operation Type Enum): ls / cat / add / append / edit / rename / chmod / delete\n- FileEncodingEnum (File Encoding Enum): utf-8 / gbk / gb2312 / ascii\n\n【Parameter Details】\n- action: Operation type (required, enum values see above)\n- file_path: Target file/directory path (required, absolute path)\n- content: Content to write/append (required for edit/append, optional for add, default None)\n- new_path: New path after rename (required for rename, absolute path)\n- mode: Permission mode (required for chmod, e.g.755)\n- detail: Whether to show detailed info for ls (default False)\n- overwrite: Whether to overwrite existing file for add (default False)\n- recursive: Whether to delete directory recursively for delete (default False)\n- encoding: File encoding (enum values see above, default utf-8)\n【Return Value Explanation】\n- success: Execution result (True=success, False=failure)\n- message: Execution info/error prompt (multilingual, the add operation distinguishes between empty file and file with content creation)\n- result: Operation result (structured list for ls/cat, empty list for others)\n- file_path: Operated file path (automatically updated to new path after rename)\n- target: Execution target (fixed as 127.0.0.1, local execution)" } } } \ No newline at end of file diff --git a/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/tool.py b/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/tool.py index 9d1d1c131..b62cfd183 100644 --- a/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/tool.py +++ b/mcp_center/servers/oe_cli_mcp_server/mcp_tools/base_tools/file_tool/tool.py @@ -47,9 +47,20 @@ def file_tool( result["message"] = f"本地文件内容读取完成(路径:{file_path})" if is_zh else f"Local file content read (path: {file_path})" elif action == FileActionEnum.ADD: - fm.add(file_path.strip(), overwrite=overwrite) + add_content = content.strip() if content is not None else None + fm.add( + file_path=file_path.strip(), + overwrite=overwrite, + content=add_content, + encoding=encoding.value # 传递编码(枚举值转字符串) + ) result["success"] = True - result["message"] = f"本地文件创建成功(路径:{file_path})" if is_zh else f"Local file created (path: {file_path})" + # 优化提示:区分是否写入内容 + if add_content: + result["message"] = f"本地文件创建并写入内容成功(路径:{file_path})" if is_zh else f"Local file created and content written (path: {file_path})" + else: + result["message"] = f"本地文件创建成功(路径:{file_path})" if is_zh else f"Local file created (path: {file_path})" + elif action == FileActionEnum.APPEND: if not content: -- Gitee