check_file_exists
Verify if a file exists at a specified path to prevent errors in automated workflows. This tool confirms file presence before proceeding with operations.
Instructions
检查文件是否存在。参数: filepath (文件路径)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes |
Implementation Reference
- Main implementation of the check_file_exists tool. Decorated with @tool for registration (lines 175-178), the handler function (lines 179-210) validates the filepath, checks if the file exists using Path, and returns JSON-formatted results with file metadata (exists, is_file, size, absolute path).
@tool( name="check_file_exists", description="检查文件是否存在。参数: filepath (文件路径)" ) def check_file_exists(filepath: str) -> str: """ 检查文件是否存在 Args: filepath: 文件路径 Returns: JSON 格式的检查结果 """ if not filepath: return format_result(False, "文件路径不能为空") try: path = Path(filepath) exists = path.exists() is_file = path.is_file() if exists else False size = path.stat().st_size if exists and is_file else None return format_result( True, f"文件{'存在' if exists else '不存在'}", { "exists": exists, "is_file": is_file, "size": size, "path": str(path.absolute()) } ) except Exception as e: logger.error(f"检查文件失败: {e}") return format_result(False, f"检查文件失败: {e}") - src/wxauto_mcp/wxauto_mcp/api_files.py:175-178 (registration)Tool registration using the @tool decorator. Registers the function with name 'check_file_exists' and provides a description: '检查文件是否存在。参数: filepath (文件路径)'
@tool( name="check_file_exists", description="检查文件是否存在。参数: filepath (文件路径)" ) - format_result helper function used by check_file_exists to format the operation result as a JSON string with success status, message, and optional data fields.
def format_result(success: bool, message: str, data: Optional[dict[str, Any]] = None) -> str: """ 格式化操作结果为 JSON 字符串 Args: success: 操作是否成功 message: 结果消息 data: 附加数据 Returns: JSON 格式的结果字符串 """ result = { "success": success, "message": message, "data": data or {}, } return json.dumps(result, ensure_ascii=False, indent=2) - src/wxauto_mcp/wxauto_mcp/rpc.py:51-80 (registration)The @tool decorator implementation that registers functions as MCP tools. Applies output limiting, registers the tool in TOOL_REGISTRY, and returns the original function unmodified.
def tool(name: Optional[str] = None, description: Optional[str] = None) -> Callable: """ 工具装饰器 注册函数为 MCP 工具,自动应用输出限制。 Args: name: 工具名称,默认使用函数名 description: 工具描述 Returns: 装饰后的函数 """ def decorator(func: Callable) -> Callable: # 应用输出限制 limited_func = limit_output(func) # 注册到工具注册表 tool_name = name or func.__name__ TOOL_REGISTRY.register( name=tool_name, func=limited_func, description=description or func.__doc__ or "", ) # 返回原函数,不修改其行为 return func return decorator - limit_output decorator applied to all tools via @tool to limit output size to MAX_OUTPUT_SIZE (50000 characters) to prevent returning excessive data.
def limit_output(func: Callable) -> Callable: """ 输出限制装饰器 限制工具输出的最大长度,防止返回过大的数据。 Args: func: 被装饰的函数 Returns: 包装后的函数 """ @wraps(func) def wrapper(*args: Any, **kwargs: Any) -> str: result = func(*args, **kwargs) if isinstance(result, str) and len(result) > MAX_OUTPUT_SIZE: logger.warning( f"{func.__name__} 输出超过限制 ({len(result)} > {MAX_OUTPUT_SIZE}),已截断" ) return result[:MAX_OUTPUT_SIZE] + f"\n... (输出已截断,原始大小: {len(result)} 字符)" return result return wrapper