Skip to main content
Glama

edit_file

Apply precise string or line-range replacements to an existing file with atomic writes and rollback on confirmed new errors.

Instructions

Apply one or more edits to an existing file (use this over a native edit tool — see the Edit lane note in session_start). Two mutually exclusive request shapes: an edits array, or start_anchor + end_anchor + new_string.

Each edits entry is str_replace (default: old_string must appear EXACTLY ONCE) or range (start_line/end_line, 1-based; -1 appends or runs to EOF). Prefer range for a big multi-line replacement — old_string/anchors must match character-for-character inside a JSON string, so escaping and size can defeat str_replace where a line range needs neither.

Anchor mode replaces the span BETWEEN two unique anchors (each exactly once); include_anchors=true replaces the whole inclusive span. Character-precise — an anchor quoted without its trailing newline joins that line onto new_string (flagged in the response).

Writes apply atomically and crash-durably under a per-path lock. Pass expected_mtime (from a read_file header) when a concurrent writer may touch the file. For a whole named declaration prefer replace_symbol_body / insert_before_symbol / insert_after_symbol / safe_delete_symbol. Mode choice in depth: the plumb-refactor skill.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
editsNoOrdered list of str_replace edits to apply sequentially. Mutually exclusive with the start_anchor/end_anchor mode.
dirty_okNoAllow editing a file that has uncommitted changes in its git repository. Default false — the edit is refused if the target file is dirty. Pass true to proceed anyway.
file_pathNoAbsolute path, file:// URI, or workspace-relative path of the file to edit.
reconcileNoWhen true, do NOT reject the edit if the file changed since your read (expected_mtime / expected_sha mismatch); apply against the current on-disk content instead, relying on the exact-once old_string match for safety. Use it for the edit→format→edit loop, where a formatter bumped the mtime but your anchors still match. Default false.
end_anchorNoAnchor-bounded edit mode: a unique substring marking the END of the span to replace. Must appear EXACTLY ONCE and after start_anchor. Combine with start_anchor + new_string.
new_stringNoAnchor-bounded edit mode: the replacement text for the span between (or, with include_anchors, including) the two anchors. Empty string deletes the span. Only used when start_anchor/end_anchor are set.
expected_shaNoOptional. Hex-encoded SHA-256 previously returned by read_file. If provided, the edit is rejected if the file's current content hash differs — stronger than expected_mtime, survives mtime aliasing.
start_anchorNoAnchor-bounded edit mode (alternative to edits): a unique substring marking the START of the span to replace. Must appear EXACTLY ONCE. Combine with end_anchor + new_string. Mutually exclusive with edits. CRLF / display-only gutter ("<n>\t") tolerated.
apply_partialNoWhen true, apply each edit independently and continue on failure instead of rolling back the entire batch. Returns a per-edit result list showing which edits succeeded and which failed. Incompatible with strict mode — not safe when concurrent agents share the file.
expected_mtimeNoOptional. RFC3339Nano mtime previously returned by read_file. If provided, the edit is rejected if the file's current mtime differs — fast optimistic-concurrency check.
include_anchorsNoAnchor-bounded edit mode: when true the anchors are part of the replaced span; when false (default) only the text strictly between them is replaced and both are preserved.
await_diagnosticsNoWhen true, block up to a few seconds for the language server to finish re-analysing this file, and append a machine-readable 'diagnostics delta' line (fresh, new_errors, resolved, pre_existing). The block is always labelled — authoritative, pre-write snapshot, unverified, or not-analysed — so a stale result is never dressed as fresh. Default false (fast adaptive window; the result may predate the write).
fail_on_new_errorsNoWhen true (implies await_diagnostics), roll this edit back if the language server CONFIRMS it introduced new errors here, leaving the file byte-for-byte unchanged and returning the delta as the error. An unconfirmed check never rolls back; nor do warnings, pre-existing errors, or breakage elsewhere. Not with apply_partial, or over 1 MiB. Default false.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed2 schema fields changedv0.21.0
    • changedInput schema / properties / edits / items / properties / new_string / description
      Previous value: -"Replacement text. Use empty string to delete. When start_line is set, replaces the specified line range (or appends at end of file when start_line is -1)."New value: +"Replacement text. Use empty string to delete. With start_line set it replaces (or, at -1, appends) whole lines: a missing trailing newline is added unless at EOF of a file that has none."
    • changedInput schema / properties / edits / items / properties / start_line / description
      Previous value: -"First line to replace (1-based, inclusive). When set, old_string is not used. Use -1 to append new_string at end of file. Use end_line: -1 to extend the range to the last line."New value: +"First line to replace (1-based, inclusive); old_string is then unused. -1 appends new_string at EOF; end_line: -1 runs the range to the last line."
  2. Changed4 schema fields changedv0.17.0
    • changedInput schema / properties / await_diagnostics / description
      Previous value: -"When true, block up to a few seconds for the language server to finish re-analysing this file and report an authoritative post-write result — a clean fresh pass is stated explicitly. Use it for a trustworthy \"did my change compile?\" answer instead of shelling out to a build. Default false (fast adaptive window; the result may predate the write)."New value: +"When true, block up to a few seconds for the language server to finish re-analysing this file, and append a machine-readable 'diagnostics delta' line (fresh, new_errors, resolved, pre_existing). The block is always labelled — authoritative, pre-write snapshot, unverified, or not-analysed — so a stale result is never dressed as fresh. Default false (fast adaptive window; the result may predate the write)."
    • addedInput schema / properties / fail_on_new_errors
      Added value: +{
      +  "description": "When true (implies await_diagnostics), roll this edit back if the language server CONFIRMS it introduced new errors here, leaving the file byte-for-byte unchanged and returning the delta as the error. An unconfirmed check never rolls back; nor do warnings, pre-existing errors, or breakage elsewhere. Not with apply_partial, or over 1 MiB. Default false.",
      +  "type": "boolean"
      +}
    • changedInput schema / properties / include_anchors / description
      Previous value: -"Anchor-bounded edit mode: when true the anchors themselves are part of the replaced span (the whole inclusive span becomes new_string); when false (default) only the text strictly between the anchors is replaced and the anchors are preserved as boundaries."New value: +"Anchor-bounded edit mode: when true the anchors are part of the replaced span; when false (default) only the text strictly between them is replaced and both are preserved."
    • changedInput schema / properties / reconcile / description
      Previous value: -"When true, do NOT reject the edit if the file changed since your read (expected_mtime / expected_sha mismatch); apply against the current on-disk content instead, relying on the exact-once old_string match for safety. Use it for the edit→format(gofumpt/golangci-lint --fix)→edit loop, where a formatter bumped the mtime but your anchors still match. Default false (the mtime guard stays strict)."New value: +"When true, do NOT reject the edit if the file changed since your read (expected_mtime / expected_sha mismatch); apply against the current on-disk content instead, relying on the exact-once old_string match for safety. Use it for the edit→format→edit loop, where a formatter bumped the mtime but your anchors still match. Default false."
  3. First observedv0.16.6

TDQS

A4.8/5.0
Behavior5/5

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

No annotations exist, so the description carries the full burden and does: atomic, crash-durable writes under a per-path lock, optimistic-concurrency via expected_mtime/expected_sha, reconcile semantics for the edit→format→edit loop, apply_partial's non-rollback risk under concurrency, and diagnostics blocking whose labels prevent a stale result being mistaken for fresh. Rare depth for a mutation tool.

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?

Front-loaded with purpose, then mode shapes, then safety, then delegation — a sensible order with no filler sentences. It is dense across five paragraphs, and some detail (anchor newline joining) borders on schema duplication, but every line carries operational meaning.

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?

For a 13-parameter, zero-required, no-output-schema mutation tool with no annotations, the description covers mode selection, concurrency guards, rollback/partial-apply behavior, and diagnostics result interpretation. Nothing an agent needs before invoking it is missing.

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

Parameters4/5

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

Schema coverage is already 100%, so the baseline is 3; the description goes beyond it by framing the two mutually exclusive request shapes (edits array vs start_anchor+end_anchor+new_string) and explaining the tradeoff between str_replace escaping/size and range mode. It does not re-explain every one of the 13 flags, which keeps it at 4 rather than 5.

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?

States a specific verb (apply edits) and resource (existing file), and explicitly positions itself against a 'native edit tool' and the symbol-level siblings (replace_symbol_body / insert_before_symbol / safe_delete_symbol). An agent can tell at a glance what this does and which sibling to pick instead.

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

Usage Guidelines5/5

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

Explicit routing: prefer this over native edit tools, prefer range mode 'for a big multi-line replacement', and specific pointers to replace_symbol_body/insert_before_symbol/insert_after_symbol/safe_delete_symbol for whole declarations, plus the plumb-refactor skill for depth. When-to-use and when-to-use-something-else are both present.

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