image-export
Export images by ID or key into specified directories using this MCP Index Notes tool. Returns file paths for easy access and integration.
Instructions
Export images (by id or key) to files. Returns file paths.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dir | No | ||
| id | No | ||
| key | No | ||
| limit | No |
Implementation Reference
- src/mcp.ts:1428-1465 (handler)Handler for 'image-export' tool: parses input schema, fetches images from database (by id or key), creates export directory, writes base64 data to files with appropriate extensions, returns directory and file paths.case 'image-export': { const parsed = ImageExportSchema.parse(args ?? {}); if (!('getImageById' in (db as any))) { throw new Error('Image storage not supported in current store implementation'); } const fs = await import('fs'); const path = await import('path'); const exportDir = parsed.dir ? path.resolve(parsed.dir) : path.resolve(process.cwd(), 'exported-images'); if (!fs.existsSync(exportDir)) fs.mkdirSync(exportDir, { recursive: true }); let images: any[] = []; if (parsed.id) { const one = (db as any).getImageById(parsed.id, true); if (one) images = [one]; } else if (parsed.key) { images = (db as any).getImagesByKey(parsed.key, parsed.limit, true); } else { return { content: [{ type: 'text', text: JSON.stringify({ error: 'Provide id or key' }) }] }; } const files: string[] = []; for (const img of images) { if (!img.data) continue; const safeKey = img.key.replace(/[^a-zA-Z0-9._-]+/g, '_').slice(0, 60); const ext = (() => { switch (img.mime) { case 'image/png': return '.png'; case 'image/jpeg': return '.jpg'; case 'image/gif': return '.gif'; case 'image/webp': return '.webp'; default: return '.bin'; } })(); const filename = `${safeKey}-${img.id}${ext}`; const outPath = path.join(exportDir, filename); fs.writeFileSync(outPath, Buffer.from(img.data, 'base64')); files.push(outPath); } return { content: [{ type: 'text', text: JSON.stringify({ dir: exportDir, files }) }] }; }
- src/mcp.ts:97-109 (registration)Tool registration entry for 'image-export' in the tools array, defining name, description, and input schema.{ name: 'image-export', description: 'Export images (by id or key) to files. Returns file paths.', inputSchema: { type: 'object', properties: { id: { type: 'number' }, key: { type: 'string' }, limit: { type: 'number' }, dir: { type: 'string' }, }, }, },
- src/types.ts:117-124 (schema)Zod input schema and TypeScript type for image-export tool parameters.export const ImageExportSchema = z.object({ id: z.number().int().positive().optional(), key: z.string().optional(), limit: z.number().int().positive().max(100).optional().default(20), dir: z.string().optional(), includeData: z.boolean().optional().default(true), // force include data if not provided }); export type ImageExportInput = z.infer<typeof ImageExportSchema>;
- src/db.ts:212-217 (helper)Database helper method to retrieve a single image by ID, optionally including base64 data.getImageById(id: number, includeData = false): ImageRecord | null { const row = this.db .prepare(`SELECT id, key, mime, size, metadata, created_at ${includeData ? ', data' : ''} FROM images WHERE id = ?`) .get(id); return row ? this.rowToImage(row, includeData) : null; }
- src/db.ts:205-210 (helper)Database helper method to retrieve images by key, with limit and optional base64 data inclusion.getImagesByKey(key: string, limit = 10, includeData = false): ImageRecord[] { const rows = this.db .prepare(`SELECT id, key, mime, size, metadata, created_at ${includeData ? ', data' : ''} FROM images WHERE key = ? ORDER BY created_at DESC LIMIT ?`) .all(key, limit); return rows.map((r: any) => this.rowToImage(r, includeData)); }