Skip to main content
Glama
tusharpatil2912

Pollinations Multimodal MCP Server

generateImage

Create images from text descriptions using AI models, returning base64-encoded data with customizable dimensions and generation parameters.

Instructions

Generate an image and return the base64-encoded data

Input Schema

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

Implementation Reference

  • The primary handler function for the 'generateImage' MCP tool. Validates input parameters, generates an image URL internally, fetches the image, converts it to base64-encoded data, and constructs an MCP response with 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;
        }
    }
  • Zod input schema for the 'generateImage' tool defining required 'prompt' string and optional 'options' object with model, seed, width, and height parameters.
    {
        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"),
    },
  • Tool registration array for 'generateImage' containing name, description, input schema, and handler reference. Exported as part of imageTools for use in the main server.
    [
        "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,
    ],
  • Internal utility function that builds the Pollinations image API URL from prompt and options, used by both generateImage and generateImageUrl handlers.
    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:87-87 (registration)
    Final MCP server registration loop that applies all tool definitions, including the 'generateImage' tool from imageTools, using McpServer.tool(name, description, schema, handler).
    toolDefinitions.forEach((tool) => server.tool(...tool));

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