fix(filesystem): normalize path separators in search_files results#3965
Open
ianliuy wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(filesystem): normalize path separators in search_files results#3965ianliuy wants to merge 1 commit intomodelcontextprotocol:mainfrom
ianliuy wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
On Windows, path.relative() returns backslash-separated paths (e.g.,
src\index.ts), but minimatch expects forward slashes for glob pattern
matching (e.g., **/*.ts). This causes search_files and directory_tree
exclude patterns to fail on Windows.
Fix by normalizing relativePath to use forward slashes via
.split(path.sep).join('/') before passing to minimatch in both
searchFilesWithValidation (lib.ts) and buildTree (index.ts).
Fixes modelcontextprotocol#3517
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Yiyang Liu <37043548+ianliuy@users.noreply.github.com>
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.
What's broken?
The filesystem server's
search_filestool anddirectory_treeexclude patterns return inconsistent results across platforms. On Windows, glob patterns like**/*.tsfail to match because paths use backslashes internally.Who is affected?
Anyone using the filesystem MCP server on Windows. Linux/macOS users are unaffected since
path.sepis already/.When does it trigger?
Every time
search_filesordirectory_tree(with exclude patterns) is called on Windows. The glob patterns silently fail to match, returning incorrect results.Where is the bug?
src/filesystem/lib.ts—searchFilesWithValidation(), line computingrelativePathsrc/filesystem/index.ts—buildTree(), line computingrelativePathBoth use
path.relative()which returns backslash-separated paths on Windows (e.g.,src\index.ts), then pass them directly tominimatchwhich expects forward slashes.Why does it happen?
path.relative()uses the OS-native separator (\on Windows,/on Linux).minimatchis a glob library that operates on forward-slash paths. When backslash paths are passed to minimatch on Windows, patterns like**/*.tsdon't matchsrc\index.ts.How did we fix it?
Normalize
relativePathto forward slashes immediately after computing it:This is applied in both locations. The fix is minimal (2 lines changed) and only affects the path format used for glob matching — the returned full paths from
search_filesremain OS-native.How do we know it works?
All 57 related tests pass (lib.test.ts, directory-tree.test.ts, structured-content.test.ts including the
search_filestest). The.split(path.sep).join('/')pattern is a no-op on Linux/macOS (wherepath.sepis already/), so there's zero risk of regression on those platforms.Fixes #3517