UNPKG

@recraft-ai/mcp-recraft-server

Version:
82 lines (81 loc) 4.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceBackgroundHandler = exports.replaceBackgroundTool = void 0; const api_1 = require("../api"); const utils_1 = require("../utils"); const zod_1 = __importDefault(require("zod")); const parameters_1 = require("../utils/parameters"); const download_1 = require("../utils/download"); exports.replaceBackgroundTool = { name: "replace_background", description: "Generate an image using Recraft from an input image with its detected background replaced based on the prompt.\n" + "You can specify the input image, style, model, and number of images to generate.\n" + "You should provide the same style/substyle/styleID settings as were used for input image generation (if exists) if there are no specific requirements to change the style.\n" + "Other parameters are recommended to keep default if you don't have any specific requirements on them.\n" + "You can use styles to refine the image background generation, and also to generate raster or vector images.\n" + "Local paths or URLs to generated images and their previews will be returned in the response.", inputSchema: { type: "object", properties: { imageURI: parameters_1.PARAMETERS.imageURI, prompt: { type: "string", description: "Text prompt of the background areas that will be changed.\n" + "Its length should be from 1 to 1024 characters." }, style: Object.assign(Object.assign({}, parameters_1.PARAMETERS.imageStyle), { description: parameters_1.PARAMETERS.imageStyle.description + "\n" + parameters_1.STYLE_PRESERVATION_WARNING }), substyle: Object.assign(Object.assign({}, parameters_1.PARAMETERS.imageSubStyle), { description: parameters_1.PARAMETERS.imageSubStyle.description + "\n" + parameters_1.STYLE_PRESERVATION_WARNING }), styleID: Object.assign(Object.assign({}, parameters_1.PARAMETERS.imageStyleID), { description: parameters_1.PARAMETERS.imageStyleID.description + "\n" + parameters_1.STYLE_PRESERVATION_WARNING }), numberOfImages: parameters_1.PARAMETERS.numberOfImages, }, required: ["imageURI", "prompt"] } }; const replaceBackgroundHandler = (server, args) => __awaiter(void 0, void 0, void 0, function* () { try { const { imageURI, prompt, style, substyle, styleID, numberOfImages } = zod_1.default.object({ imageURI: zod_1.default.string(), prompt: zod_1.default.string(), style: zod_1.default.nativeEnum(api_1.ImageStyle).optional(), substyle: zod_1.default.nativeEnum(api_1.ImageSubStyle).optional(), styleID: zod_1.default.string().optional(), numberOfImages: zod_1.default.number().optional() }).parse(args); const imageData = yield (0, download_1.downloadImage)(imageURI); const result = yield server.api.imageApi.replaceBackground({ image: yield (0, utils_1.imageDataToBlob)(imageData), prompt: prompt, style: styleID ? undefined : style, substyle: styleID ? undefined : substyle, styleId: styleID, responseFormat: 'url', n: numberOfImages, expire: server.isLocalResultsStorage, }); return yield server.transformGenerateImageResponseToCallToolResult(result); } catch (error) { return { content: [ { type: 'text', text: `Error generating image: ${error}` } ], isError: true }; } }); exports.replaceBackgroundHandler = replaceBackgroundHandler;