UNPKG

@mcp-abap-adt/connection

Version:

ABAP connection layer for MCP ABAP ADT server

66 lines (65 loc) 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseAbapConnection = void 0; const axios_1 = require("axios"); const AbstractAbapConnection_js_1 = require("./AbstractAbapConnection.js"); /** * Basic Authentication connection for on-premise SAP systems */ class BaseAbapConnection extends AbstractAbapConnection_js_1.AbstractAbapConnection { constructor(config, logger, sessionId, options) { BaseAbapConnection.validateConfig(config); super(config, logger || null, sessionId, options); } /** * Connect to SAP system with Basic Auth * Fetches CSRF token which also establishes session cookies */ async connect() { const baseUrl = await this.getBaseUrl(); const discoveryUrl = `${baseUrl}/sap/bc/adt/discovery`; this.logger?.debug(`[DEBUG] BaseAbapConnection - Connecting to SAP system: ${discoveryUrl}`); try { // Try to get CSRF token (this will also get cookies) const token = await this.fetchCsrfToken(discoveryUrl, 3, 1000); this.setCsrfToken(token); this.logger?.debug('Successfully connected to SAP system', { hasCsrfToken: !!this.getCsrfToken(), hasCookies: !!this.getCookies(), cookieLength: this.getCookies()?.length || 0, }); } catch (error) { // For Basic auth, log warning but don't fail // The retry logic in makeAdtRequest will handle transient errors automatically const errorMsg = error instanceof Error ? error.message : String(error); this.logger?.warn(`[WARN] BaseAbapConnection - Could not connect to SAP system upfront: ${errorMsg}. Will retry on first request.`); // Still try to extract cookies from error response if available if (error instanceof axios_1.AxiosError && error.response?.headers) { // updateCookiesFromResponse is private, but cookies are extracted in fetchCsrfToken if (this.getCookies()) { this.logger?.debug(`[DEBUG] BaseAbapConnection - Cookies extracted from error response during connect (first 100 chars): ${this.getCookies()?.substring(0, 100)}...`); } } } } buildAuthorizationHeader() { const { username, password } = this.getConfig(); const safeUsername = username ?? ''; const safePassword = password ?? ''; const token = Buffer.from(`${safeUsername}:${safePassword}`).toString('base64'); return `Basic ${token}`; } static validateConfig(config) { if (config.authType !== 'basic') { throw new Error(`Basic authentication connection expects authType "basic", got "${config.authType}"`); } if (!config.username || !config.password) { throw new Error('Basic authentication requires both username and password'); } if (!config.client) { throw new Error('Basic authentication requires SAP_CLIENT to be provided'); } } } exports.BaseAbapConnection = BaseAbapConnection;