delete_file
Delete a file after policy checks and optional backup. Ensures safety by blocking dangerous operations and creating backups before deletion.
Instructions
Delete a single file after policy checks and optional pre-delete backup.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| ctx | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/tools/file_tools.py:235-282 (handler)Main handler function for 'delete_file' tool. Resolves path, checks policy, verifies file exists and is not a directory, optionally creates backup, then calls os.remove() to delete the file.
def delete_file(path: str, ctx: Context | None = None) -> str: """Delete a single file after policy checks and optional pre-delete backup.""" context_tokens = activate_runtime_context(ctx) path = str(pathlib.Path(WORKSPACE_ROOT) / path) if not os.path.isabs(path) else path try: refresh_policy_if_changed() path_check = check_path_policy(path, tool="delete_file") if path_check: result = PolicyResult(allowed=False, reason=path_check[0], decision_tier="blocked", matched_rule=path_check[1]) else: result = PolicyResult(allowed=True, reason="allowed", decision_tier="allowed", matched_rule=None) if result.allowed: if not os.path.exists(path): append_log_entry(build_log_entry("delete_file", result, path=path, error="file not found")) return f"Error: file not found: {path}" if os.path.isdir(path): result = PolicyResult( allowed=False, reason=f"'{path}' is a directory - delete_file only removes individual files. Use execute_command for directory operations (note: bulk/recursive deletions are also subject to policy).", decision_tier="blocked", matched_rule=None, ) log_entry = build_log_entry("delete_file", result, path=path) if not result.allowed: append_log_entry(log_entry) return f"[POLICY BLOCK] {result.reason}" backup_enabled = bool(POLICY.get("audit", {}).get("backup_enabled", True)) backup_location = backup_paths([path]) if backup_enabled else "" if backup_location: log_entry["backup_location"] = backup_location append_log_entry(log_entry) try: os.remove(path) except OSError as e: return f"Error deleting file: {e}" return f"Successfully deleted {path}. " + ( f"Backup saved to {backup_location} - the file can be recovered from there." if backup_location else "No content-change backup was needed." ) - src/server.py:21-31 (registration)Registration: delete_file is imported and registered as an MCP tool via mcp.tool()(delete_file) in the server entrypoint.
for tool in [ server_info, restore_backup, execute_command, read_file, write_file, edit_file, delete_file, list_directory, ]: mcp.tool()(tool) - src/tools/__init__.py:1-14 (registration)Re-export: delete_file is imported from .file_tools and included in __all__ so it can be imported from the tools package.
from .command_tools import execute_command, server_info from .file_tools import delete_file, edit_file, list_directory, read_file, write_file from .restore_tools import restore_backup __all__ = [ "server_info", "execute_command", "read_file", "write_file", "edit_file", "delete_file", "list_directory", "restore_backup", ] - src/mcp_config_manager.py:30-39 (registration)Tool listing: 'delete_file' is listed in AIRG_MCP_TOOLS configuration constant for MCP config management.
AIRG_MCP_TOOLS = [ "server_info", "restore_backup", "execute_command", "read_file", "write_file", "edit_file", "delete_file", "list_directory", ] - src/airg_hook.py:11-18 (helper)Hook redirect: The 'Delete' action is redirected to the MCP tool 'mcp__ai-runtime-guard__delete_file' for the AIRG hook overlay.
REDIRECTS = { "Bash": "mcp__ai-runtime-guard__execute_command", "Shell": "mcp__ai-runtime-guard__execute_command", "Write": "mcp__ai-runtime-guard__write_file", "Edit": "mcp__ai-runtime-guard__edit_file", "MultiEdit": "mcp__ai-runtime-guard__edit_file", "Delete": "mcp__ai-runtime-guard__delete_file", }