fileRead
Reads file content from specified paths, supporting plain text and JSON formats. Enables users to extract and process data directly from files for various applications.
Instructions
讀取檔案內容,支援純文本和JSON格式
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encoding | No | ||
| filePath | Yes | ||
| mode | No | text |
Implementation Reference
- main.ts:176-217 (registration)Registration of the 'fileRead' tool, including inline schema validation and handler logic that delegates to FileWriterTool for actual file reading.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 : "未知錯誤"}` }] }; } } );
- tools/fileWriterTool.ts:15-29 (helper)Static helper method in FileWriterTool class that reads the content of a text file with optional encoding.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 : '未知錯誤'}`; } }
- tools/fileWriterTool.ts:36-62 (helper)Static helper method in FileWriterTool class that reads and parses a JSON file, returning structured result or error.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 : '未知錯誤'}` }; } }
- main.ts:178-182 (schema)Zod schema for fileRead tool input parameters: filePath (required), mode (text/json, default text), encoding (optional).{ filePath: z.string(), mode: z.enum(['text', 'json']).default('text'), encoding: z.string().optional() },