Skip to main content
Glama

generate-image

Create images from text prompts using AI generation through the MCP-Claude server's integration with Replicate.

Instructions

Generate an image using Replicate

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesPrompt for the image generation

Implementation Reference

  • src/index.ts:217-260 (registration)
    Registration of the 'generate-image' MCP tool, including description, input schema, and inline handler function.
    server.tool( "generate-image", "Generate an image using Replicate", { prompt: z.string().describe("Prompt for the image generation"), }, async ({ prompt }) => { const imageUrl = await generateImageWithReplicate(prompt); if (!imageUrl) { return { content: [ { type: "text", text: "Failed to generate image", }, ], }; } // Fetch the image and convert to base64 const response = await fetch(imageUrl); const arrayBuffer = await response.arrayBuffer(); const base64Data = Buffer.from(arrayBuffer).toString("base64"); // return { // content: [ // { // type: "image", // data: base64Data, // mimeType: "image/jpeg", // }, // ], // }; return { content: [ { type: "text", text: `Generated image URL: ${imageUrl}`, }, ], }; } );
  • Inline handler function that executes the tool logic: generates image using helper, fetches it, and returns URL in text content.
    async ({ prompt }) => { const imageUrl = await generateImageWithReplicate(prompt); if (!imageUrl) { return { content: [ { type: "text", text: "Failed to generate image", }, ], }; } // Fetch the image and convert to base64 const response = await fetch(imageUrl); const arrayBuffer = await response.arrayBuffer(); const base64Data = Buffer.from(arrayBuffer).toString("base64"); // return { // content: [ // { // type: "image", // data: base64Data, // mimeType: "image/jpeg", // }, // ], // }; return { content: [ { type: "text", text: `Generated image URL: ${imageUrl}`, }, ], }; }
  • Zod input schema defining the 'prompt' parameter for the tool.
    { prompt: z.string().describe("Prompt for the image generation"), },
  • Helper function that performs the actual image generation by calling the Replicate API with the given prompt and default parameters.
    export async function generateImageWithReplicate( prompt: string, options: Partial<ReplicatePredictionInput> = {} ): Promise<string> { const apiToken = process.env.REPLICATE_API_TOKEN; if (!apiToken) { throw new Error("REPLICATE_API_TOKEN environment variable is not set"); } // Default parameters const defaultInput: ReplicatePredictionInput = { width: 768, height: 768, prompt: prompt, refine: "expert_ensemble_refiner", scheduler: "K_EULER", lora_scale: 0.6, num_outputs: 1, guidance_scale: 7.5, apply_watermark: false, high_noise_frac: 0.8, negative_prompt: "", prompt_strength: 0.8, num_inference_steps: 25, ...options, }; const payload: ReplicatePredictionPayload = { version: "7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc", input: defaultInput, }; try { const response = await fetch("https://api.replicate.com/v1/predictions", { method: "POST", headers: { Authorization: `Bearer ${apiToken}`, "Content-Type": "application/json", Prefer: "wait", // Synchronous mode }, body: JSON.stringify(payload), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || "Failed to create prediction"); } const data: ReplicatePredictionResponse = await response.json(); if (data.error) { throw new Error(data.error); } if ( !data.output || !Array.isArray(data.output) || data.output.length === 0 ) { throw new Error("No output generated or invalid output format"); } const imageUrl = data.output[0]; return imageUrl; } catch (error) { console.error("Error generating image with Replicate:", error); throw error; } }

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/virajsamarasinghe/MCP-Claude'

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