Skip to content

mcp: accept Windows drive letter file roots - #1443

Open
akshita317 wants to merge 5 commits into
modelcontextprotocol:mainfrom
akshita317:mcp-file-roots-windows
Open

akshita317 wants to merge 5 commits into
modelcontextprotocol:mainfrom
akshita317:mcp-file-roots-windows

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

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 as soon as the client reported any roots. TestFileRoot was 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:

  • TestFileRoot is no longer skipped on Windows and has drive letter cases there (upper and lower case, .., file:/C:/…, c%3A, and file:///foo still rejected). They fail without the change; the existing cases run unchanged elsewhere.
  • TestFileURIPath covers the conversion for windows and linux on 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.

akshita317 and others added 2 commits October 24, 2026 01:27
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 chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread mcp/resource.go Resolved
Comment thread mcp/resource_test.go Resolved
Comment thread mcp/resource.go Resolved
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.
@akshita317

Copy link
Copy Markdown
Contributor Author

@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):

$ go test ./mcp/ -run 'TestEndToEnd|TestFileRoot|TestFileURIPath|TestComputeURIFilepath|TestReadFileResource' -count=1 -v   # go1.25.3 windows/amd64
--- PASS: TestEndToEnd (0.01s)
--- PASS: TestFileRoot (0.00s)
--- PASS: TestFileURIPath (0.00s)
--- PASS: TestComputeURIFilepath (0.00s)
--- PASS: TestReadFileResource (0.00s)
--- PASS: TestComputeURIFilepathLocalizeError (0.00s)
ok  	github.com/modelcontextprotocol/go-sdk/mcp	0.395s

$ go test ./mcp/ -count=1
ok  	github.com/modelcontextprotocol/go-sdk/mcp	34.097s

As you say, no CI workflow runs on Windows, so these only run on a machine like this one.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

@nahapetyan-serob

Copy link
Copy Markdown
Contributor

@akshita317, once you have addressed @chrikrah's comment about TestComputeURIFilepath, we can merge.

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.
@akshita317

akshita317 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@chrikrah Thanks, good catch. Added in ee9c453: fileRoot now refuses a host with a port, or one ending in :, with "host has a port", before the UNC branch runs. file://C:/foo and file://server:445/share/foo are TestFileRoot rows on every platform. Both rows fail without the check.

@nahapetyan-serob The same commit resolves the TODO(jba) in TestComputeURIFilepath. On Windows it also runs every case against C:\files and \\server\share\files, as fileRoot returns them.

On Windows 11 (go1.25.3 windows/amd64):

$ go test ./mcp/ -run 'TestFileRoot|TestFileURIPath|TestComputeURIFilepath' -count=1 -v
--- PASS: TestFileRoot
--- PASS: TestFileURIPath
--- PASS: TestComputeURIFilepath   (27 subtests: 9 cases x /files, C:\files, \\server\share\files)
--- PASS: TestComputeURIFilepathLocalizeError
$ go test ./mcp/ -count=1
ok  	github.com/modelcontextprotocol/go-sdk/mcp

@nahapetyan-serob

Copy link
Copy Markdown
Contributor

@akshita317, thank you for contribution!

This branch has not been deployed

No deployments
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.

4 participants