wanx-t2i-image-generation-result
Fetch text-to-image generation results using Alibaba Cloud's Tongyi Wanxiang API. Designed for integration via TypeScript MCP server to process task IDs and retrieve visual outputs.
Instructions
获取阿里云万相文生图大模型的文生图结果
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| task_id | Yes |
Implementation Reference
- src/index.ts:44-49 (handler)The core handler function for the "wanx-t2i-image-generation-result" tool. It takes a task_id, polls the task status until completion using the helper pollTaskUntilDone, and returns the results as a text content block with JSON stringified output.async ({ task_id }) => { const result = await pollTaskUntilDone(task_id); return { content: [{ type: "text", text: JSON.stringify(result.output.results) }], }; }
- src/index.ts:43-43 (schema)Zod schema for the tool input parameters: requires a 'task_id' string.{ task_id: z.string() },
- src/index.ts:40-50 (registration)Registration of the MCP tool "wanx-t2i-image-generation-result" using McpServer.tool(), specifying name, Chinese description, input schema, and handler function.server.tool( "wanx-t2i-image-generation-result", "获取阿里云万相文生图大模型的文生图结果", { task_id: z.string() }, async ({ task_id }) => { const result = await pollTaskUntilDone(task_id); return { content: [{ type: "text", text: JSON.stringify(result.output.results) }], }; } );
- src/wanx-t2i.ts:96-113 (helper)Helper function that polls the Alibaba Cloud Wanxiang task status using getTaskStatus until it succeeds, fails, or times out. Directly called by the tool handler.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"); };
- src/wanx-t2i.ts:70-93 (helper)Supporting helper that queries the task status from the API endpoint `/tasks/{taskId}` using axios and the configured API key. Called by pollTaskUntilDone.export const getTaskStatus = async (taskId: string) => { try { const apiKey = config.api.apiKey; if (!apiKey) { throw new Error("API key is not configured"); } const response = await axios.get(`${config.api.baseUrl}/tasks/${taskId}`, { headers: { Authorization: `Bearer ${apiKey}`, }, }); return response.data; } catch (error: any) { if (error.response) { throw new Error( error.response.data.message || "Failed to get task status" ); } throw error; } };