@@ -6,19 +6,30 @@ import type { Root } from '@modelcontextprotocol/sdk/types.js';
66import { fileURLToPath } from "url" ;
77
88/**
9- * Converts a root URI to a normalized directory path with basic security validation.
9+ * Converts a root URI to normalized directory paths with basic security validation.
10+ *
11+ * Returns both the original normalized path and the resolved path when they differ.
12+ * This keeps roots-provided directories symmetric with command-line directories,
13+ * so paths addressed through either a symlink/mapped-drive form or its resolved
14+ * target continue to pass allow-list validation.
15+ *
1016 * @param rootUri - File URI (file://...) or plain directory path
11- * @returns Promise resolving to validated path or null if invalid
17+ * @returns Promise resolving to validated paths or null if invalid
1218 */
13- async function parseRootUri ( rootUri : string ) : Promise < string | null > {
19+ async function parseRootUri ( rootUri : string ) : Promise < string [ ] | null > {
1420 try {
1521 const rawPath = rootUri . startsWith ( 'file://' ) ? fileURLToPath ( rootUri ) : rootUri ;
16- const expandedPath = rawPath . startsWith ( '~/' ) || rawPath === '~'
17- ? path . join ( os . homedir ( ) , rawPath . slice ( 1 ) )
22+ const expandedPath = rawPath . startsWith ( '~/' ) || rawPath === '~'
23+ ? path . join ( os . homedir ( ) , rawPath . slice ( 1 ) )
1824 : rawPath ;
1925 const absolutePath = path . resolve ( expandedPath ) ;
26+ const normalizedOriginal = normalizePath ( absolutePath ) ;
2027 const resolvedPath = await fs . realpath ( absolutePath ) ;
21- return normalizePath ( resolvedPath ) ;
28+ const normalizedResolved = normalizePath ( resolvedPath ) ;
29+
30+ return normalizedOriginal === normalizedResolved
31+ ? [ normalizedResolved ]
32+ : [ normalizedOriginal , normalizedResolved ] ;
2233 } catch {
2334 return null ; // Path doesn't exist or other error
2435 }
@@ -41,37 +52,44 @@ function formatDirectoryError(dir: string, error?: unknown, reason?: string): st
4152
4253/**
4354 * Resolves requested root directories from MCP root specifications.
44- *
55+ *
4556 * Converts root URI specifications (file:// URIs or plain paths) into normalized
4657 * directory paths, validating that each path exists and is a directory.
4758 * Includes symlink resolution for security.
48- *
59+ *
4960 * @param requestedRoots - Array of root specifications with URI and optional name
5061 * @returns Promise resolving to array of validated directory paths
5162 */
5263export async function getValidRootDirectories (
5364 requestedRoots : readonly Root [ ]
5465) : Promise < string [ ] > {
5566 const validatedDirectories : string [ ] = [ ] ;
56-
67+ const seenDirectories = new Set < string > ( ) ;
68+
5769 for ( const requestedRoot of requestedRoots ) {
58- const resolvedPath = await parseRootUri ( requestedRoot . uri ) ;
59- if ( ! resolvedPath ) {
70+ const candidatePaths = await parseRootUri ( requestedRoot . uri ) ;
71+ if ( ! candidatePaths ) {
6072 console . error ( formatDirectoryError ( requestedRoot . uri , undefined , 'invalid path or inaccessible' ) ) ;
6173 continue ;
6274 }
63-
64- try {
65- const stats : Stats = await fs . stat ( resolvedPath ) ;
66- if ( stats . isDirectory ( ) ) {
67- validatedDirectories . push ( resolvedPath ) ;
68- } else {
69- console . error ( formatDirectoryError ( resolvedPath , undefined , 'non-directory root' ) ) ;
75+
76+ for ( const candidatePath of candidatePaths ) {
77+ try {
78+ const stats : Stats = await fs . stat ( candidatePath ) ;
79+ if ( ! stats . isDirectory ( ) ) {
80+ console . error ( formatDirectoryError ( candidatePath , undefined , 'non-directory root' ) ) ;
81+ continue ;
82+ }
83+
84+ if ( ! seenDirectories . has ( candidatePath ) ) {
85+ validatedDirectories . push ( candidatePath ) ;
86+ seenDirectories . add ( candidatePath ) ;
87+ }
88+ } catch ( error ) {
89+ console . error ( formatDirectoryError ( candidatePath , error ) ) ;
7090 }
71- } catch ( error ) {
72- console . error ( formatDirectoryError ( resolvedPath , error ) ) ;
7391 }
7492 }
75-
93+
7694 return validatedDirectories ;
77- }
95+ }
0 commit comments