Skip to main content
Glama

select-account

Select a specific Microsoft account to manage multiple tenants and access Microsoft 365 services through the Microsoft Graph API.

Instructions

Select a specific Microsoft account to use

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
accountIdYesThe account ID to select

Implementation Reference

  • Registers the 'select-account' MCP tool. Includes tool name, description, input schema (accountId: string), and handler function that calls authManager.selectAccount and returns success/error response.
    server.tool(
      'select-account',
      'Select a specific Microsoft account to use',
      {
        accountId: z.string().describe('The account ID to select'),
      },
      async ({ accountId }) => {
        try {
          const success = await authManager.selectAccount(accountId);
          if (success) {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({ message: `Selected account: ${accountId}` }),
                },
              ],
            };
          } else {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({ error: `Account not found: ${accountId}` }),
                },
              ],
            };
          }
        } catch (error) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  error: `Failed to select account: ${(error as Error).message}`,
                }),
              },
            ],
          };
        }
      }
    );
  • The handler function for the 'select-account' tool. Takes accountId, calls authManager.selectAccount(accountId), and formats success or error response as MCP content.
      async ({ accountId }) => {
        try {
          const success = await authManager.selectAccount(accountId);
          if (success) {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({ message: `Selected account: ${accountId}` }),
                },
              ],
            };
          } else {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({ error: `Account not found: ${accountId}` }),
                },
              ],
            };
          }
        } catch (error) {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  error: `Failed to select account: ${(error as Error).message}`,
                }),
              },
            ],
          };
        }
      }
    );
  • Core helper method in AuthManager class that implements account selection logic: finds account by ID, sets selectedAccountId, persists to storage (keytar/file), clears token cache, and logs.
    async selectAccount(accountId: string): Promise<boolean> {
      const accounts = await this.listAccounts();
      const account = accounts.find((acc: AccountInfo) => acc.homeAccountId === accountId);
    
      if (!account) {
        logger.error(`Account with ID ${accountId} not found`);
        return false;
      }
    
      this.selectedAccountId = accountId;
      await this.saveSelectedAccount();
    
      // Clear cached tokens to force refresh with new account
      this.accessToken = null;
      this.tokenExpiry = null;
    
      logger.info(`Selected account: ${account.username} (${accountId})`);
      return true;
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ForITLLC/forit-microsoft-graph'

If you have feedback or need assistance with the MCP directory API, please join our Discord server