Skip to main content
Glama

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
NameRequiredDescriptionDefault
dirNo
idNo
keyNo
limitNo

Implementation Reference

  • 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' }, }, }, },
  • 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>;
  • 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; }
  • 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)); }

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/vjsr007/mcp-index-notes'

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