Skip to main content
Glama

generate_image

Transform text prompts into custom images with specified models, aspect ratios, and references for character or style adjustments. Ideal for dynamic visual content creation.

Instructions

Generates an image from a text prompt

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
aspect_ratioNo
callback_urlNo
character_refNo
image_refNo
modelNophoton-1
modify_image_refNo
promptYes
style_refNo

Implementation Reference

  • The async handler function that executes the generate_image tool. It validates input using GenerateImageInput, calls the Luma API at /generations/image, and returns the image URL and details.
    async def generate_image(params: dict) -> str: """Generate an image using the Luma API.""" try: input_data = GenerateImageInput(**params) except Exception as e: error_msg = str(e) if "model" in error_msg: raise ValueError(f"Invalid model: {params.get('model')}") from e elif "aspect_ratio" in error_msg: raise ValueError(f"Invalid aspect ratio: {params.get('aspect_ratio')}") from e raise model_value = input_data.model.value aspect_ratio_value = input_data.aspect_ratio.value if input_data.aspect_ratio else None request_data = input_data.model_dump(exclude_none=True) response = await _make_luma_request("POST", "/generations/image", request_data) if "assets" not in response or "image" not in response["assets"]: raise ValueError("No image URL in API response") output = ["Image generation completed"] output.append(f"Prompt: {input_data.prompt}") output.append(f"Model: {model_value}") if aspect_ratio_value: output.append(f"Aspect ratio: {aspect_ratio_value}") output.append(f"Image URL: {response['assets']['image']}") return "\n".join(output)
  • Pydantic schema for validating input parameters to the generate_image tool, including prompt, model, aspect_ratio, and optional image references.
    class GenerateImageInput(BaseModel): """ Input parameters for image generation. """ prompt: str model: ImageModel = ImageModel.PHOTON_1 aspect_ratio: Optional[AspectRatio] = None callback_url: Optional[str] = None image_ref: Optional[list[ImageRef]] = None style_ref: Optional[list[ImageRef]] = None character_ref: Optional[dict[str, ImageIdentity]] = None modify_image_ref: Optional[ModifyImageRef] = None
  • Registration of the generate_image tool in the MCP server's list_tools() function, defining its name, description, and input schema.
    Tool( name=LumaTools.GENERATE_IMAGE, description="Generates an image from a text prompt", inputSchema=GenerateImageInput.model_json_schema(), ),
  • Dispatch logic in the MCP server's call_tool() function that invokes the generate_image handler when the tool is called.
    case LumaTools.GENERATE_IMAGE: result = await generate_image(arguments) return [TextContent(type="text", text=result)]
  • Helper function used by generate_image to make authenticated HTTP requests to the Luma API.
    async def _make_luma_request(method: str, endpoint: str, data: Optional[dict] = None) -> dict: """Make a request to the Luma API.""" api_key = os.getenv("LUMA_API_KEY") if not api_key: raise ValueError("LUMA_API_KEY environment variable is not set") headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } async with httpx.AsyncClient(timeout=30.0) as client: try: response = await client.request( method, f"https://api.lumalabs.ai/dream-machine/v1{endpoint}", headers=headers, json=data if data else None, ) if response.status_code >= 400: error_msg = f"API request failed with status {response.status_code}" try: error_data = response.json() if "error" in error_data: error_msg = f"{error_msg}: {error_data['error']}" except Exception: pass raise ValueError(error_msg) return response.json() except httpx.NetworkError as e: logger.error(f"Network error occurred: {str(e)}") raise

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/bobtista/luma-ai-mcp-server'

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