UNPKG

@houmak/minerva-mcp-server

Version:

Minerva Model Context Protocol (MCP) Server for Microsoft 365 and Azure integrations

278 lines (277 loc) 8.8 kB
import { execa } from 'execa'; import { logger } from '../logger.js'; export class CLIM365Manager { config; _isAuthenticated = false; constructor(config) { this.config = config; } async initialize() { if (!this.config.enabled) { logger.info('CLI M365 is disabled'); return; } try { // Vérifier si CLI M365 est installé await this.checkInstallation(); // Vérifier l'authentification await this.checkAuthentication(); logger.info('CLI M365 manager initialized successfully'); } catch (error) { logger.error('Error initializing CLI M365 manager:', error); throw error; } } async checkInstallation() { try { const { stdout } = await execa('m365', ['--version'], { timeout: 5000 }); logger.info(`CLI M365 version: ${stdout.trim()}`); } catch (error) { throw new Error('CLI M365 is not installed or not accessible'); } } async checkAuthentication() { try { const { stdout } = await execa('m365', ['status'], { timeout: 10000 }); this._isAuthenticated = stdout.includes('Logged in as'); logger.info(`CLI M365 authentication status: ${this._isAuthenticated ? 'Authenticated' : 'Not authenticated'}`); } catch (error) { this._isAuthenticated = false; logger.warn('CLI M365 authentication check failed:', error); } } async executeCommand(command) { if (!this.config.enabled) { throw new Error('CLI M365 is disabled'); } if (command.requiresAuth && !this._isAuthenticated) { throw new Error('Authentication required for this command'); } const args = [ ...command.args, '--output', this.config.outputFormat ]; try { logger.info(`Executing CLI M365 command: ${command.command} ${command.args.join(' ')}`); const { stdout, stderr } = await execa('m365', [command.command, ...args], { timeout: this.config.timeout }); if (stderr) { logger.warn(`CLI M365 stderr: ${stderr}`); } if (this.config.outputFormat === 'json') { try { return JSON.parse(stdout); } catch (parseError) { logger.warn('Failed to parse JSON output:', parseError); return { raw: stdout, parsed: false }; } } return { output: stdout, success: true }; } catch (error) { logger.error(`CLI M365 command failed: ${command.command}`, error); throw error; } } // Commandes Teams async getTeams() { return await this.executeCommand({ command: 'teams', args: ['team', 'list'], description: 'List all teams', category: 'teams', requiresAuth: true }); } async getTeamChannels(teamId) { return await this.executeCommand({ command: 'teams', args: ['channel', 'list', '--teamId', teamId], description: 'List channels in a team', category: 'teams', requiresAuth: true }); } async createTeam(displayName, description) { const args = ['team', 'add', '--displayName', displayName]; if (description) { args.push('--description', description); } return await this.executeCommand({ command: 'teams', args, description: 'Create a new team', category: 'teams', requiresAuth: true }); } // Commandes Planner async getPlans(groupId) { return await this.executeCommand({ command: 'planner', args: ['plan', 'list', '--groupId', groupId], description: 'List plans in a group', category: 'planner', requiresAuth: true }); } async getPlanTasks(planId) { return await this.executeCommand({ command: 'planner', args: ['task', 'list', '--planId', planId], description: 'List tasks in a plan', category: 'planner', requiresAuth: true }); } async createTask(planId, title, description) { const args = ['task', 'add', '--planId', planId, '--title', title]; if (description) { args.push('--description', description); } return await this.executeCommand({ command: 'planner', args, description: 'Create a new task', category: 'planner', requiresAuth: true }); } // Commandes Purview async getRetentionLabels() { return await this.executeCommand({ command: 'purview', args: ['retentionlabel', 'list'], description: 'List retention labels', category: 'purview', requiresAuth: true }); } async getRetentionPolicies() { return await this.executeCommand({ command: 'purview', args: ['retentionpolicy', 'list'], description: 'List retention policies', category: 'purview', requiresAuth: true }); } // Commandes SharePoint async getSites() { return await this.executeCommand({ command: 'spo', args: ['site', 'list'], description: 'List SharePoint sites', category: 'sharepoint', requiresAuth: true }); } async getSiteLists(siteUrl) { return await this.executeCommand({ command: 'spo', args: ['list', 'list', '--webUrl', siteUrl], description: 'List lists in a site', category: 'sharepoint', requiresAuth: true }); } // Commandes OneDrive async getOneDriveFiles(userId) { const args = ['onedrive', 'file', 'list']; if (userId) { args.push('--userId', userId); } return await this.executeCommand({ command: 'spo', args, description: 'List OneDrive files', category: 'onedrive', requiresAuth: true }); } // Commandes Exchange async getMailboxes() { return await this.executeCommand({ command: 'outlook', args: ['mailbox', 'list'], description: 'List mailboxes', category: 'exchange', requiresAuth: true }); } async getMailRules(userId) { return await this.executeCommand({ command: 'outlook', args: ['mailrule', 'list', '--userId', userId], description: 'List mail rules', category: 'exchange', requiresAuth: true }); } // Commandes génériques async executeCustomCommand(command, args = []) { const [mainCommand, ...subCommands] = command.split(' '); return await this.executeCommand({ command: mainCommand, args: [...subCommands, ...args], description: `Custom command: ${command}`, category: 'custom', requiresAuth: true }); } // Utilitaires async getStatus() { return await this.executeCommand({ command: 'status', args: [], description: 'Get CLI M365 status', category: 'system', requiresAuth: false }); } async login() { try { await execa('m365', ['login'], { timeout: 60000, stdio: 'inherit' }); this._isAuthenticated = true; logger.info('CLI M365 login successful'); } catch (error) { logger.error('CLI M365 login failed:', error); throw error; } } async logout() { try { await execa('m365', ['logout'], { timeout: 10000 }); this._isAuthenticated = false; logger.info('CLI M365 logout successful'); } catch (error) { logger.error('CLI M365 logout failed:', error); throw error; } } getConfig() { return { ...this.config }; } isEnabled() { return this.config.enabled; } isAuthenticated() { return this._isAuthenticated; } }