Skip to content

Commit 9eee402

Browse files
committed
backup config files before overwriting
1 parent c5cb3a5 commit 9eee402

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/platform/base.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { readFile, writeFile, mkdir } from 'fs/promises';
2-
import { dirname } from 'path';
1+
import { readFile, writeFile, mkdir, copyFile, stat } from 'fs/promises';
2+
import { dirname, join } from 'path';
33

44
/**
55
* Base interface that all MCP config formats must implement.
@@ -92,10 +92,28 @@ export abstract class ConfigManager<T extends MCPConfig, S = unknown> {
9292
const wasEnabled = this.serverName in servers;
9393
const serverConfig = this.createServerConfig(baseUrl, apiToken);
9494
servers[this.serverName] = serverConfig;
95+
await this.createBackup();
9596
await this.writeConfig(config);
9697
return wasEnabled;
9798
}
9899

100+
async createBackup(): Promise<void> {
101+
try {
102+
await stat(this.configPath);
103+
} catch (error) {
104+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
105+
return;
106+
}
107+
throw error;
108+
}
109+
110+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
111+
const dir = dirname(this.configPath);
112+
const backupPath = join(dir, `mcp.backup.${timestamp}.json`);
113+
114+
await copyFile(this.configPath, backupPath);
115+
}
116+
99117
/**
100118
* Disable the server configuration.
101119
* @returns true if the server was enabled and has been disabled, false if it was not enabled

0 commit comments

Comments
 (0)