Skip to main content
Glama

codeLineDelete

Remove specific line ranges from code files to delete unwanted sections or clean up code. Specify file path and line numbers to execute deletions.

Instructions

每次使用前、使用後必須先使用(codeFileRead)。刪除程式碼檔案指定範圍的行。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes
startLineYes
endLineNo

Implementation Reference

  • Core handler function that deletes specified lines from a file by reading content, splitting into lines, removing the range with splice, and writing back. Handles validation and errors.
    static async deleteLines( filePath: string, startLine: number, endLine?: number ): Promise<string> { try { // 檢查檔案是否存在 if (!existsSync(filePath)) { return `錯誤: 檔案 ${filePath} 不存在`; } // 設置結束行,如果未提供則等於起始行 const effectiveEndLine = endLine || startLine; // 確保起始行小於等於結束行 if (startLine > effectiveEndLine) { return `錯誤: 起始行 ${startLine} 大於結束行 ${effectiveEndLine}`; } // 讀取檔案內容 const fileContent = await fs.readFile(filePath, 'utf8'); const lines = fileContent.split(/\r?\n/); // 檢查行號是否有效 if (startLine < 1 || startLine > lines.length) { return `錯誤: 起始行 ${startLine} 超出範圍,檔案共有 ${lines.length} 行`; } if (effectiveEndLine < 1 || effectiveEndLine > lines.length) { return `錯誤: 結束行 ${effectiveEndLine} 超出範圍,檔案共有 ${lines.length} 行`; } // 刪除指定範圍的行 lines.splice(startLine - 1, effectiveEndLine - startLine + 1); // 寫回檔案 await fs.writeFile(filePath, lines.join('\n'), 'utf8'); if (startLine === effectiveEndLine) { return `成功刪除第 ${startLine} 行`; } else { return `成功刪除第 ${startLine} 行到第 ${effectiveEndLine} 行`; } } catch (error) { console.error(`刪除內容時發生錯誤: ${error}`); return `刪除內容時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`; } }
  • Zod input schema defining parameters for the codeLineDelete tool: filePath, startLine (required), endLine (optional).
    { filePath: z.string(), startLine: z.number(), endLine: z.number().optional() },
  • main.ts:109-128 (registration)
    Registers the 'codeLineDelete' MCP tool with name, description, input schema, and an async wrapper handler that calls the core deleteLines function and formats the MCP response.
    server.tool("codeLineDelete", "每次使用前、使用後必須先使用(codeFileRead)。刪除程式碼檔案指定範圍的行。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)", { filePath: z.string(), startLine: z.number(), endLine: z.number().optional() }, async ({ filePath, startLine, endLine }) => { try { const result = await myFileDelete.deleteLines(filePath, startLine, endLine); return { content: [{ type: "text", text: result }] }; } catch (error) { return { content: [{ type: "text", text: `刪除內容失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );

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