wanx-t2i-image-generation
Generate detailed images from text prompts using Alibaba Cloud's Tongyi Wanxiang API. Input prompts and negative prompts to create custom visuals, with results retrieved via a separate tool.
Instructions
使用阿里云万相文生图大模型的文生图能力,由于图片生成耗时比较久,需要调用 wanx-t2i-image-generation-result 工具获取结果
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| negative_prompt | Yes | ||
| prompt | Yes | ||
| seed | No |
Implementation Reference
- src/index.ts:24-38 (registration)Registration of the wanx-t2i-image-generation MCP tool, including input schema and handler function.server.tool( "wanx-t2i-image-generation", "使用阿里云万相文生图大模型的文生图能力,由于图片生成耗时比较久,需要调用 wanx-t2i-image-generation-result 工具获取结果", { prompt: z.string(), negative_prompt: z.string(), seed: z.number().optional() }, async ({ prompt, negative_prompt, seed }) => { const result = await createImageTask({ prompt, negative_prompt, seed: seed ?? Math.floor(Math.random() * (4294967291 - 0)), }); return { content: [{ type: "text", text: JSON.stringify(result.output) }], }; } );
- src/index.ts:27-27 (schema)Zod schema defining tool inputs: prompt, negative_prompt (required strings), seed (optional number).{ prompt: z.string(), negative_prompt: z.string(), seed: z.number().optional() },
- src/index.ts:28-37 (handler)Tool handler: wraps createImageTask call, generates random seed if none provided, returns task output as JSON text.async ({ prompt, negative_prompt, seed }) => { const result = await createImageTask({ prompt, negative_prompt, seed: seed ?? Math.floor(Math.random() * (4294967291 - 0)), }); return { content: [{ type: "text", text: JSON.stringify(result.output) }], }; }
- src/wanx-t2i.ts:5-67 (helper)Core helper function that constructs and POSTs async image generation request to Aliyun DashScope Wanxiang text-to-image API.export const createImageTask = async ({ prompt = "", negative_prompt = "", model = config.api.defaultModel, size = "1024*1024", n = 1, seed = 0, prompt_extend = true, watermark = false, }) => { try { const apiKey = config.api.apiKey; if (!apiKey) { throw new Error("API key is not configured"); } // 构建请求体 const requestBody = { model, input: { prompt, negative_prompt, }, parameters: { size, n, seed, prompt_extend, watermark, }, }; // 添加可选参数 if (negative_prompt) { requestBody.input.negative_prompt = negative_prompt; } if (watermark !== null) { requestBody.parameters.watermark = watermark; } // 发送请求 const response = await axios.post( `${config.api.baseUrl}/services/aigc/text2image/image-synthesis`, requestBody, { headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, "X-DashScope-Async": "enable", }, } ); return response.data; } catch (error: any) { if (error.response) { throw new Error(error.response.data.message || "Failed to create task"); } throw error; } };
- src/wanx-t2i.ts:96-113 (helper)Helper utility to poll task status using getTaskStatus until SUCCEEDED/FAILED or timeout, used by companion result tool.export const pollTaskUntilDone = async (taskId: string) => { let retries = 0; while (retries < config.maxRetries) { const taskData = await getTaskStatus(taskId); const status = taskData.output.task_status; if (status === "SUCCEEDED" || status === "FAILED") { return taskData; } // 等待一段时间后再次查询 await new Promise((resolve) => setTimeout(resolve, config.pollingInterval)); retries++; } throw new Error("Task polling timeout"); };