-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.ts
More file actions
115 lines (100 loc) · 2.95 KB
/
utils.ts
File metadata and controls
115 lines (100 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import fs from "node:fs";
import path from "node:path";
import type {
ValidClient,
ClientConfig,
McpServersClientConfig,
VscodeClientConfig,
} from "./types.js";
import { clientPaths } from "./config.js";
export function getConfigPath(client: ValidClient): string {
const configPath = clientPaths[client];
if (!configPath) {
throw new Error(`Invalid client: ${client}`);
}
return configPath;
}
export function readConfig(client: ValidClient): ClientConfig {
const configPath = getConfigPath(client);
if (!fs.existsSync(configPath)) {
return client === "vscode"
? { servers: {} }
: { mcpServers: {} };
}
try {
const rawConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
if (client === "vscode") {
return {
...rawConfig,
servers:
rawConfig.servers && typeof rawConfig.servers === "object"
? rawConfig.servers
: {},
};
}
return {
...rawConfig,
mcpServers: rawConfig.mcpServers || {},
};
} catch (error) {
return client === "vscode"
? { servers: {} }
: { mcpServers: {} };
}
}
export function writeConfig(client: ValidClient, config: ClientConfig): void {
const configPath = getConfigPath(client);
const configDir = path.dirname(configPath);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
if (client === "vscode") {
const vscodeConfig = config as VscodeClientConfig;
if (!vscodeConfig.servers || typeof vscodeConfig.servers !== "object") {
throw new Error("Invalid servers structure");
}
let existingRaw: Record<string, unknown> = {};
try {
if (fs.existsSync(configPath)) {
existingRaw = JSON.parse(fs.readFileSync(configPath, "utf8"));
}
} catch {
// If reading fails, continue with empty existing config
}
const existingServers =
existingRaw.servers &&
typeof existingRaw.servers === "object" &&
!Array.isArray(existingRaw.servers)
? (existingRaw.servers as Record<string, unknown>)
: {};
const mergedConfig = {
...existingRaw,
servers: {
...existingServers,
...vscodeConfig.servers,
},
};
fs.writeFileSync(configPath, JSON.stringify(mergedConfig, null, 2));
return;
}
const mcpConfig = config as McpServersClientConfig;
if (!mcpConfig.mcpServers || typeof mcpConfig.mcpServers !== "object") {
throw new Error("Invalid mcpServers structure");
}
let existingConfig: McpServersClientConfig = { mcpServers: {} };
try {
if (fs.existsSync(configPath)) {
existingConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
}
} catch (error) {
// If reading fails, continue with empty existing config
}
const mergedConfig = {
...existingConfig,
mcpServers: {
...existingConfig.mcpServers,
...mcpConfig.mcpServers,
},
};
fs.writeFileSync(configPath, JSON.stringify(mergedConfig, null, 2));
}