Skip to main content
Glama
Suixinlei

Tongyi Wanxiang MCP Server

by Suixinlei

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
NameRequiredDescriptionDefault
negative_promptYes
promptYes
seedNo

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) }],
        };
      }
    );
  • Zod schema defining tool inputs: prompt, negative_prompt (required strings), seed (optional number).
    { prompt: z.string(), negative_prompt: z.string(), seed: z.number().optional() },
  • 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) }],
      };
    }
  • 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;
      }
    };
  • 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");
    };
Install Server

Other Tools

Related 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/Suixinlei/tongyi-wanx-mcp-server'

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