it-tools-mcp
Version:
Full MCP 2025-06-18 compliant server with 121+ IT tools, logging, ping, progress tracking, cancellation, and sampling utilities
86 lines (85 loc) ⢠3.46 kB
JavaScript
import { z } from "zod";
export function registerGenerateQr(server) {
server.registerTool("generate_qr_code", {
description: "Generate QR code for any text including URLs, WiFi networks, contact info, etc.",
inputSchema: {
text: z.string().describe("Text to encode in QR code (URLs, WiFi: WIFI:T:WPA;S:network;P:password;;, contact info, etc.)"),
size: z.number().describe("Size multiplier (1-3)").optional(),
},
// VS Code compliance annotations
annotations: {
title: "Generate Qr Code",
description: "Generate QR code for any text including URLs, WiFi networks, contact info, etc",
readOnlyHint: false
}
}, async ({ text, size = 1 }) => {
try {
const QRCode = (await import("qrcode")).default;
if (size < 1 || size > 3) {
return {
content: [
{
type: "text",
text: "Size must be between 1 and 3.",
},
],
};
}
// Generate QR code as base64 data URL
console.log(`[DEBUG] Generating QR code for: "${text}" with size: ${size}`);
const dataUrl = await QRCode.toDataURL(text, {
type: 'image/png',
errorCorrectionLevel: 'M',
width: Math.max(256, size * 128), // Minimum 256px, scales with size parameter
margin: 2,
color: {
dark: '#000000', // Black
light: '#FFFFFF' // White
}
});
console.log(`[DEBUG] QR code generated successfully`);
// Extract just the base64 data (remove the data:image/png;base64, prefix)
const base64Data = dataUrl.split(',')[1];
const markdown = ``;
return {
content: [
{
type: "text",
text: `š± QR Code for: "${text}"
\nš Data encoded: "${text}" (${text.length} characters)
šÆ Error correction: Medium (M)
š Image size: ${Math.max(256, size * 128)}x${Math.max(256, size * 128)} pixels
\nā
This QR code can be scanned with any QR code reader app
š” Generated using the 'qrcode' npm library!
\n---\n**Markdown for inline display:**\n${markdown}`,
},
{
type: "image",
data: base64Data,
mimeType: "image/png"
}
],
};
}
catch (error) {
console.error(`[DEBUG] QR code generation failed:`, error);
return {
content: [
{
type: "text",
text: `Error generating QR code: ${error instanceof Error ? error.message : 'Unknown error'}\n\nDebug info:\n- Text: \"${text}\"\n- Size: ${size}`,
},
],
};
}
// Fallback return in case of unexpected behavior
return {
content: [
{
type: "text",
text: "Unknown error: No response generated by qr-generate.",
},
],
};
});
}