generate_qr_code
Create QR codes for text or URLs with customizable error correction, border size, and pixel dimensions to share information quickly.
Instructions
Generate a QR code for the given data.
Args: data: The text or URL to encode in the QR code error_correction: Error correction level - "L" (Low ~7%), "M" (Medium ~15%), "Q" (Quartile ~25%), "H" (High ~30%) border: Size of the border (minimum is 4) box_size: Size of each box in pixels (default 10)
Returns: Base64 encoded PNG image of the QR code that can be displayed or saved
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | ||
| error_correction | No | M | |
| border | No | ||
| box_size | No |
Implementation Reference
- server.py:137-186 (handler)The main handler function for the 'generate_qr_code' tool, decorated with @mcp.tool() which also registers it in the MCP server. It generates a QR code from input data using the qrcode library, applies specified error correction, border, and box size, and returns a base64-encoded PNG image.@mcp.tool() def generate_qr_code(data: str, error_correction: str = "M", border: int = 4, box_size: int = 10) -> str: """ Generate a QR code for the given data. Args: data: The text or URL to encode in the QR code error_correction: Error correction level - "L" (Low ~7%), "M" (Medium ~15%), "Q" (Quartile ~25%), "H" (High ~30%) border: Size of the border (minimum is 4) box_size: Size of each box in pixels (default 10) Returns: Base64 encoded PNG image of the QR code that can be displayed or saved """ try: # Set error correction level error_levels = { "L": qrcode.constants.ERROR_CORRECT_L, "M": qrcode.constants.ERROR_CORRECT_M, "Q": qrcode.constants.ERROR_CORRECT_Q, "H": qrcode.constants.ERROR_CORRECT_H } if error_correction not in error_levels: return f"Error: Invalid error correction level. Use L, M, Q, or H" # Create QR code instance qr = qrcode.QRCode( version=1, # Controls size (1 is smallest) error_correction=error_levels[error_correction], box_size=box_size, border=max(border, 4), # Minimum border is 4 ) # Add data qr.add_data(data) qr.make(fit=True) # Create image img = qr.make_image(fill_color="black", back_color="white") # Convert to base64 for easy transmission img_buffer = io.BytesIO() img.save(img_buffer, format='PNG') img_str = base64.b64encode(img_buffer.getvalue()).decode() return f"QR code generated successfully for: '{data[:50]}{'...' if len(data) > 50 else ''}'\nBase64 PNG: data:image/png;base64,{img_str}" except Exception as e: return f"Error generating QR code: {str(e)}"