image-upsert
Store image data (base64 or file path) under a unique key, returning an image ID. Integrates with MCP Index Notes for efficient note management, tagging, and semantic connections.
Instructions
Store an image (base64 data or file path) under a key. Returns image id.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | No | Base64-encoded image data (no data: prefix) | |
| file | No | Path to image file to read if data not provided | |
| key | Yes | ||
| metadata | No | ||
| mime | No | MIME type e.g. image/png if data provided |
Implementation Reference
- src/mcp.ts:1370-1397 (handler)Executes the image-upsert tool: validates input with ImageUpsertSchema, loads image data from base64 or file path, guesses MIME type if needed, calls db.insertImage, returns the new image ID.case 'image-upsert': { const parsed = ImageUpsertSchema.parse(args); if (!('insertImage' in (db as any))) { throw new Error('Image storage not supported in current store implementation'); } const fs = await import('fs'); let raw: Buffer | null = null; if (parsed.data) { raw = Buffer.from(parsed.data, 'base64'); } else if (parsed.file) { raw = fs.readFileSync(parsed.file); } else { throw new Error('Provide data (base64) or file'); } // crude mime guess if not provided let mime = parsed.mime; if (!mime && parsed.file) { const ext = parsed.file.toLowerCase(); if (ext.endsWith('.png')) mime = 'image/png'; else if (ext.endsWith('.jpg') || ext.endsWith('.jpeg')) mime = 'image/jpeg'; else if (ext.endsWith('.gif')) mime = 'image/gif'; else if (ext.endsWith('.webp')) mime = 'image/webp'; else mime = 'application/octet-stream'; } if (!mime) mime = 'application/octet-stream'; const id = (db as any).insertImage({ key: parsed.key, mime, data: raw!, metadata: parsed.metadata }); return { content: [{ type: 'text', text: JSON.stringify({ id }) }] }; }
- src/types.ts:93-101 (schema)Zod input schema for image-upsert tool defining key (required), optional data (base64), file path, mime type, and metadata.export const ImageUpsertSchema = z.object({ key: z.string().min(1, 'key is required'), // Either provide raw base64 data (without data:mime prefix) or a file path to read data: z.string().optional(), file: z.string().optional(), mime: z.string().optional(), metadata: z.record(z.any()).optional().default({}), }); export type ImageUpsertInput = z.infer<typeof ImageUpsertSchema>;
- src/mcp.ts:62-75 (registration)MCP tool registration defining name 'image-upsert', description, and inputSchema (JSON schema version of the Zod schema).name: 'image-upsert', description: 'Store an image (base64 data or file path) under a key. Returns image id.', inputSchema: { type: 'object', properties: { key: { type: 'string' }, data: { type: 'string', description: 'Base64-encoded image data (no data: prefix)' }, file: { type: 'string', description: 'Path to image file to read if data not provided' }, mime: { type: 'string', description: 'MIME type e.g. image/png if data provided' }, metadata: { type: 'object' }, }, required: ['key'], }, },
- src/db.ts:193-203 (helper)Database helper method that inserts image record into SQLite 'images' table, storing key, mime, size, metadata (JSON), and binary data (BLOB), returns inserted ID.insertImage(img: { key: string; mime: string; data: Buffer; metadata?: any }): number { const stmt = this.db.prepare(`INSERT INTO images (key, mime, size, metadata, data) VALUES (@key, @mime, @size, @metadata, @data)`); const info = stmt.run({ key: img.key, mime: img.mime, size: img.data.length, metadata: JSON.stringify(img.metadata ?? {}), data: img.data, }); return Number(info.lastInsertRowid); }