comfy_upload_image
Upload images to ComfyUI's input folder for AI workflow processing. Supports custom filenames and overwrite control to manage your image assets.
Instructions
Upload an image to ComfyUI's input folder for use in workflows. Supports custom filenames and overwrite control.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_path | Yes | ||
| filename | No | ||
| overwrite | No |
Input Schema (JSON Schema)
{
"properties": {
"filename": {
"type": "string"
},
"image_path": {
"type": "string"
},
"overwrite": {
"default": false,
"type": "boolean"
}
},
"required": [
"image_path"
],
"type": "object"
}
Implementation Reference
- src/tools/utils.ts:5-39 (handler)The main handler function for the comfy_upload_image tool, which calls uploadImage and formats the response or error.export async function handleUploadImage(input: UploadImageInput) { try { const result = uploadImage(input.image_path, input.filename, input.overwrite); return { content: [{ type: "text", text: JSON.stringify({ filename: result.filename, path: result.path, size: result.size, message: `Image uploaded successfully as ${result.filename}` }, null, 2) }] }; } catch (error: any) { if (error.message.includes('not found')) { return { content: [{ type: "text", text: JSON.stringify(ComfyUIErrorBuilder.fileNotFound(input.image_path), null, 2) }], isError: true }; } return { content: [{ type: "text", text: JSON.stringify(ComfyUIErrorBuilder.executionError(error.message), null, 2) }], isError: true }; } }
- src/types/tools.ts:122-126 (schema)Zod schema defining the input parameters for the tool: image_path (required), filename (optional), overwrite (optional boolean).export const UploadImageSchema = z.object({ image_path: z.string(), filename: z.string().optional(), overwrite: z.boolean().optional().default(false) });
- src/server.ts:130-134 (registration)Registration of the tool in the ListTools response, including name, description, and input schema reference.{ name: 'comfy_upload_image', description: 'Upload an image to ComfyUI\'s input folder for use in workflows. Supports custom filenames and overwrite control.', inputSchema: zodToJsonSchema(UploadImageSchema) as any, },
- src/utils/filesystem.ts:88-133 (helper)Core utility function that performs the file copy to ComfyUI input directory, handles naming conflicts, validation, and returns file info.export function uploadImage(sourcePath: string, filename?: string, overwrite: boolean = false): { filename: string; path: string; size: number } { const config = getConfig(); const inputDir = getFullPath(config.paths.input); // Ensure input directory exists if (!existsSync(inputDir)) { mkdirSync(inputDir, { recursive: true }); } // Validate source file if (!existsSync(sourcePath)) { throw new Error(`Source file not found: ${sourcePath}`); } // Determine target filename let targetFilename = filename || basename(sourcePath); targetFilename = sanitizeFilename(targetFilename); // Validate image format if (!validateImageFormat(targetFilename)) { throw new Error(`Invalid image format: ${targetFilename}`); } // Handle existing file let targetPath = join(inputDir, targetFilename); if (existsSync(targetPath) && !overwrite) { const ext = extname(targetFilename); const nameWithoutExt = basename(targetFilename, ext); let counter = 1; do { targetFilename = `${nameWithoutExt}_${counter}${ext}`; targetPath = join(inputDir, targetFilename); counter++; } while (existsSync(targetPath)); } // Copy file copyFileSync(sourcePath, targetPath); const stat = statSync(targetPath); return { filename: targetFilename, path: targetPath, size: stat.size }; }