Skip to main content
Glama

generate-qrcode-dataurl

Convert text or URLs into QR codes as base64 data URLs for easy embedding in web applications, with customizable options for error correction, size, colors, and image format.

Instructions

Generate a QR code from text or URL and return it as a base64 data URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
optionsNoQR code generation options
textYesThe text or URL to encode in the QR code

Implementation Reference

  • The core handler function that implements the tool logic: destructures input, builds QR options, generates data URL with qrcode library, returns image content or error response.
    async ({ text, options = {} }) => { try { const qrOptions = { errorCorrectionLevel: options.errorCorrectionLevel || 'M', width: options.width, margin: options.margin || defaultMargin, color: { dark: options.color?.dark || getDefaultDarkColor(), light: options.color?.light || getDefaultLightColor() } }; const dataUrl = await QRCode.toDataURL(text, qrOptions); return { content: [ { type: "text", text: `QR code generated successfully for: "${text}"` }, { type: "image", data: dataUrl, mimeType: options.type || "image/png" } ] }; } catch (error) { return { content: [ { type: "text", text: `Error generating QR code: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } }
  • Zod schema defining customizable options for QR code generation (error level, size, margin, colors, output type), used in the tool's inputSchema.
    const QRCodeOptionsSchema = z.object({ errorCorrectionLevel: z.enum(['L', 'M', 'Q', 'H']).optional().default('M'), width: z.number().min(50).max(2000).optional(), margin: z.number().min(0).max(10).optional().default(defaultMargin), color: z.object({ dark: z.string().optional().default(getDefaultDarkColor()), light: z.string().optional().default(getDefaultLightColor()) }).optional(), type: z.enum(['image/png', 'image/jpeg', 'image/webp']).optional().default('image/png') });
  • src/index.ts:29-78 (registration)
    MCP tool registration via server.registerTool, specifying name, metadata (title/description), input schema, and handler function.
    server.registerTool( "generate-qrcode-dataurl", { title: "Generate QR Code as Data URL", description: "Generate a QR code from text or URL and return it as a base64 data URL", inputSchema: { text: z.string().min(1).describe("The text or URL to encode in the QR code"), options: QRCodeOptionsSchema.optional().describe("QR code generation options") } }, async ({ text, options = {} }) => { try { const qrOptions = { errorCorrectionLevel: options.errorCorrectionLevel || 'M', width: options.width, margin: options.margin || defaultMargin, color: { dark: options.color?.dark || getDefaultDarkColor(), light: options.color?.light || getDefaultLightColor() } }; const dataUrl = await QRCode.toDataURL(text, qrOptions); return { content: [ { type: "text", text: `QR code generated successfully for: "${text}"` }, { type: "image", data: dataUrl, mimeType: options.type || "image/png" } ] }; } catch (error) { return { content: [ { type: "text", text: `Error generating QR code: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } } );
  • Utility functions providing default hex color values for QR code foreground (dark) and background (light), used in options merging.
    function getDefaultLightColor(): string { return '#FFFFFF'; } function getDefaultDarkColor(): string { return '#000000'; }
  • Default margin value for QR code generation, used as fallback in options.
    const defaultMargin = 4;

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/antoBrugnot/qrcode-mcp'

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