generate
Transform text prompts into images using advanced AI models. Specify aspect ratios, dimensions, and output filenames for customized visual outputs with MCP Flux Studio.
Instructions
Generate an image from a text prompt
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| aspect_ratio | No | Aspect ratio of the output image | |
| height | No | Image height (ignored if aspect-ratio is set) | |
| model | No | Model to use for generation | flux.1.1-pro |
| output | No | Output filename | generated.jpg |
| prompt | Yes | Text prompt for image generation | |
| width | No | Image width (ignored if aspect-ratio is set) |
Input Schema (JSON Schema)
{
"properties": {
"aspect_ratio": {
"description": "Aspect ratio of the output image",
"enum": [
"1:1",
"4:3",
"3:4",
"16:9",
"9:16"
],
"type": "string"
},
"height": {
"description": "Image height (ignored if aspect-ratio is set)",
"type": "number"
},
"model": {
"default": "flux.1.1-pro",
"description": "Model to use for generation",
"enum": [
"flux.1.1-pro",
"flux.1-pro",
"flux.1-dev",
"flux.1.1-ultra"
],
"type": "string"
},
"output": {
"default": "generated.jpg",
"description": "Output filename",
"type": "string"
},
"prompt": {
"description": "Text prompt for image generation",
"type": "string"
},
"width": {
"description": "Image width (ignored if aspect-ratio is set)",
"type": "number"
}
},
"required": [
"prompt"
],
"type": "object"
}
Implementation Reference
- src/index.ts:292-313 (handler)Handler function for the MCP 'generate' tool: validates input arguments, constructs command-line arguments for the Flux CLI, executes the CLI via runPythonCommand, and returns the output as text content.case 'generate': { const args = request.params.arguments as GenerateArgs; // Validate required fields const prompt = this.validateRequiredString(args.prompt, 'prompt'); // Validate optional numeric fields const width = this.validateNumber(args.width, 'width', 256, 2048); const height = this.validateNumber(args.height, 'height', 256, 2048); const cmdArgs = ['generate']; cmdArgs.push('--prompt', prompt); if (args.model) cmdArgs.push('--model', args.model); if (args.aspect_ratio) cmdArgs.push('--aspect-ratio', args.aspect_ratio); if (width) cmdArgs.push('--width', width.toString()); if (height) cmdArgs.push('--height', height.toString()); if (args.output) cmdArgs.push('--output', args.output); const output = await this.runPythonCommand(cmdArgs); return { content: [{ type: 'text', text: output }], }; }
- src/index.ts:131-168 (registration)Registration of the 'generate' tool in the MCP ListTools response, including its name, description, and detailed input schema.{ name: 'generate', description: 'Generate an image from a text prompt', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'Text prompt for image generation', }, model: { type: 'string', description: 'Model to use for generation', enum: ['flux.1.1-pro', 'flux.1-pro', 'flux.1-dev', 'flux.1.1-ultra'], default: 'flux.1.1-pro', }, aspect_ratio: { type: 'string', description: 'Aspect ratio of the output image', enum: ['1:1', '4:3', '3:4', '16:9', '9:16'], }, width: { type: 'number', description: 'Image width (ignored if aspect-ratio is set)', }, height: { type: 'number', description: 'Image height (ignored if aspect-ratio is set)', }, output: { type: 'string', description: 'Output filename', default: 'generated.jpg', }, }, required: ['prompt'], }, },
- src/types.ts:37-44 (schema)TypeScript interface defining the input arguments (schema) for the 'generate' tool.export interface GenerateArgs { prompt: string; model?: FluxModel; aspect_ratio?: AspectRatio; width?: number; height?: number; output?: string; }
- src/index.ts:45-85 (helper)Core helper function that executes the Flux CLI (fluxcli.py) by spawning a Python child process with the provided arguments and returns the stdout output.private async runPythonCommand(args: string[]): Promise<string> { return new Promise((resolve, reject) => { // Validate arguments if (!args || args.length === 0) { reject(new Error('No command arguments provided')); return; } // Use python from virtual environment if available const pythonPath = process.env.VIRTUAL_ENV ? `${process.env.VIRTUAL_ENV}/bin/python` : 'python3'; const childProcess = spawn(pythonPath, ['fluxcli.py', ...args], { cwd: this.fluxPath, env: process.env, // Pass through all environment variables }); let output = ''; let errorOutput = ''; childProcess.stdout?.on('data', (data) => { output += data.toString(); }); childProcess.stderr?.on('data', (data) => { errorOutput += data.toString(); }); childProcess.on('error', (error) => { reject(new Error(`Failed to spawn Python process: ${error.message}`)); }); childProcess.on('close', (code) => { if (code === 0) { resolve(output); } else { reject(new Error(`Flux command failed (exit code ${code}): ${errorOutput}`)); } }); }); }
- src/index.ts:90-98 (helper)Helper function for validating required string parameters in tool inputs, used for 'prompt' in generate.private validateRequiredString(value: unknown, fieldName: string): string { if (typeof value !== 'string' || value.trim() === '') { throw new McpError( ErrorCode.InvalidParams, `${fieldName} is required and must be a non-empty string` ); } return value; }