remove-account
Remove a cached Microsoft account by specifying its email address or account ID. Use list-accounts to see available accounts before removal.
Instructions
Remove a Microsoft account from the cache. Accepts email address (e.g. user@outlook.com) or account ID. Use list-accounts to discover available accounts.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| account | Yes | Email address or account ID of the account to remove |
Implementation Reference
- src/auth-tools.ts:202-245 (handler)MCP tool registration for 'remove-account'. Defines the tool's schema (accepts 'account' string via Zod) and handler function that calls authManager.removeAccount(). Returns success/error messages.
server.tool( 'remove-account', 'Remove a Microsoft account from the cache. Accepts email address (e.g. user@outlook.com) or account ID. Use list-accounts to discover available accounts.', { account: z.string().describe('Email address or account ID of the account to remove'), }, async ({ account }) => { try { const success = await authManager.removeAccount(account); if (success) { return { content: [ { type: 'text', text: JSON.stringify({ message: `Removed account: ${account}` }), }, ], }; } else { return { content: [ { type: 'text', text: JSON.stringify({ error: `Failed to remove account from cache: ${account}` }), }, ], isError: true, }; } } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: `Failed to remove account: ${(error as Error).message}`, }), }, ], isError: true, }; } } ); - src/auth-tools.ts:205-207 (schema)Input schema for 'remove-account' tool: requires a single 'account' string parameter (email or account ID).
{ account: z.string().describe('Email address or account ID of the account to remove'), }, - src/auth.ts:883-903 (helper)AuthManager.removeAccount() implementation. Resolves the account by identifier (email or homeAccountId), removes it from MSAL token cache, clears selection if it was the selected account, and saves state.
async removeAccount(identifier: string): Promise<boolean> { const account = await this.resolveAccount(identifier); try { await this.msalApp.getTokenCache().removeAccount(account); // If this was the selected account, clear the selection if (this.selectedAccountId === account.homeAccountId) { this.selectedAccountId = null; await this.saveSelectedAccount(); this.accessToken = null; this.tokenExpiry = null; } logger.info(`Removed account: ${account.username} (${account.homeAccountId})`); return true; } catch (error) { logger.error(`Failed to remove account ${identifier}: ${(error as Error).message}`); return false; } } - src/auth.ts:921-949 (helper)AuthManager.resolveAccount() helper used by removeAccount. Matches identifier against username (case-insensitive) or homeAccountId, throws if not found.
async resolveAccount(identifier: string): Promise<AccountInfo> { const accounts = await this.msalApp.getTokenCache().getAllAccounts(); if (accounts.length === 0) { throw new Error('No accounts found. Please login first.'); } const lowerIdentifier = identifier.toLowerCase(); // Try username (email) match first let account = accounts.find((a: AccountInfo) => a.username?.toLowerCase() === lowerIdentifier) ?? null; // Fall back to homeAccountId match if (!account) { account = accounts.find((a: AccountInfo) => a.homeAccountId === identifier) ?? null; } if (!account) { const availableAccounts = accounts .map((a: AccountInfo) => a.username || a.name || 'unknown') .join(', '); throw new Error( `Account '${identifier}' not found. Available accounts: ${availableAccounts}` ); } return account; } - src/server.ts:103-106 (registration)Registration trigger: registerAuthTools() is called from server.ts, conditionally based on HTTP mode and enableAuthTools flag.
const shouldRegisterAuthTools = !this.options.http || this.options.enableAuthTools; if (shouldRegisterAuthTools) { registerAuthTools(server, this.authManager); }