authenticate-with-credentials
Authenticate to the PI API by providing a username and password as a last resort authentication method, using the format 'username password'.
Instructions
Authenticate with the PI API using username and password (last resort option)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| credentials | Yes | Username and password as 'username password' |
Implementation Reference
- build/index.js:459-461 (registration)The register call for the 'authenticate-with-credentials' tool via server.tool().
server.tool("authenticate-with-credentials", "Authenticate with the PI API using username and password (last resort option)", { credentials: z.string().describe("Username and password as 'username password'") }, async ({ credentials }) => { - build/index.js:460-460 (schema)Input schema for the tool: expects a 'credentials' string (username and password as 'username password').
credentials: z.string().describe("Username and password as 'username password'") - build/index.js:461-535 (handler)The handler function that parses credentials, performs basic auth against /tokens endpoint, stores the token, and returns success/error.
}, async ({ credentials }) => { try { if (!apiUrlSet) { return { isError: true, content: [{ type: "text", text: "API URL not set. Please set the API URL first using the set-api-url tool." }] }; } // Parse credentials - simple space separation const parts = credentials.trim().split(/\s+/); if (parts.length < 2) { return { isError: true, content: [{ type: "text", text: "Invalid credentials format. Please provide as 'username password'" }] }; } // First part is username, rest is considered password (in case password has spaces) const username = parts[0]; const password = parts.slice(1).join(' '); if (!username || !password) { return { isError: true, content: [{ type: "text", text: "Both username and password are required. Please provide as 'username password'" }] }; } // Authenticate with the credentials const credentialsBase64 = Buffer.from(`${username}:${password}`).toString("base64"); const response = await fetch(`${API_BASE_URL}/tokens`, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `basic ${credentialsBase64}` } }); if (!response.ok) { const errorText = await response.text(); return { isError: true, content: [{ type: "text", text: `Authentication failed: ${response.status} - ${errorText}` }] }; } const data = await response.json(); if (data && typeof data === 'object' && 'token' in data && typeof data.token === 'string') { authToken = data.token; connectionVerified = true; } else { return { isError: true, content: [{ type: "text", text: "Authentication failed: Invalid response format" }] }; } return { content: [{ type: "text", text: "✅ Authentication successful. You can now use other tools and resources." }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error authenticating: ${getErrorMessage(error)}` }] }; } });