Skip to main content
Glama

Apply patch

apply_patch
Destructive

Apply a unified diff to one or more files atomically: all hunks are validated in memory first, and any failing hunk rolls every file back from an automatic checkpoint.

Instructions

Apply a unified diff (git apply compatible) to one or more files, all-or-nothing. Every file is validated in memory before anything is written; a failing hunk rolls every file back from an automatic checkpoint, like edit_batch. dry_run=true validates and reports without writing. Use when: a change arrives as a diff (git diff, a patch file, another tool's output) and must land completely or not at all — files created, modified, deleted or renamed. Avoid when: you have no diff (edit_file/edit_batch) or only need a new file (write_file). Not for directories or symbolic links. Output: dry_run: files[] {action, relative_path, hunks, bytes} plus added/modified/deleted/renamed counts; nothing is written. success: same list plus sha256_after per file. Failure: nothing is changed; data carries rollback_succeeded and, for a mismatching hunk, nearest_context lines for the file and hunk that failed.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "patch": "<patch>"
    },
    "name": "apply_patch"
  }
}

Input Schema

NameRequiredDescriptionDefault
patchYesUnified diff. Multiple files supported: each section starts with 'diff --git a/old b/new' or '--- a/old' + '+++ b/new' ('/dev/null' for created/deleted files).
reasonNoOptional why (max 400 chars), audit-logged.
dry_runNotrue validates the patch against the current files and reports what would change, writing nothing (default false).
expected_sha256NoOptional map of workspace-relative path -> hex SHA-256; the patch is refused if a file changed since it was read. Keys must be paths the patch touches.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed5 schema fields changedv4.5.0
    • removedInput schema / properties / dry_run / anyOf
      Removed value: -[
      -  {
      -    "type": "boolean"
      -  },
      -  {
      -    "maximum": 1,
      -    "minimum": 0,
      -    "type": "integer"
      -  }
      -]
    • changedInput schema / properties / dry_run / description
      Previous value: -"1 validates the patch against the current files and reports what would change, writing nothing (default 0)."New value: +"true validates the patch against the current files and reports what would change, writing nothing (default false)."
    • removedInput schema / properties / dry_run / maximum
      Removed value: -1
    • removedInput schema / properties / dry_run / minimum
      Removed value: -0
    • changedInput schema / properties / dry_run / type
      Previous value: -"integer"New value: +"boolean"
  2. Addedv4.0.0

TDQS

A4.6/5.0
Behavior5/5

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

Annotations already declare destructiveHint=true and readOnlyHint=false, but the description goes well beyond that: in-memory validation of every file before writing, automatic checkpoint rollback on a failing hunk, and precisely what failure returns (rollback_succeeded, nearest_context for the offending hunk). It also discloses that dry_run writes nothing and what each outcome's payload contains.

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?

Front-loaded with the operation and its atomicity guarantee, then organized under Use when / Avoid when / Output labels. The Output section is verbose but earns its place because no output schema exists, and no sentence is redundant.

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

Completeness4/5

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

For a destructive, multi-file mutation with a nested-object parameter and no output schema, the definition covers atomicity, rollback, dry-run semantics and failure payloads thoroughly. The one gap is the stale-read guard (expected_sha256), which is never referenced in prose despite being a correctness-critical control for a patch tool.

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

Parameters3/5

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

Schema coverage is 100%, so the baseline is 3: patch format, expected_sha256 staleness guard and reason maxLength are all already documented in the schema. The description adds real meaning only for dry_run by tying it to a reporting-only outcome, and never mentions expected_sha256 or its interaction with concurrent edits, so it does not materially exceed the schema's own documentation.

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 concrete verb and resource (apply a unified diff to one or more files) plus the defining scope constraint: all-or-nothing, git-apply compatible. It distinguishes itself from edit_batch, edit_file and write_file by naming them explicitly, so an agent can route correctly without opening another schema.

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?

Has explicit 'Use when' (change arrives as a diff and must land completely or not at all) and 'Avoid when' (no diff, or only a new file) sections, each mapped to a named alternative tool. It also excludes unsupported targets (directories, symbolic links), leaving nothing to inference.

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