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
| Name | Required | Description | Default |
|---|---|---|---|
| options | No | QR code generation options | |
| text | Yes | The text or URL to encode in the QR code |
Implementation Reference
- src/index.ts:39-77 (handler)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 }; } }
- src/index.ts:17-26 (schema)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 }; } } );
- src/index.ts:293-299 (helper)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'; }
- src/index.ts:14-14 (helper)Default margin value for QR code generation, used as fallback in options.const defaultMargin = 4;