replicate-flux-mcp
Version:
MCP for Replicate Flux Model
61 lines (60 loc) • 2.15 kB
JavaScript
import { replicate } from "../services/replicate.js";
import { handleError } from "../utils/error.js";
import { outputToBase64 } from "../utils/image.js";
import { CONFIG } from "../config/index.js";
export const registerGenerateMultipleImagesTool = async (input) => {
const { prompts, support_image_mcp_response_type, ...commonParams } = input;
try {
// Process all prompts in parallel
const generationPromises = prompts.map(async (prompt) => {
const [output] = (await replicate.run(CONFIG.imageModelId, {
input: {
prompt,
...commonParams,
},
}));
const imageUrl = output.url();
if (support_image_mcp_response_type) {
const imageBase64 = await outputToBase64(output);
return {
prompt,
imageUrl,
imageBase64,
};
}
return {
prompt,
imageUrl,
};
});
// Wait for all image generation to complete
const results = await Promise.all(generationPromises);
// Build response content
const responseContent = [];
// Add intro text
responseContent.push({
type: "text",
text: `Generated ${results.length} images based on your prompts:`,
});
// Add each image with its prompt
for (const result of results) {
responseContent.push({
type: "text",
text: `\n\nPrompt: "${result.prompt}"\nImage URL: ${result.imageUrl}`,
});
if (support_image_mcp_response_type && result.imageBase64) {
responseContent.push({
type: "image",
data: result.imageBase64,
mimeType: `image/${input.output_format === "jpg" ? "jpeg" : input.output_format}`,
});
}
}
return {
content: responseContent,
};
}
catch (error) {
handleError(error);
}
};