Skip to main content
Glama

Preview SVG

preview

Render SVG content to PNG images for visual inspection. Use after rendering to review and iterate on SVG designs until the result matches intent.

Instructions

Render SVG content to a PNG image so the AI can visually inspect the output.

When to use:

  • After render_svg, call preview to see what was generated

  • Use in a revision loop: render → preview → critique → revise → preview again

  • Stop when the visual result matches the intent

Behavior:

  • Returns a PNG image (base64) rendered from the SVG string

  • Background is transparent by default

  • CSS animations and SMIL are rendered as a static snapshot (t=0) — motion is not captured

  • Format is auto-detected from content; pass format: "svg" explicitly if needed

Width:

  • Omit width to use the SVG's own declared width/viewBox

  • Pass width to scale the output (useful for small SVGs that need a larger preview)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYesSVG string to render as PNG
formatNoContent format; auto-detected from content if omitted
widthNoRender width in pixels; defaults to SVG's own declared width

Implementation Reference

  • src/index.ts:154-208 (registration)
    Registration of the 'preview' tool on the MCP server with input schema (content, format, width) and async handler.
    server.registerTool(
      "preview",
      {
        title: "Preview SVG",
        description: `
    Render SVG content to a PNG image so the AI can visually inspect the output.
    
    **When to use:**
    - After render_svg, call preview to see what was generated
    - Use in a revision loop: render → preview → critique → revise → preview again
    - Stop when the visual result matches the intent
    
    **Behavior:**
    - Returns a PNG image (base64) rendered from the SVG string
    - Background is transparent by default
    - CSS animations and SMIL are rendered as a static snapshot (t=0) — motion is not captured
    - Format is auto-detected from content; pass format: "svg" explicitly if needed
    
    **Width:**
    - Omit width to use the SVG's own declared width/viewBox
    - Pass width to scale the output (useful for small SVGs that need a larger preview)
        `.trim(),
        inputSchema: z.object({
          content: z.string().describe("SVG string to render as PNG"),
          format: z
            .enum(["svg", "html"])
            .optional()
            .describe("Content format; auto-detected from content if omitted"),
          width: z
            .number()
            .positive()
            .optional()
            .describe("Render width in pixels; defaults to SVG's own declared width"),
        }),
      },
      async ({ content, format, width }) => {
        const start = Date.now();
        try {
          const pngBuffer = renderPreview(content, format, width);
          const base64 = pngBuffer.toString("base64");
          const elapsed = Date.now() - start;
          console.error(`[nakkas] preview OK — ${pngBuffer.length} bytes, ${elapsed}ms`);
          return {
            content: [{ type: "image", data: base64, mimeType: "image/png" }],
          };
        } catch (err) {
          const message = err instanceof Error ? err.message : String(err);
          console.error(`[nakkas] preview ERROR — ${message}`);
          return {
            content: [{ type: "text", text: `Error rendering preview: ${message}` }],
            isError: true,
          };
        }
      }
    );
  • Handler function for the 'preview' tool: calls renderPreview(), converts result to base64 PNG, and returns it as an image response.
      async ({ content, format, width }) => {
        const start = Date.now();
        try {
          const pngBuffer = renderPreview(content, format, width);
          const base64 = pngBuffer.toString("base64");
          const elapsed = Date.now() - start;
          console.error(`[nakkas] preview OK — ${pngBuffer.length} bytes, ${elapsed}ms`);
          return {
            content: [{ type: "image", data: base64, mimeType: "image/png" }],
          };
        } catch (err) {
          const message = err instanceof Error ? err.message : String(err);
          console.error(`[nakkas] preview ERROR — ${message}`);
          return {
            content: [{ type: "text", text: `Error rendering preview: ${message}` }],
            isError: true,
          };
        }
      }
    );
  • Public API function that auto-detects format and dispatches to the appropriate renderer (currently SVG only, HTML throws).
    export function renderPreview(
      content: string,
      format?: "svg" | "html",
      width?: number
    ): Buffer {
      const detected = format ?? autoDetectFormat(content);
    
      if (detected === "svg") {
        return svgToPng(content, width);
      }
    
      if (detected === "html") {
        throw new Error(
          "HTML preview is not yet supported. Provide SVG content, or check back for a future update."
        );
      }
    
      throw new Error(
        "Could not detect content format. Ensure content starts with <svg or <html, " +
          "or explicitly pass format: 'svg'."
      );
    }
  • Detects content format (svg/html/unknown) by inspecting the leading tag of the content string.
    export function autoDetectFormat(content: string): PreviewFormat {
      const trimmed = content.trimStart();
      if (/^<svg[\s>]/i.test(trimmed)) return "svg";
      if (/^<!doctype html|^<html[\s>]/i.test(trimmed)) return "html";
      return "unknown";
    }
  • Renders an SVG string to a transparent PNG buffer using resvg-js, optionally fitting to a given width.
    export function svgToPng(svgString: string, width?: number): Buffer {
      const opts = width ? { fitTo: { mode: "width" as const, value: width } } : {};
      const resvg = new Resvg(svgString, opts);
      const pngData = resvg.render();
      return Buffer.from(pngData.asPng());
    }
Behavior4/5

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

With no annotations, the description covers key behaviors: returns PNG base64, transparent background, static animation snapshot, auto-detection of format, and width handling. Missing potential error cases.

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?

Well-organized into sections (main, when to use, behavior, width), each sentence is necessary and adds clear value without redundancy.

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

Completeness4/5

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

No output schema, but description explains return format and key behaviors. Covers most aspects for a preview tool, though could mention response structure or size limits.

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

Parameters4/5

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

Schema coverage is 100%, and description adds value for width (scaling, omission behavior) and format (auto-detection), beyond the schema descriptions.

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?

The description clearly states it renders SVG to PNG for visual inspection, and distinguishes from siblings by mentioning use after render_svg and before save.

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?

Explicitly describes when to use (after render_svg, in revision loop) and when to stop (visual match), but does not explicitly state when not to use or provide alternative tools.

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/arikusi/nakkas'

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