Skip to main content
Glama

scan_directory

Scan a specified directory to extract image metadata, optionally including subdirectories for comprehensive retrieval. Designed for efficient image data organization.

Instructions

Scan a directory for images and return metadata

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
directoryYesDirectory path to scan for images
recursiveNoWhether to scan subdirectories recursively

Implementation Reference

  • Main handler for executing the 'scan_directory' tool. Validates input, scans the specified directory (recursively if requested) for supported image files, retrieves metadata for each using getImageMetadata, and returns a textual summary with JSON list of image details.
    case "scan_directory": { const { directory, recursive = false } = request.params.arguments as { directory: string, recursive?: boolean }; if (!directory) { throw new McpError(ErrorCode.InvalidParams, "Directory path is required"); } if (!fs.existsSync(directory)) { throw new McpError(ErrorCode.InvalidRequest, `Directory not found: ${directory}`); } try { // Function to scan directory recursively async function scanDir(dir: string, results: ImageMetadata[] = []): Promise<ImageMetadata[]> { // Use fs instead of fsExtra for readdir const entries = await fs.promises.readdir(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory() && recursive) { await scanDir(fullPath, results); } else if (entry.isFile()) { const ext = path.extname(entry.name).toLowerCase(); if (SUPPORTED_FORMATS.includes(ext)) { try { const metadata = await getImageMetadata(fullPath); results.push(metadata); } catch (error) { console.error(`Error processing ${fullPath}:`, error); } } } } return results; } const images = await scanDir(directory); return { content: [ { type: "text", text: `Found ${images.length} images in ${directory}${recursive ? ' (including subdirectories)' : ''}:\n\n` + JSON.stringify(images.map(img => ({ filename: img.filename, path: img.path, format: img.format, dimensions: `${img.width}x${img.height}`, size: `${(img.size / 1024).toFixed(2)} KB` })), null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error scanning directory: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
  • src/index.ts:309-326 (registration)
    Registration of the 'scan_directory' tool in the ListToolsRequestSchema handler, defining its name, description, and input schema.
    { name: "scan_directory", description: "Scan a directory for images and return metadata", inputSchema: { type: "object", properties: { directory: { type: "string", description: "Directory path to scan for images" }, recursive: { type: "boolean", description: "Whether to scan subdirectories recursively" } }, required: ["directory"] } }
  • Input schema definition for the 'scan_directory' tool, specifying parameters for directory path (required) and optional recursive scan.
    inputSchema: { type: "object", properties: { directory: { type: "string", description: "Directory path to scan for images" }, recursive: { type: "boolean", description: "Whether to scan subdirectories recursively" } }, required: ["directory"] }
  • Helper function getImageMetadata used by scan_directory to fetch image file stats, Sharp metadata (format, dimensions), and cache results for efficiency.
    async function getImageMetadata(imagePath: string): Promise<ImageMetadata> { // Check cache first if (imageMetadataCache.has(imagePath)) { return imageMetadataCache.get(imagePath)!; } try { // Use fs.promises.stat instead of fsExtra.stat const stats = await fs.promises.stat(imagePath); const metadata = await sharp(imagePath).metadata(); const imageMetadata: ImageMetadata = { path: imagePath, filename: path.basename(imagePath), format: metadata.format || path.extname(imagePath).replace('.', ''), width: metadata.width, height: metadata.height, size: stats.size, created: stats.birthtime, modified: stats.mtime }; // Cache the metadata imageMetadataCache.set(imagePath, imageMetadata); return imageMetadata; } catch (error) { console.error(`Error getting metadata for ${imagePath}:`, error); throw new McpError( ErrorCode.InternalError, `Failed to process image: ${error instanceof Error ? error.message : String(error)}` ); } }

Other Tools

Related Tools

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/rupeedev/mcp-image-reader'

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