generate-qrcode-svg
Generate QR codes in SVG format from text or URLs with customizable options including error correction, colors, size, and margins for versatile integration needs.
Instructions
Generate a QR code from text or URL and return it as SVG format
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:81-136 (registration)Full registration of the 'generate-qrcode-svg' tool using server.registerTool, including title, description, inputSchema, and the handler function.server.registerTool( "generate-qrcode-svg", { title: "Generate QR Code as SVG", description: "Generate a QR code from text or URL and return it as SVG format", inputSchema: { text: z.string().min(1).describe("The text or URL to encode in the QR code"), options: 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() }).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 svgString = await QRCode.toString(text, { ...qrOptions, type: 'svg' }); return { content: [ { type: "text", text: `QR code SVG generated successfully for: "${text}"\n\n${svgString}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error generating QR code SVG: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } } );
- src/index.ts:99-135 (handler)Handler function that takes text and options, constructs QR code options, generates SVG using QRCode.toString, and returns the SVG as text content or error.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 svgString = await QRCode.toString(text, { ...qrOptions, type: 'svg' }); return { content: [ { type: "text", text: `QR code SVG generated successfully for: "${text}"\n\n${svgString}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error generating QR code SVG: ${error instanceof Error ? error.message : 'Unknown error'}` } ], isError: true }; } }
- src/index.ts:86-97 (schema)Zod input schema for the tool: required 'text' string and optional 'options' object with errorCorrectionLevel, width, margin, and color settings.inputSchema: { text: z.string().min(1).describe("The text or URL to encode in the QR code"), options: 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() }).optional().describe("QR code generation options") }
- src/index.ts:293-295 (helper)Helper functions providing default colors for QR code (light: white, dark: black), used in options.function getDefaultLightColor(): string { return '#FFFFFF'; }
- src/index.ts:14-14 (helper)Default margin value used for QR code generation.const defaultMargin = 4;