authenticate-with-credentials
Authenticate to the PI API using username and password credentials for secure access to PI Dashboard resources, a fallback option when other methods are unavailable.
Instructions
Authenticate with the PI API using username and password (last resort option)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| credentials | Yes | Username and password as 'username password' |
Implementation Reference
- build/index.js:461-535 (handler)Handler function that parses username/password from credentials string, performs Basic Auth POST to /tokens endpoint, sets global authToken and connectionVerified on success, returns appropriate success/error messages.}, 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)}` }] }; } });
- build/index.js:460-460 (schema)Zod input schema validating the 'credentials' parameter as a string containing 'username password'.credentials: z.string().describe("Username and password as 'username password'")
- build/index.js:459-535 (registration)Registers the 'authenticate-with-credentials' tool on the MCP server with description, input schema using Zod, and inline async handler function.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 }) => { 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)}` }] }; } });