Skip to main content
Glama

generateImage

Create or edit images using text prompts or base images. Automatically opens results in your browser and provides direct links for easy access.

Instructions

Generate images using the 4o-image API and automatically open the results in your browser.

This tool generates images based on your prompt and automatically opens them in your default browser, while also returning a clickable link.

The tool supports two modes:

  1. Text-to-image - Create new images using just a text prompt

  2. Image editing - Provide a base image and prompt for editing or style transfer

The response will include a direct link to the generated image and detailed information.

Visit our website: https://4o-image.app/

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imageBase64NoOptional base image (Base64 encoded) for image editing or upscaling
promptYesText description of the desired image content

Implementation Reference

  • Executes the generateImage tool: processes params (prompt, imageBase64), converts base64 to buffer, calls image-service.generateImage, opens result in browser, returns MCP-formatted text response with details and link.
    async processImageGeneration(params) { try { const { prompt, imageBase64 } = params; const endpoint = "4oimage"; const startTime = Date.now(); let imageBuffer = null; // Convert Base64 image to Buffer if provided if (imageBase64) { try { const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ""); imageBuffer = Buffer.from(base64Data, 'base64'); } catch (error) { return { content: [{ type: "text", text: `Error converting image: ${error.message}` }] }; } } try { // Create progress callback const progressCallback = (update) => { }; // Wait for image generation const result = await generateImage(imageBuffer, prompt, { endpoint, progressCallback }); // Process results if (result.success && result.imageUrl) { const imageUrl = result.imageUrl; try { // Open image URL in browser await open(imageUrl); } catch (openError) { } // Return response const responseText = `Image generated successfully!\n` + `The image has been opened in your default browser.\n\n` + `Generation details:\n` + `- Prompt: "${prompt}"\n` + `- Image URL: ${imageUrl}\n\n` + `Visit our website: https://4o-image.app/\n\n` + `You can also click the URL above to view the image again.`; return { content: [ { type: "text", text: responseText } ] }; } else { // Handle errors let errorMessage = "Image generation failed"; if (!result.success) { errorMessage = result.error || "Unknown error"; } else if (!result.imageUrl) { errorMessage = "Invalid image URL generated"; } return { content: [{ type: "text", text: `Image generation failed: ${errorMessage}` }] }; } } catch (error) { return { content: [{ type: "text", text: `Error generating image: ${error.message}` }] }; } } catch (outerError) { return { content: [{ type: "text", text: `Error processing image generation request: ${outerError.message}` }] }; } }
  • Tool definition including name, description, and inputSchema for prompt (required) and optional imageBase64.
    const GENERATE_IMAGE_TOOL = { name: "generateImage", description: `Generate images using the 4o-image API and automatically open the results in your browser. This tool generates images based on your prompt and automatically opens them in your default browser, while also returning a clickable link. The tool supports two modes: 1. Text-to-image - Create new images using just a text prompt 2. Image editing - Provide a base image and prompt for editing or style transfer The response will include a direct link to the generated image and detailed information. Visit our website: https://4o-image.app/`, inputSchema: { type: "object", properties: { prompt: { type: "string", description: "Text description of the desired image content" }, imageBase64: { type: "string", description: "Optional base image (Base64 encoded) for image editing or upscaling" } }, required: ["prompt"] } };
  • Registers the generateImage tool by including it in the ListTools response.
    server.setRequestHandler(ListToolsRequestSchema, async () => { const response = { tools: [GENERATE_IMAGE_TOOL] }; return response; });
  • Core helper function: submits image generation task to 4o-image API, polls status until completion, extracts and validates image URL, returns success with URL or error.
    export async function generateImage(imageBuffer, prompt = "", options = {}) { const endpoint = "4oimage"; const { progressCallback } = options; const startTime = Date.now(); try { // 1. Submit task const submitResult = await submitImageTask(imageBuffer, prompt); if (!submitResult.success) { return { success: false, error: submitResult.error, code: submitResult.code }; } const taskId = submitResult.taskId; // 2. Poll task status const result = await pollTaskUntilComplete(taskId, progressCallback); // 3. Return result if (result && typeof result === 'object') { // Check if image URL exists and is valid if (result.image_url && typeof result.image_url === 'string' && result.image_url.startsWith('http')) { const totalTime = Date.now() - startTime; return { success: true, imageUrl: result.image_url }; } else { return { success: false, error: "Invalid image URL" }; } } else { return { success: false, error: "Invalid result object" }; } } catch (error) { const totalTime = Date.now() - startTime; return { success: false, error: error.message }; } }

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/Antipas/4oimage-mcp'

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