feat(filesystem): add symlink resolution and home directory support to roots protocol

- Add symlink resolution using fs.realpath() for security consistency
- Support home directory expansion (~/) in root URI specifications
- Improve error handling with null checks, detailed error messages, and informative logging
- Change allowedDirectories from constant to variable to support roots protocol directory management
This commit is contained in:
Nandha Reddy
2025-07-02 13:35:34 +10:00
parent f3891aaf69
commit 2c922a93f9
2 changed files with 40 additions and 22 deletions

View File

@@ -43,7 +43,7 @@ function expandHome(filepath: string): string {
}
// Store allowed directories in normalized and resolved form
const allowedDirectories = await Promise.all(
let allowedDirectories = await Promise.all(
args.map(async (dir) => {
const expanded = expandHome(dir);
const absolute = path.resolve(expanded);
@@ -897,10 +897,13 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
});
// Updates allowed directories based on MCP client roots
async function updateAllowedDirectoriesFromRoots(roots: Root[]) {
const rootDirs = await getValidRootDirectories(roots);
if (rootDirs.length > 0) {
allowedDirectories.splice(0, allowedDirectories.length, ...rootDirs);
async function updateAllowedDirectoriesFromRoots(requestedRoots: Root[]) {
const validatedRootDirs = await getValidRootDirectories(requestedRoots);
if (validatedRootDirs.length > 0) {
allowedDirectories = [...validatedRootDirs];
console.error(`Updated allowed directories from MCP roots: ${validatedRootDirs.length} valid directories`);
} else {
console.error("No valid root directories provided by client");
}
}