export-chart
Export a chart by its ID to JSON or CSV, enabling data analysis or system integration.
Instructions
Export a chart in various formats
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Chart ID | |
| format | Yes | Export format |
Implementation Reference
- build/index.js:835-868 (handler)The 'export-chart' tool handler: exports a chart in JSON or CSV format by making an authenticated request to /charts/{id}/{format}. Handles both binary (base64) and JSON/text responses.
server.tool("export-chart", "Export a chart in various formats", { id: z.number().describe("Chart ID"), format: z.enum(["json", "csv"]).describe("Export format") }, async ({ id, format }) => { try { const result = await authenticatedRequest(`/charts/${id}/${format}`); if (result && typeof result === 'object' && 'contentType' in result && 'data' in result && typeof result.data === 'string') { // This is a binary response return { content: [{ type: "text", text: `Chart exported successfully as ${format.toUpperCase()}.\nContent type: ${result.contentType}\nBase64 data: ${result.data.substring(0, 100)}...` }] }; } else { // This is a JSON or text response return { content: [{ type: "text", text: typeof result === 'string' ? result : JSON.stringify(result, null, 2) }] }; } } catch (error) { return { isError: true, content: [{ type: "text", text: `Error exporting chart: ${getErrorMessage(error)}` }] }; } }); - build/index.js:836-838 (schema)Input schema for export-chart: requires id (number) and format (enum: 'json' or 'csv').
id: z.number().describe("Chart ID"), format: z.enum(["json", "csv"]).describe("Export format") }, async ({ id, format }) => { - build/index.js:835-835 (registration)Registration of the 'export-chart' tool via server.tool(...) with description 'Export a chart in various formats'.
server.tool("export-chart", "Export a chart in various formats", { - build/index.js:57-125 (helper)The authenticatedRequest helper function that performs the actual HTTP request. For CSV content (text/csv), it returns a base64-encoded buffer wrapped in an object with contentType and data properties. For JSON, it returns parsed data.
async function authenticatedRequest(endpoint, method = "GET", body = null, queryParams = {}) { if (!apiUrlSet) { throw new Error("API URL not set. Please set the API URL using the set-api-url tool."); } if (!authToken) { throw new Error("Not authenticated. Please authenticate first."); } // Build URL with query parameters let url = `${API_BASE_URL}${endpoint}`; // Add orgId if available if (orgId !== null) { queryParams.orgId = orgId.toString(); } // Add query parameters if any if (Object.keys(queryParams).length > 0) { const queryString = Object.entries(queryParams) .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) .join("&"); url = `${url}?${queryString}`; } logInfo(`Making ${method} request to ${url}`); const headers = { "Authorization": `bearer ${authToken}`, "Content-Type": "application/json" }; const options = { method, headers }; if (body !== null && ["POST", "PUT"].includes(method)) { options.body = JSON.stringify(body); logInfo(`Request body: ${JSON.stringify(body)}`); } try { const response = await fetch(url, options); if (!response.ok) { const errorText = await response.text(); logError(`API request failed with status ${response.status}: ${errorText}`); throw new Error(`API request failed with status ${response.status}: ${response.statusText}`); } // Check if the response is JSON or binary const contentType = response.headers.get("content-type") || ""; if (contentType.includes("application/json")) { const jsonData = await response.json(); logInfo(`Received JSON response: ${JSON.stringify(jsonData).substring(0, 200)}...`); return jsonData; } else if (contentType.includes("text/csv")) { // For binary/file responses, return a base64 encoded string const buffer = await response.arrayBuffer(); const base64 = Buffer.from(buffer).toString("base64"); logInfo(`Received binary response of type ${contentType}, length: ${base64.length}`); return { contentType, data: base64 }; } else { // Otherwise, return as text const text = await response.text(); logInfo(`Received text response: ${text.substring(0, 200)}...`); return text; } } catch (error) { logError(`API request error: ${getErrorMessage(error)}`); throw error; } } - build/index.js:46-50 (helper)The getErrorMessage helper used to safely extract error messages in the catch block of the export-chart handler.
const getErrorMessage = (error) => { if (error instanceof Error) return error.message; return String(error); };