UNPKG

@tiberriver256/mcp-server-azure-devops

Version:

Azure DevOps reference server for the Model Context Protocol (MCP)

114 lines 4.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMe = getMe; const axios_1 = __importDefault(require("axios")); const identity_1 = require("@azure/identity"); const errors_1 = require("../../../shared/errors"); /** * Get details of the currently authenticated user * * This function returns basic profile information about the authenticated user. * * @param connection The Azure DevOps WebApi connection * @returns User profile information including id, displayName, and email * @throws {AzureDevOpsError} If retrieval of user information fails */ async function getMe(connection) { try { // Extract organization from the connection URL const { organization } = extractOrgFromUrl(connection.serverUrl); // Get the authorization header const authHeader = await getAuthorizationHeader(); // Make direct call to the Profile API endpoint // Note: This API is in the vssps.dev.azure.com domain, not dev.azure.com const response = await axios_1.default.get(`https://vssps.dev.azure.com/${organization}/_apis/profile/profiles/me?api-version=7.1`, { headers: { Authorization: authHeader, 'Content-Type': 'application/json', }, }); const profile = response.data; // Return the user profile with required fields return { id: profile.id, displayName: profile.displayName || '', email: profile.emailAddress || '', }; } catch (error) { // Handle authentication errors if (axios_1.default.isAxiosError(error) && (error.response?.status === 401 || error.response?.status === 403)) { throw new errors_1.AzureDevOpsAuthenticationError(`Authentication failed: ${error.message}`); } // If it's already an AzureDevOpsError, rethrow it if (error instanceof errors_1.AzureDevOpsError) { throw error; } // Otherwise, wrap it in a generic error throw new errors_1.AzureDevOpsError(`Failed to get user information: ${error instanceof Error ? error.message : String(error)}`); } } /** * Extract organization from the Azure DevOps URL * * @param url The Azure DevOps URL * @returns The organization */ function extractOrgFromUrl(url) { // First try modern dev.azure.com format let match = url.match(/https?:\/\/dev\.azure\.com\/([^/]+)/); // If not found, try legacy visualstudio.com format if (!match) { match = url.match(/https?:\/\/([^.]+)\.visualstudio\.com/); } // Fallback: capture the first path segment for any URL if (!match) { match = url.match(/https?:\/\/[^/]+\/([^/]+)/); } const organization = match ? match[1] : ''; if (!organization) { throw new errors_1.AzureDevOpsValidationError('Could not extract organization from URL'); } return { organization, }; } /** * Get the authorization header for API requests * * @returns The authorization header */ async function getAuthorizationHeader() { try { // For PAT authentication, we can construct the header directly if (process.env.AZURE_DEVOPS_AUTH_METHOD?.toLowerCase() === 'pat' && process.env.AZURE_DEVOPS_PAT) { // For PAT auth, we can construct the Basic auth header directly const token = process.env.AZURE_DEVOPS_PAT; const base64Token = Buffer.from(`:${token}`).toString('base64'); return `Basic ${base64Token}`; } // For Azure Identity / Azure CLI auth, we need to get a token // using the Azure DevOps resource ID // Choose the appropriate credential based on auth method const credential = process.env.AZURE_DEVOPS_AUTH_METHOD?.toLowerCase() === 'azure-cli' ? new identity_1.AzureCliCredential() : new identity_1.DefaultAzureCredential(); // Azure DevOps resource ID for token acquisition const AZURE_DEVOPS_RESOURCE_ID = '499b84ac-1321-427f-aa17-267ca6975798'; // Get token for Azure DevOps const token = await credential.getToken(`${AZURE_DEVOPS_RESOURCE_ID}/.default`); if (!token || !token.token) { throw new Error('Failed to acquire token for Azure DevOps'); } return `Bearer ${token.token}`; } catch (error) { throw new errors_1.AzureDevOpsAuthenticationError(`Failed to get authorization header: ${error instanceof Error ? error.message : String(error)}`); } } //# sourceMappingURL=feature.js.map