diff --git a/src/config/index.ts b/src/config/index.ts index 7b69af4..e08cd44 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -42,8 +42,9 @@ export const loadOriginalSettings = (): McpSettings => { console.log(`Loaded settings from ${settingsPath}`); return settings; - } catch (error) { - console.error(`Failed to load settings from ${settingsPath}:`, error); + } catch (error: unknown) { + const errorMessage = error instanceof Error ? error.message : String(error); + console.warn(`Failed to load settings from ${settingsPath}:`, errorMessage); const defaultSettings = { mcpServers: {}, users: [] }; // Cache default settings diff --git a/src/utils/path.ts b/src/utils/path.ts index 0748d11..27c6055 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -5,6 +5,13 @@ import { dirname } from 'path'; // Project root directory - use process.cwd() as a simpler alternative const rootDir = process.cwd(); +function getParentPath(p: string, filename: string): string { + if (p.endsWith(filename)) { + p = p.slice(0, -filename.length); + } + return path.resolve(p); +} + /** * Find the path to a configuration file by checking multiple potential locations. * @param filename The name of the file to locate (e.g., 'servers.json', 'mcp_settings.json') @@ -15,11 +22,18 @@ export const getConfigFilePath = (filename: string, description = 'Configuration if (filename === 'mcp_settings.json') { const envPath = process.env.MCPHUB_SETTING_PATH; if (envPath) { - // check envPath is file or directory - const stats = fs.statSync(envPath); - if (stats.isFile()) { + // Ensure directory exists + const dir = getParentPath(envPath, filename); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + console.log(`Created directory for settings at ${dir}`); + } + + // if full path, return as is + if (envPath?.endsWith(filename)) { return envPath; } + // if directory, return path under that directory return path.resolve(envPath, filename); }