list_organizations
Retrieve all Azure DevOps organizations available to the authenticated user, enabling quick access and management of resources without manual searching.
Instructions
List all Azure DevOps organizations accessible to the current authentication
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- Core implementation of the listOrganizations tool handler. Authenticates using PAT or Azure Identity, fetches user profile to get publicAlias, then lists organizations via VSSPS API, transforms and returns Organization[]export async function listOrganizations( config: AzureDevOpsConfig, ): Promise<Organization[]> { try { // Determine auth method and create appropriate authorization header let authHeader: string; if (config.authMethod === AuthenticationMethod.PersonalAccessToken) { // PAT authentication if (!config.personalAccessToken) { throw new AzureDevOpsAuthenticationError( 'Personal Access Token (PAT) is required when using PAT authentication', ); } authHeader = createBasicAuthHeader(config.personalAccessToken); } else { // Azure Identity authentication (DefaultAzureCredential or AzureCliCredential) const credential = config.authMethod === AuthenticationMethod.AzureCli ? new AzureCliCredential() : new DefaultAzureCredential(); const token = await credential.getToken( `${AZURE_DEVOPS_RESOURCE_ID}/.default`, ); if (!token || !token.token) { throw new AzureDevOpsAuthenticationError( 'Failed to acquire Azure Identity token', ); } authHeader = `Bearer ${token.token}`; } // Step 1: Get the user profile to get the publicAlias const profileResponse = await axios.get( 'https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=6.0', { headers: { Authorization: authHeader, 'Content-Type': 'application/json', }, }, ); // Extract the publicAlias const publicAlias = profileResponse.data.publicAlias; if (!publicAlias) { throw new AzureDevOpsAuthenticationError( 'Unable to get user publicAlias from profile', ); } // Step 2: Get organizations using the publicAlias const orgsResponse = await axios.get( `https://app.vssps.visualstudio.com/_apis/accounts?memberId=${publicAlias}&api-version=6.0`, { headers: { Authorization: authHeader, 'Content-Type': 'application/json', }, }, ); // Define the shape of the API response interface AzureDevOpsOrganization { accountId: string; accountName: string; accountUri: string; } // Transform the response return orgsResponse.data.value.map((org: AzureDevOpsOrganization) => ({ id: org.accountId, name: org.accountName, url: org.accountUri, })); } catch (error) { // Handle profile API errors as authentication errors if (axios.isAxiosError(error) && error.config?.url?.includes('profile')) { throw new AzureDevOpsAuthenticationError( `Authentication failed: ${error.toJSON()}`, ); } else if ( error instanceof Error && (error.message.includes('profile') || error.message.includes('Unauthorized') || error.message.includes('Authentication')) ) { throw new AzureDevOpsAuthenticationError( `Authentication failed: ${error.message}`, ); } if (error instanceof AzureDevOpsError) { throw error; } throw new AzureDevOpsAuthenticationError( `Failed to list organizations: ${error instanceof Error ? error.message : String(error)}`, ); } }
- src/features/organizations/tool-definitions.ts:9-15 (registration)Tool registration/definition for 'list_organizations' including name, description, and input schema (empty object). Part of organizationsTools array.{ name: 'list_organizations', description: 'List all Azure DevOps organizations accessible to the current authentication', inputSchema: zodToJsonSchema(ListOrganizationsSchema), }, ];
- Zod schema for list_organizations input (empty object since no parameters required)./** * Schema for the list organizations request * Note: This is an empty schema because the operation doesn't require any parameters */ export const ListOrganizationsSchema = z.object({});
- MCP request handler dispatcher for organizations tools, specifically handles 'list_organizations' by constructing config from environment variables and calling the core listOrganizations function, returning JSON stringified result.export const handleOrganizationsRequest: RequestHandler = async ( connection: WebApi, request: CallToolRequest, ): Promise<{ content: Array<{ type: string; text: string }> }> => { switch (request.params.name) { case 'list_organizations': { // Use environment variables for authentication method and PAT // This matches how other features handle authentication const config: AzureDevOpsConfig = { authMethod: process.env.AZURE_DEVOPS_AUTH_METHOD?.toLowerCase() === 'pat' ? AuthenticationMethod.PersonalAccessToken : process.env.AZURE_DEVOPS_AUTH_METHOD?.toLowerCase() === 'azure-cli' ? AuthenticationMethod.AzureCli : AuthenticationMethod.AzureIdentity, personalAccessToken: process.env.AZURE_DEVOPS_PAT, organizationUrl: connection.serverUrl || '', }; const result = await listOrganizations(config); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], }; } default: throw new Error(`Unknown organizations tool: ${request.params.name}`); } };
- Helper function to create Basic Auth header from PAT for API requests.* Creates a Basic Auth header for the Azure DevOps API * * @param pat Personal Access Token * @returns Basic Auth header value */ function createBasicAuthHeader(pat: string): string { const token = Buffer.from(`:${pat}`).toString('base64'); return `Basic ${token}`; }