Skip to main content
Glama
Preston-Harrison

Filesystem MCP Server

edit_file

Apply multiple text replacements to files and view unified diffs of changes made. Specify file path and edit operations to modify UTF-8 text files within allowed directories.

Instructions

Apply multiple text replacements to a file and return a unified diff.

Args: path (str): File path to edit (absolute or relative to allowed directories) edits (List[Dict[str, str]]): List of edit operations, each with 'oldText' and 'newText' keys

Returns: str: Unified diff showing changes made, or error message if failed

Note: - Path must be within allowed directory roots - File must be a UTF-8 text file - Edits are applied sequentially in the order provided - Each 'oldText' must match exactly (first occurrence is replaced) - Returns unified diff format showing before/after changes - File is atomically updated using temporary file - If no changes made, returns 'No changes made.'

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
editsYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • main.py:569-622 (handler)
    The core handler implementation for the 'edit_file' MCP tool. It is registered via @mcp.tool decorator (conditionally if not in read-only mode). Applies sequential string replacements specified in the 'edits' list to the target file, generates and returns a unified diff, and atomically updates the file using a temporary file.
    @mcp.tool
    def edit_file(path: str, edits: List[Dict[str, str]]) -> str:
        """Apply multiple text replacements to a file and return a unified diff.
    
        Args:
            path (str): File path to edit (absolute or relative to allowed directories)
            edits (List[Dict[str, str]]): List of edit operations, each with 'oldText' and 'newText' keys
    
        Returns:
            str: Unified diff showing changes made, or error message if failed
    
        Note:
            - Path must be within allowed directory roots
            - File must be a UTF-8 text file
            - Edits are applied sequentially in the order provided
            - Each 'oldText' must match exactly (first occurrence is replaced)
            - Returns unified diff format showing before/after changes
            - File is atomically updated using temporary file
            - If no changes made, returns 'No changes made.'
        """
        try:
            rp = _resolve(path)
            if not _is_text(rp):
                return (
                    f"Error editing file: '{rp}' is not a UTF-8 text file or is binary"
                )
            original = rp.read_text(encoding="utf-8")
            modified = original
            for i, edit in enumerate(edits):
                old = edit.get("oldText", "")
                new = edit.get("newText", "")
                if old not in modified:
                    return f"Error editing file: could not find text to replace in edit {i + 1}. Make sure the text matches exactly:\n{old}"
                modified = modified.replace(old, new, 1)
    
            if modified == original:
                return "No changes made."
    
            diff = "\n".join(
                difflib.unified_diff(
                    original.splitlines(),
                    modified.splitlines(),
                    fromfile=str(rp),
                    tofile=str(rp),
                    lineterm="",
                )
            )
            tmp = rp.with_suffix(rp.suffix + ".tmp")
            tmp.write_text(modified, encoding="utf-8")
            tmp.replace(rp)
            return diff
        except Exception as e:
            return _human_error(e, "editing file")
  • main.py:569-569 (registration)
    The @mcp.tool decorator registers the edit_file function with the FastMCP server, making it available as an MCP tool.
    @mcp.tool
  • The function signature and docstring define the input schema: path (str), edits (List[Dict with 'oldText' and 'newText']), and output as str (diff or error message). FastMCP uses this for tool schema.
    def edit_file(path: str, edits: List[Dict[str, str]]) -> str:
        """Apply multiple text replacements to a file and return a unified diff.
    
        Args:
            path (str): File path to edit (absolute or relative to allowed directories)
            edits (List[Dict[str, str]]): List of edit operations, each with 'oldText' and 'newText' keys
    
        Returns:
            str: Unified diff showing changes made, or error message if failed
    
        Note:
            - Path must be within allowed directory roots
            - File must be a UTF-8 text file
            - Edits are applied sequentially in the order provided
            - Each 'oldText' must match exactly (first occurrence is replaced)
            - Returns unified diff format showing before/after changes
            - File is atomically updated using temporary file
            - If no changes made, returns 'No changes made.'
        """
Behavior5/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It thoroughly explains key behaviors: sequential application of edits, exact matching of 'oldText', atomic updates via temporary file, and the return format (unified diff or error message). This covers critical operational details beyond basic functionality.

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

Conciseness5/5

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

The description is well-structured with clear sections (Args, Returns, Note), front-loads the core purpose, and every sentence adds value—no wasted words. It efficiently conveys necessary information without redundancy.

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

Completeness5/5

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

Given the tool's complexity (file editing with multiple parameters), no annotations, and an output schema present, the description is complete. It covers purpose, usage, parameters, behavior, and output expectations, leaving no significant gaps for the agent to operate effectively.

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

Parameters5/5

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

Schema description coverage is 0%, so the description must fully compensate. It clearly explains both parameters: 'path' as the file path with directory constraints, and 'edits' as a list of operations with 'oldText' and 'newText' keys, including details on matching and ordering. This adds essential meaning beyond the bare schema.

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 specific action ('Apply multiple text replacements to a file') and the resource ('a file'), distinguishing it from siblings like create_file (creation), delete_file (deletion), or read_text_file (reading). It precisely defines the tool's function beyond just the name '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 Guidelines4/5

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

The description provides clear context for when to use this tool (for applying text replacements to files) and includes notes on prerequisites (path restrictions, UTF-8 requirement). However, it does not explicitly state when NOT to use it or name alternatives (e.g., using create_file for new files instead), which prevents a perfect score.

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/Preston-Harrison/fs-mcp-py'

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