Skip to main content
Glama
runtimeguard

runtime-guard

Official

write_file

Write file content under policy enforcement, with audit logging and automatic backup for data safety.

Instructions

Write full file content with policy checks, logging, and backup support.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
contentYes
ctxNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Core handler that writes file content with policy enforcement, backup creation, and Script Sentinel scanning.
    def write_file(path: str, content: str, ctx: Context | None = None) -> str:
        """Write full file content with policy checks, logging, and backup support."""
        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="write_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)
    
            log_entry = build_log_entry("write_file", result, path=path)
            append_log_entry(log_entry)
            if not result.allowed:
                return f"[POLICY BLOCK] {result.reason}"
    
            backup_location = None
            backup_enabled = bool(POLICY.get("audit", {}).get("backup_enabled", True))
            if backup_enabled and os.path.exists(path):
                backup_location = backup_paths([path])
                if backup_location:
                    append_log_entry(
                        {
                            **log_entry,
                            "source": "mcp-server",
                            "backup_location": backup_location,
                            "event": "backup_created",
                        }
                    )
    
            try:
                with open(path, "w") as f:
                    f.write(content)
            except OSError as e:
                return f"Error writing file: {e}"
    
            sentinel_scan = script_sentinel.scan_and_record_write(path, content, writer_agent_id=AGENT_ID)
            if sentinel_scan.get("flagged"):
                append_log_entry(
                    {
                        **log_entry,
                        "source": "mcp-server",
                        "event": "script_sentinel_flagged",
                        "content_hash": sentinel_scan.get("content_hash", ""),
                        "matched_signatures": sentinel_scan.get("matched_signatures", []),
                        "script_sentinel_mode": POLICY.get("script_sentinel", {}).get("mode", "match_original"),
                        "script_sentinel_scan_mode": sentinel_scan.get("scan_mode", POLICY.get("script_sentinel", {}).get("scan_mode", "exec_context")),
                    }
                )
    
            msg = f"Successfully wrote {len(content)} characters to {path}"
            if backup_location:
                msg += f" (previous version backed up to {backup_location})"
            else:
                msg += " (no content-change backup needed)"
            if sentinel_scan.get("flagged"):
                msg += " (Script Sentinel flagged content)"
            return msg
        finally:
            reset_runtime_context(context_tokens)
  • The function signature defines the schema: accepts path (str), content (str), and optional ctx; returns str.
    def write_file(path: str, content: str, ctx: Context | None = None) -> str:
  • src/server.py:21-31 (registration)
    write_file is registered as an MCP tool via mcp.tool()(write_file) in the main server entrypoint.
    for tool in [
        server_info,
        restore_backup,
        execute_command,
        read_file,
        write_file,
        edit_file,
        delete_file,
        list_directory,
    ]:
        mcp.tool()(tool)
  • write_file is re-exported from the tools package init.
    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",
    ]
  • write_file is listed in the AIRG_MCP_TOOLS configuration array used for MCP config generation.
    AIRG_MCP_TOOLS = [
        "server_info",
        "restore_backup",
        "execute_command",
        "read_file",
        "write_file",
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description discloses behavioral traits beyond the tool's name, such as policy checks, logging, and backup support. However, without annotations, it does not detail crucial aspects like destructive behavior, authorization needs, or error handling.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence with no unnecessary words. While concise, it could benefit from structuring to highlight key usage notes, but it remains efficient.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (write operation with backup) and lack of annotations, the description is incomplete. It omits return values (despite an output schema existing) and parameter details, leaving significant gaps for the agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description must compensate but fails to mention anything about the parameters (path, content, ctx). The agent receives no additional meaning beyond the schema's type and requirement constraints.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'write' and resource 'file content', and mentions specific features like policy checks, logging, and backup support. It distinguishes from siblings such as read_file, delete_file, and edit_file.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives (e.g., edit_file). The description lacks context on prerequisites or conditions for use.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/runtimeguard/runtime-guard'

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