@recraft-ai/mcp-recraft-server
Version:
MCP Server implementation for recraft.ai API
74 lines (73 loc) • 3.88 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecraftServer = void 0;
const fs_1 = require("fs");
const response_1 = require("./utils/response");
class RecraftServer {
constructor(api, imageStorageDirectory) {
this.initialized = false;
this.initializeIfNeeded = () => {
if (this.initialized) {
return;
}
this.initialized = true;
if (this.imageStorageDirectory && !(0, fs_1.existsSync)(this.imageStorageDirectory)) {
(0, fs_1.mkdirSync)(this.imageStorageDirectory, { recursive: true });
}
};
this.transformGenerateImageResponseToCallToolResult = (result) => __awaiter(this, void 0, void 0, function* () {
const { downloadedImages: images, previews } = yield (0, response_1.downloadImagesAndMakePreviews)(this.imageStorageDirectory, result.data);
const pathOrUrlDesc = this.isLocalResultsStorage ? 'path' : 'URL';
const ending = `${images.length === 1 ? '' : 's'}`;
const message = `Generated ${images.length} image${ending}.\n` +
`Original image${ending} ${images.length === 1 ? 'is' : 'are'} saved to:\n${images.map(({ pathOrUrl }) => `- ${pathOrUrl}`).join('\n')}` +
`\nBelow you can see lower quality preview${ending} of generated image${ending}.` +
`${previews.length < images.length ? `\nNote: last ${images.length - previews.length} images are not shown due to message limit, but you can still find them by given ${pathOrUrlDesc}s.` : ''}`;
const content = [];
content.push({
type: 'text',
text: message,
});
content.push(...previews);
return {
content: content,
isError: false
};
});
this.transformSingleImageOperationToCallToolResult = (image, message) => __awaiter(this, void 0, void 0, function* () {
const { downloadedImages, previews } = yield (0, response_1.downloadImagesAndMakePreviews)(this.imageStorageDirectory, [image]);
const imageData = downloadedImages[0];
const pathOrUrlDesc = this.isLocalResultsStorage ? 'local path' : 'URL';
const totalMessage = message + '\n' +
`Resulting image is saved to:\n- ${imageData.pathOrUrl}\n` +
(previews.length == 0 ?
`Note: preview image is not shown due to message limit, but you can find the resulting image by ${pathOrUrlDesc}.` :
`Below you can see the lower quality preview image of the result.`);
const content = [];
content.push({
type: 'text',
text: totalMessage,
});
content.push(...previews);
return {
content: content,
isError: false
};
});
this.api = api;
this.imageStorageDirectory = imageStorageDirectory;
}
get isLocalResultsStorage() {
return !!this.imageStorageDirectory;
}
}
exports.RecraftServer = RecraftServer;