check-connection
Validate the current API URL and authentication to ensure a successful connection to PI Dashboard resources.
Instructions
Check if the current API URL and authentication are valid
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- build/index.js:263-263 (schema)The tool is defined with an empty schema object {} meaning no input parameters are required.
server.tool("check-connection", "Check if the current API URL and authentication are valid", {}, async () => { - build/index.js:263-307 (handler)The main handler function for the check-connection tool. It checks if API URL and auth token are set, then calls verifyConnection() to validate the connection by making a lightweight API request to /tokens/keepAlive.
server.tool("check-connection", "Check if the current API URL and authentication are valid", {}, async () => { try { if (!apiUrlSet || !API_BASE_URL) { return { content: [{ type: "text", text: "API URL not set. Please set the API URL using the set-api-url tool." }] }; } if (!authToken) { return { content: [{ type: "text", text: "Not authenticated. Please authenticate using the authenticate tool." }] }; } // Verify the connection const isConnected = await verifyConnection(); if (isConnected) { return { content: [{ type: "text", text: `✅ Connection successful! The API URL and token are valid. You're ready to use the PI API.` }] }; } else { return { isError: true, content: [{ type: "text", text: `❌ Connection failed. The token might be invalid or expired. Please try to authenticate again.` }] }; } } catch (error) { return { isError: true, content: [{ type: "text", text: `Connection check failed: ${getErrorMessage(error)}` }] }; } }); - build/index.js:144-162 (helper)The verifyConnection helper function that attempts a POST request to /tokens/keepAlive endpoint to verify the API connection is working. Returns true/false and sets connectionVerified flag.
async function verifyConnection() { if (!apiUrlSet || !API_BASE_URL) { return false; } if (!authToken) { return false; } try { // Try a lightweight request to verify the connection await authenticatedRequest("/tokens/keepAlive", "POST"); connectionVerified = true; return true; } catch (error) { logError(`Connection verification failed: ${getErrorMessage(error)}`); connectionVerified = false; return false; } } - build/index.js:263-263 (registration)Tool registered via server.tool('check-connection', ...) on the McpServer instance named 'PI API Server'.
server.tool("check-connection", "Check if the current API URL and authentication are valid", {}, async () => { - build/index.js:57-100 (helper)The authenticatedRequest helper used internally by verifyConnection to make authenticated HTTP requests with the bearer token.
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();