auth_login
Authenticate to an API, extract a JWT from the response, and store it locally for reuse in subsequent requests.
Instructions
Authenticate against an API, extract a JWT, and store it locally for reuse
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | The URL to make the authentication request to | |
| headers | No | Optional headers to include in the auth request | |
| body | No | The request body for authentication | |
| requestType | No | Request type for the auth call | |
| fieldFiles | No | Array of field names that should be treated as files for form-data auth requests | |
| jwtPath | Yes | Dot-notation path to the JWT in the response (e.g., token.access_token) | |
| folder | Yes | Folder where the token file will be stored | |
| user_title | No | Optional label for the stored token (default: 'default') |
Implementation Reference
- src/index.ts:861-899 (handler)The main handler logic for the auth_login tool. Performs a POST request to the auth URL, extracts the JWT token from the response using the dot-notation jwtPath, validates it, stores it in the specified folder's tokens.json under the user_title, and returns a success message.case "auth_login": { const url = String(args?.url || ''); const headers = args?.headers as Record<string, string> || {}; const body = args?.body; const requestType = args?.requestType as 'json' | 'form-data' || 'json'; const fieldFiles = args?.fieldFiles as string[] || []; const jwtPath = String(args?.jwtPath || ''); const folder = String(args?.folder || ''); const userTitle = String(args?.user_title || 'default'); if (!url) { throw new Error("URL is required for auth_login"); } if (!jwtPath) { throw new Error("jwtPath is required to extract the token"); } if (!folder) { throw new Error("folder is required to store the token"); } const result = await makeHttpRequest('POST', { url, headers, body, requestType, fieldFiles }); const token = extractValueByPath(result.data, jwtPath); if (!token || typeof token !== 'string') { throw new Error(`Could not find a token at path '${jwtPath}' in the response`); } saveToken(folder, userTitle, token); return { content: [{ type: "text", text: `Stored token for user '${userTitle}' at ${resolveTokenFilePath(folder)}` }] }; }
- src/index.ts:235-282 (registration)Registration of the auth_login tool in the listTools handler, including its description and full input schema definition.{ name: "auth_login", description: "Authenticate against an API, extract a JWT, and store it locally for reuse", inputSchema: { type: "object", properties: { url: { type: "string", description: "The URL to make the authentication request to" }, headers: { type: "object", description: "Optional headers to include in the auth request", additionalProperties: { type: "string" } }, body: { description: "The request body for authentication" }, requestType: { type: "string", enum: ["json", "form-data"], description: "Request type for the auth call" }, fieldFiles: { type: "array", items: { type: "string" }, description: "Array of field names that should be treated as files for form-data auth requests" }, jwtPath: { type: "string", description: "Dot-notation path to the JWT in the response (e.g., token.access_token)" }, folder: { type: "string", description: "Folder where the token file will be stored" }, user_title: { type: "string", description: "Optional label for the stored token (default: 'default')" } }, required: ["url", "jwtPath", "folder"] } },
- src/index.ts:393-407 (helper)Helper function called by the auth_login handler to persist the extracted JWT token to tokens.json in the specified folder, creating or updating the user entry.function saveToken(folder: string, userTitle: string, token: string) { const tokenFile = resolveTokenFilePath(folder); mkdirSync(folder, { recursive: true }); const tokens = loadStoredTokens(folder); const existingIndex = tokens.findIndex((entry) => entry.user_title_name === userTitle); if (existingIndex >= 0) { tokens[existingIndex].token = token; } else { tokens.push({ user_title_name: userTitle, token }); } writeFileSync(tokenFile, JSON.stringify(tokens, null, 2), 'utf-8'); }
- src/index.ts:409-416 (helper)Helper function used by the auth_login handler to extract the JWT token from the API response using dot-notation path (e.g., 'data.token.access_token').function extractValueByPath(obj: any, path: string): any { return path.split('.').reduce((current, key) => { if (current && typeof current === 'object' && key in current) { return current[key as keyof typeof current]; } return undefined; }, obj); }