read_file
Enforce path-policy rules to read a text file from the workspace, ensuring only allowed files are accessed.
Instructions
Read a text file from the workspace after path-policy enforcement.
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:20-45 (handler)The core read_file function that enforces path policy, logs the action, and reads the file contents.
def read_file(path: str, ctx: Context | None = None) -> str: """Read a text file from the workspace after path-policy enforcement.""" 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="read_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) append_log_entry(build_log_entry("read_file", result, path=path)) if not result.allowed: return f"[POLICY BLOCK] {result.reason}" try: with open(path, "r", errors="replace") as f: return f.read() except FileNotFoundError: return f"Error: file not found: {path}" except OSError as e: return f"Error reading file: {e}" finally: reset_runtime_context(context_tokens) - src/server.py:21-31 (registration)Registers read_file as an MCP tool by passing it to mcp.tool().
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)Exports read_file in the public tools package __all__.
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 (helper)Lists read_file as one of the AIRG MCP tools in the config manager.
AIRG_MCP_TOOLS = [ "server_info", "restore_backup", "execute_command", "read_file", "write_file", "edit_file", "delete_file", "list_directory", ] - src/airg_hook.py:345-375 (helper)AIRG hook handling 'beforereadfile' event for native Read tool interception, redirecting users to use mcp__ai-runtime-guard__read_file.
if hook_event == "beforereadfile": file_path = str(payload.get("file_path", "")).strip() lowered = file_path.lower() violated = _blocked_by_policy(file_path) if file_path else None if not violated and lowered: if any(lowered.endswith(suffix) for suffix in SENSITIVE_READ_SUFFIXES) or "/secrets/" in lowered: violated = "sensitive read target" if violated: reason = f"AIRG policy: read blocked ({violated}). Use AIRG MCP tools within allowed path policy." _append_log( _build_activity_entry( payload=payload, tool_name="Read", allowed=False, event="hook_before_read_blocked", hook_reason=reason, hook_detail=file_path, ) ) return _emit_deny(reason, hook_event_name="beforeReadFile") _append_log( _build_activity_entry( payload=payload, tool_name="Read", allowed=True, event="hook_before_read_allowed", hook_reason="read_allowed", hook_detail=file_path, ) ) return _allow()