import { promises as fs, type Stats } from 'fs';
import path from 'path';
import os from 'os';
import { normalizePath } from '../utils/path-utils.js';
import { logger } from '../logger.js';
import type { Root } from '@modelcontextprotocol/sdk/types.js';
async function parseRootUri(rootUri: string): Promise<string | null> {
try {
const rawPath = rootUri.startsWith('file://') ? rootUri.slice(7) : rootUri;
const expandedPath = rawPath.startsWith('~/') || rawPath === '~' ? path.join(os.homedir(), rawPath.slice(1)) : rawPath;
const absolutePath = path.resolve(expandedPath);
const resolvedPath = await fs.realpath(absolutePath);
return normalizePath(resolvedPath);
} catch {
return null;
}
}
function formatDirectoryError(dir: string, error?: unknown, reason?: string): string {
if (reason) return `Skipping ${reason}: ${dir}`;
const message = error instanceof Error ? error.message : String(error);
return `Skipping invalid directory: ${dir} due to error: ${message}`;
}
export async function getValidRootDirectories(requestedRoots: readonly Root[]): Promise<string[]> {
const validatedDirectories: string[] = [];
for (const requestedRoot of requestedRoots) {
const resolvedPath = await parseRootUri(requestedRoot.uri);
if (!resolvedPath) {
logger.error(formatDirectoryError(requestedRoot.uri, undefined, 'invalid path or inaccessible'));
continue;
}
try {
const stats: Stats = await fs.stat(resolvedPath);
if (stats.isDirectory()) validatedDirectories.push(resolvedPath);
else logger.error(formatDirectoryError(resolvedPath, undefined, 'non-directory root'));
} catch (error) {
logger.error(formatDirectoryError(resolvedPath, error));
}
}
return validatedDirectories;
}