generateQRCode
Create QR codes from input data for terminal, SVG, or base64 output, with customizable error correction levels. Integrates with the Toolkit MCP Server for efficient QR generation.
Instructions
Generate a QR code from input data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | Data to encode in QR code | |
| errorCorrectionLevel | No | Error correction level | M |
| type | No | Output type (terminal, svg, or base64) | terminal |
Implementation Reference
- src/tools/generator.ts:54-96 (handler)The main handler function for generateQRCode tool. It takes input data, output type (terminal, svg, base64), and error correction level, generates the QR code using appropriate libraries, and returns the result as text content.handler: async ({ data, type = 'terminal', errorCorrectionLevel = 'M' }: { data: string; type?: 'terminal' | 'svg' | 'base64'; errorCorrectionLevel?: 'L' | 'M' | 'Q' | 'H' }) => { try { let result: string; const options = { errorCorrectionLevel, margin: 1, width: type === 'svg' ? 200 : undefined }; switch (type) { case 'terminal': // Use qrcode-terminal for better terminal output result = await generateTerminalQR(data, { small: true }); break; case 'svg': result = await QRCode.toString(data, { ...options, type: 'svg' }); break; case 'base64': const buffer = await QRCode.toBuffer(data, { ...options, type: 'png' }); result = `data:image/png;base64,${buffer.toString('base64')}`; break; default: throw new Error(`Unsupported output type: ${type}`); } return { content: [{ type: 'text', text: result }] }; } catch (error) { throw new Error(`QR code generation failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/tools/generator.ts:32-53 (schema)Input schema validation for the generateQRCode tool parameters.inputSchema: { type: 'object', properties: { data: { type: 'string', description: 'Data to encode in QR code' }, type: { type: 'string', description: 'Output type (terminal, svg, or base64)', enum: ['terminal', 'svg', 'base64'], default: 'terminal' }, errorCorrectionLevel: { type: 'string', description: 'Error correction level', enum: ['L', 'M', 'Q', 'H'], default: 'M' } }, required: ['data'] },
- src/index.ts:28-35 (registration)Combines and registers all tool groups, including generatorTools containing generateQRCode, into the central allTools object used for tool listing and execution in the MCP server.const allTools: ToolKit = { ...systemTools, ...networkTools, ...geoTools, ...generatorTools, ...dateTimeTools, ...securityTools };
- src/tools/generator.ts:6-9 (helper)Helper function to promisify qrcode-terminal.generate for asynchronous terminal QR code generation.// Promisify qrcode-terminal.generate const generateTerminalQR = promisify((text: string, opts: any, cb: (error: Error | null, result: string) => void) => { qrcodeTerminal.generate(text, opts, (result: string) => cb(null, result)); });