scan_directory
Scan a directory to find images and extract their metadata, supporting recursive subdirectory searches for comprehensive file analysis.
Instructions
Scan a directory for images and return metadata
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| directory | Yes | Directory path to scan for images | |
| recursive | No | Whether to scan subdirectories recursively |
Implementation Reference
- src/index.ts:513-580 (handler)The execution logic for the scan_directory tool. It validates input, scans the directory (recursively if specified), filters for supported image formats, retrieves metadata for each image using getImageMetadata, and returns a text content block with a JSON summary of found images.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, including 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"] } }
- src/index.ts:312-324 (schema)Input schema definition for the scan_directory tool, specifying directory (required) and optional recursive flag.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"]