get-chart
Retrieve a specific chart from PI Dashboard by providing its unique ID.
Instructions
Get a chart by ID
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Chart ID |
Implementation Reference
- build/index.js:795-813 (registration)Tool registration for 'get-chart' using server.tool() with the MCP SDK. Registers the tool with name 'get-chart' and description 'Get a chart by ID', expecting a single 'id' parameter of type number.
server.tool("get-chart", "Get a chart by ID", { id: z.number().describe("Chart ID") }, async ({ id }) => { try { const chart = await authenticatedRequest(`/charts/${id}`); return { content: [{ type: "text", text: `Chart details:\n${JSON.stringify(chart, null, 2)}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error fetching chart: ${getErrorMessage(error)}` }] }; } }); - build/index.js:796-796 (schema)Input schema for the 'get-chart' tool: id (z.number()) representing the Chart ID.
id: z.number().describe("Chart ID") - build/index.js:797-813 (handler)Handler function for the 'get-chart' tool. Calls authenticatedRequest('/charts/{id}') to fetch chart details by ID, then returns the chart data as formatted JSON. On error, returns an isError response.
}, async ({ id }) => { try { const chart = await authenticatedRequest(`/charts/${id}`); return { content: [{ type: "text", text: `Chart details:\n${JSON.stringify(chart, null, 2)}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error fetching chart: ${getErrorMessage(error)}` }] }; } }); - build/index.js:57-125 (helper)Helper function 'authenticatedRequest' used by the get-chart handler to make authenticated API requests with Bearer token auth, query params, and JSON/binary response handling.
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)Helper function 'getErrorMessage' used for consistent error message extraction across error handlers.
const getErrorMessage = (error) => { if (error instanceof Error) return error.message; return String(error); };