Skip to main content
Glama

generateImage

Create custom images from text prompts using AI, with options to specify dimensions, models, and seeds for precise control over the generated output.

Instructions

Generate an image and return the base64-encoded data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
optionsNoAdditional options for image generation
promptYesThe text description of the image to generate

Implementation Reference

  • The main handler function for the 'generateImage' tool. It validates the prompt, generates an image URL internally, fetches the image data, converts it to base64, and returns an MCP response with the image content and metadata.
    async function generateImage(params) { const { prompt, options = {} } = params; if (!prompt || typeof prompt !== "string") { throw new Error("Prompt is required and must be a string"); } // First, generate the image URL (but don't use the MCP response format) const urlResult = await _generateImageUrlInternal(prompt, options); try { // Fetch the image from the URL const response = await fetch(urlResult.imageUrl); if (!response.ok) { throw new Error(`Failed to generate image: ${response.statusText}`); } // Get the image data as an ArrayBuffer const imageBuffer = await response.arrayBuffer(); // Convert the ArrayBuffer to a base64 string const base64Data = Buffer.from(imageBuffer).toString("base64"); // Determine the mime type from the response headers or default to image/jpeg const contentType = response.headers.get("content-type") || "image/jpeg"; const metadata = { prompt: urlResult.prompt, width: urlResult.width, height: urlResult.height, model: urlResult.model, seed: urlResult.seed, }; // Return the response in MCP format return createMCPResponse([ createImageContent(base64Data, contentType), createTextContent( `Generated image from prompt: "${prompt}"\n\nImage metadata: ${JSON.stringify(metadata, null, 2)}`, ), ]); } catch (error) { console.error("Error generating image:", error); throw error; } }
  • The tool registration definition for 'generateImage' as an array [name, description, schema, handler] exported in imageTools for use with MCP server.tool().
    [ "generateImage", "Generate an image and return the base64-encoded data", { prompt: z .string() .describe("The text description of the image to generate"), options: z .object({ model: z .string() .optional() .describe("Model name to use for generation"), seed: z .number() .optional() .describe("Seed for reproducible results"), width: z .number() .optional() .describe("Width of the generated image"), height: z .number() .optional() .describe("Height of the generated image"), }) .optional() .describe("Additional options for image generation"), }, generateImage, ],
  • Zod input schema for the 'generateImage' tool, defining 'prompt' as required string and 'options' as optional object with model, seed, width, height.
    { prompt: z .string() .describe("The text description of the image to generate"), options: z .object({ model: z .string() .optional() .describe("Model name to use for generation"), seed: z .number() .optional() .describe("Seed for reproducible results"), width: z .number() .optional() .describe("Width of the generated image"), height: z .number() .optional() .describe("Height of the generated image"), }) .optional() .describe("Additional options for image generation"), },
  • Internal helper function used by generateImage to construct the Pollinations API image URL and metadata from prompt and options.
    async function _generateImageUrlInternal(prompt, options = {}) { const { model, seed, width = 1024, height = 1024 } = options; // Construct the URL with query parameters const encodedPrompt = encodeURIComponent(prompt); const path = `prompt/${encodedPrompt}`; const queryParams = { model, seed, width, height }; const url = buildUrl(IMAGE_API_BASE_URL, path, queryParams); // Return the URL with metadata return { imageUrl: url, prompt, width, height, model, seed, }; }
  • src/index.js:86-88 (registration)
    Final MCP server registration loop that calls server.tool() for each tool definition, including the spread 'generateImage' from imageTools.
    // Register all tools using the spread operator to pass the tool definition arrays toolDefinitions.forEach((tool) => server.tool(...tool));

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/tusharpatil2912/pollinations-mcp'

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