feat: Add compare_directories tool for directory comparison - #3890
feat: Add compare_directories tool for directory comparison#3890nagual2 wants to merge 11 commits into
Conversation
… usage, add types
…archFilesWithValidation calls
olaservo
left a comment
There was a problem hiding this comment.
Thanks for the contribution. A couple of implementation issues and a scope question:
Implementation issues:
- The
compareContentparameter is accepted in the schema and function signature but never used — comparison always uses size/mtime regardless of the flag. Either implement content comparison or remove the parameter. - Files with identical content but different timestamps will be reported as "different," and files with different content but the same size/mtime will be reported as "identical." This should at least be documented clearly, or addressed with actual content hashing when
compareContentis true.
Scope question:
Per our CONTRIBUTING.md, we're selective about new features that aren't core to a server's purpose. Directory comparison feels like it could be composed by an LLM using existing tools (directory_tree, read_file), or handled by a dedicated server. Would you consider publishing this as a standalone MCP server on the Registry instead?
This review was assisted by Claude Code.
- Add compareFileContents helper for byte-by-byte comparison - compareContent=true now reads and compares actual file contents - Files with same content (but different mtime) go to identical - Fix: compareContent parameter is now actually used - When sizes differ, skip content comparison for efficiency
- Test same-size different-content detection with compareContent=true - Test same-content different-mtime detection (should be identical) - Test binary file content comparison - Test compareContent=false behavior (mtime-based)
|
@olaservo Thanks for the detailed review! I've addressed the implementation issues: Fixed:
|
- save and restore global allowedDirectories state - fixes tests that expect validatePath to work with test directories
The test was writing to marker.txt expecting diff-mtime.txt mtime to change. Now it directly re-writes diff-mtime.txt to update its mtime.
|
@claude review |
|
Claude finished @cliffhall's task in 1m 37s —— View job PR Review:
|
LuuOW
left a comment
There was a problem hiding this comment.
Technical audit: Verified MCP server implementation for consistency with current SDK patterns.
|
Why built‑in directory comparison in MCP dramatically reduces token usage It is true that a modern LLM can, in theory, compare two directories by using basic tools like list_directory, get_file_info, and read_file. The model could script the comparison step by step or even generate code on the fly. However, in practice this approach leads to massive token waste and inefficiency for several reasons.
Retrieve the file list for both directories – 2 calls. For every pair of identically named files, obtain metadata or read content – up to 100 additional read_file or get_file_info calls. Result: over 100 tool calls and thousands of tokens just to fetch file lists and contents.
With a native compare_directories tool in the MCP server, token consumption is reduced by 1–2 orders of magnitude: Instead of dozens or hundreds of tool calls and kilobytes of data, a single call with parameters (directory paths, comparison flags) is sufficient. The server returns a compact, structured result – only the lists of differences (missing files, files with differing content, identical files, etc.). No full content of identical files is transmitted. The LLM is freed from performing step‑by‑step comparison logic. This reduces the chance of mistakes and frees context for higher‑level reasoning. Analogy: Of course an LLM could add two numbers by repeatedly calling a get_digit tool, but a native add tool is orders of magnitude more efficient. compare_directories is exactly that kind of “native operation” for a file system. Therefore, although directory comparison can be emulated with basic primitives, from the perspective of token economy, latency, and reliability, it fully deserves a place in a reference MCP server – as an example of using MCP for resource‑intensive operations that are too expensive to delegate to the LLM step by step. |

Summary
Adds
compare_directoriestool to filesystem MCP server for comparing two directory structures.New Tool:
compare_directoriesCompares two directories and returns:
Changes
src/filesystem/lib.ts- AddedcompareDirectories()functionsrc/filesystem/index.ts- Registered toolsrc/filesystem/__tests__/lib.test.ts- Added 6 testsTests
✅ Files unique to each directory
✅ Content difference detection
✅ Identical file identification
✅ Empty directory handling
✅ Nested structures
All tests pass with
npm test.