Skip to main content
Glama
koopatroopa787

MCP PC Control Server

edit_file

Make line-based edits to text files by specifying original lines and their replacements. Returns a git-style diff showing changes made to files on your PC.

Instructions

Make line-based edits to a text file. Provide original lines and their replacements. Returns a git-style diff showing the changes made. Each edit replaces exact line sequences with new content.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesThe path to the file to edit
editsYesArray of edit operations to apply

Implementation Reference

  • The handler for the 'edit_file' tool. Reads the target file, applies each specified edit by replacing 'oldText' with 'newText' (ensuring 'oldText' exists first), overwrites the file with the modified content, generates a simple unified diff using the helper function, and returns the diff.
    case "edit_file": {
      const filePath = args.path as string;
      const edits = args.edits as Array<{
        oldText: string;
        newText: string;
      }>;
    
      let content = await fs.readFile(filePath, "utf-8");
      const originalContent = content;
    
      // Apply each edit
      for (const edit of edits) {
        if (!content.includes(edit.oldText)) {
          throw new Error(
            `Could not find text to replace: ${edit.oldText.substring(0, 100)}...`
          );
        }
        content = content.replace(edit.oldText, edit.newText);
      }
    
      await fs.writeFile(filePath, content, "utf-8");
    
      // Generate diff
      const diff = generateDiff(originalContent, content, filePath);
    
      return {
        content: [
          {
            type: "text",
            text: `Successfully edited ${filePath}\n\n${diff}`,
          },
        ],
      };
    }
  • Input schema for the 'edit_file' tool, defining required parameters: 'path' (string) and 'edits' (array of objects with 'oldText' and 'newText').
    inputSchema: {
      type: "object",
      properties: {
        path: {
          type: "string",
          description: "The path to the file to edit",
        },
        edits: {
          type: "array",
          description: "Array of edit operations to apply",
          items: {
            type: "object",
            properties: {
              oldText: {
                type: "string",
                description: "The exact text to search for (can be multiple lines)",
              },
              newText: {
                type: "string",
                description: "The text to replace it with",
              },
            },
            required: ["oldText", "newText"],
          },
        },
      },
      required: ["path", "edits"],
    },
  • src/index.ts:52-83 (registration)
    Registration of the 'edit_file' tool in the TOOLS array, which is returned by the listTools handler. Includes name, description, and input schema.
    {
      name: "edit_file",
      description: "Make line-based edits to a text file. Provide original lines and their replacements. Returns a git-style diff showing the changes made. Each edit replaces exact line sequences with new content.",
      inputSchema: {
        type: "object",
        properties: {
          path: {
            type: "string",
            description: "The path to the file to edit",
          },
          edits: {
            type: "array",
            description: "Array of edit operations to apply",
            items: {
              type: "object",
              properties: {
                oldText: {
                  type: "string",
                  description: "The exact text to search for (can be multiple lines)",
                },
                newText: {
                  type: "string",
                  description: "The text to replace it with",
                },
              },
              required: ["oldText", "newText"],
            },
          },
        },
        required: ["path", "edits"],
      },
    },
  • Helper function 'generateDiff' that creates a simple git-style unified diff between original and modified file contents. Called by the edit_file handler to include the diff in the response.
    function generateDiff(
      original: string,
      modified: string,
      filepath: string
    ): string {
      const originalLines = original.split("\n");
      const modifiedLines = modified.split("\n");
    
      let diff = `--- ${filepath}\n+++ ${filepath}\n`;
      let lineNum = 0;
    
      while (
        lineNum < originalLines.length ||
        lineNum < modifiedLines.length
      ) {
        if (originalLines[lineNum] !== modifiedLines[lineNum]) {
          diff += `@@ -${lineNum + 1} +${lineNum + 1} @@\n`;
    
          if (lineNum < originalLines.length) {
            diff += `- ${originalLines[lineNum]}\n`;
          }
          if (lineNum < modifiedLines.length) {
            diff += `+ ${modifiedLines[lineNum]}\n`;
          }
        }
        lineNum++;
      }
    
      return diff;
    }
Behavior3/5

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

With no annotations provided, the description carries the full burden. It discloses key behaviors: the tool performs mutations (edits), returns a git-style diff, and operates on exact line sequences. However, it lacks details on permissions needed, error handling (e.g., if lines don't match), or whether edits are atomic. The description doesn't contradict annotations (none exist).

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 front-loaded with the core purpose in the first sentence, followed by supporting details. Every sentence earns its place: the first states the action, the second explains parameters and output, and the third clarifies the edit mechanism. No wasted words.

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?

Given the tool's moderate complexity (mutating files with structured edits), no annotations, and no output schema, the description is reasonably complete. It covers the purpose, parameter intent, and output format (git-style diff). However, it could benefit from more behavioral context like error cases or idempotency, especially since there's no output schema to describe the diff structure.

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 schema already documents both parameters (path and edits array with oldText/newText). The description adds some context by mentioning 'exact line sequences' and 'line-based edits,' which clarifies the semantics of oldText/newText, but doesn't provide additional syntax or format details beyond what the schema offers.

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 clearly states the tool's purpose with specific verbs ('Make line-based edits') and resource ('to a text file'), and distinguishes it from siblings like write_file (which presumably creates/overwrites entire files) and read_file (which only reads). The phrase 'line-based edits' provides precise differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context by specifying 'line-based edits' and 'exact line sequences,' suggesting this tool is for targeted modifications rather than full file rewrites. However, it doesn't explicitly state when to use alternatives like write_file for complete replacements or when not to use this tool (e.g., for binary files).

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/koopatroopa787/first_mcp'

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