Skip to main content
Glama

edit_file

Apply precise edits to existing files with exact-match validation and atomic, crash-durable writes, supporting string replacements, line ranges, and anchor-bounded rewrites.

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 request shapes (mutually exclusive): pass an edits array, OR start_anchor + end_anchor + new_string. In the edits array, each edit is one of two modes: (1) str_replace (default): old_string must appear EXACTLY ONCE — rejected if absent or ambiguous. (2) range: start_line/end_line (1-based) replace that line span with new_string; start_line: -1 appends at end of file, end_line: -1 runs through the last line (the clean way to delete a block or append, no anchor needed). Anchor mode replaces the span BETWEEN two unique anchors with new_string — each anchor must match EXACTLY ONCE and end_anchor must follow start_anchor; include_anchors=false (default) keeps the anchors and rewrites only the text between them, include_anchors=true replaces the whole inclusive span. Ideal for rewriting a block whose interior changes but whose stable boundary lines do not. CRLF is tolerated; edits apply sequentially in memory then write atomically and crash-durably (temp file fsynced + rename + parent-dir fsync) under a per-path lock. Pass expected_mtime (from a read_file header) to guarantee the file is unchanged since you read it. For a SOLE agent doing a burst of sequential edits to one file, OMITTING expected_mtime is the blessed fast path: the EXACTLY-ONCE old_string match is itself the safety check, so you need not thread the fresh mtime each edit returns through the next one (reach for expected_mtime/expected_sha only when a concurrent writer may touch the file). If the call fails with a transport/connection error, the atomic temp+rename guarantees the file is either fully updated or untouched — never partially written; re-read to see which. Replacing, inserting around, or deleting an entire named declaration? Prefer replace_symbol_body / insert_before_symbol / insert_after_symbol / safe_delete_symbol — addressed by name_path, no coordinates to compute.

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(gofumpt/golangci-lint --fix)→edit loop, where a formatter bumped the mtime but your anchors still match. Default false (the mtime guard stays strict).
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 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.
await_diagnosticsNoWhen 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).
Behavior5/5

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

With no annotations, the description fully discloses behavior: atomic temp-file+rename write, crash durability (fsynced temp file + parent dir), per-path locking, EXACTLY-ONCE old_string matching with rejection on ambiguity, sequential in-memory edit application, and the effect of transport errors. This goes far beyond what annotations could convey.

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 long but every sentence carries essential information. It is front-loaded with the core purpose and alternatives, then systematically covers modes, safety, concurrency guidance, and edge cases. No filler or redundancy; structured in a logical flow.

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 complex 12-parameter tool with no output schema and no annotations, the description is exceptionally complete. It covers all request shapes, safety mechanisms, concurrency scenarios, error behavior, and even mentions return artifacts (mtime, per-edit results). The only minor omission is a formal success response spec, but the description provides enough context 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 coverage is 100% (baseline 3), but the description adds critical semantics: mutually exclusive request shapes, the meaning of -1 for start_line/end_line, anchor mode include_anchors behavior, expected_mtime fast-path guidance, and apply_partial failure isolation. All of this meaningfully supplements the raw parameter descriptions.

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 opens with 'Apply one or more edits to an existing file', a specific verb+resource statement. It clearly distinguishes itself from siblings by explicitly recommending symbol-based alternatives for named declarations and referencing the 'Edit lane note in session_start' for native edit tools.

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?

Provides explicit when-to-use guidance: 'use this over a native edit tool', and detailed alternatives: 'Prefer replace_symbol_body / insert_before_symbol / insert_after_symbol / safe_delete_symbol'. It also advises on when to omit expected_mtime for sole agents vs. use it for concurrent writes, and when apply_partial is unsafe.

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/plumbkit/plumb'

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