Skip to main content
Glama
arathald

mcp-editor

by arathald

insert

Insert text at a specific line in a file to modify content precisely without overwriting existing data.

Instructions

Insert text at a specific line in the file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesAbsolute path to the file
insert_lineYesLine number where text should be inserted
new_strYesText to insert

Implementation Reference

  • The core handler function for the 'insert' tool. It validates the path and insert_line, reads the file, inserts the new text at the specified line number (0-indexed?), constructs a snippet around the insertion point, writes the updated content back to the file, updates edit history, and returns a formatted output with line numbers.
    async insert(args: InsertArgs): Promise<string> {
        await validatePath('insert', args.path);
    
        const fileContent = await readFile(args.path);
        const newStr = args.new_str.replace(/\t/g, '    ');
        const fileLines = fileContent.split('\n');
        const nLinesFile = fileLines.length;
    
        if (args.insert_line < 0 || args.insert_line > nLinesFile) {
            throw new ToolError(
                `Invalid \`insert_line\` parameter: ${args.insert_line}. It should be within the range of lines of the file: [0, ${nLinesFile}]`
            );
        }
    
        const newStrLines = newStr.split('\n');
        const newFileLines = [
            ...fileLines.slice(0, args.insert_line),
            ...newStrLines,
            ...fileLines.slice(args.insert_line)
        ];
    
        const snippetLines = [
            ...fileLines.slice(Math.max(0, args.insert_line - SNIPPET_LINES), args.insert_line),
            ...newStrLines,
            ...fileLines.slice(args.insert_line, args.insert_line + SNIPPET_LINES)
        ];
    
        const newFileContent = newFileLines.join('\n');
        const snippet = snippetLines.join('\n');
    
        await writeFile(args.path, newFileContent);
    
        if (!this.fileHistory[args.path]) {
            this.fileHistory[args.path] = [];
        }
        this.fileHistory[args.path].push(fileContent);
    
        let successMsg = `The file ${args.path} has been edited. `;
        successMsg += makeOutput(
            snippet,
            'a snippet of the edited file',
            Math.max(1, args.insert_line - SNIPPET_LINES + 1)
        );
        successMsg += 'Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary.';
    
        return successMsg;
    }
  • JSON schema definition for the input parameters of the 'insert' tool, as registered in the ListTools response.
    inputSchema: {
        type: "object",
        properties: {
            path: {
                type: "string",
                description: "Absolute path to the file"
            },
            insert_line: {
                type: "number",
                description: "Line number where text should be inserted"
            },
            new_str: {
                type: "string",
                description: "Text to insert"
            }
        },
        required: ["path", "insert_line", "new_str"]
    }
  • src/server.ts:103-125 (registration)
    Registration of the 'insert' tool in the tools list returned by ListToolsRequestHandler, including name, description, and input schema.
    {
        name: "insert",
        description: "Insert text at a specific line in the file",
        inputSchema: {
            type: "object",
            properties: {
                path: {
                    type: "string",
                    description: "Absolute path to the file"
                },
                insert_line: {
                    type: "number",
                    description: "Line number where text should be inserted"
                },
                new_str: {
                    type: "string",
                    description: "Text to insert"
                }
            },
            required: ["path", "insert_line", "new_str"]
        }
    },
    {
  • src/server.ts:168-173 (registration)
    Dispatch logic in CallToolRequestHandler that validates arguments using isInsertArgs and calls the editor.insert handler.
    case "insert":
        if (!request.params.arguments || !isInsertArgs(request.params.arguments)) {
            throw new ToolError("Invalid arguments for insert command");  // Fixed
        }
        result = await this.editor.insert(request.params.arguments);
        break;
  • TypeScript interface definition for InsertArgs and the runtime type guard isInsertArgs used for input validation.
    export interface InsertArgs extends Record<string, unknown> {
        path: string;
        insert_line: number;
        new_str: string;
    }
    
    export interface UndoEditArgs extends Record<string, unknown> {
        path: string;
    }
    
    export function isViewArgs(args: Record<string, unknown>): args is ViewArgs {
        return typeof args.path === "string" &&
            (args.view_range === undefined ||
                (Array.isArray(args.view_range) &&
                    args.view_range.length === 2 &&
                    args.view_range.every(n => typeof n === "number")));
    }
    
    export function isCreateArgs(args: Record<string, unknown>): args is CreateArgs {
        return typeof args.path === "string" && typeof args.file_text === "string";
    }
    
    export function isStrReplaceArgs(args: Record<string, unknown>): args is StringReplaceArgs {
        return typeof args.path === "string" &&
            typeof args.old_str === "string" &&
            (args.new_str === undefined || typeof args.new_str === "string");
    }
    
    export function isInsertArgs(args: Record<string, unknown>): args is InsertArgs {
        return typeof args.path === "string" &&
            typeof args.insert_line === "number" &&
            typeof args.new_str === "string";
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool inserts text but doesn't mention whether this is a destructive operation (e.g., modifies the file permanently), what permissions are required, error handling (e.g., if line number is out of bounds), or side effects. This leaves significant gaps in understanding the tool's behavior beyond its basic function.

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 directly states the tool's purpose without any unnecessary words. It is appropriately sized and front-loaded, making it easy to understand at a glance, with every part contributing to clarity.

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?

Given the complexity of a file editing tool with no annotations and no output schema, the description is incomplete. It doesn't cover behavioral aspects like safety, permissions, or error handling, nor does it explain return values or side effects. For a tool that modifies files, this lack of context makes it inadequate for safe and effective use.

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?

The input schema has 100% description coverage, clearly documenting all three parameters (path, insert_line, new_str). The description adds no additional meaning beyond what the schema provides, such as formatting details or usage examples. According to the rules, with high schema coverage, the baseline score is 3, as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Insert text') and target ('at a specific line in the file'), providing a specific verb+resource combination. However, it doesn't explicitly differentiate from sibling tools like 'create' (which might create files) or 'string_replace' (which might replace text rather than insert), leaving some ambiguity about when to choose this over alternatives.

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 'create', 'string_replace', or 'undo_edit'. It lacks context about prerequisites (e.g., file must exist), exclusions, or typical scenarios for insertion versus other editing operations, offering only a basic functional statement.

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/arathald/mcp-editor'

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