MCPcopy
hub / github.com/modelcontextprotocol/servers / normalizePath

Function normalizePath

src/filesystem/path-utils.ts:39–112  ·  view source on GitHub ↗
(p: string)

Source from the content-addressed store, hash-verified

37 * @returns Normalized path
38 */
39export function normalizePath(p: string): string {
40 // Remove any surrounding quotes and whitespace
41 p = p.trim().replace(/^["']|["']$/g, '');
42
43 // Check if this is a Unix path that should not be converted
44 // WSL paths (/mnt/) should ALWAYS be preserved as they work correctly in WSL with Node.js fs
45 // Regular Unix paths should also be preserved
46 const isUnixPath = p.startsWith('/') && (
47 // Always preserve WSL paths (/mnt/c/, /mnt/d/, etc.)
48 p.match(/^\/mnt\/[a-z]\//i) ||
49 // On non-Windows platforms, treat all absolute paths as Unix paths
50 (process.platform !== 'win32') ||
51 // On Windows, preserve Unix paths that aren't Unix-style Windows paths (/c/, /d/, etc.)
52 (process.platform === 'win32' && !p.match(/^\/[a-zA-Z]\//))
53 );
54
55 if (isUnixPath) {
56 // For Unix paths, just normalize without converting to Windows format
57 // Replace double slashes with single slashes and remove trailing slashes
58 return p.replace(/\/+/g, '/').replace(/(?<!^)\/$/, '');
59 }
60
61 // Convert Unix-style Windows paths (/c/, /d/) to Windows format if on Windows
62 // This function will now leave /mnt/ paths unchanged
63 p = convertToWindowsPath(p);
64
65 // Handle double backslashes, preserving leading UNC \\
66 if (p.startsWith('\\\\')) {
67 // For UNC paths, first normalize any excessive leading backslashes to exactly \\
68 // Then normalize double backslashes in the rest of the path
69 let uncPath = p;
70 // Replace multiple leading backslashes with exactly two
71 uncPath = uncPath.replace(/^\\{2,}/, '\\\\');
72 // Now normalize any remaining double backslashes in the rest of the path
73 const restOfPath = uncPath.substring(2).replace(/\\\\/g, '\\');
74 p = '\\\\' + restOfPath;
75 } else {
76 // For non-UNC paths, normalize all double backslashes
77 p = p.replace(/\\\\/g, '\\');
78 }
79
80 // On Windows, if we have a bare drive letter (e.g. "C:"), append a separator
81 // so path.normalize doesn't return "C:." which can break path validation.
82 if (process.platform === 'win32' && /^[a-zA-Z]:$/.test(p)) {
83 p = p + path.sep;
84 }
85
86 // Use Node's path normalization, which handles . and .. segments
87 let normalized = path.normalize(p);
88
89 // Fix UNC paths after normalization (path.normalize can remove a leading backslash)
90 if (p.startsWith('\\\\') && !normalized.startsWith('\\\\')) {
91 normalized = '\\' + normalized;
92 }
93
94 // Handle Windows paths: convert slashes and ensure drive letter is capitalized
95 if (normalized.match(/^[a-zA-Z]:/)) {
96 let result = normalized.replace(/\//g, '\\');

Callers 5

validatePathFunction · 0.85
index.tsFile · 0.85
parseRootUriFunction · 0.85
path-utils.test.tsFile · 0.85

Calls 1

convertToWindowsPathFunction · 0.85

Tested by

no test coverage detected