Skip to content

refactor(filesystem): extract buildDirectoryTree to lib.ts#4055

Open
FazeelUsmani wants to merge 1 commit intomodelcontextprotocol:mainfrom
FazeelUsmani:refactor/filesystem-extract-build-directory-tree
Open

refactor(filesystem): extract buildDirectoryTree to lib.ts#4055
FazeelUsmani wants to merge 1 commit intomodelcontextprotocol:mainfrom
FazeelUsmani:refactor/filesystem-extract-build-directory-tree

Conversation

@FazeelUsmani
Copy link
Copy Markdown

@FazeelUsmani FazeelUsmani commented Apr 27, 2026

Description

closes ##3537

Move the buildTree closure out of the directory_tree handler in index.ts and export it as buildDirectoryTree from lib.ts. Replaces the duplicated buildTreeForTesting copy in __tests__/directory-tree.test.ts so tests now exercise the production function directly.

No behavior change.

Server Details

  • Server: filesystem
  • Changes to: internal refactor (no tool/resource/prompt surface changes)

Motivation and Context

buildTree was defined as a closure inside the directory_tree request handler. The test suite kept a near-identical copy (buildTreeForTesting) to exercise the recursion logic. Any drift between the two would silently invalidate the tests - exactly the kind of duplication a future change is most likely to break.

Hoisting buildTree to lib.ts and importing it in the test removes the duplication and ensures regressions are caught against the production function.

How Has This Been Tested?

npm test in src/filesystem - 146/146 tests pass.

The directory-tree.test.ts suite now drives the real buildDirectoryTree through a real tmpdir tree, which exercises validatePath end-to-end (the previous helper bypassed it; an fs.realpath call was added to handle macOS's /var/private/var symlink).

Breaking Changes

None. The directory_tree tool's input schema, output schema, and runtime behavior are unchanged.

Types of changes

  • No functional change (refactor / test cleanup)

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

Move the buildTree closure out of the directory_tree handler and
export it as buildDirectoryTree. Replaces the duplicated
buildTreeForTesting in the test suite so tests now exercise the
production function directly.

No behavior change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant