Skip to main content
Glama

generate_image

Create custom images using text prompts with Stable Diffusion. Customize dimensions, sampling steps, and other parameters to generate tailored visuals for your needs.

Instructions

Generate an image using Stable Diffusion

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
batch_sizeNoNumber of images to generate (default: 1)
cfg_scaleNoCFG scale (default: 1)
distilled_cfg_scaleNoDistilled CFG scale (default: 3.5)
heightNoImage height (default: 1024)
negative_promptNoThings to exclude from the image
output_pathNoCustom output path for the generated image
promptYesThe prompt describing the desired image
restore_facesNoEnable face restoration
sampler_nameNoSampling algorithm (default: Euler)Euler
scheduler_nameNoScheduler algorithm (default: Simple)Simple
seedNoRandom seed (-1 for random)
stepsNoNumber of sampling steps (default: 4)
tilingNoGenerate tileable images
widthNoImage width (default: 1024)

Implementation Reference

  • Main handler for 'generate_image' tool: validates input, calls Stable Diffusion txt2img API, decodes and saves images with metadata, returns list of generated image paths.
    case 'generate_image': { const args = request.params.arguments; if (!isGenerateImageArgs(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); const payload: SDAPIPayload = { prompt: args.prompt, negative_prompt: args.negative_prompt || '', steps: args.steps || 4, width: args.width || 1024, height: args.height || 1024, cfg_scale: args.cfg_scale || 1, sampler_name: args.sampler_name || 'Euler', seed: args.seed ?? -1, n_iter: args.batch_size || 1, distilled_cfg_scale: args.distilled_cfg_scale || 3.5, scheduler: args.scheduler_name || 'Simple', tiling: !!args.tiling, restore_faces: !!args.restore_faces }; const response = await this.axiosInstance.post('/sdapi/v1/txt2img', payload); if (!response.data.images?.length) throw new Error('No images generated'); const results = []; for (const imageData of response.data.images) { const base64Data = imageData.includes(',') ? imageData.split(',')[1] : imageData; const pngInfoResponse = await this.axiosInstance.post('/sdapi/v1/png-info', { image: `data:image/png;base64,${imageData}` }); const outputPath = path.join(outputDir, `sd_${randomUUID()}.png`); const imageBuffer = Buffer.from(base64Data, 'base64'); await sharp(imageBuffer) .withMetadata({ exif: { IFD0: { ImageDescription: pngInfoResponse.data.info } } }) .toFile(outputPath); results.push({ path: outputPath, parameters: pngInfoResponse.data.info }); } return { content: [{ type: 'text', text: JSON.stringify(results) }] }; }
  • TypeScript interface defining the input arguments for the generate_image tool.
    interface GenerateImageArgs { prompt: string; negative_prompt?: string; steps?: number; width?: number; height?: number; cfg_scale?: number; sampler_name?: string; scheduler_name?: string; seed?: number; batch_size?: number; restore_faces?: boolean; tiling?: boolean; output_path?: string; distilled_cfg_scale?: number; }
  • src/index.ts:148-171 (registration)
    Tool registration in the MCP server's tools list, including name, description, and detailed JSON schema for input validation.
    { name: 'generate_image', description: 'Generate an image using Stable Diffusion', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'The prompt describing the desired image' }, negative_prompt: { type: 'string', description: 'Things to exclude from the image' }, steps: { type: 'number', description: 'Number of sampling steps (default: 4)', minimum: 1, maximum: 150 }, width: { type: 'number', description: 'Image width (default: 1024)', minimum: 512, maximum: 2048 }, height: { type: 'number', description: 'Image height (default: 1024)', minimum: 512, maximum: 2048 }, cfg_scale: { type: 'number', description: 'CFG scale (default: 1)', minimum: 1, maximum: 30 }, sampler_name: { type: 'string', description: 'Sampling algorithm (default: Euler)', default: 'Euler' }, scheduler_name: { type: 'string', description: 'Scheduler algorithm (default: Simple)', default: 'Simple' }, seed: { type: 'number', description: 'Random seed (-1 for random)', minimum: -1 }, batch_size: { type: 'number', description: 'Number of images to generate (default: 1)', minimum: 1, maximum: 4 }, restore_faces: { type: 'boolean', description: 'Enable face restoration' }, tiling: { type: 'boolean', description: 'Generate tileable images' }, distilled_cfg_scale: { type: 'number', description: 'Distilled CFG scale (default: 3.5)', minimum: 1, maximum: 30 }, output_path: { type: 'string', description: 'Custom output path for the generated image' } }, required: ['prompt'] } },
  • Runtime type guard function to validate and cast input arguments to GenerateImageArgs type before handling the tool call.
    function isGenerateImageArgs(value: unknown): value is GenerateImageArgs { if (typeof value !== 'object' || value === null) return false; const v = value as Record<string, unknown>; // Validate string fields if (typeof v.prompt !== 'string') return false; if (v.negative_prompt !== undefined && typeof v.negative_prompt !== 'string') return false; // Convert and validate numeric fields if (v.steps !== undefined) { const steps = Number(v.steps); if (isNaN(steps) || steps < 1 || steps > 150) return false; v.steps = steps; } if (v.batch_size !== undefined) { const batchSize = Number(v.batch_size); if (isNaN(batchSize) || batchSize < 1 || batchSize > 4) return false; v.batch_size = batchSize; } return true; }

Other Tools

Related Tools

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/Ichigo3766/image-gen-mcp'

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