login
Authenticate with Microsoft Graph API using device code flow to access Microsoft 365 services across multiple tenants. Manage multi-tenant accounts through a single flexible interface.
Instructions
Authenticate with Microsoft using device code flow
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| force | No | Force a new login even if already logged in |
Implementation Reference
- src/auth-tools.ts:7-57 (registration)Registration of the 'login' MCP tool, including its description, input schema (force parameter), and complete handler function that implements device code authentication flow using AuthManager.server.tool( 'login', 'Authenticate with Microsoft using device code flow', { force: z.boolean().default(false).describe('Force a new login even if already logged in'), }, async ({ force }) => { try { if (!force) { const loginStatus = await authManager.testLogin(); if (loginStatus.success) { return { content: [ { type: 'text', text: JSON.stringify({ status: 'Already logged in', ...loginStatus, }), }, ], }; } } const text = await new Promise<string>((resolve, reject) => { authManager.acquireTokenByDeviceCode(resolve).catch(reject); }); return { content: [ { type: 'text', text: JSON.stringify({ error: 'device_code_required', message: text.trim(), }), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: `Authentication failed: ${(error as Error).message}` }), }, ], }; } } );
- src/server.ts:80-84 (registration)Conditional registration of auth tools (including 'login') via registerAuthTools call during server initialization.const shouldRegisterAuthTools = !this.options.http || this.options.enableAuthTools; if (shouldRegisterAuthTools) { // Pass graphClient to enable the graph-request tool registerAuthTools(this.server, this.authManager, this.graphClient); }
- src/auth-tools.ts:11-12 (schema)Zod input schema for the 'login' tool defining the optional 'force' boolean parameter.force: z.boolean().default(false).describe('Force a new login even if already logged in'), },
- src/auth-tools.ts:13-56 (handler)The executor/handler function for the 'login' tool. Handles checking current login status, forcing new login if specified, initiating device code flow, and returning appropriate MCP content responses.async ({ force }) => { try { if (!force) { const loginStatus = await authManager.testLogin(); if (loginStatus.success) { return { content: [ { type: 'text', text: JSON.stringify({ status: 'Already logged in', ...loginStatus, }), }, ], }; } } const text = await new Promise<string>((resolve, reject) => { authManager.acquireTokenByDeviceCode(resolve).catch(reject); }); return { content: [ { type: 'text', text: JSON.stringify({ error: 'device_code_required', message: text.trim(), }), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: `Authentication failed: ${(error as Error).message}` }), }, ], }; } }