Skip to main content
Glama
comeonzhj
by comeonzhj

generate-image

Create AI-generated images by specifying text content, illustration elements, background colors, and aspect ratios for visual content needs.

Instructions

当用户需要生成图片时使用的工具

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes用户需要在图片上显示的文字
illustrationYes根据用户要显示的文字,提取3-5个可以作为图片配饰的插画元素关键词
colorYes图片的背景主色调
ratioYes图片比例。支持: 4:3 (512*384), 3:4 (384*512), 16:9 (512*288), 9:16 (288*512)

Implementation Reference

  • The handler function that implements the core logic of the 'generate-image' tool. It validates the ratio, checks API credentials, generates a prompt using helper functions, calls the external Jimeng AI API to generate the image, and returns either the image URL in a formatted text response or an error message.
    async ({ text, illustration, color, ratio }: { text: string; illustration: string; color: string; ratio: string }) => { const imageSize = RATIO_MAPPING[ratio]; if (!imageSize) { return { content: [ { type: "text", text: `错误:不支持的图片比例 ${ratio}。支持的比例: 4:3, 3:4, 16:9, 9:16` } ] }; } // 检查API密钥是否配置 if (!JIMENG_ACCESS_KEY || !JIMENG_SECRET_KEY) { return { content: [ { type: "text", text: "错误:未设置环境变量 JIMENG_ACCESS_KEY 和 JIMENG_SECRET_KEY,无法调用API。" } ] }; } // 生成组合后的prompt const prompt = generatePrompt(text, illustration, color); const imageUrl = await callJimengAPI(prompt, imageSize); if (!imageUrl) { return { content: [ { type: "text", text: "生成图片失败,请检查网络连接和API密钥配置。" } ] }; } return { content: [ { type: "text", text: `图片生成成功!\n\n显示文字: ${text}\n配饰元素: ${illustration}\n背景色调: ${color}\n图片比例: ${ratio} (${imageSize.width}×${imageSize.height})\n生成提示词: ${prompt}\n图片URL: ${imageUrl}` } ] }; }
  • Zod input schema for the 'generate-image' tool defining parameters: text (string), illustration (string), color (string), ratio (enum: '4:3', '3:4', '16:9', '9:16').
    { text: z.string().describe("用户需要在图片上显示的文字"), illustration: z.string().describe("根据用户要显示的文字,提取3-5个可以作为图片配饰的插画元素关键词"), color: z.string().describe("图片的背景主色调"), ratio: z.enum(["4:3", "3:4", "16:9", "9:16"]).describe("图片比例。支持: 4:3 (512*384), 3:4 (384*512), 16:9 (512*288), 9:16 (288*512)") },
  • src/index.ts:180-240 (registration)
    Registers the 'generate-image' tool on the MCP server, providing name, description, input schema, and handler function.
    server.tool( "generate-image", "当用户需要生成图片时使用的工具", { text: z.string().describe("用户需要在图片上显示的文字"), illustration: z.string().describe("根据用户要显示的文字,提取3-5个可以作为图片配饰的插画元素关键词"), color: z.string().describe("图片的背景主色调"), ratio: z.enum(["4:3", "3:4", "16:9", "9:16"]).describe("图片比例。支持: 4:3 (512*384), 3:4 (384*512), 16:9 (512*288), 9:16 (288*512)") }, async ({ text, illustration, color, ratio }: { text: string; illustration: string; color: string; ratio: string }) => { const imageSize = RATIO_MAPPING[ratio]; if (!imageSize) { return { content: [ { type: "text", text: `错误:不支持的图片比例 ${ratio}。支持的比例: 4:3, 3:4, 16:9, 9:16` } ] }; } // 检查API密钥是否配置 if (!JIMENG_ACCESS_KEY || !JIMENG_SECRET_KEY) { return { content: [ { type: "text", text: "错误:未设置环境变量 JIMENG_ACCESS_KEY 和 JIMENG_SECRET_KEY,无法调用API。" } ] }; } // 生成组合后的prompt const prompt = generatePrompt(text, illustration, color); const imageUrl = await callJimengAPI(prompt, imageSize); if (!imageUrl) { return { content: [ { type: "text", text: "生成图片失败,请检查网络连接和API密钥配置。" } ] }; } return { content: [ { type: "text", text: `图片生成成功!\n\n显示文字: ${text}\n配饰元素: ${illustration}\n背景色调: ${color}\n图片比例: ${ratio} (${imageSize.width}×${imageSize.height})\n生成提示词: ${prompt}\n图片URL: ${imageUrl}` } ] }; } );
  • Core helper function that calls the Jimeng AI (Volcengine) API to generate the image based on prompt and dimensions, handling authentication signing and returning the generated image URL.
    async function callJimengAPI(prompt: string, ratio: { width: number; height: number }): Promise<string | null> { // 查询参数 const queryParams = { 'Action': 'CVProcess', 'Version': '2022-08-31' }; const formattedQuery = formatQuery(queryParams); // 请求体参数 const bodyParams = { req_key: "jimeng_high_aes_general_v21_L", prompt: prompt, return_url: true, width: ratio.width, height: ratio.height }; const formattedBody = JSON.stringify(bodyParams); try { // 生成签名和请求头 const { headers, requestUrl } = signV4Request( JIMENG_ACCESS_KEY!, JIMENG_SECRET_KEY!, SERVICE, formattedQuery, formattedBody ); const response = await fetch(requestUrl, { method: 'POST', headers: headers, body: formattedBody }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const responseText = await response.text(); // 替换转义字符,与Python示例保持一致 const cleanedResponse = responseText.replace(/\\u0026/g, "&"); const result = JSON.parse(cleanedResponse); // 根据火山引擎即梦AI API响应格式解析结果 if (result.ResponseMetadata && result.ResponseMetadata.Error) { throw new Error(`API error: ${result.ResponseMetadata.Error.Message || 'Unknown error'}`); } // 返回生成的图片URL - 根据搜索结果,即梦AI返回的是data.image_urls数组 if (result.data && result.data.image_urls && result.data.image_urls.length > 0) { return result.data.image_urls[0]; } return null; } catch (error) { console.error("调用即梦AI API时出错:", error); return null; } }
  • Helper function that constructs the prompt string for the image generation API using the provided text, illustration keywords, and color.
    function generatePrompt(text: string, illustration: string, color: string): string { return `字体设计:"${text}",黑色字体,斜体,带阴影。干净的背景,白色到${color}渐变。点缀浅灰色、半透明${illustration}等元素插图做配饰插画。`; }

Other 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/comeonzhj/jimengpic-mcp'

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