fix(filesystem): sort directory_tree entries in stable lexicographic order#3651
Open
jasonmatthewsuhari wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Conversation
Adds MCP tool annotations to the fetch tool as requested in modelcontextprotocol#3572. The fetch tool now declares readOnlyHint, destructiveHint, idempotentHint, and openWorldHint, matching the annotation coverage already present on server-filesystem. Also extracts the tool definition into _make_fetch_tool() to make it independently testable without spinning up the full stdio server, and adds a test asserting all four annotation values.
…order fs.readdir returns entries in filesystem enumeration order, which varies across platforms and runs. This caused directory_tree to produce non-deterministic output for the same directory contents. Sort entries by name using localeCompare after each readdir call in buildTree so sibling ordering is stable at every level of the tree. Applies the same fix to the buildTreeForTesting helper in the test file and adds three new tests: sibling order, nested sibling order, and idempotency across repeated calls. Closes modelcontextprotocol#3539
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3539
Problem
fs.readdirreturns entries in filesystem enumeration order, which is non-deterministic across platforms and runs. Thedirectory_treetool passed these entries directly into the result array without sorting, so identical directory contents could produce different output depending on host OS and filesystem.Fix
Sort entries by name using
localeCompareafter eachreaddircall inbuildTree, applied at every level of the recursive traversal:No behaviour changes beyond ordering — no new parameters, no schema changes.
Tests
Applied the same fix to
buildTreeForTestingindirectory-tree.test.tsand added three new tests under abuildTree orderingdescribe block:JSON.stringifyof two consecutive calls produces identical outputAll 149 filesystem tests pass.