Skip to main content
Glama
pwilkin

MCP File Editor Server

by pwilkin

insert_into_file

Insert content into a file at a specified line position using the MCP File Editor Server. Choose to insert before or after a target line, or append to the end, with content verification for precise file modifications.

Instructions

Insert content into a file at a specific line position.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentsYesContent to insert
file_pathYesAbsolute path to the file
line_contentsYesExpected content of the target line (used for verification)
line_numberYesLine number to insert at (1-based). Use 0 to append to end.
whereYesWhether to insert before or after the target line

Implementation Reference

  • The execute function implementing the insert_into_file tool: validates path, reads file, verifies target line if applicable, inserts new content before/after specified line or appends, writes back to file, and returns success message.
    execute: async ({ file_path, line_number, line_contents, where, contents }) => {
      const absolutePath = validateAbsolutePath(file_path, 'file_path');
      validateFileExists(absolutePath);
    
      try {
        const content = fs.readFileSync(absolutePath, 'utf-8');
        const lines = content.split('\n');
    
        if (line_number === 0) {
          // Append to end
          lines.push(contents);
        } else {
          // Verify line content
          verifyLineContent(absolutePath, line_number, line_contents);
    
          // Insert at specified position
          const insertIndex = where === 'after' ? line_number : line_number - 1;
          lines.splice(insertIndex, 0, contents);
        }
    
        const newContent = lines.join('\n');
        fs.writeFileSync(absolutePath, newContent, 'utf-8');
    
        const position = line_number === 0 ? 'end of file' : `${where} line ${line_number}`;
        return `Successfully inserted content ${position} in "${absolutePath}".`;
      } catch (error: any) {
        if (error instanceof UserError) throw error;
        throw new UserError(`Error inserting content into file "${absolutePath}": ${error.message}`);
      }
    }
  • Zod parameter schema for the insert_into_file tool defining input validation for file_path, line_number (0 for append), line_contents (for verification), where (before/after), and contents to insert.
    parameters: z.object({
      file_path: z.string().describe('Absolute path to the file'),
      line_number: z.number().int().min(0).describe('Line number to insert at (1-based). Use 0 to append to end.'),
      line_contents: z.string().describe('Expected content of the target line (used for verification)'),
      where: z.enum(['before', 'after']).describe('Whether to insert before or after the target line'),
      contents: z.string().describe('Content to insert')
  • src/index.ts:170-210 (registration)
    Registration of the insert_into_file tool with the FastMCP server via server.addTool, including name, description, schema, and execute handler.
    server.addTool({
      name: 'insert_into_file',
      description: 'Insert content into a file at a specific line position.',
      parameters: z.object({
        file_path: z.string().describe('Absolute path to the file'),
        line_number: z.number().int().min(0).describe('Line number to insert at (1-based). Use 0 to append to end.'),
        line_contents: z.string().describe('Expected content of the target line (used for verification)'),
        where: z.enum(['before', 'after']).describe('Whether to insert before or after the target line'),
        contents: z.string().describe('Content to insert')
      }),
      execute: async ({ file_path, line_number, line_contents, where, contents }) => {
        const absolutePath = validateAbsolutePath(file_path, 'file_path');
        validateFileExists(absolutePath);
    
        try {
          const content = fs.readFileSync(absolutePath, 'utf-8');
          const lines = content.split('\n');
    
          if (line_number === 0) {
            // Append to end
            lines.push(contents);
          } else {
            // Verify line content
            verifyLineContent(absolutePath, line_number, line_contents);
    
            // Insert at specified position
            const insertIndex = where === 'after' ? line_number : line_number - 1;
            lines.splice(insertIndex, 0, contents);
          }
    
          const newContent = lines.join('\n');
          fs.writeFileSync(absolutePath, newContent, 'utf-8');
    
          const position = line_number === 0 ? 'end of file' : `${where} line ${line_number}`;
          return `Successfully inserted content ${position} in "${absolutePath}".`;
        } catch (error: any) {
          if (error instanceof UserError) throw error;
          throw new UserError(`Error inserting content into file "${absolutePath}": ${error.message}`);
        }
      }
    });
Behavior2/5

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

With no annotations provided, the description carries full burden but only states the basic operation. It doesn't disclose whether this is a destructive mutation, what permissions are needed, how errors are handled, or what happens if the file doesn't exist. For a file modification tool with zero annotation coverage, this is insufficient behavioral context.

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 a single, efficient sentence that states the core functionality without any wasted words. It's appropriately sized for a tool with this level of complexity and gets straight to the point.

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

Completeness2/5

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

For a file mutation tool with 5 parameters, no annotations, and no output schema, the description is inadequate. It doesn't explain what happens on success/failure, return values, error conditions, or how it differs from similar tools. The agent would need to guess too much about this tool's behavior.

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 all parameters are documented in the schema. The description adds no parameter-specific information beyond what's already in the schema descriptions. The baseline of 3 is appropriate when the schema does all the parameter documentation work.

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 specific action ('Insert content'), target resource ('into a file'), and precise positioning ('at a specific line position'). It distinguishes from siblings like 'replace_in_file' or 'delete_from_file' by focusing on insertion rather than replacement or deletion.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives like 'replace_in_file' or 'replace_lines_in_file'. It doesn't mention prerequisites, error conditions, or typical use cases, leaving the agent to infer usage from the tool name alone.

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

Related 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/pwilkin/mcp-file-edit'

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