Skip to main content
Glama

aiPicMatting

Remove backgrounds from images stored in cloud storage using AI-powered matting. Specify image path, width, and height parameters for precise extraction.

Instructions

图片处理-抠图

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
objectKeyYes图片在存储桶里的路径
widthNo宽度
heightNo高度

Implementation Reference

  • MCP tool handler function that calls the CIAIInstance.aiPicMatting service method and formats the response.
    async ({ objectKey, width = '5', height = '5' }) => {
      const res = await CIAIInstance.aiPicMatting(objectKey, width, height);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(res.data, null, 2),
          },
        ],
        isError: !res.isSuccess,
      };
    },
  • Zod schema defining input parameters: objectKey (required), width and height (optional).
    {
      objectKey: z.string().describe('图片在存储桶里的路径'),
      width: z.string().optional().describe('宽度'),
      height: z.string().optional().describe('高度'),
    },
  • src/server.ts:379-399 (registration)
    Registration of the 'aiPicMatting' tool using McpServer.tool(), including description, schema, and handler.
    server.tool(
      'aiPicMatting',
      '图片处理-抠图',
      {
        objectKey: z.string().describe('图片在存储桶里的路径'),
        width: z.string().optional().describe('宽度'),
        height: z.string().optional().describe('高度'),
      },
      async ({ objectKey, width = '5', height = '5' }) => {
        const res = await CIAIInstance.aiPicMatting(objectKey, width, height);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(res.data, null, 2),
            },
          ],
          isError: !res.isSuccess,
        };
      },
    );
  • Helper method in CIAIService class that performs the actual AI image matting (抠图) using Tencent COS image_process API with AIImageCrop rule.
    async aiPicMatting(objectKey: string, width: string, height: string) {
      try {
        const result = await new Promise((resolve, reject) => {
          const outPutFileid = generateOutPutFileId(objectKey);
          this.cos.request(
            {
              Bucket: this.bucket, // 存储桶,必须字段
              Region: this.region, // 存储桶所在地域,必须字段 如 ap-beijing
              Key: objectKey, // 对象文件名,例如:folder/document.jpg。
              Method: 'POST', // 固定值
              Action: 'image_process', // 固定值
              Headers: {
                'Pic-Operations': JSON.stringify({
                  rules: [
                    {
                      fileid: `${outPutFileid}`,
                      rule:
                        'ci-process=AIImageCrop&width=' +
                        width +
                        '&height=' +
                        height,
                    },
                  ],
                }),
              },
            },
            function (error, data) {
              if (error) {
                // 处理请求失败
                reject(error);
              } else {
                // 处理请求成功
                resolve(data);
              }
            },
          );
        });
    
        // const localPath = "结果.png"; // 填写要写入的本地文件路径
        // if (result.Body) {
        //   fs.writeFileSync(localPath, result.Body); // 将图片内容保存本地路径
        // } else {
        //   throw new Error("Result body is undefined");
        // }
        // if (!result.Body) {
        //   throw new Error("Result body is undefined");
        // }
        // const base64Image = `data:image/jpeg;base64,${typeof result.Body === "string" ? Buffer.from(result.Body).toString("base64") : result.Body.toString("base64")}`;
    
        return {
          isSuccess: true,
          message: '图片处理成功',
          data: result,
        };
      } catch (error) {
        return {
          isSuccess: false,
          message: '图片处理失败',
          data: error,
        };
      }
    }

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/xiaomizhoubaobei/MCP'

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