Skip to main content
Glama

fileRead

Read file contents from text and JSON formats to access data stored in files for processing or analysis.

Instructions

讀取檔案內容,支援純文本和JSON格式

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes
modeNotext
encodingNo

Implementation Reference

  • main.ts:176-217 (registration)
    Registration of the 'fileRead' tool including description, input schema, and inline handler function that supports text and JSON modes by delegating to FileWriterTool.
    server.tool("fileRead", "讀取檔案內容,支援純文本和JSON格式", { filePath: z.string(), mode: z.enum(['text', 'json']).default('text'), encoding: z.string().optional() }, async ({ filePath, mode = 'text', encoding = 'utf8' }) => { try { if (mode === 'text') { const content = await FileWriterTool.readTextFile(filePath, encoding as BufferEncoding); // 檢查是否有錯誤 if (content.startsWith('錯誤') || content.startsWith('讀取檔案時發生錯誤')) { return { content: [{ type: "text", text: content }] }; } return { content: [{ type: "text", text: content }] }; } else { const result = await FileWriterTool.readJsonFile(filePath); if (!result.success) { return { content: [{ type: "text", text: result.error || '讀取JSON檔案失敗' }] }; } return { content: [{ type: "text", text: JSON.stringify(result.data, null, 2) }] }; } } catch (error) { return { content: [{ type: "text", text: `檔案讀取失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );
  • Core implementation for reading text files, used by fileRead tool in text mode.
    static async readTextFile(filePath: string, encoding: BufferEncoding = 'utf8'): Promise<string> { try { // 檢查檔案是否存在 if (!existsSync(filePath)) { return `錯誤: 檔案 ${filePath} 不存在`; } // 讀取檔案內容 const content = await fs.readFile(filePath, { encoding }); return content; } catch (error) { console.error(`讀取檔案時發生錯誤: ${error}`); return `讀取檔案時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`; } }
  • Core implementation for reading and parsing JSON files, used by fileRead tool in json mode.
    static async readJsonFile(filePath: string): Promise<{ success: boolean; data?: any; error?: string }> { try { // 讀取檔案 const content = await this.readTextFile(filePath); // 如果讀取出錯,返回錯誤 if (content.startsWith('錯誤') || content.startsWith('讀取檔案時發生錯誤')) { return { success: false, error: content }; } // 解析JSON try { const jsonData = JSON.parse(content); return { success: true, data: jsonData }; } catch (parseError) { return { success: false, error: `JSON解析錯誤: ${parseError instanceof Error ? parseError.message : '未知錯誤'}` }; } } catch (error) { return { success: false, error: `讀取JSON檔案時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}` }; } }
  • Zod input schema defining parameters for fileRead tool: filePath (required), mode (text/json), encoding (optional).
    filePath: z.string(), mode: z.enum(['text', 'json']).default('text'), encoding: z.string().optional() },
  • main.ts:183-216 (handler)
    Inline handler function for fileRead tool that handles input parameters, calls appropriate FileWriterTool methods based on mode, processes results, and formats MCP response.
    async ({ filePath, mode = 'text', encoding = 'utf8' }) => { try { if (mode === 'text') { const content = await FileWriterTool.readTextFile(filePath, encoding as BufferEncoding); // 檢查是否有錯誤 if (content.startsWith('錯誤') || content.startsWith('讀取檔案時發生錯誤')) { return { content: [{ type: "text", text: content }] }; } return { content: [{ type: "text", text: content }] }; } else { const result = await FileWriterTool.readJsonFile(filePath); if (!result.success) { return { content: [{ type: "text", text: result.error || '讀取JSON檔案失敗' }] }; } return { content: [{ type: "text", text: JSON.stringify(result.data, null, 2) }] }; } } 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