compare_files
Compare two files or directories to identify differences and analyze changes between them.
Instructions
Compare two files or directories
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path1 | Yes | First path | |
| path2 | Yes | Second path |
Implementation Reference
- src/tools/metadata.ts:349-416 (handler)The handler implementation of the 'compare_files' tool, which takes two file paths, validates their existence, and performs a comparison.
async function compareFilesImpl(input: { path1: string; path2: string; }): Promise<ToolResult> { try { const path1 = path.resolve(input.path1); const path2 = path.resolve(input.path2); const [stats1, stats2] = await Promise.all([ fs.stat(path1).catch(() => null), fs.stat(path2).catch(() => null), ]); if (!stats1) { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'FILE_NOT_FOUND', message: `First path not found: ${input.path1}`, }), }, ], }; } if (!stats2) { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'FILE_NOT_FOUND', message: `Second path not found: ${input.path2}`, }), }, ], }; } const comparison = { path1, path2, sameType: stats1.isFile() === stats2.isFile(), sameSize: stats1.size === stats2.size, file1: { isFile: stats1.isFile(), size: stats1.size, sizeFormatted: formatBytes(stats1.size), modified: stats1.mtime.toISOString(), }, file2: { isFile: stats2.isFile(), size: stats2.size, sizeFormatted: formatBytes(stats2.size), modified: stats2.mtime.toISOString(), }, newerFile: stats1.mtime > stats2.mtime ? 'path1' : 'path2', sizeDifference: stats1.size - stats2.size, sizeDifferenceFormatted: formatBytes(Math.abs(stats1.size - stats2.size)), }; return { content: [ { - src/tools/metadata.ts:484-495 (registration)The registration of the 'compare_files' tool using the server.tool method, including parameter schema and calling the handler.
// compare_files tool server.tool( 'compare_files', 'Compare two files or directories', { path1: z.string().describe('First path'), path2: z.string().describe('Second path'), }, async (args) => { return await compareFilesImpl(args); } );