feat: implement settings cache with load, save, clear, and status functions (#167)

This commit is contained in:
samanhappy
2025-06-07 20:36:52 +08:00
committed by GitHub
parent f8149c4b0b
commit 56c6447469
2 changed files with 45 additions and 3 deletions

View File

@@ -15,18 +15,37 @@ const defaultConfig = {
mcpHubVersion: getPackageVersion(), mcpHubVersion: getPackageVersion(),
}; };
// Settings cache
let settingsCache: McpSettings | null = null;
export const getSettingsPath = (): string => { export const getSettingsPath = (): string => {
return getConfigFilePath('mcp_settings.json', 'Settings'); return getConfigFilePath('mcp_settings.json', 'Settings');
}; };
export const loadSettings = (): McpSettings => { export const loadSettings = (): McpSettings => {
// If cache exists, return cached data directly
if (settingsCache) {
return settingsCache;
}
const settingsPath = getSettingsPath(); const settingsPath = getSettingsPath();
try { try {
const settingsData = fs.readFileSync(settingsPath, 'utf8'); const settingsData = fs.readFileSync(settingsPath, 'utf8');
return JSON.parse(settingsData); const settings = JSON.parse(settingsData);
// Update cache
settingsCache = settings;
console.log(`Loaded settings from ${settingsPath}:`, settings);
return settings;
} catch (error) { } catch (error) {
console.error(`Failed to load settings from ${settingsPath}:`, error); console.error(`Failed to load settings from ${settingsPath}:`, error);
return { mcpServers: {}, users: [] }; const defaultSettings = { mcpServers: {}, users: [] };
// Cache default settings
settingsCache = defaultSettings;
return defaultSettings;
} }
}; };
@@ -34,6 +53,10 @@ export const saveSettings = (settings: McpSettings): boolean => {
const settingsPath = getSettingsPath(); const settingsPath = getSettingsPath();
try { try {
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8'); fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
// Update cache after successful save
settingsCache = settings;
return true; return true;
} catch (error) { } catch (error) {
console.error(`Failed to save settings to ${settingsPath}:`, error); console.error(`Failed to save settings to ${settingsPath}:`, error);
@@ -41,6 +64,22 @@ export const saveSettings = (settings: McpSettings): boolean => {
} }
}; };
/**
* Clear settings cache, force next loadSettings call to re-read from file
*/
export const clearSettingsCache = (): void => {
settingsCache = null;
};
/**
* Get current cache status (for debugging)
*/
export const getSettingsCacheInfo = (): { hasCache: boolean } => {
return {
hasCache: settingsCache !== null,
};
};
export const replaceEnvVars = (env: Record<string, any>): Record<string, any> => { export const replaceEnvVars = (env: Record<string, any>): Record<string, any> => {
const res: Record<string, string> = {}; const res: Record<string, string> = {};
for (const [key, value] of Object.entries(env)) { for (const [key, value] of Object.entries(env)) {

View File

@@ -109,7 +109,10 @@ export const handleSseMessage = async (req: Request, res: Response): Promise<voi
export const handleMcpPostRequest = async (req: Request, res: Response): Promise<void> => { export const handleMcpPostRequest = async (req: Request, res: Response): Promise<void> => {
const sessionId = req.headers['mcp-session-id'] as string | undefined; const sessionId = req.headers['mcp-session-id'] as string | undefined;
const group = req.params.group; const group = req.params.group;
console.log(`Handling MCP post request for sessionId: ${sessionId} and group: ${group}`); const body = req.body;
console.log(
`Handling MCP post request for sessionId: ${sessionId} and group: ${group} with body: ${JSON.stringify(body)}`,
);
// Check bearer auth // Check bearer auth
if (!validateBearerAuth(req)) { if (!validateBearerAuth(req)) {
res.status(401).send('Bearer authentication required or invalid token'); res.status(401).send('Bearer authentication required or invalid token');