qrcode
Create customizable QR codes by inputting text, adjusting size, colors, error correction, and margin settings for versatile and tailored use cases.
Instructions
Generate a QR code image
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| darkColor | No | The dark color of qrcode, default is #000000 | #000000 |
| errorCorrectionLevel | No | The error correction level of qrcode, default is M | M |
| lightColor | No | The light color of qrcode, default is #ffffff | #ffffff |
| margin | No | The margin of qrcode, default is 4 | |
| size | No | The size of qrcode, default is 256 | |
| text | Yes | The input string to generate qrcode |
Implementation Reference
- src/index.ts:55-78 (handler)The handler function for the 'qrcode' tool. It receives input parameters, calls generateQRCode with appropriate options, extracts the base64 data from the data URL, and returns it as image content in the MCP format.async ({ text, size, darkColor, lightColor, errorCorrectionLevel, margin, }) => { const qrcode = await generateQRCode(text, { width: size, color: { dark: darkColor, light: lightColor, }, errorCorrectionLevel, margin, }); const base64Image = qrcode.split(",")[1]; return Promise.resolve({ content: [{ type: "image", data: base64Image, mimeType: "image/png" }], }); }
- src/index.ts:32-54 (schema)Zod schema defining the input parameters for the 'qrcode' tool, including text, size, colors, error correction level, and margin with defaults.{ text: z.string().describe("The input string to generate qrcode"), size: z .number() .describe("The size of qrcode, default is 256") .default(256), darkColor: z .string() .describe("The dark color of qrcode, default is #000000") .default("#000000"), lightColor: z .string() .describe("The light color of qrcode, default is #ffffff") .default("#ffffff"), errorCorrectionLevel: z .enum(["L", "M", "Q", "H"]) .describe("The error correction level of qrcode, default is M") .default("M"), margin: z .number() .describe("The margin of qrcode, default is 4") .default(4), },
- src/index.ts:29-79 (registration)Registration of the 'qrcode' tool on the MCP server using server.tool(), including name, description, input schema, and handler function.server.tool( "qrcode", "Generate a QR code image", { text: z.string().describe("The input string to generate qrcode"), size: z .number() .describe("The size of qrcode, default is 256") .default(256), darkColor: z .string() .describe("The dark color of qrcode, default is #000000") .default("#000000"), lightColor: z .string() .describe("The light color of qrcode, default is #ffffff") .default("#ffffff"), errorCorrectionLevel: z .enum(["L", "M", "Q", "H"]) .describe("The error correction level of qrcode, default is M") .default("M"), margin: z .number() .describe("The margin of qrcode, default is 4") .default(4), }, async ({ text, size, darkColor, lightColor, errorCorrectionLevel, margin, }) => { const qrcode = await generateQRCode(text, { width: size, color: { dark: darkColor, light: lightColor, }, errorCorrectionLevel, margin, }); const base64Image = qrcode.split(",")[1]; return Promise.resolve({ content: [{ type: "image", data: base64Image, mimeType: "image/png" }], }); } );
- src/service/qrcode.ts:19-44 (helper)Supporting function that generates the QR code data URL using the 'qrcode' library, merges default and provided options, handles errors.async function generateQRCode( text: string, options: QRCodeOptions = {} ): Promise<string> { try { const defaultOptions: QRCodeOptions = { width: 256, margin: 4, color: { dark: "#000000", light: "#ffffff", }, errorCorrectionLevel: "M", }; // 合并默认选项和用户选项 const qrOptions = { ...defaultOptions, ...options }; // 生成二维码(返回 base64 格式) const qrCodeImage = await QRCode.toDataURL(text, qrOptions); return qrCodeImage; } catch (error) { console.error("generateQRCode error:", error); throw error; } }
- src/service/qrcode.ts:3-11 (schema)TypeScript interface defining options for the generateQRCode helper function.interface QRCodeOptions { width?: number; // 二维码宽度 margin?: number; // 二维码边距 color?: { dark?: string; // 二维码颜色 light?: string; // 背景颜色 }; errorCorrectionLevel?: "L" | "M" | "Q" | "H"; // 错误纠正级别 }