create-chart-using-googlecharts
Generate chart images from Google Charts code, providing URLs for sharing or saving files locally for integration.
Instructions
Create charts using Google Charts - get chart image URL or save chart image to file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Whether to get chart URL or save as file | |
| outputPath | No | Path where to save the file (only used with action=save_file) | |
| code | Yes | JavaScript drawChart function code | |
| packages | No | Google Charts packages to load (default: 'corechart') | |
| width | No | Chart width in pixels | |
| height | No | Chart height in pixels | |
| mapsApiKey | No | Google Maps API key (for geo charts) |
Implementation Reference
- src/tools/googlecharts.ts:208-311 (handler)Main handler function implementing the tool logic: input validation, chart generation using QuickChart Google Charts API, image fetching, base64 encoding, and optional file saving.export async function handleGoogleChartsTool(args: any): Promise<any> { const code = args.code as string; const action = args.action as string; validateCode(code); validateAction(action); validateOutputPath(args.outputPath, action); validatePackages(args.packages); validateDimensions(args.width, args.height); validateMapsApiKey(args.mapsApiKey); const postConfig = buildGoogleChartsConfig(code, { packages: args.packages as string, width: args.width as number, height: args.height as number, mapsApiKey: args.mapsApiKey as string, }); const chartUrl = buildGoogleChartsUrl(code); const result: any = { content: [ { type: "text", text: "Below is the chart URL:", }, { type: "text", text: chartUrl, }, ], metadata: { chartType: args.packages || "corechart", generatedAt: new Date().toISOString(), chartUrl: chartUrl, }, }; try { const pngData = await fetchGoogleChartsContent(postConfig, "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 chart 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 fetchGoogleChartsContent(postConfig, 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 chart: ${ error instanceof Error ? error.message : String(error) }` ); } }
- src/tools/googlecharts.ts:11-51 (schema)Tool definition with input schema for parameters like action, code, outputPath, dimensions, etc.export const CREATE_CHART_USING_GOOGLECHARTS_TOOL: Tool = { name: "create-chart-using-googlecharts", description: "Create charts using Google Charts - get chart image URL or save chart image to file", inputSchema: { type: "object", properties: { action: { type: "string", enum: ["get_url", "save_file"], description: "Whether to get chart URL or save as file", }, outputPath: { type: "string", description: "Path where to save the file (only used with action=save_file)", }, code: { type: "string", description: "JavaScript drawChart function code", }, packages: { type: "string", description: "Google Charts packages to load (default: 'corechart')", }, width: { type: "integer", description: "Chart width in pixels", }, height: { type: "integer", description: "Chart height in pixels", }, mapsApiKey: { type: "string", description: "Google Maps API key (for geo charts)", }, }, required: ["action", "code"], }, };
- src/tools/index.ts:68-71 (registration)Registration of the tool handler in the central tool handlers mapping."create-chart-using-googlecharts": { handler: handleGoogleChartsTool, toolName: ToolNames.GOOGLECHARTS, },
- src/tools/index.ts:38-39 (registration)Inclusion of the tool in the ALL_TOOLS array for conditional enabling.{ tool: CREATE_CHART_USING_GOOGLECHARTS_TOOL, name: ToolNames.GOOGLECHARTS }, { tool: CREATE_CHART_USING_NATURAL_LANGUAGE_TOOL, name: ToolNames.TEXTCHART },