通义万相 MCP 服务器

通义万相 MCP 服务器

By Suixinlei GitHub

-

Overview

what is Tongyi Wanx Mcp Server?

Tongyi Wanx Mcp Server is a TypeScript-based Model Context Protocol (MCP) server that provides the text-to-image capabilities of Alibaba Cloud's Tongyi Wanx. This server allows large language models (LLMs) to directly call the image generation API through the MCP protocol.

how to use Tongyi Wanx Mcp Server?

To use the server, you can run the following command in your terminal, replacing <你的百炼 API 密钥> with your actual API key:

npx -y tongyi-wanx-mcp-server --api-key <你的百炼 API 密钥>

key features of Tongyi Wanx Mcp Server?

  • Text-to-Image Capability Integration: Integrates with Alibaba Cloud's Tongyi Wanx text-to-image API for high-quality AI image generation.
  • Asynchronous Task Processing: Supports long-running image generation tasks with asynchronous polling to retrieve final results.
  • MCP Protocol Support: Complies with the Model Context Protocol specification, allowing seamless collaboration with MCP-compatible LLMs.

use cases of Tongyi Wanx Mcp Server?

  1. Generating high-quality images from textual descriptions.
  2. Integrating AI image generation into applications that require visual content creation.
  3. Supporting creative projects that need rapid prototyping of visual ideas.

FAQ from Tongyi Wanx Mcp Server?

  • Can I use this server for any type of image generation?

Yes! The server is designed to generate images based on textual prompts, making it versatile for various applications.

  • Is there a limit to the number of images I can generate?

The server allows you to specify the number of images to generate, but be mindful of your API usage limits.

  • How do I handle errors during image generation?

The server provides error messages and status codes to help you troubleshoot issues during the image generation process.

Content

通义万相 MCP 服务器

这是一个基于 TypeScript 的 Model Context Protocol (MCP) 服务器,专门提供阿里云通义万相的文生图(Text-to-Image)能力。该服务器通过 MCP 协议,允许大语言模型(LLM)直接调用通义万相的图像生成 API。

功能特点

  • 文生图能力集成:接入阿里云通义万相文生图 API,支持高质量的 AI 图像生成
  • 异步任务处理:支持长时间运行的图像生成任务,通过异步轮询获取最终结果
  • MCP 协议支持:符合 Model Context Protocol 规范,可与支持 MCP 的 LLM 无缝协作

环境要求

  • Node.js >= 16.x
  • npm >= 8.x 或 pnpm

如何使用

以百炼平台举例

{
  "mcpServers": {
    "tongyi-wanxiang": {
      "command": "npx",
      "args": [
        "-y",
        "tongyi-wanx-mcp-server@latest"
      ],
      "env": {
        "DASHSCOPE_API_KEY": "<你的通义万相 API 密钥>"
      }
    }
  }
}

如何开发

安装依赖

# 使用 npm
npm install

# 或使用 pnpm
pnpm install

构建与运行

# 构建项目
npm run build
# 或
pnpm run build

# 运行服务器
npm start
# 或
pnpm start

# 使用调试工具运行
npm run debug
# 或
pnpm run debug

API 使用

该服务器提供以下 MCP 工具:

1. 文生图生成(wanx-t2i-image-generation)

启动图像生成任务,返回任务 ID。

参数

  • prompt: 图像生成提示词
  • negative_prompt: 负面提示词(不希望在图像中出现的元素)

返回

  • 包含 task_id 的任务信息

2. 获取生成结果(wanx-t2i-image-generation-result)

通过任务 ID 获取图像生成结果。

参数

  • task_id: 由文生图生成工具返回的任务 ID

返回

  • 图像生成结果,包含图像 URL

项目结构

project/
├── src/                  # 源代码目录
│   ├── index.ts          # 主入口文件,MCP 服务器定义
│   ├── imageServer.ts    # 通义万相 API 集成
│   └── config.ts         # 配置文件
├── dist/                 # 编译后的代码目录
├── package.json          # 项目配置
├── tsconfig.json         # TypeScript 配置
└── README.md             # 项目说明

通义万相 API 参数说明

文生图 API 支持的参数

  • model: 模型名称,默认为 wanx2.1-t2i-turbo
  • size: 图像尺寸,默认为 1024*1024
  • n: 生成图像数量,默认为 1
  • seed: 随机种子,用于复现结果
  • prompt_extend: 是否启用提示词扩展,默认为 true
  • watermark: 是否添加水印,默认为 false

高级配置

您可以在 src/config.ts 中修改以下配置:

  • pollingInterval: 轮询任务状态的间隔时间(毫秒)
  • maxRetries: 最大轮询次数
  • defaultModel: 默认使用的模型

注意事项

  1. 请确保您有有效的通义万相 API 访问权限和密钥
  2. 图像生成是一个异步过程,可能需要数秒到数十秒不等
  3. 请合理设置轮询间隔和最大重试次数,以适应您的使用场景

参考资料

No tools information available.
No content found.