Skip to main content
Glama

read_file

Reads file contents from specified paths with policy enforcement for AI agents, ensuring actions stay within configured boundaries.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
ctxNo

Implementation Reference

  • The read_file tool implementation, which includes policy checks, size validation, logging, and file reading.
    def read_file(path: str, ctx: Context | None = None) -> str:
        context_tokens = activate_runtime_context(ctx)
        path = str(pathlib.Path(WORKSPACE_ROOT) / path) if not os.path.isabs(path) else path
    
        try:
            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)
    
            if result.allowed:
                max_mb = POLICY.get("allowed", {}).get("max_file_size_mb", 10)
                try:
                    size_mb = os.path.getsize(path) / (1024 * 1024)
                    if size_mb > max_mb:
                        result = PolicyResult(
                            allowed=False,
                            reason=f"File size {size_mb:.1f} MB exceeds the policy limit of {max_mb} MB (allowed.max_file_size_mb)",
                            decision_tier="blocked",
                            matched_rule="allowed.max_file_size_mb",
                        )
                except (FileNotFoundError, OSError):
                    pass
    
            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)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jimmyracheta/ai-runtime-guard'

If you have feedback or need assistance with the MCP directory API, please join our Discord server