insert
Use this tool to insert text at a specified line in a file by providing the file path, line number, and new text, ensuring precise file modifications.
Instructions
Insert text at a specific line in the file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| insert_line | Yes | Line number where text should be inserted | |
| new_str | Yes | Text to insert | |
| path | Yes | Absolute path to the file |
Input Schema (JSON Schema)
{
"properties": {
"insert_line": {
"description": "Line number where text should be inserted",
"type": "number"
},
"new_str": {
"description": "Text to insert",
"type": "string"
},
"path": {
"description": "Absolute path to the file",
"type": "string"
}
},
"required": [
"path",
"insert_line",
"new_str"
],
"type": "object"
}
Implementation Reference
- src/editor.ts:140-186 (handler)The 'insert' tool handler in FileEditor class. Reads the file, inserts new_str at insert_line, writes back, shows snippet, and adds to history.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:103-124 (registration)Registration of 'insert' tool in ListToolsRequestSchema response, defining 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 to 'insert' handler in CallToolRequestSchema switch case.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-34 (schema)Type definition for input arguments of the 'insert' tool.export interface InsertArgs extends Record<string, unknown> { path: string; insert_line: number; new_str: string; }
- src/types.ts:58-62 (schema)Type guard for validating 'insert' tool arguments.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"; }