delete-category
Remove a specific category by its ID from the PI Dashboard using the MCP Server, streamlining resource management and organization.
Instructions
Delete a category
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Category ID |
Implementation Reference
- build/index.js:721-739 (registration)Tool registration for 'delete-category' including input schema and inline handler function that performs a DELETE request to `/categories/{id}`.server.tool("delete-category", "Delete a category", { id: z.number().describe("Category ID") }, async ({ id }) => { try { await authenticatedRequest(`/categories/${id}`, "DELETE"); return { content: [{ type: "text", text: `Category with ID ${id} successfully deleted.` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error deleting category: ${getErrorMessage(error)}` }] }; } });
- build/index.js:723-739 (handler)The handler function implementing the tool logic: authenticates and sends DELETE request to the API endpoint `/categories/{id}`, returns success message or error.}, async ({ id }) => { try { await authenticatedRequest(`/categories/${id}`, "DELETE"); return { content: [{ type: "text", text: `Category with ID ${id} successfully deleted.` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error deleting category: ${getErrorMessage(error)}` }] }; } });
- build/index.js:722-722 (schema)Zod input schema specifying the required 'id' parameter as a number (Category ID).id: z.number().describe("Category ID")
- build/index.js:57-125 (helper)Helper utility function used by the handler to perform authenticated HTTP requests to the PI API, specifically the DELETE call for deleting the category.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; } }