create-barcode
Generate barcodes or QR codes by encoding text into customizable images. Output as a URL or save directly to a file for various barcode types, dimensions, and rotations.
Instructions
Create barcodes using QuickChart - get barcode image URL or save barcode image to file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Whether to get barcode URL or save as file | |
| height | No | Barcode height | |
| includeText | No | Include human-readable text below barcode | |
| outputPath | No | Path where to save the file (only used with action=save_file) | |
| rotate | No | Rotation: N=Normal, R=Right 90°, L=Left 90°, I=Inverted 180° | |
| scale | No | Scale factor | |
| text | Yes | Data to encode in the barcode | |
| type | Yes | Barcode type (e.g., qr, code128, ean13, datamatrix, upca, etc.) | |
| width | No | Barcode width |
Implementation Reference
- src/tools/barcode.ts:235-341 (handler)Main handler function for the 'create-barcode' tool. Validates inputs, generates barcode URL using QuickChart, fetches and encodes image as base64, handles save to file if requested, and returns structured content with metadata.export async function handleBarcodeTool(args: any): Promise<any> { const type = args.type as string; const text = args.text as string; const action = args.action as string; validateBarcodeType(type); validateText(text); validateAction(action); validateOutputPath(args.outputPath, action); validateDimensions(args.width, args.height); validateScale(args.scale); validateRotation(args.rotate); const params = buildBarcodeParams(type, text, { width: args.width as number, height: args.height as number, scale: args.scale as number, includeText: args.includeText as boolean, rotate: args.rotate as string, }); const chartUrl = buildBarcodeUrl(type, text); const result: any = { content: [ { type: "text", text: "Below is the barcode URL:", }, { type: "text", text: chartUrl, }, ], metadata: { chartType: "barcode", generatedAt: new Date().toISOString(), chartUrl: chartUrl, }, }; try { const pngData = await fetchBarcodeContent(params, "png"); const pngBase64 = Buffer.from(pngData).toString("base64"); result.content.push( { type: "text", text: "Below is the PNG image:", }, { type: "image", data: pngBase64, mimeType: "image/png", } ); result.metadata.pngBase64 = pngBase64; } catch (error) { result.content.unshift({ type: "text", text: "⚠️ Failed to fetch barcode image", }); result.content.push({ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}`, }); result.metadata.error = error instanceof Error ? error.message : String(error); } if (action === "get_url") { return result; } const format = "png"; const outputPath = getDownloadPath( args.outputPath as string | undefined, format ); try { const dir = path.dirname(outputPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } const data = await fetchBarcodeContent(params, format); fs.writeFileSync(outputPath, data); result.metadata.savedPath = outputPath; result.content.push({ type: "text", text: "Below is the saved file path:", }); result.content.push({ type: "text", text: outputPath, }); return result; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to save barcode: ${ error instanceof Error ? error.message : String(error) }` ); } }
- src/tools/barcode.ts:11-62 (schema)Schema definition for the 'create-barcode' tool, specifying input parameters, types, descriptions, and required fields.export const CREATE_BARCODE_TOOL: Tool = { name: "create-barcode", description: "Create barcodes using QuickChart - get barcode image URL or save barcode image to file", inputSchema: { type: "object", properties: { action: { type: "string", enum: ["get_url", "save_file"], description: "Whether to get barcode URL or save as file", }, outputPath: { type: "string", description: "Path where to save the file (only used with action=save_file)", }, type: { type: "string", description: "Barcode type (e.g., qr, code128, ean13, datamatrix, upca, etc.)", }, text: { type: "string", description: "Data to encode in the barcode", }, width: { type: "integer", description: "Barcode width", }, height: { type: "integer", description: "Barcode height", }, scale: { type: "integer", description: "Scale factor", }, includeText: { type: "boolean", description: "Include human-readable text below barcode", }, rotate: { type: "string", enum: ["N", "R", "L", "I"], description: "Rotation: N=Normal, R=Right 90°, L=Left 90°, I=Inverted 180°", }, }, required: ["action", "type", "text"], }, };
- src/tools/index.ts:9-341 (registration)Registration of the 'create-barcode' tool in the main tools index, including import of handler and schema, addition to ALL_TOOLS list, and mapping in TOOL_HANDLERS object.import { CREATE_BARCODE_TOOL, handleBarcodeTool } from "./barcode.js"; import { CREATE_CHART_USING_GOOGLECHARTS_TOOL, handleGoogleChartsTool, } from "./googlecharts.js"; import { CREATE_DIAGRAM_USING_GRAPHVIZ_TOOL, handleGraphvizTool, } from "./graphviz.js"; import { CREATE_SPARKLINE_USING_CHARTJS_TOOL, handleSparklineTool, } from "./sparkline.js"; import { CREATE_TABLE_TOOL, handleTableTool } from "./table.js"; import { CREATE_CHART_USING_NATURAL_LANGUAGE_TOOL, handleTextChartTool, } from "./textchart.js"; import { CREATE_WATERMARK_TOOL, handleWatermarkTool } from "./watermark.js"; import { CREATE_QR_CODE_TOOL, handleQRCodeTool } from "./qrcode.js"; import { GET_VISUALIZATION_TOOL_HELP_TOOL, handleGetVisualizationToolHelpTool, } from "./help.js"; // All available tools const ALL_TOOLS: Array<{ tool: Tool; name: string }> = [ { tool: CREATE_CHART_USING_CHARTJS_TOOL, name: ToolNames.CHART }, { tool: CREATE_CHART_USING_APEXCHARTS_TOOL, name: ToolNames.APEXCHARTS }, { tool: CREATE_CHART_USING_GOOGLECHARTS_TOOL, name: ToolNames.GOOGLECHARTS }, { tool: CREATE_CHART_USING_NATURAL_LANGUAGE_TOOL, name: ToolNames.TEXTCHART }, { tool: CREATE_SPARKLINE_USING_CHARTJS_TOOL, name: ToolNames.SPARKLINE }, { tool: CREATE_DIAGRAM_USING_GRAPHVIZ_TOOL, name: ToolNames.GRAPHVIZ }, { tool: CREATE_WORDCLOUD_TOOL, name: ToolNames.WORDCLOUD }, { tool: CREATE_BARCODE_TOOL, name: ToolNames.BARCODE }, { tool: CREATE_QR_CODE_TOOL, name: ToolNames.QRCODE }, { tool: CREATE_TABLE_TOOL, name: ToolNames.TABLE }, { tool: CREATE_WATERMARK_TOOL, name: ToolNames.WATERMARK }, { tool: GET_VISUALIZATION_TOOL_HELP_TOOL, name: ToolNames.HELP }, ]; // Export only enabled tools export const TOOLS: Tool[] = ALL_TOOLS.filter(({ name }) => isToolEnabled(name) ).map(({ tool }) => tool); // Tool handler mapping (only includes enabled tools) const ALL_TOOL_HANDLERS: Record< string, { handler: (args: any) => Promise<any>; toolName: string } > = { "create-chart-using-chartjs": { handler: handleChartTool, toolName: ToolNames.CHART, }, "create-chart-using-apexcharts": { handler: handleApexChartsTool, toolName: ToolNames.APEXCHARTS, }, "create-chart-using-googlecharts": { handler: handleGoogleChartsTool, toolName: ToolNames.GOOGLECHARTS, }, "create-chart-using-natural-language": { handler: handleTextChartTool, toolName: ToolNames.TEXTCHART, }, "create-sparkline-using-chartjs": { handler: handleSparklineTool, toolName: ToolNames.SPARKLINE, }, "create-diagram-using-graphviz": { handler: handleGraphvizTool, toolName: ToolNames.GRAPHVIZ, }, "create-wordcloud": { handler: handleWordCloudTool, toolName: ToolNames.WORDCLOUD, }, "create-barcode": { handler: handleBarcodeTool, toolName: ToolNames.BARCODE }, "create-qr-code": { handler: handleQRCodeTool, toolName: ToolNames.QRCODE }, "create-table": { handler: handleTableTool, toolName: ToolNames.TABLE }, "create-watermark": { handler: handleWatermarkTool, toolName: ToolNames.WATERMARK, }, "get-visualization-tool-help": { handler: handleGetVisualizationToolHelpTool, toolName: ToolNames.HELP, }, }; export const TOOL_HANDLERS: Record<string, (args: any) => Promise<any>> = Object.fromEntries( Object.entries(ALL_TOOL_HANDLERS) .filter(([, { toolName }]) => isToolEnabled(toolName)) .map(([toolId, { handler }]) => [toolId, handler]) ); // Export individual tools for convenience export { CREATE_CHART_USING_CHARTJS_TOOL, CREATE_CHART_USING_APEXCHARTS_TOOL, CREATE_CHART_USING_GOOGLECHARTS_TOOL, CREATE_CHART_USING_NATURAL_LANGUAGE_TOOL, CREATE_SPARKLINE_USING_CHARTJS_TOOL, CREATE_DIAGRAM_USING_GRAPHVIZ_TOOL, CREATE_WORDCLOUD_TOOL, CREATE_BARCODE_TOOL, CREATE_QR_CODE_TOOL, CREATE_TABLE_TOOL, CREATE_WATERMARK_TOOL, GET_VISUALIZATION_TOOL_HELP_TOOL, };
- src/tools/barcode.ts:198-202 (helper)Helper function to fetch barcode image content from QuickChart API.async function fetchBarcodeContent( params: Record<string, string>, format: string = "png" ): Promise<any> { const queryString = new URLSearchParams(params).toString();
- src/tools/barcode.ts:163-187 (helper)Helper function to build query parameters for the barcode generation.function buildBarcodeParams( type: string, text: string, options: { width?: number; height?: number; scale?: number; includeText?: boolean; rotate?: string; } = {} ): Record<string, string> { const params: Record<string, string> = { type, text, }; if (options.width !== undefined) params.width = options.width.toString(); if (options.height !== undefined) params.height = options.height.toString(); if (options.scale !== undefined) params.scale = options.scale.toString(); if (options.includeText !== undefined) params.includeText = options.includeText.toString(); if (options.rotate) params.rotate = options.rotate; return params; }