UNPKG

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.

76 lines (75 loc) 3.65 kB
import { StabilityAiApiClient } from "../stabilityAi/stabilityAiApiClient.js"; import * as fs from "fs"; import open from "open"; import { z } from "zod"; const UpscaleConservativeArgsSchema = z.object({ imageFileLocation: z.string(), prompt: z.string(), negativePrompt: z.string().optional(), creativity: z.number().min(0.2).max(0.5).optional(), }); export const upscaleConservativeToolDefinition = { name: "upscale-conservative", description: `Enhance image resolution to 4K using AI with conservative upscaling. This is an expensive operation: ONLY do it if the user specifically requests it, or if the user wants further increased quality after calling upscale-fast. This preserves image details while upscaling 20-40x. This tool has access to the ${process.env.IMAGE_STORAGE_DIRECTORY} directory. The user may provide a file name, or you may pull the path to the file from prior conversation context.\n\nIf the user provides the location, they may provide an absolute path. Or, if they provide a file name, you can just assume it is definitely present 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. Note that this server only has access to the ${process.env.IMAGE_STORAGE_DIRECTORY} directory.`, }, prompt: { type: "string", description: "A description of what you wish to see in the output image.", }, negativePrompt: { type: "string", description: "Optional: What you do not wish to see in the output image.", }, creativity: { type: "number", description: "Optional: Controls detail creation, between 0.2 and 0.5.", }, }, required: ["imageFileLocation", "prompt"], }, }; export async function upscaleConservative(args) { const validatedArgs = UpscaleConservativeArgsSchema.parse(args); const client = new StabilityAiApiClient(process.env.STABILITY_AI_API_KEY); try { const response = await client.upscaleConservative(validatedArgs.imageFileLocation, validatedArgs.prompt, { negativePrompt: validatedArgs.negativePrompt, creativity: validatedArgs.creativity, }); 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 "${validatedArgs.imageFileLocation}" with conservative upscaling`, }, { type: "text", text: `Automatically opened the file on the user's device: it is located at ${IMAGE_STORAGE_DIRECTORY}/${filename}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; return { content: [ { type: "text", text: `Failed to upscale image: ${errorMessage}`, }, ], }; } }