UNPKG

@ldavis9000aws/swarmui-generator

Version:

A Model Context Protocol server for SwarmUI image generation with TypeScript

146 lines 7.6 kB
/** * Generate Images MCP Tool * * This file contains the MCP tool definition for generating images with SwarmUI. * It handles the processing of image generation requests and formatting of responses. */ /** * Input schema for the generate_images tool */ export const generateImagesSchema = { description: 'Generates one or more images based on a textual prompt using SwarmUI and its underlying Stable Diffusion models. This tool is ideal for creating new visual content based on textual descriptions. It supports various optional parameters to fine-tune the output, such as negative prompts to exclude unwanted elements, specific image dimensions, guidance scale for prompt adherence, inference steps for quality/speed balance, a seed for reproducibility, choice of model, and selection of a scheduler/sampler. The tool returns the generated image(s) typically as base64 encoded data.', type: 'object', properties: { prompt: { type: 'string', description: 'The main textual description of the image to be generated. This should be as descriptive as possible to guide the AI (e.g., "a majestic lion in a snowy forest"). This parameter is required.' }, negative_prompt: { type: 'string', description: 'Optional. A textual description of elements, styles, or concepts to avoid in the generated image (e.g., "blurry, low quality, cartoonish").' }, width: { type: 'integer', minimum: 256, maximum: 4096, description: 'Optional. The desired width of the generated image in pixels (e.g., 512, 1024, or 1920). If not specified, the SwarmUI server or the selected model\'s default width will be used.' }, height: { type: 'integer', minimum: 256, maximum: 4096, description: 'Optional. The desired height of the generated image in pixels (e.g., 512, 768, or 1080). If not specified, the SwarmUI server or the selected model\'s default height will be used.' }, guidance_scale: { type: 'number', minimum: 1, maximum: 30, description: 'Optional. A numerical value (e.g., 7.0, 8.5) that controls how strictly the image generation adheres to the prompt. Higher values result in images more closely matching the prompt but can reduce diversity. If not specified, a default value from the server or model is used.' }, num_inference_steps: { type: 'integer', minimum: 1, maximum: 150, description: 'Optional. The number of denoising steps the model performs during image generation (e.g., 20, 30, 50). More steps generally lead to higher image quality but increase generation time. If not specified, a default value from the server or model is used.' }, num_images: { type: 'integer', minimum: 1, maximum: 10, description: 'Optional. The number of images to generate in a single request/batch (e.g., 1 or 4). Defaults to 1 if not specified by the user or server.' }, seed: { type: 'integer', description: 'Optional. An integer used as a random seed for the generation process, allowing for reproducible results. Provide -1 or omit for a random seed to be chosen by the server. Defaults to -1 (random) if not specified.' }, model_name: { type: 'string', description: 'Optional. The specific model to use for generation (e.g., "OfficialStableDiffusion/sd_xl_base_1.0"). Use the "list_models" tool to discover available models. If omitted, the SwarmUI server\'s default model will be used.' }, scheduler: { type: 'string', description: 'Optional. The scheduler or sampler algorithm to use for the generation process (e.g., "dpmpp_2m", "euler_ancestral"). Use the "list_schedulers" tool to discover available schedulers. If omitted, the SwarmUI server\'s default scheduler will be used.' } }, required: ['prompt'] }; /** * Handler for the generate_images tool */ export async function generateImagesHandler(params, swarmui) { try { const options = { negativePrompt: params.negative_prompt, width: params.width, height: params.height, cfgScale: params.guidance_scale, steps: params.num_inference_steps, seed: params.seed, model: params.model_name, scheduler: params.scheduler, batchSize: params.num_images, }; const result = await swarmui.generateImage(params.prompt, options); if (!result.images || result.images.length === 0) { return { content: [{ type: "text", text: 'Image generation request processed, but no images were returned by the API. This might indicate an issue with the SwarmUI server or the specific generation parameters.' }], isError: true, _meta: { success: false, reason: "no_images_returned" } }; } const resultText = `Successfully generated ${result.images.length} image(s) for prompt: "${params.prompt}"`; const content = [{ type: "text", text: resultText }]; const imagePromises = result.images.map(async (imagePathOrData, index) => { try { let base64Data; let mimeType = "image/jpeg"; if (imagePathOrData.startsWith('data:')) { const parts = imagePathOrData.match(/^data:(image\/\w+);base64,(.+)$/); if (!parts) { console.error(`Error processing image ${index + 1}: Invalid data URL format`); return null; } mimeType = parts[1]; base64Data = parts[2]; } else { base64Data = await swarmui.downloadAndEncodeImage(imagePathOrData); if (imagePathOrData.toLowerCase().endsWith('.png')) mimeType = "image/png"; } return { type: "image", data: base64Data, mimeType: mimeType }; } catch (error) { console.error(`Error processing image ${index + 1} (${imagePathOrData}):`, error); content.push({ type: "text", text: `Error processing image ${index + 1} (${imagePathOrData}): ${error instanceof Error ? error.message : String(error)}` }); return null; } }); const imageContents = await Promise.all(imagePromises); imageContents.forEach(img => { if (img) content.push(img); }); return { content: content, _meta: { success: true, imageCount: imageContents.filter(Boolean).length } }; } catch (error) { console.error('Error in generateImagesHandler:', error); return { content: [{ type: "text", text: `Error generating images: ${error instanceof Error ? error.message : String(error)}` }], isError: true, _meta: { success: false, error: error instanceof Error ? error.message : String(error) } }; } } //# sourceMappingURL=generate-images.js.map