@mcp-apps/api-tools-mcp-server
Version:
MCP server for interacting with APIs and web services
83 lines • 3.59 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccessToken = getAccessToken;
const identity_1 = require("@azure/identity");
const dotenv = __importStar(require("dotenv"));
dotenv.config();
// Map to store tokens by clientId + tenantId combination
const tokenCache = new Map();
// Helper function to create a cache key
function createCacheKey(clientId, tenantId) {
return `${clientId}|${tenantId}`;
}
async function getAccessToken(clientId, tenantId, scopes) {
const now = Date.now();
// Resolve clientId and tenantId, using environment variables as fallback
const resolvedClientId = clientId || process.env.AZURE_CLIENT_ID || '';
const resolvedTenantId = tenantId || process.env.AZURE_TENANT_ID || 'common';
// Create cache key for this client+tenant combination
const cacheKey = createCacheKey(resolvedClientId, resolvedTenantId);
// Check if we have a valid cached token for this combination
const cachedEntry = tokenCache.get(cacheKey);
if (cachedEntry && cachedEntry.expiresAt > now) {
return cachedEntry.token;
}
try {
const credential = new identity_1.InteractiveBrowserCredential({
clientId: resolvedClientId,
tenantId: resolvedTenantId,
loginStyle: "popup"
});
const tokenResponse = await credential.getToken([`${resolvedClientId}/.default`].concat(scopes ? scopes : []));
if (!tokenResponse || !tokenResponse.token) {
throw new Error("Failed to acquire Azure DevOps token");
}
// Set expiration time (expiresOn is in seconds from epoch)
const expirationTime = tokenResponse.expiresOnTimestamp;
const expiresAt = expirationTime - (5 * 60 * 1000); // Token lifetime minus 5 minute safety buffer
// Store the token in cache using the client+tenant key
tokenCache.set(cacheKey, {
token: tokenResponse.token,
expiresAt: expiresAt
});
return tokenResponse.token;
}
catch (error) {
console.error("Error acquiring token:", error);
throw new Error("Failed to acquire Azure DevOps access token");
}
}
//# sourceMappingURL=token-manager.js.map