ms365-mcp-server
Version:
Microsoft 365 MCP Server for managing Microsoft 365 email through natural language interactions with full OAuth2 authentication support
63 lines (62 loc) • 2.08 kB
JavaScript
import fs from 'fs';
import path from 'path';
import os from 'os';
/**
* Get configuration directory with simple fallback logic
* 1. First try: os.homedir()/.ms365-mcp
* 2. Fallback: /home/siya/.ms365-mcp
*/
export function getConfigDir() {
const primaryPath = path.join(os.homedir(), '.ms365-mcp');
const fallbackPath = '/home/siya/.ms365-mcp';
// Check if primary path exists or can be created
try {
if (fs.existsSync(primaryPath)) {
return primaryPath;
}
// Try to create primary path
fs.mkdirSync(primaryPath, { recursive: true });
return primaryPath;
}
catch (error) {
// If primary path fails, use fallback
try {
if (!fs.existsSync(fallbackPath)) {
fs.mkdirSync(fallbackPath, { recursive: true });
}
return fallbackPath;
}
catch (fallbackError) {
// If everything fails, return primary path anyway
return primaryPath;
}
}
}
/**
* Check if config files exist in a directory
*/
export function hasConfigFiles(dirPath) {
const credentialsFile = path.join(dirPath, 'credentials.json');
const tokenFile = path.join(dirPath, 'token.json');
const msalCacheFile = path.join(dirPath, 'msal-cache.json');
return fs.existsSync(credentialsFile) ||
fs.existsSync(tokenFile) ||
fs.existsSync(msalCacheFile);
}
/**
* Get configuration directory, checking fallback if no config files found in primary
*/
export function getConfigDirWithFallback() {
const primaryPath = path.join(os.homedir(), '.ms365-mcp');
const fallbackPath = '/home/siya/.ms365-mcp';
// If primary path has config files, use it
if (fs.existsSync(primaryPath) && hasConfigFiles(primaryPath)) {
return primaryPath;
}
// If fallback path has config files, use it
if (fs.existsSync(fallbackPath) && hasConfigFiles(fallbackPath)) {
return fallbackPath;
}
// Default to primary path for new installations
return getConfigDir();
}