UNPKG

@kinetixarts/server-craft-it

Version:

Craft IT - Model Context Protocol (MCP) compliant Server for AI-Powered Asset Generation using Gemini

82 lines 3.49 kB
import { z } from "zod"; import { ImageGenerationService } from "./services/index.js"; /** * Register all tools with the MCP server * * @param server The FastMCP server instance */ export function registerTools(server, config = {}) { server.addTool({ name: "generate", description: "Generate an asset (image/icon) from a prompt and parameters.", parameters: z.object({ prompt: z.string().describe("Textual description of the asset."), parameters: z .object({ primary_color: z.string().describe("Hex code or color name").optional(), style_descriptor: z.string().describe("Style descriptor, e.g., 'flat', 'minimalist'").optional(), }) .optional(), output_config: z.object({ format: z.enum(["png", "jpg"]).describe("Output format: 'png' or 'jpg'"), width: z.number().int().min(64).max(1024).optional().describe("Width in pixels (64-1024)"), height: z.number().int().min(64).max(1024).optional().describe("Height in pixels (64-1024)"), storage_path: z.string().optional().describe("Path where to save the generated image") }) }), async execute({ prompt, parameters, output_config }, context) { try { // Get the result from the image generation service const result = await ImageGenerationService.generateImage({ prompt, parameters, output_config, commandLineStoragePath: config.outputPath || undefined, }); // Create content array with image const content = [ { type: "image", data: result.base64, mimeType: result.mimeType, }, { type: "text", text: `Asset ID: ${result.id}`, } ]; // Add file path information if available if (result.filePath) { content.push({ type: "text", text: `Image saved to: ${result.filePath}`, }); } return { content }; } catch (error) { // Create detailed error message let errorMessage = error instanceof Error ? error.message : String(error); // Add more context if it's a storage error if (errorMessage.includes('Failed to save image') || errorMessage.includes('Failed to create directory') || errorMessage.includes('Error storing generated image')) { errorMessage += "\nThe image was generated but could not be saved to disk. " + "Please check the storage path permissions or try a different location."; } return { content: [ { type: "text", text: `Error: ${errorMessage}`, }, ], isError: true, }; } } }); } //# sourceMappingURL=tools.js.map