qrcode
Generate customizable QR code images from text input with adjustable size, colors, error correction, and margin settings.
Instructions
Generate a QR code image
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The input string to generate qrcode | |
| size | No | The size of qrcode, default is 256 | |
| darkColor | No | The dark color of qrcode, default is #000000 | #000000 |
| lightColor | No | The light color of qrcode, default is #ffffff | #ffffff |
| errorCorrectionLevel | No | The error correction level of qrcode, default is M | M |
| margin | No | The margin of qrcode, default is 4 |
Implementation Reference
- src/index.ts:55-78 (handler)The handler function for the 'qrcode' MCP tool. It receives input parameters, calls generateQRCode to produce a data URL, extracts the base64 part, and returns it as image content.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(), specifying 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)Core helper function that generates QR code data URL using the 'qrcode' library, merging default and provided options.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 QR code generation, used internally by generateQRCode.interface QRCodeOptions { width?: number; // 二维码宽度 margin?: number; // 二维码边距 color?: { dark?: string; // 二维码颜色 light?: string; // 背景颜色 }; errorCorrectionLevel?: "L" | "M" | "Q" | "H"; // 错误纠正级别 }