Skip to main content
Glama

refine_image

Refine an existing image by performing a denoising pass guided by a text prompt. Control the degree of change with the denoise parameter.

Instructions

Refine an existing image using img2img: fetch the source image, upload it to ComfyUI, and run a denoising pass guided by the prompt. Lower denoise preserves more of the original; higher denoise gives more freedom to the prompt.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesText prompt describing the desired refined image
source_image_urlYesURL of the source image to refine. Will be fetched and uploaded to ComfyUI.
denoiseNoHow much to change the source image (0 = no change, 1 = fully regenerate). Typical: 0.3-0.7
negative_promptNo
stepsNo
cfgNo
seedNo
checkpointNo

Implementation Reference

  • Handler function that fetches the source image, builds an img2img workflow with denoising, runs it via ComfyUIClient, and returns generated image URLs.
      async (args) => {
        const upload = await client.fetchAndUploadImage(args.source_image_url);
    
        const workflow = img2img({
          prompt: args.prompt,
          negativePrompt: args.negative_prompt ?? "",
          sourceImage: upload.name,
          denoise: args.denoise,
          steps: args.steps,
          cfg: args.cfg,
          seed: args.seed ?? Math.floor(Math.random() * 2 ** 32),
          checkpoint: args.checkpoint ?? DEFAULT_CHECKPOINT,
        });
    
        const result = await client.runWorkflow(workflow);
    
        const lines = [
          `Refined image (prompt_id: ${result.promptId}, denoise: ${args.denoise}, source: ${upload.name}):`,
          ...result.images.map((url, i) => `  ${i + 1}. ${url}`),
        ];
        return { content: [{ type: "text" as const, text: lines.join("\n") }] };
      },
    );
  • Zod schema defining the refine_image tool's input parameters: prompt, source_image_url, denoise (default 0.5), negative_prompt, steps (default 25), cfg (default 7), seed, and checkpoint.
    const refineImageSchema = {
      prompt: z
        .string()
        .min(1)
        .describe("Text prompt describing the desired refined image"),
      source_image_url: z
        .string()
        .url()
        .describe(
          "URL of the source image to refine. Will be fetched and uploaded to ComfyUI.",
        ),
      denoise: z
        .number()
        .min(0)
        .max(1)
        .default(0.5)
        .describe(
          "How much to change the source image (0 = no change, 1 = fully regenerate). Typical: 0.3-0.7",
        ),
      negative_prompt: z.string().optional(),
      steps: z.number().int().min(1).max(150).default(25),
      cfg: z.number().min(1).max(30).default(7),
      seed: z.number().int().optional(),
      checkpoint: z.string().optional(),
    };
  • Registers the 'refine_image' tool on the MCP server with its schema and handler callback.
    export function registerRefineTool(
      server: McpServer,
      client: ComfyUIClient,
    ): void {
      server.tool(
        "refine_image",
        "Refine an existing image using img2img: fetch the source image, upload it to ComfyUI, and run a denoising pass guided by the prompt. Lower denoise preserves more of the original; higher denoise gives more freedom to the prompt.",
        refineImageSchema,
        async (args) => {
          const upload = await client.fetchAndUploadImage(args.source_image_url);
    
          const workflow = img2img({
            prompt: args.prompt,
            negativePrompt: args.negative_prompt ?? "",
            sourceImage: upload.name,
            denoise: args.denoise,
            steps: args.steps,
            cfg: args.cfg,
            seed: args.seed ?? Math.floor(Math.random() * 2 ** 32),
            checkpoint: args.checkpoint ?? DEFAULT_CHECKPOINT,
          });
    
          const result = await client.runWorkflow(workflow);
    
          const lines = [
            `Refined image (prompt_id: ${result.promptId}, denoise: ${args.denoise}, source: ${upload.name}):`,
            ...result.images.map((url, i) => `  ${i + 1}. ${url}`),
          ];
          return { content: [{ type: "text" as const, text: lines.join("\n") }] };
        },
      );
    }
  • src/server.ts:43-43 (registration)
    Call site where registerRefineTool is invoked during server build to wire up the refine_image tool.
    registerRefineTool(s, client);
  • The img2img workflow builder used by refine_image. Builds a ComfyUI workflow with LoadImage, VAEEncode, CLIPTextEncode (pos/neg), CheckpointLoaderSimple, KSampler (with denoise), VAEDecode, and SaveImage nodes.
    export function img2img(params: Img2ImgParams): Workflow {
      return {
        "3": {
          class_type: "KSampler",
          inputs: {
            seed: params.seed,
            steps: params.steps,
            cfg: params.cfg,
            sampler_name: "euler",
            scheduler: "normal",
            denoise: params.denoise,
            model: ["4", 0],
            positive: ["6", 0],
            negative: ["7", 0],
            latent_image: ["11", 0],
          },
        },
        "4": {
          class_type: "CheckpointLoaderSimple",
          inputs: { ckpt_name: params.checkpoint },
        },
        "6": {
          class_type: "CLIPTextEncode",
          inputs: { text: params.prompt, clip: ["4", 1] },
        },
        "7": {
          class_type: "CLIPTextEncode",
          inputs: { text: params.negativePrompt, clip: ["4", 1] },
        },
        "8": {
          class_type: "VAEDecode",
          inputs: { samples: ["3", 0], vae: ["4", 2] },
        },
        "9": {
          class_type: "SaveImage",
          inputs: { filename_prefix: "comfyui-mcp-refine", images: ["8", 0] },
        },
        "10": {
          class_type: "LoadImage",
          inputs: { image: params.sourceImage },
        },
        "11": {
          class_type: "VAEEncode",
          inputs: { pixels: ["10", 0], vae: ["4", 2] },
        },
      };
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations provided, so description carries full burden. It discloses the internal process (fetch, upload, denoising pass) and denoise semantics, but omits details like authorization needs, rate limits, or whether original image is preserved. Some behavioral context is missing.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two efficient sentences with no waste. Key information about tool purpose and denoise behavior is front-loaded. Every sentence adds distinct value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 8 parameters, no output schema, and complex img2img workflow, description lacks explanation of return values, usage of advanced parameters, or comparison with similar sibling tools like generate_variations or generate_with_controlnet. Incomplete for full context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is low (38%). Description adds value for denoise parameter with usage guidance, but for 5 other parameters (negative_prompt, steps, cfg, seed, checkpoint) no additional semantic is provided beyond schema. Description does not adequately compensate for low coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states verb (refine), resource (existing image), and technical method (img2img with denoising). It distinguishes from siblings like generate_image by explicitly noting it modifies an existing image instead of creating from scratch.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides guidance on denoise parameter (lower preserves original, higher gives freedom) but lacks explicit when-to-use or when-not-to-use compared to sibling tools like generate_variations or generate_with_controlnet.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/miller-joe/comfyui-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server