mcp-server-stability-ai
Version:
MCP [Model Context Protocol](https://modelcontextprotocol.io/) Server integrating MCP Clients with [Stability AI](https://stability.ai/) image manipulation functionalities: generate, edit, upscale, and more.
40 lines (39 loc) • 1.88 kB
JavaScript
import { StabilityAiApiClient } from "../stabilityAi/stabilityAiApiClient.js";
import * as fs from "fs";
import open from "open";
export const eraseToolDefinition = {
name: "erase",
description: `Erase parts of an image by making them transparent first. The image file must be available in the filesystem before using this tool.\n\nIf the user provides a file location, they may provide an absolute path. Or, if they provide a file name, just assume it is in the ${process.env.IMAGE_STORAGE_DIRECTORY} directory.`,
inputSchema: {
type: "object",
properties: {
imageFileLocation: {
type: "string",
description: `The absolute path to the image file on the filesystem. Transparent areas will be erased. Note that this server only has access to the ${process.env.IMAGE_STORAGE_DIRECTORY} directory.`,
},
},
required: ["imageFileLocation"],
},
};
export const erase = async (args) => {
const client = new StabilityAiApiClient(process.env.STABILITY_AI_API_KEY);
const response = await client.erase(args);
const imageAsBase64 = response.base64Image;
const filename = `${Date.now()}.png`;
const IMAGE_STORAGE_DIRECTORY = process.env.IMAGE_STORAGE_DIRECTORY;
fs.mkdirSync(IMAGE_STORAGE_DIRECTORY, { recursive: true });
fs.writeFileSync(`${IMAGE_STORAGE_DIRECTORY}/${filename}`, imageAsBase64, "base64");
open(`${IMAGE_STORAGE_DIRECTORY}/${filename}`);
return {
content: [
{
type: "text",
text: `Processed image "${args.imageFileLocation}" to erase transparent areas`,
},
{
type: "text",
text: `Automatically opened the file on the user's device: it is located at ${IMAGE_STORAGE_DIRECTORY}/${filename}`,
},
],
};
};