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}`);
        }
      }
    });
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