Repository navigation
mcp: accept Windows drive letter file roots - #1443
akshita317 wants to merge 5 commits into
Conversation
fileRoot turns the client's roots into absolute file paths for FileResourceHandler. On Windows a root for a drive letter path is a URI such as file:///C:/Users/me/project (RFC 8089, Appendix E.2), whose path is /C:/Users/me/project. fileRoot converted that to \C:\Users\me\project, which is not an absolute path on Windows, so every such root failed with "not an absolute path", and with it every read through FileResourceHandler once the client had any roots. Drop the slash before a drive letter on Windows. VS Code's form, file:///c%3A/..., decodes to the same path. TestFileRoot is no longer skipped on Windows and gets drive letter cases there; they fail without the change. TestFileURIPath covers the conversion for both Windows and other systems on any platform.
chrikrah
left a comment
There was a problem hiding this comment.
From my perspective this can be merged. Passing goos in is what makes it hold: no workflow here runs on Windows, so the new TestFileRoot cases never execute in CI, while TestFileURIPath does on Linux. This takes up the Windows work 8361df7 deferred for roots only; computeURIFilepath still skips there.
$ go test ./mcp/ -run 'TestFileRoot|TestFileURIPath' -count=1 # head c9e6d6b, merge base 6f47cb7, go1.27.1 linux/amd64
ok github.com/modelcontextprotocol/go-sdk/mcp 0.011s
$ <prefix the drive-letter condition in fileURIPath with false> && <same command>
--- FAIL: TestFileURIPath (0.00s)
resource_test.go:75: fileURIPath("/C:/foo", "windows") = "/C:/foo", want "C:/foo"
FAIL
$ GOOS=windows go test -c -o /dev/null ./mcp/ && go test ./... -count=1
# compiles; 14 packages ok
# not run: the Windows TestFileRoot cases, no Windows host here
Did you run those cases on a Windows machine?
A file URI with a host other than localhost names a UNC path on Windows (RFC 8089, Appendix E.3.1), so file://server/share/dir is now the root \server\share\dir rather than a path that drops the host. With file roots working on Windows, the resource tests skipped there pass, so remove the skips. TestEndToEnd built its root as "file://" followed by the path, which on Windows is not a file URI (file://C:\dir); build it with net/url instead. Drop the stale comment about Localize in fileRoot.
|
@chrikrah Yes. Here they are on a Windows 11 machine, at 16a042b (which also takes the follow-ups nahapetyan-serob asked for: UNC roots, and unskipping the other Windows tests): As you say, no CI workflow runs on Windows, so these only run on a machine like this one. |
chrikrah
left a comment
There was a problem hiding this comment.
@akshita317 Thanks for the Windows run. 16a042b is good to go for me.
$ go test ./mcp/ -run 'TestFileRoot|TestFileURIPath' -count=1 # 16a042b, base 6f47cb7, go1.27.1 linux/amd64
ok github.com/modelcontextprotocol/go-sdk/mcp 0.032s
# same command, UNC branch of fileURIPath disabled
resource_test.go:80: fileURIPath("server", "/share/foo", "windows") = "/share/foo", want "//server/share/foo"
FAIL
$ GOOS=windows go test -c -o /dev/null ./mcp/ && go vet ./mcp/ # clean
# not run: the Windows-only tests, no Windows host here
non-blocking: file://C:/foo now parses as host C: and becomes //C:/foo, a UNC path to filepath.IsAbs. RFC 8089 section 2 allows no port in a file authority, and main answers not an absolute path here. The "file://" + rootAbs slip TestEndToEnd made now gives a dead root.
$ go test ./mcp/ -run TestProbeDriveHost -v # throwaway test at 16a042b
file://C:/foo -> fileURIPath("C:", "/foo", "windows") = "//C:/foo"
$ go run probe.go # url.Parse only
file://server:445/share host="server:445" port="445" path="/share" err=<nil>
# not run on Windows: IsAbs read from path_windows.go
The 8361df7 TODO(jba) for UNC roots in computeURIFilepath still stands. Rejecting rurl.Port() != "" || strings.HasSuffix(rurl.Host, ":") in fileRoot plus a TestFileRoot row fits here. Can you add it?
|
@akshita317, once you have addressed @chrikrah's comment about |
The authority of a file URI is a host without a port (RFC 8089, Section 2). Refusing one also catches "file://C:/dir", whose host is "C:": with UNC roots, it would otherwise become \C:\dir on Windows. Run TestComputeURIFilepath on Windows against a drive letter directory and a UNC directory too, which resolves its TODO.
|
@chrikrah Thanks, good catch. Added in ee9c453: @nahapetyan-serob The same commit resolves the On Windows 11 (go1.25.3 windows/amd64): |
|
@akshita317, thank you for contribution! |
fileRootturns the client's roots into absolute file paths forFileResourceHandler. On Windows, a root for a drive letter path is a URI such asfile:///C:/Users/me/project(RFC 8089, Appendix E.2), whose path is/C:/Users/me/project.fileRootconverted that to\C:\Users\me\project, which is not an absolute path on Windows, so every such root failed withnot an absolute path, and with it every read throughFileResourceHandleras soon as the client reported any roots.TestFileRootwas skipped on Windows with a "TODO: fix for Windows".This drops the slash before a drive letter on Windows (
fileURIPath). VS Code's form,file:///c%3A/..., decodes to the same path. Nothing changes on other systems.Tests:
TestFileRootis no longer skipped on Windows and has drive letter cases there (upper and lower case,..,file:/C:/…,c%3A, andfile:///foostill rejected). They fail without the change; the existing cases run unchanged elsewhere.TestFileURIPathcovers the conversion forwindowsandlinuxon any platform, so CI on Linux checks it too.The other Windows-skipped tests in
resource_test.go(TestComputeURIFilepath,TestReadFileResource, …) use POSIX paths in their expectations; I left them for a separate change.AI assistance: prepared with Claude Code.