generateMeme
Create meme images using Imgflip templates by inputting a numeric ID and custom text. Simplify meme generation for AI models with structured inputs via the MCP server.
Instructions
Generate a meme image from Imgflip using the numeric template id and text
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| templateNumericId | Yes | ||
| text0 | Yes | ||
| text1 | No |
Implementation Reference
- src/index.ts:23-63 (handler)The handler function that implements the generateMeme tool logic: prepares form data with template ID and texts, calls Imgflip API to caption image, downloads the resulting image, encodes to base64, and returns as inline image content or error.async ({ templateNumericId, text0, text1 }) => { try { // Prepare the Imgflip API request const formData = new FormData(); formData.append("template_id", templateNumericId); formData.append("text0", text0); if (text1) formData.append("text1", text1); formData.append("username", process.env.IMGFLIP_USERNAME || ""); formData.append("password", process.env.IMGFLIP_PASSWORD || ""); // Send the request to the Imgflip API const response = await axios.post("https://api.imgflip.com/caption_image", formData, { headers: { "Content-Type": "multipart/form-data", }, }); // Get the image URL from the response const imageUrl = response.data.data.url; // Download the image const imageResponse = await axios.get(imageUrl, { responseType: "arraybuffer" }); const imageDataBase64 = imageResponse.data.toString("base64"); // Return the image as a base64 encoded string return { content: [{ type: "image", data: imageDataBase64, mimeType: "image/png" }], }; } catch (error) { // Return an error message return { content: [ { type: "text", text: "Failed to generate meme image", }, ], isError: true, }; } }
- src/index.ts:19-22 (schema)Input schema using Zod: templateNumericId (string), text0 (string), text1 (optional string). Defines parameters for the meme template ID and top/bottom texts.templateNumericId: z.string(), text0: z.string(), text1: z.string().optional(), },
- src/index.ts:15-64 (registration)Registration of the generateMeme tool on the MCP server with name, description, input schema, and inline handler function.server.tool( "generateMeme", "Generate a meme image from Imgflip using the numeric template id and text", { templateNumericId: z.string(), text0: z.string(), text1: z.string().optional(), }, async ({ templateNumericId, text0, text1 }) => { try { // Prepare the Imgflip API request const formData = new FormData(); formData.append("template_id", templateNumericId); formData.append("text0", text0); if (text1) formData.append("text1", text1); formData.append("username", process.env.IMGFLIP_USERNAME || ""); formData.append("password", process.env.IMGFLIP_PASSWORD || ""); // Send the request to the Imgflip API const response = await axios.post("https://api.imgflip.com/caption_image", formData, { headers: { "Content-Type": "multipart/form-data", }, }); // Get the image URL from the response const imageUrl = response.data.data.url; // Download the image const imageResponse = await axios.get(imageUrl, { responseType: "arraybuffer" }); const imageDataBase64 = imageResponse.data.toString("base64"); // Return the image as a base64 encoded string return { content: [{ type: "image", data: imageDataBase64, mimeType: "image/png" }], }; } catch (error) { // Return an error message return { content: [ { type: "text", text: "Failed to generate meme image", }, ], isError: true, }; } } );