generate_qr_code
Convert text into a QR code image with customizable parameters like size, border, and colors. Ideal for quick data sharing through scannable codes.
Instructions
Generate QR code(生成二维码图片) from text and return as image with description.
Args:
text: Text content to convert to QR code
box_size: Size of each box in pixels (1-50)
border: Number of boxes for border (0-20)
fill_color: Foreground color
back_color: Background color
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| back_color | No | white | |
| border | No | ||
| box_size | No | ||
| fill_color | No | black | |
| text | Yes |
Implementation Reference
- qrcode_mcp_server.py:20-63 (handler)MCP tool handler for generate_qr_code: validates inputs, calls helper utility, constructs and returns ImageContent per MCP protocol.@mcp.tool() async def generate_qr_code( text: str, box_size: int = 10, border: int = 4, fill_color: str = "black", back_color: str = "white", ) -> ImageContent: """Generate QR code(生成二维码图片) from text and return as image with description. Args: text: Text content to convert to QR code box_size: Size of each box in pixels (1-50) border: Number of boxes for border (0-20) fill_color: Foreground color back_color: Background color """ if not text or not text.strip(): raise ValueError("Text content cannot be empty") # 验证参数范围 if not (1 <= box_size <= 50): raise ValueError("box_size must be between 1 and 50") if not (0 <= border <= 20): raise ValueError("border must be between 0 and 20") try: # 生成QR码的base64编码 base64_result = text_to_qr_base64( text=text, box_size=box_size, border=border, fill_color=fill_color, back_color=back_color, image_format="JPEG", ) # 按照MCP文档要求的格式返回图片 return ImageContent(type="image", data=base64_result, mimeType="image/jpeg") except Exception as e: logger.error(f"Failed to generate QR code: {e}") raise RuntimeError(f"Failed to generate QR code: {str(e)}")
- qrcode_utils.py:7-65 (helper)Core helper function that uses qrcode and PIL libraries to generate QR code image from text parameters and returns base64 encoded string.def text_to_qr_base64( text: str, error_correction: int = qrcode.constants.ERROR_CORRECT_M, box_size: int = 10, border: int = 4, fill_color: str = "black", back_color: str = "white", image_format: str = "PNG", ) -> str: """ 将输入的字符串转换成QR码图片,并返回base64编码的字符串 Args: text (str): 要转换的文本内容 error_correction (int): 错误纠正级别,默认为中等级别 box_size (int): 每个方块的像素大小,默认为10 border (int): 边框的方块数量,默认为4 fill_color (str): 前景色,默认为黑色 back_color (str): 背景色,默认为白色 image_format (str): 图片格式,默认为PNG Returns: str: base64编码的图片字符串 Raises: ValueError: 当输入文本为空时 Exception: 其他处理异常 """ if not text or not text.strip(): raise ValueError("输入文本不能为空") try: # 创建QR码实例 qr = qrcode.QRCode( version=1, # 控制QR码的大小,1是最小的 error_correction=error_correction, box_size=box_size, border=border, ) # 添加数据 qr.add_data(text) qr.make(fit=True) # 创建图片 img = qr.make_image(fill_color=fill_color, back_color=back_color) # 将图片转换为字节流 img_buffer = io.BytesIO() img.save(img_buffer, format=image_format) img_buffer.seek(0) # 转换为base64 img_base64 = base64.b64encode(img_buffer.getvalue()).decode("utf-8") return img_base64 except Exception as e: raise Exception(f"生成QR码时发生错误: {str(e)}")
- qrcode_mcp_server.py:20-20 (registration)Decorator that registers the generate_qr_code function as an MCP tool.@mcp.tool()
- qrcode_mcp_server.py:21-27 (schema)Function signature defining input schema (parameters with types and defaults) and output type (ImageContent).async def generate_qr_code( text: str, box_size: int = 10, border: int = 4, fill_color: str = "black", back_color: str = "white", ) -> ImageContent: