get-category
Retrieve category details by ID from the PI Dashboard to organize and manage data resources.
Instructions
Get a category by ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Category ID |
Implementation Reference
- build/index.js:630-646 (handler)The handler function for the get-category tool. It fetches the category by ID using the authenticatedRequest helper and returns formatted details or an error.}, async ({ id }) => { try { const category = await authenticatedRequest(`/categories/${id}`); return { content: [{ type: "text", text: `Category details:\n${JSON.stringify(category, null, 2)}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error fetching category: ${getErrorMessage(error)}` }] }; } });
- build/index.js:629-629 (schema)Input schema using Zod, requiring a numeric 'id' for the category ID.id: z.number().describe("Category ID")
- build/index.js:628-646 (registration)Registration of the 'get-category' tool on the MCP server, including description, schema, and handler function.server.tool("get-category", "Get a category by ID", { id: z.number().describe("Category ID") }, async ({ id }) => { try { const category = await authenticatedRequest(`/categories/${id}`); return { content: [{ type: "text", text: `Category details:\n${JSON.stringify(category, null, 2)}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error fetching category: ${getErrorMessage(error)}` }] }; } });
- build/index.js:57-125 (helper)Core helper function for all authenticated API calls, used by the get-category handler to fetch /categories/{id}.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)Utility function to safely extract error message strings, used in the handler's catch block.const getErrorMessage = (error) => { if (error instanceof Error) return error.message; return String(error); };