Skip to main content
Glama

apply_code_patch

Apply a verified code patch to a file by replacing exact code blocks, with automatic syntax validation and instant rollback if checks fail.

Instructions

Applies an atomic, verified code edit to a specific file by substituting original_code with new_code, with automatic syntax validation and instant rollback on failure.

• Side Effects: Modifies the target file on the local filesystem. If syntax checks pass, the file is overwritten with patched contents; if syntax validation fails, the file is immediately restored to its exact original state (zero dirty diff). • Auth & Permissions: Requires write permissions for the target file on the host filesystem within the workspace boundary. Path traversal outside workspace root is blocked. • Rate Limits: None. Local disk I/O. • Return Shape: Returns a JSON object with 'status' ('success' or 'error'), 'file_path', 'lines_changed', 'verification' ('passed' or 'reverted'), and 'message'. • Failure Modes: Fails and aborts without touching the file if file_path is not found, if original_code does not match the file content uniquely, or if the compiler/linter check fails after patch application. • When to use: Use when you have finalized a bug fix or refactoring snippet and need safe, transactional application with zero risk of syntax corruption. • When NOT to use: Do NOT use for speculative edits without prior diagnosis (use get_error_context first), and do NOT use for whole-file generation when only a small block changes. • Prerequisites: Target file must exist and be within the current workspace directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dry_runNoOptional. When true, validates that the patch matches uniquely and checks syntax without writing any changes to disk. Defaults to false.
new_codeYesThe replacement code block to substitute in place of original_code. Must maintain correct language syntax and indentation matching the surrounding code.
file_pathYesTarget file path to modify. Can be relative to the workspace root or an absolute path located inside the workspace boundary. Path traversal outside the workspace is rejected.
original_codeYesThe exact character-for-character contiguous code block to be replaced, including exact leading indentation, newlines, and whitespace. Must match exactly one location in the file.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed4 schema fields changedv1.1.8
    • addedInput schema / properties / dry_run
      Added value: +{
      +  "description": "Optional. When true, validates that the patch matches uniquely and checks syntax without writing any changes to disk. Defaults to false.",
      +  "type": "boolean"
      +}
    • changedInput schema / properties / file_path / description
      Previous value: -"Absolute path to the file"New value: +"Target file path to modify. Can be relative to the workspace root or an absolute path located inside the workspace boundary. Path traversal outside the workspace is rejected."
    • changedInput schema / properties / new_code / description
      Previous value: -"The new code block"New value: +"The replacement code block to substitute in place of original_code. Must maintain correct language syntax and indentation matching the surrounding code."
    • changedInput schema / properties / original_code / description
      Previous value: -"The exact code block to be replaced"New value: +"The exact character-for-character contiguous code block to be replaced, including exact leading indentation, newlines, and whitespace. Must match exactly one location in the file."
  2. First observedv0.1.0

TDQS

A4.6/5.0
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 and covers it comprehensively: side effects, permissions, rate limits, return shape, rollback behavior, and failure modes. It explicitly states that the file is overwritten on success, restored on failure, and that path traversal is blocked. This is far beyond the typical threshold.

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 well-structured with clear sections (side effects, auth, rate limits, return shape, failure modes, usage, prerequisites) and front-loads the core function. It is slightly redundant—syntax validation and rollback are stated both in the opening sentence and in side effects/failure modes—but every section otherwise earns its place.

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 mutation tool with 4 parameters, no annotations, and no output schema, the description is complete: it explains side effects, permissions, rate limits, return shape, failure modes, prerequisites, and when to use it. Nothing an agent needs to invoke this tool safely and correctly is missing. The return shape section compensates for the absent output schema.

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 description coverage is 100%, so the baseline is 3, and the description does not add parameter-specific detail beyond the schema. It repeats some constraints already in the schema (exact match, syntax/indentation), so the description adds no meaningful new parameter semantics, but the schema itself is already rich.

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 a specific verb ('Applies') tied to a resource ('atomic, verified code edit to a specific file') and a clear mechanism (substituting original_code with new_code). It also names distinct behaviors like automatic syntax validation and rollback, which separates it from siblings focused on analysis or context retrieval.

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?

The description includes explicit 'When to use' and 'When NOT to use' sections, with conditions and an alternative ('use get_error_context first'). It clearly states when the tool is appropriate (finalized fix needing safe transactional application) and when it is not (speculative edits, whole-file generation), leaving no ambiguity.

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