Skip to main content
Glama

insert_to_file

Insert specified content into a file at a precise position using GonMCPtool. Supports previewing changes without modifying the file, ensuring accurate updates to your content.

Instructions

在檔案的特定位置插入新內容

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes要插入的內容
dryRunNo預覽變更而不實際修改檔案
pathYes
positionYes

Implementation Reference

  • Core implementation of the insert_to_file tool logic. Handles insertion at a specific line number or after a marker string in the file. Supports dry-run mode with diff preview. Reads file, modifies content, writes back if not dry-run.
    static async insertIntoFile( filePath: string, position: number | string, content: string, dryRun: boolean = false ): Promise<string> { try { // 檢查檔案是否存在 if (!existsSync(filePath)) { return `錯誤: 檔案 ${filePath} 不存在`; } // 讀取檔案內容 let fileContent = await fs.readFile(filePath, 'utf8'); let originalContent = fileContent; let lines = fileContent.split(/\r?\n/); // 處理不同的插入位置類型 if (typeof position === 'number') { // 按行號插入(行號從1開始) if (position < 1 || position > lines.length + 1) { return `錯誤: 行號 ${position} 超出範圍 (1 - ${lines.length + 1})`; } // 在指定行前插入 const insertIndex = position - 1; lines.splice(insertIndex, 0, content); fileContent = lines.join('\n'); } else if (typeof position === 'string') { // 按標記字符串插入 if (!fileContent.includes(position)) { return `錯誤: 在檔案中找不到標記「${position}」`; } // 在標記後插入 fileContent = fileContent.replace(position, position + content); } // 如果是預覽模式,生成差異報告 if (dryRun) { return this.generateDiff(originalContent, fileContent, filePath); } // 寫入檔案 await fs.writeFile(filePath, fileContent, 'utf8'); return `內容已成功插入檔案: ${filePath}`; } catch (error) { console.error(`插入內容時發生錯誤: ${error}`); return `插入內容時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`; } }
  • main.ts:245-268 (registration)
    Tool registration in main.ts, including name, description, inline schema validation, and wrapper async handler that calls FileEditorTool.insertIntoFile and formats MCP response.
    server.tool("insert_to_file", "在檔案的特定位置插入新內容", { path: z.string(), position: z.union([ z.number().describe("行號 (從1開始)"), z.string().describe("作為標記的文字,內容將插入在此標記之後") ]), content: z.string().describe("要插入的內容"), dryRun: z.boolean().default(false).describe("預覽變更而不實際修改檔案") }, async ({ path, position, content, dryRun = false }) => { try { const result = await FileEditorTool.insertIntoFile(path, position, content, dryRun); return { content: [{ type: "text", text: result }] }; } catch (error) { return { content: [{ type: "text", text: `插入內容失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );
  • Zod schema for input validation of insert_to_file tool: path (string), position (number line or string marker), content (string), dryRun (boolean optional).
    { path: z.string(), position: z.union([ z.number().describe("行號 (從1開始)"), z.string().describe("作為標記的文字,內容將插入在此標記之後") ]), content: z.string().describe("要插入的內容"), dryRun: z.boolean().default(false).describe("預覽變更而不實際修改檔案") },
  • Helper method to generate a line-by-line diff preview for dry-run mode, showing changes with -/+ prefixes.
    private static generateDiff(oldContent: string, newContent: string, filePath: string): string { const oldLines = oldContent.split(/\r?\n/); const newLines = newContent.split(/\r?\n/); let diff = `差異預覽 - ${filePath}\n`; diff += `===================================================================\n`; // 簡單的差異檢測 if (oldContent === newContent) { return '沒有變更'; } // 尋找不同的行 for (let i = 0; i < Math.max(oldLines.length, newLines.length); i++) { const oldLine = i < oldLines.length ? oldLines[i] : ''; const newLine = i < newLines.length ? newLines[i] : ''; if (oldLine !== newLine) { diff += `行 ${i+1}:\n`; diff += `- ${oldLine}\n`; diff += `+ ${newLine}\n\n`; } } return diff; }

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/GonTwVn/GonMCPtool'

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