Skip to main content

An MCP Server to patch existing files

Project description

Patch File MCP

An MCP Server to patch existing files using block format. This allows AI agents (like Claude) to make precise changes to files in your projects.

Overview

Patch File MCP provides a simple way to modify files by applying patches in block format. The key benefits include:

  • Makes targeted changes to specific parts of files without rewriting the entire content
  • Supports multiple patches to the same file in a single request
  • Ensures safety through exact text matching and uniqueness verification
  • Better alternative to the edit_block tool from desktop-commander for most file editing tasks

Installation

Using uvx

This method uses uvx (from the uv Python package manager) to run the server without permanent installation:

Prerequisites

Install uvx from uv if you don't have it already.

Set up MCP Client (Claude Desktop, Cursor, etc.)

Merge the following config with your existing config file (e.g. claude_desktop_config.json):

{
  "mcpServers": {
    "patch-file": {
      "command": "uvx",
      "args": [
        "patch-file-mcp",
        "--allowed-dir", "/Users/your-username/projects",
        "--allowed-dir", "/Users/your-username/Documents/code"
      ]
    }
  }
}

Note: Replace /Users/your-username with the actual path to your own projects and code directories.

Install from Source

Prerequisites

  • Python 3.11 or higher
  • Pip package manager

Clone the repository

git clone https://github.com/your-username/patch-file-mcp.git
cd patch-file-mcp
python -m venv venv
source venv/bin/activate
pip install -e .

Set up MCP Client (Claude Desktop, Cursor, etc.)

Merge the following config with your existing config file (e.g. claude_desktop_config.json):

{
  "mcpServers": {
    "patch-file": {
      "command": "path/to/your/venv/bin/patch-file-mcp",
      "args": [
        "--allowed-dir", "/Users/your-username/projects",
        "--allowed-dir", "/Users/your-username/Documents/code"
      ]
    }
  }
}

Note: Replace /Users/your-username with the actual path to your own projects and code directories.

Arguments

The --allowed-dir argument is used to specify the directories that the server has access to. You can use it multiple times to allow access to multiple directories. All directories inside the allowed directories are also allowed. It is optional. If not provided, the server will only have access to the home directory of the user running the server.

Usage

The MCP server is started by the client (e.g., Claude Desktop) based on the configuration you provide. You don't need to start the server manually.

Tools

Patch File MCP provides one main tool:

patch_file

Update the file by applying a patch/edit to it using block format.

patch_file(file_path: str, patch_content: str)
  • file_path: Path to the file to be patched.
  • patch_content: Content to search and replace in the file using block format with SEARCH/REPLACE markers. Multiple blocks are supported.

The patch content must have the following format:

<<<<<<< SEARCH
Text to find in the file
=======
Text to replace it with
>>>>>>> REPLACE

You can include multiple search-replace blocks in a single request:

<<<<<<< SEARCH
First text to find
=======
First replacement
>>>>>>> REPLACE
<<<<<<< SEARCH
Second text to find
=======
Second replacement
>>>>>>> REPLACE

This tool verifies that each search text appears exactly once in the file to ensure the correct section is modified. If a search text appears multiple times or isn't found, it will report an error.

Example Workflow

  1. Begin a conversation with Claude about modifying a file in your project
  2. Claude generates a block format patch that makes the desired changes
  3. Claude uses patch_file to apply these changes to your file
  4. If the patch fails, Claude provides detailed error information to help you fix the issue

Security Considerations

  • All file operations are restricted to allowed directories
  • Search texts must appear exactly once in the file to ensure correct targeting
  • Detailed error reporting helps identify and fix issues
  • Each patch operation is validated before being applied

Advantages over similar tools

  • Multiple blocks in one operation: Can apply several changes in a single call
  • Safety checks: Ensures the correct sections are modified through exact matching
  • Detailed errors: Provides clear feedback when patches can't be applied

Dependencies

  • fastmcp (>=2.2.0, <3.0.0)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patch_file_mcp-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patch_file_mcp-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file patch_file_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: patch_file_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for patch_file_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 51275a4ae6239e71f1ce40615bbd6bbe97778f28eb71fde91a9484b4e3f2bba4
MD5 e9f6280dd93ffdf9427d0ac241d7facf
BLAKE2b-256 27f22e1f2b077c0e191ca87645dccc930069e5f637177bc3e2de71d5857bfbec

See more details on using hashes here.

File details

Details for the file patch_file_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: patch_file_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for patch_file_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c12b7deb4614ad0007153dd9249fbe98a8313fa1ba3c00eb09aa94e074ea9e98
MD5 f2bd2693ee751de20b21a6069dacd131
BLAKE2b-256 c89e1f2ae7400c90492d83c9db15e199b0ece10818c2c8487bf02a2172005329

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page