Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/filesystem/__tests__/roots-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getValidRootDirectories } from '../roots-utils.js';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, realpathSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { pathToFileURL } from 'url';
import type { Root } from '@modelcontextprotocol/sdk/types.js';

describe('getValidRootDirectories', () => {
Expand Down Expand Up @@ -45,6 +46,21 @@ describe('getValidRootDirectories', () => {
expect(result).toHaveLength(3);
});

it('should handle standard file URIs from pathToFileURL', async () => {
// pathToFileURL produces properly-encoded URIs (e.g. file:///C:/... on Windows)
// This is the format VS Code and other editors send via MCP roots
const roots: Root[] = [
{ uri: pathToFileURL(testDir1).href, name: 'Standard File URI' },
{ uri: pathToFileURL(testDir2).href, name: 'Standard File URI 2' },
];

const result = await getValidRootDirectories(roots);

expect(result).toContain(testDir1);
expect(result).toContain(testDir2);
expect(result).toHaveLength(2);
});
Comment on lines +49 to +62
Copy link
Member

Choose a reason for hiding this comment

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

It appears that the tests pass on non-Windows platforms regardless of the changes in #3205. Since CI also does not seem to run tests on Windows, how about converting this into a regression test that can be reproduced on macOS and other platforms as shown below?

Suggested change
it('should handle standard file URIs from pathToFileURL', async () => {
// pathToFileURL produces properly-encoded URIs (e.g. file:///C:/... on Windows)
// This is the format VS Code and other editors send via MCP roots
const roots: Root[] = [
{ uri: pathToFileURL(testDir1).href, name: 'Standard File URI' },
{ uri: pathToFileURL(testDir2).href, name: 'Standard File URI 2' },
];
const result = await getValidRootDirectories(roots);
expect(result).toContain(testDir1);
expect(result).toContain(testDir2);
expect(result).toHaveLength(2);
});
it('should handle standard file URIs from pathToFileURL', async () => {
// pathToFileURL encodes special characters (e.g. spaces become %20, # becomes %23).
// fileURLToPath() decodes them correctly; slice(7) does not.
// e.g. pathToFileURL('/tmp/my dir').href => 'file:///tmp/my%20dir'
// fileURLToPath('file:///tmp/my%20dir') => '/tmp/my dir' (correct)
// 'file:///tmp/my%20dir'.slice(7) => '/tmp/my%20dir' (wrong)
const dirWithSpaces = join(testDir1, 'dir with spaces');
const dirWithHash = join(testDir1, 'dir#hash');
mkdirSync(dirWithSpaces);
mkdirSync(dirWithHash);
const roots: Root[] = [
{ uri: pathToFileURL(dirWithSpaces).href },
{ uri: pathToFileURL(dirWithHash).href },
];
const result = await getValidRootDirectories(roots);
expect(result).toContain(dirWithSpaces);
expect(result).toContain(dirWithHash);
expect(result).toHaveLength(2);
});


it('should normalize complex paths', async () => {
const subDir = join(testDir1, 'subdir');
mkdirSync(subDir);
Expand Down
2 changes: 1 addition & 1 deletion src/filesystem/roots-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import os from 'os';
import { normalizePath } from './path-utils.js';
import type { Root } from '@modelcontextprotocol/sdk/types.js';
import { fileURLToPath } from "url";
import { fileURLToPath } from 'url';

/**
* Converts a root URI to a normalized directory path with basic security validation.
Expand Down