Skip to main content
Glama

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
NameRequiredDescriptionDefault
path1YesFirst path
path2YesSecond path

Implementation Reference

  • 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: [
            {
  • 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);
      }
    );

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/mcp-tool-shop-org/mcp-file-forge'

If you have feedback or need assistance with the MCP directory API, please join our Discord server