upscale_images
Enhance image resolution using Stable Diffusion by specifying resize mode, target dimensions, and upscaler models. Ideal for improving clarity and detail in multiple images.
Instructions
Upscale one or more images using Stable Diffusion
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| images | Yes | Array of image file paths to upscale | |
| output_path | No | Custom output directory for upscaled images | |
| resize_mode | No | 0 for multiplier mode (default), 1 for dimension mode | |
| upscaler_1 | No | Primary upscaler model (default: R-ESRGAN 4x+) | |
| upscaler_2 | No | Secondary upscaler model (default: None) | |
| upscaling_resize | No | Upscale multiplier (default: 4) - used when resize_mode is 0 | |
| upscaling_resize_h | No | Target height in pixels (default: 512) - used when resize_mode is 1 | |
| upscaling_resize_w | No | Target width in pixels (default: 512) - used when resize_mode is 1 |
Implementation Reference
- src/index.ts:322-373 (handler)Main handler for the 'upscale_images' tool: validates args, reads and base64-encodes input images, constructs payload for Stable Diffusion's extra-batch-images API, calls it, saves upscaled images to output directory, and returns paths.case 'upscale_images': { const args = request.params.arguments; if (!isUpscaleImagesArgs(args)) { throw new McpError(ErrorCode.InvalidParams, 'Invalid parameters'); } const outputDir = args.output_path ? path.normalize(args.output_path.trim()) : DEFAULT_OUTPUT_DIR; await this.ensureDirectoryExists(outputDir); // Read and encode all images const encodedImages = await Promise.all(args.images.map(async (imagePath) => { const data = await fs.promises.readFile(imagePath); return { data: data.toString('base64'), name: path.basename(imagePath) }; })); // Convert resize_mode to number if present, otherwise use default const resizeModeNum = args.resize_mode !== undefined ? Number(args.resize_mode) : SD_RESIZE_MODE; const payload: UpscaleImagePayload = { resize_mode: resizeModeNum, show_extras_results: true, gfpgan_visibility: 0, codeformer_visibility: 0, codeformer_weight: 0, upscaling_resize: args.upscaling_resize ?? SD_UPSCALE_MULTIPLIER, upscaling_resize_w: args.upscaling_resize_w ?? SD_UPSCALE_WIDTH, upscaling_resize_h: args.upscaling_resize_h ?? SD_UPSCALE_HEIGHT, upscaling_crop: true, upscaler_1: args.upscaler_1 ?? SD_UPSCALER_1, upscaler_2: args.upscaler_2 ?? SD_UPSCALER_2, extras_upscaler_2_visibility: 0, upscale_first: false, imageList: encodedImages }; const response = await this.axiosInstance.post('/sdapi/v1/extra-batch-images', payload); if (!response.data.images?.length) throw new Error('No images upscaled'); const results = []; for (let i = 0; i < response.data.images.length; i++) { const imageData = response.data.images[i]; const outputPath = path.join(outputDir, `upscaled_${path.basename(args.images[i])}`); await fs.promises.writeFile(outputPath, Buffer.from(imageData, 'base64')); results.push({ path: outputPath }); } return { content: [{ type: 'text', text: JSON.stringify(results) }] }; }
- src/index.ts:85-94 (schema)TypeScript interface defining the input arguments for the upscale_images tool.interface UpscaleImagesArgs { images: string[]; resize_mode?: number; upscaling_resize?: number; upscaling_resize_w?: number; upscaling_resize_h?: number; upscaler_1?: string; upscaler_2?: string; output_path?: string; }
- src/index.ts:201-244 (registration)Tool registration in the ListTools response, including name, description, and detailed inputSchema.{ name: 'upscale_images', description: 'Upscale one or more images using Stable Diffusion', inputSchema: { type: 'object', properties: { images: { type: 'array', items: { type: 'string' }, description: 'Array of image file paths to upscale' }, resize_mode: { type: 'string', enum: ['0', '1'], description: '0 for multiplier mode (default), 1 for dimension mode' }, upscaling_resize: { type: 'number', description: 'Upscale multiplier (default: 4) - used when resize_mode is 0' }, upscaling_resize_w: { type: 'number', description: 'Target width in pixels (default: 512) - used when resize_mode is 1' }, upscaling_resize_h: { type: 'number', description: 'Target height in pixels (default: 512) - used when resize_mode is 1' }, upscaler_1: { type: 'string', description: 'Primary upscaler model (default: R-ESRGAN 4x+)' }, upscaler_2: { type: 'string', description: 'Secondary upscaler model (default: None)' }, output_path: { type: 'string', description: 'Custom output directory for upscaled images' } }, required: ['images'] } }
- src/index.ts:438-473 (schema)Runtime type guard function that validates incoming arguments match UpscaleImagesArgs structure and constraints.function isUpscaleImagesArgs(value: unknown): value is UpscaleImagesArgs { if (typeof value !== 'object' || value === null) return false; const v = value as Record<string, unknown>; // Validate images array if (!Array.isArray(v.images) || !v.images.every(img => typeof img === 'string')) { return false; } // Validate optional resize_mode as string '0' or '1' if (v.resize_mode !== undefined) { if (typeof v.resize_mode !== 'string' || !['0', '1'].includes(v.resize_mode)) return false; } if (v.upscaling_resize !== undefined) { const resize = Number(v.upscaling_resize); if (isNaN(resize) || resize < 1) return false; } if (v.upscaling_resize_w !== undefined) { const width = Number(v.upscaling_resize_w); if (isNaN(width) || width < 1) return false; } if (v.upscaling_resize_h !== undefined) { const height = Number(v.upscaling_resize_h); if (isNaN(height) || height < 1) return false; } // Validate optional string fields if (v.upscaler_1 !== undefined && typeof v.upscaler_1 !== 'string') return false; if (v.upscaler_2 !== undefined && typeof v.upscaler_2 !== 'string') return false; if (v.output_path !== undefined && typeof v.output_path !== 'string') return false; return true; }