create-table
Generate table images from structured data with customizable styling, then retrieve the image URL or save it directly to a file for use in reports and visualizations.
Instructions
Create table images using QuickChart - get table image URL or save table image to file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Whether to get table URL or save as file | |
| outputPath | No | Path where to save the file (only used with action=save_file) | |
| data | Yes | Table data with title, columns, and dataSource | |
| options | No | Table styling options |
Implementation Reference
- src/tools/table.ts:243-339 (handler)The main handler function that executes the create-table tool: validates inputs, generates table URL and image using QuickChart API, handles base64 image and optional file saving.export async function handleTableTool(args: any): Promise<any> { const data = args.data as any; const action = args.action as string; validateTableData(data); validateAction(action); validateOutputPath(args.outputPath, action); const config = buildTableConfig(data, args.options); const tableUrl = buildTableUrl(data); const result: any = { content: [ { type: "text", text: "Below is the table URL:", }, { type: "text", text: tableUrl, }, ], metadata: { tableType: "data", generatedAt: new Date().toISOString(), tableUrl: tableUrl, }, }; let pngData: any = null; try { pngData = await fetchTableContent(config); 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 table 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 outputPath = getDownloadPath( args.outputPath as string | undefined, "png" ); try { const dir = path.dirname(outputPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } // If pngData is null, fetch it again for file saving const dataToSave = pngData || await fetchTableContent(config); fs.writeFileSync(outputPath, dataToSave); 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 table image: ${ error instanceof Error ? error.message : String(error) }` ); } }
- src/tools/table.ts:11-120 (schema)Defines the Tool object with inputSchema for validating parameters of create-table tool, including action, data (title, columns, dataSource), outputPath, and styling options.export const CREATE_TABLE_TOOL: Tool = { name: "create-table", description: "Create table images using QuickChart - get table image URL or save table image to file", inputSchema: { type: "object", properties: { action: { type: "string", enum: ["get_url", "save_file"], description: "Whether to get table URL or save as file", }, outputPath: { type: "string", description: "Path where to save the file (only used with action=save_file)", }, data: { type: "object", description: "Table data with title, columns, and dataSource", properties: { title: { type: "string", description: "Table title", }, columns: { type: "array", items: { type: "object", properties: { title: { type: "string", description: "Column header title", }, dataIndex: { type: "string", description: "Data property key", }, width: { type: "integer", description: "Column width", }, align: { type: "string", enum: ["left", "center", "right"], description: "Text alignment", }, }, required: ["title", "dataIndex"], }, description: "Column definitions", }, dataSource: { type: "array", items: { type: "object", description: "Row data object with keys matching column dataIndex values", }, description: "Table data rows", }, }, required: ["columns", "dataSource"], }, options: { type: "object", description: "Table styling options", properties: { cellWidth: { type: "integer", description: "Cell width in pixels", }, cellHeight: { type: "integer", description: "Cell height in pixels", }, offsetLeft: { type: "integer", description: "Left offset in pixels", }, offsetRight: { type: "integer", description: "Right offset in pixels", }, fontFamily: { type: "string", description: "Font family", }, backgroundColor: { type: "string", description: "Background color", }, fontSize: { type: "integer", description: "Font size", }, borderColor: { type: "string", description: "Border color", }, headerColor: { type: "string", description: "Header background color", }, }, }, }, required: ["action", "data"], }, };
- src/tools/index.ts:90-90 (registration)Registers the create-table tool handler (handleTableTool) in the central tool handlers mapping."create-table": { handler: handleTableTool, toolName: ToolNames.TABLE },
- src/tools/index.ts:45-45 (registration)Adds the CREATE_TABLE_TOOL to the list of all available tools, associated with ToolNames.TABLE.{ tool: CREATE_TABLE_TOOL, name: ToolNames.TABLE },
- src/tools/help.ts:330-357 (helper)Provides documentation, examples, and usage information for the create-table tool."create-table": { name: "create-table", description: "Convert data to table images - get table image URL or save table image to file", documentation: "https://quickchart.io/documentation/apis/table-image-api/", additionalResources: { apiReference: "https://quickchart.io/documentation/apis/table-image-api/", }, promptExamples: [ 'Financial Reports: "Convert quarterly earnings data into professional table"', 'Comparison Charts: "Create feature comparison table for products"', 'Summary Reports: "Generate formatted tables for executive presentations"', ], usageExample: { action: "save_file", data: { title: "Q4 Sales Report", columns: [ { title: "Product", dataIndex: "product" }, { title: "Revenue", dataIndex: "revenue" }, ], dataSource: [ { product: "Product A", revenue: "$50,000" }, { product: "Product B", revenue: "$75,000" }, ], }, }, },