tailFile
Retrieve the last N lines of a file to monitor logs or extract recent data efficiently. Specify the file path and number of lines for quick file analysis and debugging.
Instructions
Read the last N lines from a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | The path to the file to tail | |
| lines | Yes | Number of lines to read from the end of the file |
Implementation Reference
- src/file.ts:98-125 (handler)The handler function that reads the last N lines from the specified file and returns them as text content.export async function tailFile(args: { filePath: string; lines: number }, { log }: LogContext) { try { log.info(`Tailing file at path: ${args.filePath} (${args.lines} lines)`); const normalizedPath = path.normalize(args.filePath); // Read the entire file content const fileContent = await fs.promises.readFile(normalizedPath, "utf8"); // Split by newlines and get the last N lines const allLines = fileContent.split('\n'); const lastLines = allLines.slice(-args.lines).join('\n'); log.info(`Successfully tailed ${args.lines} lines from file: ${normalizedPath}`); return { content: [ { type: "text", text: lastLines, }, ], }; } catch (error: any) { log.error(`Error tailing file: ${error.message}`); throw new Error(`Failed to tail file: ${error.message}`); } }
- src/index.ts:51-54 (schema)Zod input schema defining filePath (string) and lines (positive integer).parameters: z.object({ filePath: z.string().describe("The path to the file to tail"), lines: z.number().int().positive().describe("Number of lines to read from the end of the file"), }),
- src/index.ts:48-56 (registration)Registers the tailFile tool with the FastMCP server, specifying name, description, input schema, and handler.addTool({ name: "tailFile", description: "Read the last N lines from a file", parameters: z.object({ filePath: z.string().describe("The path to the file to tail"), lines: z.number().int().positive().describe("Number of lines to read from the end of the file"), }), execute: tailFile, });