Skip to main content
Glama

codeFileRead

Reads code files with line numbers to verify actual line counts for editing operations, helping users confirm file structure before making changes.

Instructions

讀取程式碼檔案並顯示行號,用於確認檔案實際行數以便編輯操作。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes

Implementation Reference

  • Core handler logic: reads the specified file, checks existence, splits into lines, adds padded line numbers to each line, joins back, handles errors.
    static async readFileWithLineNumbers(filePath: string): Promise<string> {
        try {
            // 檢查檔案是否存在
            if (!existsSync(filePath)) {
                return `錯誤: 檔案 ${filePath} 不存在`;
            }
    
            // 讀取檔案內容
            const content = await fs.readFile(filePath, 'utf8');
            const lines = content.split(/\r?\n/);
    
            // 計算行號寬度 (最大行號的位數)
            const lineNumberWidth = lines.length.toString().length;
            
            // 為每行添加行號
            const numberedLines = lines.map((line, index) => {
                const lineNumber = (index + 1).toString().padStart(lineNumberWidth, '0');
                return `${lineNumber}: ${line}`;
            });
    
            return numberedLines.join('\n');
        } catch (error) {
            console.error(`讀取檔案時發生錯誤: ${error}`);
            return `讀取檔案時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`;
        }
    }
  • main.ts:70-85 (registration)
    Registers the 'codeFileRead' MCP tool with name, description, input schema (filePath: string), and thin async handler that delegates to myFileReader.readFileWithLineNumbers and formats MCP response.
    server.tool("codeFileRead",
        "讀取程式碼檔案並顯示行號,用於確認檔案實際行數以便編輯操作。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)",
        { filePath: z.string() },
        async ({ filePath }) => {
            try {
                const result = await myFileReader.readFileWithLineNumbers(filePath);
                return {
                    content: [{ type: "text", text: result }]
                };
            } catch (error) {
                return {
                    content: [{ type: "text", text: `讀取檔案失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }]
                };
            }
        }
    );
  • main.ts:72-72 (schema)
    Input schema validation using Zod for the tool: requires 'filePath' as string.
    { filePath: z.string() },
  • main.ts:8-8 (helper)
    Import of the myFileReader class used by the tool handler.
    import { myFileReader } from "./tools/csFileReader.js";

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