generateImage
Create AI-generated images from text prompts using the Jimeng MCP Server. Specify prompts, model versions, and parameters to produce custom visual content.
Instructions
调用即梦AI生成图像
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | 生成图像的文本描述 | |
| req_key | No | 模型版本,默认值: jimeng_high_aes_general_v21_L | jimeng_high_aes_general_v21_L |
| seed | No | 随机种子,默认值:-1 | |
| negative_prompt | No | 负面提示词,描述不希望在图像中出现的内容 |
Implementation Reference
- src/index.ts:33-67 (registration)Registers the 'generateImage' tool with the MCP server in the ListToolsRequestHandler, providing name, description, and input schema.server.setRequestHandler(ListToolsRequestSchema, async () => { // Return tools even without credentials for lazy loading support const tools: Tool[] = [ { name: 'generateImage', description: '调用即梦AI生成图像', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: '生成图像的文本描述' }, req_key: { type: 'string', description: '模型版本,默认值: jimeng_high_aes_general_v21_L', default: 'jimeng_high_aes_general_v21_L' }, seed: { type: 'number', description: '随机种子,默认值:-1', default: -1 }, negative_prompt: { type: 'string', description: '负面提示词,描述不希望在图像中出现的内容' } }, required: ['prompt'] } } ]; return { tools }; });
- src/index.ts:72-143 (handler)The MCP CallToolRequest handler specifically for the 'generateImage' tool, which validates parameters, initializes the image generator with credentials, calls generateImage, and formats the response.if (name === 'generateImage') { try { const params = args as unknown as ImageGenerationParams; if (!params.prompt) { return { content: [ { type: 'text', text: 'Error: prompt parameter is required for image generation.' } ] }; } // Get credentials (will be passed via context in HTTP mode) const credentials = getCredentials(); if (!credentials.accessKeyId || !credentials.secretAccessKey) { return { content: [ { type: 'text', text: 'Error: Missing Volcengine credentials. Please configure accessKeyId and secretAccessKey.' } ] }; } const imageGenerator = new JimengImageGenerator(credentials); console.log('=== MCP 调试 ==='); console.log('请求参数:', params); const result = await imageGenerator.generateImage(params); console.log('返回结果:', JSON.stringify(result, null, 2)); console.log('Algorithm base resp:', result.algorithm_base_resp); console.log('Image URLs:', result.image_urls); console.log('Image URLs 长度:', result.image_urls?.length); console.log('结果类型:', typeof result); console.log('=============='); // 检查结果是否有效 if (!result || !result.algorithm_base_resp) { return { content: [ { type: 'text', text: `图像生成失败:无效的响应格式\n\n原始响应: ${JSON.stringify(result, null, 2)}` } ] }; } return { content: [ { type: 'text', text: `图像生成成功!\n\n状态: ${result.algorithm_base_resp.status_message || 'Unknown'}\n请求ID: ${result.algorithm_base_resp.request_id || 'Unknown'}\n\n生成的图像链接:\n${result.image_urls.map((url, index) => `${index + 1}. ${url}`).join('\n')}` } ] }; } catch (error) { return { content: [ { type: 'text', text: `图像生成失败: ${error instanceof Error ? error.message : '未知错误'}` } ] }; } }
- src/imageGenerator.ts:4-9 (schema)TypeScript interface defining the input parameters for generateImage (ImageGenerationParams).export interface ImageGenerationParams { prompt: string; req_key?: string; seed?: number; negative_prompt?: string; }
- src/imageGenerator.ts:34-140 (handler)Core implementation of the generateImage method in JimengImageGenerator class, which constructs the API request, signs it with auth, calls the Volcengine API, processes the response, and returns image URLs.async generateImage(params: ImageGenerationParams): Promise<ImageGenerationResponse> { const timestamp = this.getTimestamp(); const payload = JSON.stringify({ req_key: params.req_key || 'jimeng_high_aes_general_v21_L', prompt: params.prompt, seed: params.seed ?? -1, return_url: true, // 请求返回图像URL而非base64数据 ...(params.negative_prompt && { negative_prompt: params.negative_prompt }) }); const headers: Record<string, string> = { 'Content-Type': 'application/json', 'X-Date': timestamp, 'Host': 'visual.volcengineapi.com' }; const authorization = this.auth.signRequest( 'POST', '/', 'Action=CVProcess&Version=2022-08-31', headers, payload, timestamp ); headers['Authorization'] = authorization; try { const response = await axios.post( `${this.baseUrl}/?Action=CVProcess&Version=2022-08-31`, payload, { headers } ); // 处理实际的响应格式 const data = response.data; console.log('=== 原始API响应 ==='); console.log('Status:', response.status); console.log('Data:', JSON.stringify(data, null, 2)); console.log('=================='); // 优先处理返回URL的响应格式 if (data.code === 10000 && data.data) { // 如果有image_urls,直接使用 if (data.data.image_urls && Array.isArray(data.data.image_urls) && data.data.image_urls.length > 0) { return { algorithm_base_resp: { status_code: 0, status_message: 'Success', request_id: data.request_id || 'unknown' }, image_urls: data.data.image_urls, log_id: data.request_id || 'unknown' }; } // 如果没有URL但有base64数据,转换为data URL(备用方案) if (data.data.binary_data_base64 && Array.isArray(data.data.binary_data_base64)) { console.warn('收到base64数据而非URL,可能需要设置return_url参数'); const base64Images = data.data.binary_data_base64.map((base64: string, index: number) => `data:image/png;base64,${base64}` ); return { algorithm_base_resp: { status_code: 0, status_message: 'Success', request_id: data.request_id || 'unknown' }, image_urls: base64Images, log_id: data.request_id || 'unknown' }; } } // 如果响应格式不同,尝试适配 if (data.ResponseMetadata || data.Result) { return { algorithm_base_resp: { status_code: data.ResponseMetadata?.Error?.Code || 0, status_message: data.ResponseMetadata?.Error?.Message || 'Success', request_id: data.ResponseMetadata?.RequestId || 'unknown' }, image_urls: data.Result?.image_urls || [], log_id: data.ResponseMetadata?.RequestId || 'unknown' }; } // 如果都不匹配,返回原始响应但添加默认结构 return { algorithm_base_resp: { status_code: 0, status_message: 'Response format unknown', request_id: 'unknown' }, image_urls: [], log_id: 'unknown', raw_response: data }; } catch (error) { if (axios.isAxiosError(error)) { console.error('API Error:', error.response?.data); throw new Error(`API request failed: ${error.response?.status} ${error.response?.statusText} - ${JSON.stringify(error.response?.data)}`); } throw error; } }
- src/imageGenerator.ts:11-20 (schema)TypeScript interface defining the output response format for generateImage (ImageGenerationResponse).export interface ImageGenerationResponse { algorithm_base_resp: { status_code: number; status_message: string; request_id: string; }; image_urls: string[]; log_id: string; raw_response?: any; }