qwen-image-edit-mcp-server 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/.env +1 -0
  2. package/index.js +127 -0
  3. package/package.json +12 -0
package/.env ADDED
@@ -0,0 +1 @@
1
+ DASHSCOPE_API_KEY=sk-0aa29e10f209480aad76454bf9168098
package/index.js ADDED
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
+ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
5
+ import { negative, size, z } from "zod";
6
+ // import dotenv from 'dotenv';
7
+
8
+ // // 加载环境变量
9
+ // dotenv.config();
10
+
11
+ // 构建与配置MCP服务基本信息
12
+ const server = new McpServer({
13
+ name: "qwen-image-edit",
14
+ version: "0.0.1",
15
+ });
16
+
17
+ //【异步调用】构建待上传的json信息并上传给接口处理
18
+ async function callImageEdit(images,text,options){
19
+ const content=[];
20
+ // 添加图像
21
+ for (const imageUrl of images){
22
+ content.push({image:imageUrl});
23
+ }
24
+ // 添加文本
25
+ content.push({text:text});
26
+
27
+ // // 获取API密钥
28
+ // const apiKey = process.env.DASHSCOPE_API_KEY;
29
+ // if (!apiKey) {
30
+ // throw new Error("DASHSCOPE_API_KEY 环境变量未设置");
31
+ // }
32
+
33
+ // 接收返回
34
+ const response=await fetch(
35
+ "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
36
+ {
37
+ method:"POST",
38
+ headers:{
39
+ "Content-Type":"application/json",
40
+ "Authorization":`Bearer ${process.env.DASHSCOPE_API_KEY}` // 后续填入
41
+ },
42
+ // 构建传入的body参数
43
+ body:JSON.stringify({
44
+ model:"qwen-image-edit-plus-2025-12-15",
45
+ input:{
46
+ messages:[{role:"user",content:content}]
47
+ },
48
+ // 其他编辑参数拓展
49
+ parameters:{
50
+ n: options.n || 1,
51
+ size: options.size || "1024x1024",
52
+ prompt_extend: options.prompt_extend !==false,
53
+ watermark: options.watermark || false,
54
+ negative_prompt: options.negative_prompt || " "
55
+ }
56
+ })
57
+ }
58
+ );
59
+ const result=await response.json();
60
+ // 错误响应
61
+ if (result.code){
62
+ throw new Error(`调用失败:${result.code} - ${result.message}`);
63
+ }
64
+ // 返回调用结果
65
+ return result;
66
+ }
67
+
68
+ // 注册图像编辑工具
69
+ server.registerTool(
70
+ "edit-image",
71
+ {
72
+ title: "图像编辑工具",
73
+ description: "使用qwen-image-edit模型对图片进行编辑",
74
+ // 设定输入参数
75
+ inputSchema:{
76
+ images: z.array(z.string()).min(1).max(6).describe("图片URL数组,1~6张图片"),
77
+ text: z.string().describe("编辑指令,描述你想怎么编辑图片,比如'将背景换成海滩'"),
78
+ n: z.number().min(1).max(6).optional().describe("生成图片数量,默认为1"),
79
+ size: z.string().optional().describe("图片尺寸,默认1024*1024"),
80
+ negative_prompt: z.string().optional().describe("生成图片的负向提示,默认为空"),
81
+ prompt_extend: z.boolean().optional().describe("是否使用prompt_extend参数,默认为false"),
82
+ watermark: z.boolean().optional().describe("是否使用水印,默认为false")
83
+ },
84
+ // 注解配置
85
+ annotations:{
86
+ // 工具会修改数据
87
+ readOnlyHint: false,
88
+ // 工具不会造成破坏性影响
89
+ destructiveHint: false,
90
+ // 工具可以访问外部世界信息
91
+ openWorldHint: true
92
+ }
93
+ },
94
+
95
+ // 异步执行逻辑
96
+ async ({image,text,n,size,negative_prompt,prompt_extend,watermark})=>{
97
+ try{
98
+ // 调用接口实现图像编辑功能
99
+ const result=await callImageEdit([image],text,{n,size,negative_prompt,prompt_extend,watermark});
100
+ const choices=result.output.choices;
101
+ // 将结果格式化
102
+ const content=[];
103
+ content.push({
104
+ type:"text",
105
+ text: `图片编辑完成,共生成${choices.length}张图片:`
106
+ });
107
+ for (let i=0;i<choices.length;i++){
108
+ const imageUrl=choices[i].message.content.image;
109
+ content.push({
110
+ type:"text",
111
+ text: `图片${i+1}:${imageUrl}`
112
+ });
113
+ }
114
+ return {content};
115
+ // 错误处理机制
116
+ }catch (error){
117
+ return {
118
+ content:[{type:"text",text:`图像编辑失败:${error.message}`}],
119
+ isError: true
120
+ };
121
+ }
122
+ }
123
+ );
124
+
125
+ // 启动服务
126
+ const transport = new StdioClientTransport();
127
+ await server.connect(transport);
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "qwen-image-edit-mcp-server",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "qwen-image-edit-mcp-server": "./index.js"
7
+ },
8
+ "dependencies": {
9
+ "@modelcontextprotocol/sdk": "^1.27.1",
10
+ "zod": "^4.3.6"
11
+ }
12
+ }