@bratcliffe909/mcp-server-segmind
Version:
Model Context Protocol server for Segmind API - Generate images and videos using AI models
108 lines ⢠4.37 kB
JavaScript
import * as fs from 'fs/promises';
import * as path from 'path';
import { z } from 'zod';
import { imageCache } from '../utils/image-cache.js';
import { logger } from '../utils/logger.js';
import { BaseTool } from './base.js';
const PrepareImageSchema = z.object({
file_path: z.string().describe('Absolute path to the image file'),
max_size_kb: z.number().optional().default(800).describe('Maximum size in KB before warning (default: 800KB)'),
});
export class PrepareImageTool extends BaseTool {
name = 'prepare_image';
description = 'Prepare a local image for use with other tools. Returns a temporary image ID instead of base64 to avoid context limits. Use this ID with transform_image or enhance_image.';
async execute(params) {
try {
const validated = PrepareImageSchema.parse(params);
if (!path.isAbsolute(validated.file_path)) {
return {
content: [{
type: 'text',
text: `Error: File path must be absolute. Got: ${validated.file_path}`,
}],
isError: true,
};
}
try {
await fs.access(validated.file_path);
}
catch {
return {
content: [{
type: 'text',
text: `Error: File not found: ${validated.file_path}`,
}],
isError: true,
};
}
const ext = path.extname(validated.file_path).toLowerCase();
const mimeTypes = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.bmp': 'image/bmp',
};
const mimeType = mimeTypes[ext];
if (!mimeType) {
return {
content: [{
type: 'text',
text: `Error: Unsupported image format: ${ext}. Supported: jpg, jpeg, png, gif, webp, bmp`,
}],
isError: true,
};
}
const imageBuffer = await fs.readFile(validated.file_path);
const base64String = imageBuffer.toString('base64');
const fileSizeKB = imageBuffer.length / 1024;
const fileSizeMB = fileSizeKB / 1024;
const imageId = imageCache.store(base64String, mimeType, validated.file_path);
const content = [
{
type: 'text',
text: `ā
Image prepared successfully!`,
},
{
type: 'text',
text: `š File: ${path.basename(validated.file_path)}`,
},
{
type: 'text',
text: `š Size: ${fileSizeMB.toFixed(2)}MB (${fileSizeKB.toFixed(0)}KB)`,
},
{
type: 'text',
text: `š Image ID: ${imageId}`,
},
];
if (fileSizeKB > validated.max_size_kb) {
content.push({
type: 'text',
text: `ā ļø Warning: Image is large (${fileSizeKB.toFixed(0)}KB). This may cause slow processing.`,
});
}
content.push({
type: 'text',
text: `\nš Usage examples:`,
}, {
type: 'text',
text: `transform_image({ image: "${imageId}", prompt: "oil painting style" })`,
}, {
type: 'text',
text: `enhance_image({ image: "${imageId}", operation: "upscale" })`,
}, {
type: 'text',
text: `\nš” Tip: This image ID is temporary and will expire in 15 minutes.`,
});
return { content };
}
catch (error) {
logger.error('Failed to prepare image', { error });
return this.createErrorResponse(error);
}
}
}
export const prepareImageTool = new PrepareImageTool();
//# sourceMappingURL=prepare-image.js.map