You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DirectoryResource listing uses OS path separators and unstable order #3589
What happened DirectoryResource.read() returns str(path.relative_to(...)), so on Windows a
recursive listing contains sub\c.txt, while Linux/macOS return sub/c.txt.
The file order also follows glob(), which depends on the filesystem, so the
same directory can produce a different JSON listing on different machines.
Expected
Resource content is sent to clients, so the listing should be the same on every
platform: / separators and a stable (sorted) order.
Reproduce (on Windows)
import anyio, tempfile
from pathlib import Path
from mcp.server.mcpserver.resources import DirectoryResource
Possible fix
Use Path.as_posix() and sorted(...) in read(). I have a tested fix with
tests on my fork: tasnemawawde-afk@06f76b5
Happy to open a PR if a maintainer wants one.
(I used Claude to help find and write this fix; I reviewed it and ran the tests myself.)
Example Code
Python & MCP Python SDK
Python 3.14.7, Windows 11
MCP Python SDK: main branch at commit f1b6589
Good catch. as_posix() is the right fix at the wire level, and there is a stronger reason than cosmetic consistency: clients take these listing values and construct child resource URIs from them, and the RFC 3986 path grammar allows only / as a separator. A \ in a URI path is non-conformant and breaks resolution on any non-Windows client, so normalizing before serialization is the correct place.
On ordering: stability matters because clients that hash or diff resource listings for caching will churn on every read when glob() order varies by filesystem. sorted() over the posix-form strings is deterministic (codepoint order), so sorting after conversion is the right pairing; sort the final wire values rather than the OS paths.
One edge worth covering in the test: filenames that differ only in case on a case-insensitive filesystem (a.txt and A.txt on NTFS). sorted() gives deterministic codepoint order, which is fine, but it is worth asserting so nobody later "fixes" the sort back to filesystem enumeration order.
We hit this exact inconsistency in a gateway that aggregates multiple business APIs (corpusiq.io) where directory listings get compared across Windows and Linux workers, so making the wire format platform-neutral matters well beyond cosmetics.
That is neither insertion order (c, a, b, sub/z) nor alphabetical; it follows the raw filesystem order returned by rglob().
Mechanism is in src/mcp/server/mcpserver/resources/types.py:
list_files() at line 215 calls self.path.glob() / self.path.rglob() — those return directory entries in filesystem (readdir) order, which varies by FS and platform.
read() at line 229 builds the returned strings with str(f.relative_to(self.path)), which uses the OS-native path separator (os.sep). On Windows that produces sub\c.txt; on Linux/macOS it produces sub/c.txt.
Fix direction: sort the file list and posix-normalize the paths so the JSON listing is identical across platforms:
The reporter already has a tested fix on their fork; I'm happy to open a PR with the same approach plus coverage for both v2 (main) and v1.x if a maintainer assigns this or marks it help wanted.
Disclosure: I contribute to thyn-ai/Algenta (open-source agent tooling).
Activity
Make DirectoryResource listing stable and cross-platform
Ben-Home commentedon Sep 29, 2026
Good catch. as_posix() is the right fix at the wire level, and there is a stronger reason than cosmetic consistency: clients take these listing values and construct child resource URIs from them, and the RFC 3986 path grammar allows only / as a separator. A \ in a URI path is non-conformant and breaks resolution on any non-Windows client, so normalizing before serialization is the correct place.
On ordering: stability matters because clients that hash or diff resource listings for caching will churn on every read when glob() order varies by filesystem. sorted() over the posix-form strings is deterministic (codepoint order), so sorting after conversion is the right pairing; sort the final wire values rather than the OS paths.
One edge worth covering in the test: filenames that differ only in case on a case-insensitive filesystem (a.txt and A.txt on NTFS). sorted() gives deterministic codepoint order, which is fine, but it is worth asserting so nobody later "fixes" the sort back to filesystem enumeration order.
We hit this exact inconsistency in a gateway that aggregates multiple business APIs (corpusiq.io) where directory listings get compared across Windows and Linux workers, so making the wire format platform-neutral matters well beyond cosmetics.
0xamlab commentedon Oct 1, 2026
Confirmed the nondeterminism on current main.
Minimal repro (macOS, but the separator problem is the same path on Windows):
Output I got:
That is neither insertion order (
c,a,b,sub/z) nor alphabetical; it follows the raw filesystem order returned byrglob().Mechanism is in
src/mcp/server/mcpserver/resources/types.py:list_files()at line 215 callsself.path.glob()/self.path.rglob()— those return directory entries in filesystem (readdir) order, which varies by FS and platform.read()at line 229 builds the returned strings withstr(f.relative_to(self.path)), which uses the OS-native path separator (os.sep). On Windows that producessub\c.txt; on Linux/macOS it producessub/c.txt.Fix direction: sort the file list and posix-normalize the paths so the JSON listing is identical across platforms:
The reporter already has a tested fix on their fork; I'm happy to open a PR with the same approach plus coverage for both v2 (main) and v1.x if a maintainer assigns this or marks it
help wanted.Disclosure: I contribute to thyn-ai/Algenta (open-source agent tooling).