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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the file | |
| insert_line | Yes | Line number where text should be inserted | |
| new_str | Yes | Text to insert |
Implementation Reference
- src/editor.ts:140-186 (handler)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; }
- src/server.ts:106-123 (schema)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;
- src/types.ts:30-62 (schema)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"; }