Edit Files MCP server

Provides precise line-based editing tools for text files within allowed directories, enabling safe and controlled bulk modifications across multiple files.
Back to servers
Setup instructions
Provider
oakslee
Release date
Dec 26, 2024
Language
TypeScript
Stats
28 stars

The MCP Edit File Lines Server provides powerful functionality for making precise, line-based edits to text files through a clean API interface. It allows for string or regex pattern matching with a safety-focused two-step editing process where changes can be previewed before being applied.

Installation and Setup

Prerequisites

  • Node.js installed on your system
  • Access to the directories containing files you want to edit

Installation

To install and use the MCP Edit File Lines Server with Claude Desktop:

  1. Configure Claude Desktop by editing the configuration file:

MacOS:

~/Library/Application Support/Claude/claude_desktop_config.json

Windows:

%APPDATA%/Claude/claude_desktop_config.json
  1. Add the server configuration:
{
  "mcpServers": {
    "edit-file-lines": {
      "command": "node",
      "args": [
        "/path/to/edit-file-lines/build/index.js",
        "/path/to/allowed/directory"
      ],
      "env": {
        "MCP_EDIT_STATE_TTL": "300000"
      }
    }
  }
}

Replace /path/to/edit-file-lines/build/index.js with the actual path to the built server, and /path/to/allowed/directory with the directory containing files you want to edit.

Starting the Server Manually

If you need to start the server manually:

node build/index.js /path/to/allowed/directory [additional-directories...]

All file operations will be restricted to these allowed directories for security.

Using the MCP Server

Main Editing Tool: edit_file_lines

This is the primary tool for making line-based edits to files. It supports both string and regex pattern matching.

Basic Usage

Make edits with a dry run first (recommended):

{
  "p": "src/components/App.tsx",
  "e": [{
    "startLine": 2,
    "endLine": 2,
    "content": "primary",
    "strMatch": "blue"
  }],
  "dryRun": true
}

This will provide a diff view and a state ID for later approval.

Edit Parameters

  • p: Path to the file (relative to allowed directories)
  • e: Array of edit operations
    • startLine: First line to edit
    • endLine: Last line to edit
    • content: New content to insert
    • strMatch: String to match (optional)
    • regexMatch: Regex pattern to match (optional)
  • dryRun: Set to true to preview changes without applying them

Approving Edits: approve_edit

Apply changes from a previous dry run:

{
  "stateId": "fcbf740a"
}

This two-step process ensures you can review changes before applying them.

Viewing File Content: get_file_lines

Inspect specific lines in a file before editing:

{
  "path": "src/components/App.tsx",
  "lineNumbers": [1, 2, 3],
  "context": 1
}

Searching Files: search_file

Find specific patterns before editing:

{
  "path": "src/components/App.tsx",
  "pattern": "const",
  "contextLines": 2
}

Advanced Search Options

  • type: "text" (default) or "regex"
  • caseSensitive: Boolean (default: false)
  • wholeWord: Boolean (default: false)
  • contextLines: Number of lines to show around matches (default: 2)
  • maxMatches: Maximum number of matches to return (default: 100)
  • multiline: Enable multiline regex mode (default: false)

Common Workflows

Find and Replace Pattern

  1. Search for the pattern:
{
  "path": "src/config.ts",
  "pattern": "API_URL",
  "wholeWord": true
}
  1. Use the returned line number to make the edit:
{
  "p": "src/config.ts",
  "e": [{
    "startLine": 23,
    "endLine": 23,
    "content": "export const API_URL = 'https://new-api.example.com';",
    "strMatch": "export const API_URL = 'https://api.example.com';"
  }],
  "dryRun": true
}
  1. Approve the edit with the state ID:
{
  "stateId": "abc123def"
}

Multiple Edits in One Operation

To make several related changes at once:

{
  "p": "src/components/App.tsx",
  "e": [
    {
      "startLine": 2,
      "endLine": 2,
      "content": "primary",
      "strMatch": "blue"
    },
    {
      "startLine": 9,
      "endLine": 9,
      "content": "description = \"New caption\"",
      "strMatch": "subtitle = \"Default subtitle\""
    }
  ],
  "dryRun": true
}

Best Practices

  1. Always use dry runs first to preview changes before applying them
  2. Be specific with matches - use the most precise pattern possible
  3. Review diffs carefully before approving changes
  4. Use regex patterns for complex matches
  5. Start with small edits to build confidence with the tool
  6. Use the search tool to locate exact line numbers for editing

The server provides powerful editing capabilities while maintaining security through allowed directory restrictions and the two-step edit approval process.

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "edit-file-lines" '{"command":"node","args":["/path/to/edit-file-lines/build/index.js","<allowed-directory>"],"env":{"MCP_EDIT_STATE_TTL":"300000"}}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "edit-file-lines": {
            "command": "node",
            "args": [
                "/path/to/edit-file-lines/build/index.js",
                "<allowed-directory>"
            ],
            "env": {
                "MCP_EDIT_STATE_TTL": "300000"
            }
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "edit-file-lines": {
            "command": "node",
            "args": [
                "/path/to/edit-file-lines/build/index.js",
                "<allowed-directory>"
            ],
            "env": {
                "MCP_EDIT_STATE_TTL": "300000"
            }
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later