Skip to main content
Glama
ahmetshbz1

Filesystem MCP Server

by ahmetshbz1
roots-utils.ts1.78 kB
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; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ahmetshbz1/filesystem-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server