@duckmind/dm-windows-x64 0.63.4 → 0.63.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dm.exe +0 -0
- package/extensions/.dm-extensions.json +236 -13
- package/extensions/dm-context/package.json +1 -1
- package/extensions/dm-context/skills/context-management/SKILL.md +173 -223
- package/extensions/dm-context/skills/context-management/references/development-and-troubleshooting.md +75 -0
- package/extensions/dm-context/skills/context-management/references/interleaved-async-work.md +143 -0
- package/extensions/dm-context/skills/context-management/references/planning-and-execution.md +81 -0
- package/extensions/dm-context/skills/context-management/references/repeated-items-and-batch-work.md +69 -0
- package/extensions/dm-context/skills/context-management/references/retry-branch-and-pivot.md +80 -0
- package/extensions/dm-context/skills/context-management/references/search-research-and-reading.md +103 -0
- package/extensions/dm-context/skills/context-management/references/task-switching-and-cleanup.md +73 -0
- package/extensions/dm-context/src/context.js +3 -2
- package/extensions/dm-context/src/index.js +168 -84
- package/extensions/dm-mcp-adapter/OAUTH.md +3 -0
- package/extensions/dm-mcp-adapter/THIRD_PARTY_NOTICES.md +4 -0
- package/extensions/dm-mcp-adapter/abort.js +35 -0
- package/extensions/dm-mcp-adapter/agent-dir.js +45 -0
- package/extensions/dm-mcp-adapter/agent-plugin-loader.js +349 -0
- package/extensions/dm-mcp-adapter/app-bridge.bundle.js +5843 -0
- package/extensions/dm-mcp-adapter/cli.js +354 -0
- package/extensions/dm-mcp-adapter/commands.js +619 -0
- package/extensions/dm-mcp-adapter/config.js +1060 -0
- package/extensions/dm-mcp-adapter/consent-manager.js +60 -0
- package/extensions/dm-mcp-adapter/direct-tools.js +544 -0
- package/extensions/dm-mcp-adapter/dist/agent-dir.js +45 -0
- package/extensions/dm-mcp-adapter/dist/agent-plugin-loader.js +349 -0
- package/extensions/dm-mcp-adapter/dist/config.js +1060 -0
- package/extensions/dm-mcp-adapter/dist/mcp-bearer-store.js +311 -0
- package/extensions/dm-mcp-adapter/dist/metadata-cache.js +285 -0
- package/extensions/dm-mcp-adapter/dist/package-mcp-loader.js +144 -0
- package/extensions/dm-mcp-adapter/dist/resource-tools.js +7 -0
- package/extensions/dm-mcp-adapter/dist/types.js +247 -0
- package/extensions/dm-mcp-adapter/dist/ui-app-bridge-helpers.js +36 -0
- package/extensions/dm-mcp-adapter/dist/ui-stream-types.js +58 -0
- package/extensions/dm-mcp-adapter/dist/ui-tool-visibility.js +26 -0
- package/extensions/dm-mcp-adapter/dist/utils.js +307 -0
- package/extensions/dm-mcp-adapter/elicitation-handler.js +306 -0
- package/extensions/dm-mcp-adapter/error-signal.js +16 -0
- package/extensions/dm-mcp-adapter/errors.js +225 -0
- package/extensions/dm-mcp-adapter/failure-backoff.js +19 -0
- package/extensions/dm-mcp-adapter/glimpse-ui.js +61 -0
- package/extensions/dm-mcp-adapter/host-html-template.js +464 -0
- package/extensions/dm-mcp-adapter/index.js +1079 -0
- package/extensions/dm-mcp-adapter/init.js +624 -0
- package/extensions/dm-mcp-adapter/json-schema-validator.js +53 -0
- package/extensions/dm-mcp-adapter/lifecycle.js +385 -0
- package/extensions/dm-mcp-adapter/logger.js +115 -0
- package/extensions/dm-mcp-adapter/mcp-auth-flow.js +824 -0
- package/extensions/dm-mcp-adapter/mcp-auth.js +732 -0
- package/extensions/dm-mcp-adapter/mcp-bearer-store.js +311 -0
- package/extensions/dm-mcp-adapter/mcp-callback-server.js +388 -0
- package/extensions/dm-mcp-adapter/mcp-code.js +291 -0
- package/extensions/dm-mcp-adapter/mcp-keyring-helper.cjs +88 -0
- package/extensions/dm-mcp-adapter/mcp-oauth-provider.js +490 -0
- package/extensions/dm-mcp-adapter/mcp-output-guard.js +386 -0
- package/extensions/dm-mcp-adapter/mcp-panel.js +951 -0
- package/extensions/dm-mcp-adapter/mcp-probe.js +151 -0
- package/extensions/dm-mcp-adapter/mcp-references.js +263 -0
- package/extensions/dm-mcp-adapter/mcp-script-worker.mjs +126 -0
- package/extensions/dm-mcp-adapter/mcp-setup-panel.js +578 -0
- package/extensions/dm-mcp-adapter/mcp-status.js +80 -0
- package/extensions/dm-mcp-adapter/mcp-trace.js +215 -0
- package/extensions/dm-mcp-adapter/metadata-cache.js +292 -0
- package/extensions/dm-mcp-adapter/namespace-tools.js +155 -0
- package/extensions/dm-mcp-adapter/npx-resolver.js +465 -0
- package/extensions/dm-mcp-adapter/oauth-handler.js +17 -0
- package/extensions/dm-mcp-adapter/oauth.js +17 -0
- package/extensions/dm-mcp-adapter/onboarding-state.js +62 -0
- package/extensions/dm-mcp-adapter/package-mcp-loader.js +144 -0
- package/extensions/dm-mcp-adapter/package.json +68 -0
- package/extensions/dm-mcp-adapter/panel-keys.js +26 -0
- package/extensions/dm-mcp-adapter/prompts.js +258 -0
- package/extensions/dm-mcp-adapter/proxy-modes.js +1336 -0
- package/extensions/dm-mcp-adapter/request-headers-command.js +293 -0
- package/extensions/dm-mcp-adapter/resource-tools.js +7 -0
- package/extensions/dm-mcp-adapter/runtime-owner.js +79 -0
- package/extensions/dm-mcp-adapter/sampling-handler.js +222 -0
- package/extensions/dm-mcp-adapter/sandbox-proxy-template.js +190 -0
- package/extensions/dm-mcp-adapter/search-ranking.js +198 -0
- package/extensions/dm-mcp-adapter/server-manager.js +1258 -0
- package/extensions/dm-mcp-adapter/session-recovery.js +82 -0
- package/extensions/dm-mcp-adapter/skills/mcp-scripting/SKILL.md +39 -0
- package/extensions/dm-mcp-adapter/state.js +0 -0
- package/extensions/dm-mcp-adapter/test-runner.mjs +18 -0
- package/extensions/dm-mcp-adapter/tool-approval.js +151 -0
- package/extensions/dm-mcp-adapter/tool-metadata.js +247 -0
- package/extensions/dm-mcp-adapter/tool-registrar.js +212 -0
- package/extensions/dm-mcp-adapter/tool-result-renderer.js +355 -0
- package/extensions/dm-mcp-adapter/ts-shape.js +157 -0
- package/extensions/dm-mcp-adapter/types.js +264 -0
- package/extensions/dm-mcp-adapter/ui-app-bridge-helpers.js +36 -0
- package/extensions/dm-mcp-adapter/ui-resource-handler.js +209 -0
- package/extensions/dm-mcp-adapter/ui-server.js +815 -0
- package/extensions/dm-mcp-adapter/ui-session.js +455 -0
- package/extensions/dm-mcp-adapter/ui-stream-types.js +58 -0
- package/extensions/dm-mcp-adapter/ui-tool-visibility.js +26 -0
- package/extensions/dm-mcp-adapter/unix-socket-transport.js +83 -0
- package/extensions/dm-mcp-adapter/utils.js +307 -0
- package/extensions/dm-skills-manager/THIRD_PARTY_NOTICES.md +27 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/components.js +265 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/constants.js +31 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/creation-fallback.js +20 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/creation.js +145 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/dialog.js +738 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/dm-ai-compat.js +15 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/format.js +125 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/glyphs.js +142 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/layout.js +93 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/paths.js +76 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/registry.js +95 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/settings.js +93 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/startup.js +32 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/toggle.js +54 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/types.js +14 -0
- package/extensions/dm-skills-manager/extensions/skills-manager/ui.js +121 -0
- package/extensions/dm-skills-manager/extensions/skills-manager.js +111 -0
- package/extensions/dm-skills-manager/package.json +121 -0
- package/package.json +13 -1
- package/extensions/dm-localllm-provider/detect.test.js +0 -1051
- package/extensions/dm-localllm-provider/index.test.js +0 -149
- package/extensions/dm-localllm-provider/keychain.test.js +0 -61
|
@@ -0,0 +1,1060 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { parse as parseToml } from "smol-toml";
|
|
5
|
+
import stripJsonComments from "strip-json-comments";
|
|
6
|
+
import { getAgentPath, getConfigDirName } from "./agent-dir.js";
|
|
7
|
+
import { getAgentPluginSummaries, loadAgentPluginConfigs } from "./agent-plugin-loader.js";
|
|
8
|
+
import { loadPackageMcpConfigs } from "./package-mcp-loader.js";
|
|
9
|
+
import { isServerDisabled } from "./types.js";
|
|
10
|
+
import { toStringRecord } from "./utils.js";
|
|
11
|
+
const GENERIC_GLOBAL_CONFIG_PATH = join(homedir(), ".config", "mcp", "mcp.json");
|
|
12
|
+
const AGENTS_GLOBAL_CONFIG_PATHS = [
|
|
13
|
+
join(homedir(), ".agents", "mcp.json"),
|
|
14
|
+
join(homedir(), ".agents", "mcp", "mcp.json")
|
|
15
|
+
];
|
|
16
|
+
const PROJECT_CONFIG_NAME = ".mcp.json";
|
|
17
|
+
const PROJECT_PI_CONFIG_NAME = "mcp.json";
|
|
18
|
+
const REPOPROMPT_BINARY_CANDIDATES = [
|
|
19
|
+
join(homedir(), "RepoPrompt", "repoprompt_cli"),
|
|
20
|
+
"/Applications/Repo Prompt.app/Contents/MacOS/repoprompt-mcp"
|
|
21
|
+
];
|
|
22
|
+
export const KNOWN_SERVER_PRESETS = [
|
|
23
|
+
{
|
|
24
|
+
id: "deepwiki",
|
|
25
|
+
name: "DeepWiki",
|
|
26
|
+
summary: "Ask questions about public GitHub repositories.",
|
|
27
|
+
entry: { url: "https://mcp.deepwiki.com/mcp", protocolVersion: "auto" }
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: "context7",
|
|
31
|
+
name: "Context7",
|
|
32
|
+
summary: "Look up current library documentation and examples.",
|
|
33
|
+
entry: { url: "https://mcp.context7.com/mcp", protocolVersion: "auto" }
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "parallel-search",
|
|
37
|
+
name: "Parallel Search",
|
|
38
|
+
summary: "Search the web and fetch pages without an API key.",
|
|
39
|
+
entry: {
|
|
40
|
+
url: "https://search.parallel.ai/mcp",
|
|
41
|
+
protocolVersion: "auto",
|
|
42
|
+
directTools: true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "notion",
|
|
47
|
+
name: "Notion",
|
|
48
|
+
summary: "Search and work with your Notion workspace.",
|
|
49
|
+
entry: { url: "https://mcp.notion.com/mcp", auth: "oauth", protocolVersion: "auto" }
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "github",
|
|
53
|
+
name: "GitHub",
|
|
54
|
+
summary: "Work with GitHub through your Copilot account.",
|
|
55
|
+
entry: { url: "https://api.githubcopilot.com/mcp", auth: "oauth", protocolVersion: "auto" }
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: "chrome-devtools",
|
|
59
|
+
name: "Chrome DevTools",
|
|
60
|
+
summary: "Inspect and automate a local Chrome browser.",
|
|
61
|
+
entry: { command: "npx", args: ["-y", "chrome-devtools-mcp@1.6.0"] }
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
const IMPORT_PATHS = {
|
|
65
|
+
cursor: [join(homedir(), ".cursor", "mcp.json")],
|
|
66
|
+
"claude-code": [
|
|
67
|
+
join(homedir(), ".claude", "mcp.json"),
|
|
68
|
+
join(homedir(), ".claude.json"),
|
|
69
|
+
join(homedir(), ".claude", "claude_desktop_config.json")
|
|
70
|
+
],
|
|
71
|
+
"claude-desktop": [join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json")],
|
|
72
|
+
codex: [
|
|
73
|
+
join(homedir(), ".codex", "config.toml"),
|
|
74
|
+
join(homedir(), ".codex", "config.json")
|
|
75
|
+
],
|
|
76
|
+
opencode: [
|
|
77
|
+
join(homedir(), ".config", "opencode", "opencode.json"),
|
|
78
|
+
"./opencode.json"
|
|
79
|
+
],
|
|
80
|
+
windsurf: [join(homedir(), ".windsurf", "mcp.json")],
|
|
81
|
+
vscode: [".vscode/mcp.json"]
|
|
82
|
+
};
|
|
83
|
+
export function getPiGlobalConfigPath(overridePath) {
|
|
84
|
+
return overridePath ? resolve(overridePath) : getAgentPath("mcp.json");
|
|
85
|
+
}
|
|
86
|
+
export function getGenericGlobalConfigPath() {
|
|
87
|
+
return GENERIC_GLOBAL_CONFIG_PATH;
|
|
88
|
+
}
|
|
89
|
+
export function getProjectConfigPath(cwd = process.cwd()) {
|
|
90
|
+
return resolve(cwd, PROJECT_CONFIG_NAME);
|
|
91
|
+
}
|
|
92
|
+
export function getProjectPiConfigPath(cwd = process.cwd()) {
|
|
93
|
+
return resolve(cwd, getConfigDirName(), PROJECT_PI_CONFIG_NAME);
|
|
94
|
+
}
|
|
95
|
+
export function getSharedConfigPath(target, cwd = process.cwd()) {
|
|
96
|
+
return target === "project" ? getProjectConfigPath(cwd) : getGenericGlobalConfigPath();
|
|
97
|
+
}
|
|
98
|
+
export function getConfigDiscoveryPaths(overridePath, cwd = process.cwd()) {
|
|
99
|
+
return getConfigSources(overridePath, cwd).map((source) => ({
|
|
100
|
+
label: source.label,
|
|
101
|
+
path: source.readPath,
|
|
102
|
+
exists: existsSync(source.readPath)
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
105
|
+
export function findAvailableImportConfigs(cwd = process.cwd()) {
|
|
106
|
+
if (isExclusiveConfigMode())
|
|
107
|
+
return [];
|
|
108
|
+
const discovered = [];
|
|
109
|
+
for (const importKind of Object.keys(IMPORT_PATHS)) {
|
|
110
|
+
const importPath = resolveImportPath(importKind, cwd);
|
|
111
|
+
if (importPath) {
|
|
112
|
+
discovered.push({ kind: importKind, path: importPath });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return discovered;
|
|
116
|
+
}
|
|
117
|
+
function getConfigSourceSummaries(sourceSpecs) {
|
|
118
|
+
return sourceSpecs.map((source) => {
|
|
119
|
+
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
|
|
120
|
+
return {
|
|
121
|
+
id: source.id,
|
|
122
|
+
label: source.label,
|
|
123
|
+
path: source.readPath,
|
|
124
|
+
exists: existsSync(source.readPath),
|
|
125
|
+
scope: source.scope,
|
|
126
|
+
kind: source.shared ? "shared" : "pi",
|
|
127
|
+
serverCount: loaded ? Object.keys(loaded.mcpServers).length : 0
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
export function getMcpStandardConfigSummary(overridePath, cwd = process.cwd()) {
|
|
132
|
+
const sources = getConfigSourceSummaries(getConfigSources(overridePath, cwd));
|
|
133
|
+
return {
|
|
134
|
+
sources,
|
|
135
|
+
hasSharedServers: sources.some((source) => source.kind === "shared" && source.serverCount > 0),
|
|
136
|
+
fingerprint: JSON.stringify({ sources: sources.map((source) => [source.id, source.exists, source.serverCount]) })
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export function getMcpDiscoverySummary(overridePath, cwd = process.cwd(), options = {}) {
|
|
140
|
+
const sourceSpecs = getConfigSources(overridePath, cwd);
|
|
141
|
+
const sources = getConfigSourceSummaries(sourceSpecs);
|
|
142
|
+
const includeHostConfigs = options.includeHostConfigs !== false;
|
|
143
|
+
const importKinds = isExclusiveConfigMode() ? readValidatedConfig(getEffectivePiGlobalConfigPath(overridePath), "MCP exclusive config")?.imports ?? [] : Object.keys(IMPORT_PATHS);
|
|
144
|
+
const imports = includeHostConfigs ? importKinds.map((kind) => {
|
|
145
|
+
const imported = loadImportedConfig(kind, cwd, `Failed to inspect imported MCP config from ${kind}:`);
|
|
146
|
+
if (!imported)
|
|
147
|
+
return null;
|
|
148
|
+
return {
|
|
149
|
+
kind,
|
|
150
|
+
path: imported.path,
|
|
151
|
+
serverCount: Object.keys(extractServers(imported.value, kind)).length
|
|
152
|
+
};
|
|
153
|
+
}).filter((value) => value !== null) : [];
|
|
154
|
+
const hostConfigDiscovery = isExclusiveConfigMode() ? "off" : getConfiguredHostConfigDiscovery(overridePath, cwd);
|
|
155
|
+
const hostConfigs = imports.map((entry) => ({ ...entry, active: hostConfigDiscovery === "on" }));
|
|
156
|
+
const settings = getMergedSettings(overridePath, cwd);
|
|
157
|
+
const agentPlugins = isExclusiveConfigMode() ? [] : getAgentPluginSummaries(settings?.agentPluginPaths, cwd);
|
|
158
|
+
const totalServerCount = sources.reduce((sum, source) => sum + source.serverCount, 0) + agentPlugins.reduce((sum, plugin) => sum + plugin.serverCount, 0);
|
|
159
|
+
const hasSharedServers = sources.some((source) => source.kind === "shared" && source.serverCount > 0) || agentPlugins.some((plugin) => plugin.serverCount > 0);
|
|
160
|
+
const hasPiOwnedServers = sources.some((source) => source.kind === "pi" && source.serverCount > 0);
|
|
161
|
+
const hasAnyDetectedPaths = sources.some((source) => source.exists) || imports.length > 0 || agentPlugins.length > 0;
|
|
162
|
+
const hasAnyConfig = totalServerCount > 0 || imports.some((entry) => entry.serverCount > 0) || hasAnyDetectedPaths;
|
|
163
|
+
const summaryWithoutRepoPrompt = {
|
|
164
|
+
sources,
|
|
165
|
+
imports,
|
|
166
|
+
hostConfigs,
|
|
167
|
+
hostConfigDiscovery,
|
|
168
|
+
agentPlugins,
|
|
169
|
+
conflicts: getConfigConflicts(sourceSpecs, imports, cwd),
|
|
170
|
+
hasAnyConfig,
|
|
171
|
+
hasAnyDetectedPaths,
|
|
172
|
+
hasSharedServers,
|
|
173
|
+
hasPiOwnedServers,
|
|
174
|
+
totalServerCount
|
|
175
|
+
};
|
|
176
|
+
const fingerprint = JSON.stringify({
|
|
177
|
+
sources: sources.map((source) => [source.id, source.exists, source.serverCount]),
|
|
178
|
+
imports: imports.map((entry) => [entry.kind, entry.path, entry.serverCount]),
|
|
179
|
+
agentPlugins: agentPlugins.map((entry) => [entry.path, entry.name, entry.serverCount]),
|
|
180
|
+
hostConfigDiscovery,
|
|
181
|
+
conflicts: summaryWithoutRepoPrompt.conflicts
|
|
182
|
+
});
|
|
183
|
+
return {
|
|
184
|
+
...summaryWithoutRepoPrompt,
|
|
185
|
+
fingerprint,
|
|
186
|
+
repoPrompt: detectRepoPrompt(summaryWithoutRepoPrompt, cwd)
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export function cloneMcpConfig(config) {
|
|
190
|
+
return structuredClone(config);
|
|
191
|
+
}
|
|
192
|
+
export function loadMcpConfig(overridePath, cwd = process.cwd()) {
|
|
193
|
+
const sourceSpecs = getConfigSources(overridePath, cwd);
|
|
194
|
+
const hostConfigDiscovery = getConfiguredHostConfigDiscovery(overridePath, cwd);
|
|
195
|
+
let config = !isExclusiveConfigMode() && hostConfigDiscovery === "on" ? loadDiscoveredHostConfigs(cwd) : { mcpServers: {} };
|
|
196
|
+
for (const source of sourceSpecs) {
|
|
197
|
+
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
|
|
198
|
+
if (!loaded)
|
|
199
|
+
continue;
|
|
200
|
+
config = mergeConfigs(config, expandImports(loaded, cwd));
|
|
201
|
+
}
|
|
202
|
+
if (isExclusiveConfigMode())
|
|
203
|
+
return config;
|
|
204
|
+
const packageConfig = loadPackageMcpConfigs(cwd);
|
|
205
|
+
const pluginConfig = loadAgentPluginConfigs(config.settings?.agentPluginPaths, cwd);
|
|
206
|
+
const packageServers = Object.fromEntries(Object.entries(packageConfig.mcpServers).filter(([name]) => !Object.hasOwn(pluginConfig.mcpServers, name)));
|
|
207
|
+
return mergeConfigs({ mcpServers: packageServers }, mergeConfigs(pluginConfig, config));
|
|
208
|
+
}
|
|
209
|
+
function getMergedSettings(overridePath, cwd = process.cwd()) {
|
|
210
|
+
let settings;
|
|
211
|
+
for (const source of getConfigSources(overridePath, cwd)) {
|
|
212
|
+
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
|
|
213
|
+
if (loaded?.settings)
|
|
214
|
+
settings = { ...settings, ...loaded.settings };
|
|
215
|
+
}
|
|
216
|
+
return settings;
|
|
217
|
+
}
|
|
218
|
+
function getConfiguredHostConfigDiscovery(overridePath, cwd = process.cwd()) {
|
|
219
|
+
let configured = "off";
|
|
220
|
+
const settings = getMergedSettings(overridePath, cwd);
|
|
221
|
+
const value = settings?.hostConfigDiscovery;
|
|
222
|
+
if (value === "off" || value === "prompt" || value === "on")
|
|
223
|
+
configured = value;
|
|
224
|
+
return configured;
|
|
225
|
+
}
|
|
226
|
+
function loadDiscoveredHostConfigs(cwd) {
|
|
227
|
+
let config = { mcpServers: {} };
|
|
228
|
+
for (const importKind of Object.keys(IMPORT_PATHS)) {
|
|
229
|
+
const imported = loadImportedConfig(importKind, cwd, `Failed to discover imported MCP config from ${importKind}:`);
|
|
230
|
+
if (!imported)
|
|
231
|
+
continue;
|
|
232
|
+
config = mergeConfigs(config, {
|
|
233
|
+
mcpServers: extractServers(imported.value, importKind)
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return config;
|
|
237
|
+
}
|
|
238
|
+
function getConfigConflicts(sourceSpecs, imports, cwd) {
|
|
239
|
+
const seen = new Map;
|
|
240
|
+
const record = (name, source) => {
|
|
241
|
+
const entries = seen.get(name) ?? [];
|
|
242
|
+
if (!entries.some((entry) => entry.kind === source.kind && entry.path === source.path))
|
|
243
|
+
entries.push(source);
|
|
244
|
+
seen.set(name, entries);
|
|
245
|
+
};
|
|
246
|
+
for (const entry of imports) {
|
|
247
|
+
const imported = loadImportedConfig(entry.kind, cwd, `Failed to inspect imported MCP config from ${entry.kind}:`);
|
|
248
|
+
if (!imported)
|
|
249
|
+
continue;
|
|
250
|
+
for (const name of Object.keys(extractServers(imported.value, entry.kind))) {
|
|
251
|
+
record(name, { kind: "host", path: imported.path });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const source of sourceSpecs) {
|
|
255
|
+
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
|
|
256
|
+
if (!loaded)
|
|
257
|
+
continue;
|
|
258
|
+
if (loaded.imports?.length) {
|
|
259
|
+
for (const importKind of loaded.imports) {
|
|
260
|
+
const imported = loadImportedConfig(importKind, cwd, `Failed to inspect imported MCP config from ${importKind}:`);
|
|
261
|
+
if (!imported)
|
|
262
|
+
continue;
|
|
263
|
+
for (const name of Object.keys(extractServers(imported.value, importKind))) {
|
|
264
|
+
record(name, { kind: "host", path: imported.path });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
for (const name of Object.keys(loaded.mcpServers)) {
|
|
269
|
+
record(name, {
|
|
270
|
+
kind: source.shared ? "shared" : "pi",
|
|
271
|
+
path: source.readPath
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return [...seen.entries()].filter(([, sources]) => sources.length > 1).map(([serverName, sources]) => ({ serverName, sources, winner: sources[sources.length - 1] })).sort((left, right) => left.serverName.localeCompare(right.serverName));
|
|
276
|
+
}
|
|
277
|
+
function getConfigSources(overridePath, cwd = process.cwd()) {
|
|
278
|
+
const userPath = getEffectivePiGlobalConfigPath(overridePath);
|
|
279
|
+
const projectPath = getProjectConfigPath(cwd);
|
|
280
|
+
const projectPiPath = getProjectPiConfigPath(cwd);
|
|
281
|
+
const sources = [];
|
|
282
|
+
if (isExclusiveConfigMode()) {
|
|
283
|
+
return [{
|
|
284
|
+
id: "pi-global",
|
|
285
|
+
label: "DM exclusive config",
|
|
286
|
+
readPath: userPath,
|
|
287
|
+
writePath: userPath,
|
|
288
|
+
kind: "user",
|
|
289
|
+
shared: false,
|
|
290
|
+
scope: "global"
|
|
291
|
+
}];
|
|
292
|
+
}
|
|
293
|
+
if (GENERIC_GLOBAL_CONFIG_PATH !== userPath) {
|
|
294
|
+
sources.push({
|
|
295
|
+
id: "shared-global",
|
|
296
|
+
label: "user-global standard MCP",
|
|
297
|
+
readPath: GENERIC_GLOBAL_CONFIG_PATH,
|
|
298
|
+
writePath: userPath,
|
|
299
|
+
kind: "import",
|
|
300
|
+
importKind: "global MCP config",
|
|
301
|
+
shared: true,
|
|
302
|
+
scope: "global"
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
for (const [index, agentsPath] of AGENTS_GLOBAL_CONFIG_PATHS.entries()) {
|
|
306
|
+
if (agentsPath === userPath || agentsPath === GENERIC_GLOBAL_CONFIG_PATH)
|
|
307
|
+
continue;
|
|
308
|
+
sources.push({
|
|
309
|
+
id: index === 0 ? "agents-global" : "agents-nested-global",
|
|
310
|
+
label: index === 0 ? "user-global .agents MCP" : "user-global .agents nested MCP",
|
|
311
|
+
readPath: agentsPath,
|
|
312
|
+
writePath: userPath,
|
|
313
|
+
kind: "import",
|
|
314
|
+
importKind: index === 0 ? ".agents MCP config" : ".agents/mcp MCP config",
|
|
315
|
+
shared: true,
|
|
316
|
+
scope: "global"
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
sources.push({
|
|
320
|
+
id: "pi-global",
|
|
321
|
+
label: "DM global override",
|
|
322
|
+
readPath: userPath,
|
|
323
|
+
writePath: userPath,
|
|
324
|
+
kind: "user",
|
|
325
|
+
shared: false,
|
|
326
|
+
scope: "global"
|
|
327
|
+
});
|
|
328
|
+
if (projectPath !== userPath) {
|
|
329
|
+
sources.push({
|
|
330
|
+
id: "shared-project",
|
|
331
|
+
label: "project standard MCP",
|
|
332
|
+
readPath: projectPath,
|
|
333
|
+
writePath: projectPath,
|
|
334
|
+
kind: "project",
|
|
335
|
+
shared: true,
|
|
336
|
+
scope: "project"
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
if (projectPiPath !== userPath && projectPiPath !== projectPath) {
|
|
340
|
+
sources.push({
|
|
341
|
+
id: "pi-project",
|
|
342
|
+
label: "project DM override",
|
|
343
|
+
readPath: projectPiPath,
|
|
344
|
+
writePath: projectPiPath,
|
|
345
|
+
kind: "project",
|
|
346
|
+
shared: false,
|
|
347
|
+
scope: "project"
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
return sources;
|
|
351
|
+
}
|
|
352
|
+
function isExclusiveConfigMode() {
|
|
353
|
+
return process.env.PI_MCP_CONFIG_MODE?.trim().toLowerCase() === "exclusive";
|
|
354
|
+
}
|
|
355
|
+
function getEffectivePiGlobalConfigPath(overridePath) {
|
|
356
|
+
return getPiGlobalConfigPath(isExclusiveConfigMode() ? undefined : overridePath);
|
|
357
|
+
}
|
|
358
|
+
function mergeConfigs(base, next) {
|
|
359
|
+
const imports = mergeImports(base.imports, next.imports);
|
|
360
|
+
const settings = next.settings ? { ...base.settings, ...next.settings } : base.settings;
|
|
361
|
+
return {
|
|
362
|
+
mcpServers: mergeServerMaps(base.mcpServers, next.mcpServers),
|
|
363
|
+
...imports !== undefined ? { imports } : {},
|
|
364
|
+
...settings !== undefined ? { settings } : {}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
const URL_BOUND_AUTH_FIELDS = ["headers", "bearerToken", "bearerTokenEnv", "bearerTokenStore", "requestHeadersCommand"];
|
|
368
|
+
function mergeServerMaps(base, next) {
|
|
369
|
+
const merged = { ...base };
|
|
370
|
+
for (const [name, definition] of Object.entries(next)) {
|
|
371
|
+
const existing = merged[name];
|
|
372
|
+
let baseEntry = existing ?? {};
|
|
373
|
+
if (existing && typeof definition.command === "string") {
|
|
374
|
+
baseEntry = { ...existing };
|
|
375
|
+
for (const field of [
|
|
376
|
+
"url",
|
|
377
|
+
"headers",
|
|
378
|
+
"requestHeadersCommand",
|
|
379
|
+
"auth",
|
|
380
|
+
"bearerToken",
|
|
381
|
+
"bearerTokenEnv",
|
|
382
|
+
"oauth",
|
|
383
|
+
"httpTransport",
|
|
384
|
+
"socket"
|
|
385
|
+
]) {
|
|
386
|
+
delete baseEntry[field];
|
|
387
|
+
}
|
|
388
|
+
} else if (existing && typeof definition.url === "string") {
|
|
389
|
+
baseEntry = { ...existing };
|
|
390
|
+
for (const field of [
|
|
391
|
+
"command",
|
|
392
|
+
"args",
|
|
393
|
+
"env",
|
|
394
|
+
"cwd",
|
|
395
|
+
"pluginDataDir",
|
|
396
|
+
"literalEnv",
|
|
397
|
+
"socket"
|
|
398
|
+
]) {
|
|
399
|
+
delete baseEntry[field];
|
|
400
|
+
}
|
|
401
|
+
} else if (existing && typeof definition.socket === "string") {
|
|
402
|
+
baseEntry = { ...existing };
|
|
403
|
+
for (const field of [
|
|
404
|
+
"command",
|
|
405
|
+
"args",
|
|
406
|
+
"env",
|
|
407
|
+
"cwd",
|
|
408
|
+
"pluginDataDir",
|
|
409
|
+
"literalEnv",
|
|
410
|
+
"url",
|
|
411
|
+
"headers",
|
|
412
|
+
"requestHeadersCommand",
|
|
413
|
+
"auth",
|
|
414
|
+
"bearerToken",
|
|
415
|
+
"bearerTokenEnv",
|
|
416
|
+
"oauth",
|
|
417
|
+
"httpTransport"
|
|
418
|
+
]) {
|
|
419
|
+
delete baseEntry[field];
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (existing && typeof definition.url === "string" && definition.url !== existing.url) {
|
|
423
|
+
if (baseEntry === existing)
|
|
424
|
+
baseEntry = { ...existing };
|
|
425
|
+
for (const field of URL_BOUND_AUTH_FIELDS) {
|
|
426
|
+
delete baseEntry[field];
|
|
427
|
+
}
|
|
428
|
+
if (baseEntry.oauth !== false) {
|
|
429
|
+
delete baseEntry.oauth;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
merged[name] = { ...baseEntry, ...definition };
|
|
433
|
+
}
|
|
434
|
+
return merged;
|
|
435
|
+
}
|
|
436
|
+
function mergeImports(left, right) {
|
|
437
|
+
const merged = [...left ?? [], ...right ?? []];
|
|
438
|
+
if (merged.length === 0)
|
|
439
|
+
return;
|
|
440
|
+
return [...new Set(merged)];
|
|
441
|
+
}
|
|
442
|
+
function expandImports(config, cwd = process.cwd()) {
|
|
443
|
+
if (!config.imports?.length)
|
|
444
|
+
return config;
|
|
445
|
+
const importedServers = {};
|
|
446
|
+
for (const importKind of config.imports) {
|
|
447
|
+
const imported = loadImportedConfig(importKind, cwd, `Failed to import MCP config from ${importKind}:`);
|
|
448
|
+
if (!imported)
|
|
449
|
+
continue;
|
|
450
|
+
const servers = extractServers(imported.value, importKind);
|
|
451
|
+
for (const [name, definition] of Object.entries(servers)) {
|
|
452
|
+
if (!importedServers[name]) {
|
|
453
|
+
importedServers[name] = definition;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
imports: config.imports,
|
|
459
|
+
...config.settings !== undefined ? { settings: config.settings } : {},
|
|
460
|
+
mcpServers: mergeServerMaps(importedServers, config.mcpServers)
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
function resolveImportCandidates(importKind, cwd) {
|
|
464
|
+
return (IMPORT_PATHS[importKind] ?? []).map((candidate) => {
|
|
465
|
+
if (importKind === "opencode" && candidate === "./opencode.json") {
|
|
466
|
+
const start = resolve(cwd);
|
|
467
|
+
let gitRoot;
|
|
468
|
+
let current = start;
|
|
469
|
+
while (true) {
|
|
470
|
+
if (existsSync(join(current, ".git"))) {
|
|
471
|
+
gitRoot = current;
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
const parent = dirname(current);
|
|
475
|
+
if (parent === current)
|
|
476
|
+
break;
|
|
477
|
+
current = parent;
|
|
478
|
+
}
|
|
479
|
+
if (!gitRoot)
|
|
480
|
+
return join(start, "opencode.json");
|
|
481
|
+
current = start;
|
|
482
|
+
while (true) {
|
|
483
|
+
const projectConfig = join(current, "opencode.json");
|
|
484
|
+
if (existsSync(projectConfig) || current === gitRoot)
|
|
485
|
+
return projectConfig;
|
|
486
|
+
current = dirname(current);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return candidate.startsWith(".") ? resolve(cwd, candidate) : candidate;
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
function parseJsonConfig(raw) {
|
|
493
|
+
return JSON.parse(stripJsonComments(raw, { trailingCommas: true }));
|
|
494
|
+
}
|
|
495
|
+
function readImportedConfig(path) {
|
|
496
|
+
const raw = readFileSync(path, "utf-8");
|
|
497
|
+
return path.endsWith(".toml") ? parseToml(raw) : parseJsonConfig(raw);
|
|
498
|
+
}
|
|
499
|
+
function loadImportedConfig(importKind, cwd, warningPrefix) {
|
|
500
|
+
if (importKind === "opencode") {
|
|
501
|
+
let merged = {};
|
|
502
|
+
let highestPrecedencePath;
|
|
503
|
+
for (const path of resolveImportCandidates(importKind, cwd)) {
|
|
504
|
+
if (!existsSync(path))
|
|
505
|
+
continue;
|
|
506
|
+
try {
|
|
507
|
+
const value = readImportedConfig(path);
|
|
508
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
509
|
+
merged = mergeOpenCodeConfigs(merged, value);
|
|
510
|
+
highestPrecedencePath = path;
|
|
511
|
+
}
|
|
512
|
+
} catch (error) {
|
|
513
|
+
console.warn(warningPrefix, error);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return highestPrecedencePath ? { path: highestPrecedencePath, value: merged } : null;
|
|
517
|
+
}
|
|
518
|
+
for (const path of resolveImportCandidates(importKind, cwd)) {
|
|
519
|
+
if (!existsSync(path))
|
|
520
|
+
continue;
|
|
521
|
+
try {
|
|
522
|
+
return { path, value: readImportedConfig(path) };
|
|
523
|
+
} catch (error) {
|
|
524
|
+
console.warn(warningPrefix, error);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
function resolveImportPath(importKind, cwd = process.cwd()) {
|
|
530
|
+
return loadImportedConfig(importKind, cwd, `Failed to discover imported MCP config from ${importKind}:`)?.path ?? null;
|
|
531
|
+
}
|
|
532
|
+
function readValidatedConfig(path, label) {
|
|
533
|
+
if (!existsSync(path))
|
|
534
|
+
return null;
|
|
535
|
+
try {
|
|
536
|
+
return validateConfig(parseJsonConfig(readFileSync(path, "utf-8")));
|
|
537
|
+
} catch (error) {
|
|
538
|
+
console.warn(`Failed to load ${label}:`, error);
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function validateConfig(raw) {
|
|
543
|
+
if (!isRecord(raw)) {
|
|
544
|
+
return { mcpServers: {} };
|
|
545
|
+
}
|
|
546
|
+
return {
|
|
547
|
+
mcpServers: toServerEntries(raw.mcpServers ?? raw["mcp-servers"]),
|
|
548
|
+
...Array.isArray(raw.imports) ? { imports: raw.imports } : {},
|
|
549
|
+
...raw.settings !== undefined ? { settings: raw.settings } : {}
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function toServerEntries(servers) {
|
|
553
|
+
if (!isRecord(servers))
|
|
554
|
+
return {};
|
|
555
|
+
const entries = {};
|
|
556
|
+
for (const [name, entry] of Object.entries(servers)) {
|
|
557
|
+
if (isServerEntry(entry))
|
|
558
|
+
entries[name] = entry;
|
|
559
|
+
}
|
|
560
|
+
return entries;
|
|
561
|
+
}
|
|
562
|
+
function isServerEntry(value) {
|
|
563
|
+
return isRecord(value);
|
|
564
|
+
}
|
|
565
|
+
function isRecord(value) {
|
|
566
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
567
|
+
}
|
|
568
|
+
function mergeOpenCodeConfigs(base, next) {
|
|
569
|
+
const baseMcp = base.mcp;
|
|
570
|
+
const nextMcp = next.mcp;
|
|
571
|
+
const mergedMcp = {
|
|
572
|
+
...baseMcp && typeof baseMcp === "object" && !Array.isArray(baseMcp) ? baseMcp : {}
|
|
573
|
+
};
|
|
574
|
+
if (nextMcp && typeof nextMcp === "object" && !Array.isArray(nextMcp)) {
|
|
575
|
+
for (const [name, nextEntry] of Object.entries(nextMcp)) {
|
|
576
|
+
const baseEntry = mergedMcp[name];
|
|
577
|
+
if (baseEntry && typeof baseEntry === "object" && !Array.isArray(baseEntry) && nextEntry && typeof nextEntry === "object" && !Array.isArray(nextEntry)) {
|
|
578
|
+
const safeBase = { ...baseEntry };
|
|
579
|
+
const override = nextEntry;
|
|
580
|
+
if (typeof override.type === "string" && override.type !== safeBase.type) {
|
|
581
|
+
for (const field of ["command", "environment", "cwd", "url", "headers", "oauth"])
|
|
582
|
+
delete safeBase[field];
|
|
583
|
+
}
|
|
584
|
+
if (typeof override.url === "string" && override.url !== safeBase.url) {
|
|
585
|
+
delete safeBase.headers;
|
|
586
|
+
delete safeBase.oauth;
|
|
587
|
+
}
|
|
588
|
+
if (Array.isArray(override.command)) {
|
|
589
|
+
const baseCommand = safeBase.command;
|
|
590
|
+
const commandChanged = !Array.isArray(baseCommand) || override.command.length !== baseCommand.length || override.command.some((value, index) => value !== baseCommand[index]);
|
|
591
|
+
if (commandChanged) {
|
|
592
|
+
delete safeBase.environment;
|
|
593
|
+
delete safeBase.cwd;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
const mergedEntry = { ...safeBase, ...override };
|
|
597
|
+
for (const field of ["environment", "headers", "oauth"]) {
|
|
598
|
+
const baseField = safeBase[field];
|
|
599
|
+
const nextField = override[field];
|
|
600
|
+
if (baseField && typeof baseField === "object" && !Array.isArray(baseField) && nextField && typeof nextField === "object" && !Array.isArray(nextField)) {
|
|
601
|
+
mergedEntry[field] = { ...baseField, ...nextField };
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
mergedMcp[name] = mergedEntry;
|
|
605
|
+
} else {
|
|
606
|
+
mergedMcp[name] = nextEntry;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return { ...base, ...next, mcp: mergedMcp };
|
|
611
|
+
}
|
|
612
|
+
function extractServers(config, kind) {
|
|
613
|
+
if (!config || typeof config !== "object")
|
|
614
|
+
return {};
|
|
615
|
+
const obj = config;
|
|
616
|
+
let servers;
|
|
617
|
+
switch (kind) {
|
|
618
|
+
case "claude-desktop":
|
|
619
|
+
case "claude-code":
|
|
620
|
+
servers = obj.mcpServers;
|
|
621
|
+
break;
|
|
622
|
+
case "codex":
|
|
623
|
+
servers = obj.mcp_servers ?? obj.mcpServers;
|
|
624
|
+
break;
|
|
625
|
+
case "cursor":
|
|
626
|
+
case "windsurf":
|
|
627
|
+
case "vscode":
|
|
628
|
+
servers = obj.mcpServers ?? obj["mcp-servers"];
|
|
629
|
+
break;
|
|
630
|
+
case "opencode":
|
|
631
|
+
servers = obj.mcp;
|
|
632
|
+
break;
|
|
633
|
+
default:
|
|
634
|
+
return {};
|
|
635
|
+
}
|
|
636
|
+
if (!servers || typeof servers !== "object" || Array.isArray(servers)) {
|
|
637
|
+
return {};
|
|
638
|
+
}
|
|
639
|
+
const mappedServers = {};
|
|
640
|
+
for (const [name, entry] of Object.entries(servers)) {
|
|
641
|
+
if (kind === "opencode") {
|
|
642
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry))
|
|
643
|
+
continue;
|
|
644
|
+
const raw = entry;
|
|
645
|
+
if (raw.enabled === false)
|
|
646
|
+
continue;
|
|
647
|
+
if (raw.type === "local" && Array.isArray(raw.command) && raw.command.length > 0 && raw.command.every((value) => typeof value === "string")) {
|
|
648
|
+
const env = toStringRecord(raw.environment);
|
|
649
|
+
const command = raw.command[0];
|
|
650
|
+
if (command === undefined)
|
|
651
|
+
continue;
|
|
652
|
+
const mapped = {
|
|
653
|
+
command,
|
|
654
|
+
args: raw.command.slice(1),
|
|
655
|
+
...env ? { env } : {},
|
|
656
|
+
...typeof raw.cwd === "string" ? { cwd: raw.cwd } : {}
|
|
657
|
+
};
|
|
658
|
+
mappedServers[name] = mapped;
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
if (raw.type === "remote" && typeof raw.url === "string") {
|
|
662
|
+
const headers = toStringRecord(raw.headers);
|
|
663
|
+
const mapped = {
|
|
664
|
+
url: raw.url,
|
|
665
|
+
...headers ? { headers } : {}
|
|
666
|
+
};
|
|
667
|
+
if (raw.oauth === false) {
|
|
668
|
+
mapped.oauth = false;
|
|
669
|
+
} else if (raw.oauth && typeof raw.oauth === "object" && !Array.isArray(raw.oauth)) {
|
|
670
|
+
const oauth = raw.oauth;
|
|
671
|
+
mapped.auth = "oauth";
|
|
672
|
+
mapped.oauth = {
|
|
673
|
+
...typeof oauth.clientId === "string" ? { clientId: oauth.clientId } : {},
|
|
674
|
+
...typeof oauth.clientSecret === "string" ? { clientSecret: oauth.clientSecret } : {},
|
|
675
|
+
...typeof oauth.scope === "string" ? { scope: oauth.scope } : {},
|
|
676
|
+
...typeof oauth.authServerMetadataUrl === "string" ? { authServerMetadataUrl: oauth.authServerMetadataUrl } : {},
|
|
677
|
+
...typeof oauth.skipIssuerMetadataValidation === "boolean" ? { skipIssuerMetadataValidation: oauth.skipIssuerMetadataValidation } : {}
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
mappedServers[name] = mapped;
|
|
681
|
+
}
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
if (!isRecord(entry))
|
|
685
|
+
continue;
|
|
686
|
+
if (kind !== "codex") {
|
|
687
|
+
mappedServers[name] = entry;
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
const mapped = { ...entry };
|
|
691
|
+
const bearerTokenEnv = mapped.bearer_token_env_var;
|
|
692
|
+
const httpHeaders = mapped.http_headers;
|
|
693
|
+
const envHttpHeaders = mapped.env_http_headers;
|
|
694
|
+
if (typeof bearerTokenEnv === "string") {
|
|
695
|
+
mapped.bearerTokenEnv = bearerTokenEnv;
|
|
696
|
+
if (mapped.auth === undefined)
|
|
697
|
+
mapped.auth = "bearer";
|
|
698
|
+
}
|
|
699
|
+
if (httpHeaders && typeof httpHeaders === "object" && !Array.isArray(httpHeaders)) {
|
|
700
|
+
mapped.headers = { ...mapped.headers, ...httpHeaders };
|
|
701
|
+
}
|
|
702
|
+
if (envHttpHeaders && typeof envHttpHeaders === "object" && !Array.isArray(envHttpHeaders)) {
|
|
703
|
+
const headers = { ...mapped.headers };
|
|
704
|
+
for (const [header, envVar] of Object.entries(envHttpHeaders)) {
|
|
705
|
+
if (typeof envVar === "string" && headers[header] === undefined)
|
|
706
|
+
headers[header] = `$env:${envVar}`;
|
|
707
|
+
}
|
|
708
|
+
mapped.headers = headers;
|
|
709
|
+
}
|
|
710
|
+
delete mapped.bearer_token_env_var;
|
|
711
|
+
delete mapped.http_headers;
|
|
712
|
+
delete mapped.env_http_headers;
|
|
713
|
+
mappedServers[name] = mapped;
|
|
714
|
+
}
|
|
715
|
+
return mappedServers;
|
|
716
|
+
}
|
|
717
|
+
function serializeRawConfig(raw) {
|
|
718
|
+
return `${JSON.stringify(raw, null, 2)}
|
|
719
|
+
`;
|
|
720
|
+
}
|
|
721
|
+
function buildUnifiedDiff(beforeText, afterText) {
|
|
722
|
+
if (beforeText === afterText)
|
|
723
|
+
return "(no changes)";
|
|
724
|
+
const before = beforeText.split(`
|
|
725
|
+
`);
|
|
726
|
+
const after = afterText.split(`
|
|
727
|
+
`);
|
|
728
|
+
const rows = before.length;
|
|
729
|
+
const cols = after.length;
|
|
730
|
+
const lcs = Array.from({ length: rows + 1 }, () => Array(cols + 1).fill(0));
|
|
731
|
+
for (let i = rows - 1;i >= 0; i--) {
|
|
732
|
+
for (let j = cols - 1;j >= 0; j--) {
|
|
733
|
+
const row = lcs[i];
|
|
734
|
+
const nextRow = lcs[i + 1];
|
|
735
|
+
if (!row || !nextRow)
|
|
736
|
+
continue;
|
|
737
|
+
row[j] = before[i] === after[j] ? (nextRow[j + 1] ?? 0) + 1 : Math.max(nextRow[j] ?? 0, row[j + 1] ?? 0);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
const lines = ["--- before", "+++ after"];
|
|
741
|
+
let i = 0;
|
|
742
|
+
let j = 0;
|
|
743
|
+
while (i < rows || j < cols) {
|
|
744
|
+
if (i < rows && j < cols && before[i] === after[j]) {
|
|
745
|
+
lines.push(` ${before[i]}`);
|
|
746
|
+
i++;
|
|
747
|
+
j++;
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (j < cols && (i === rows || (lcs[i]?.[j + 1] ?? 0) >= (lcs[i + 1]?.[j] ?? 0))) {
|
|
751
|
+
lines.push(`+ ${after[j]}`);
|
|
752
|
+
j++;
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
if (i < rows) {
|
|
756
|
+
lines.push(`- ${before[i]}`);
|
|
757
|
+
i++;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return lines.join(`
|
|
761
|
+
`);
|
|
762
|
+
}
|
|
763
|
+
function buildConfigWritePreview(filePath, nextRaw) {
|
|
764
|
+
const existed = existsSync(filePath);
|
|
765
|
+
const beforeRaw = readRawConfigObject(filePath);
|
|
766
|
+
const beforeText = existed ? serializeRawConfig(beforeRaw) : "";
|
|
767
|
+
const afterText = serializeRawConfig(nextRaw);
|
|
768
|
+
return {
|
|
769
|
+
path: filePath,
|
|
770
|
+
existed,
|
|
771
|
+
changed: beforeText !== afterText,
|
|
772
|
+
beforeText,
|
|
773
|
+
afterText,
|
|
774
|
+
diffText: buildUnifiedDiff(beforeText, afterText)
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
function readRawConfigObject(filePath) {
|
|
778
|
+
if (!existsSync(filePath))
|
|
779
|
+
return {};
|
|
780
|
+
try {
|
|
781
|
+
const raw = parseJsonConfig(readFileSync(filePath, "utf-8"));
|
|
782
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
783
|
+
} catch {
|
|
784
|
+
return {};
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
function writeRawConfigObject(filePath, raw) {
|
|
788
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
789
|
+
const tmpPath = `${filePath}.${process.pid}.tmp`;
|
|
790
|
+
writeFileSync(tmpPath, `${JSON.stringify(raw, null, 2)}
|
|
791
|
+
`, "utf-8");
|
|
792
|
+
renameSync(tmpPath, filePath);
|
|
793
|
+
}
|
|
794
|
+
function getServersObject(raw) {
|
|
795
|
+
const existing = raw.mcpServers ?? raw["mcp-servers"] ?? {};
|
|
796
|
+
if (!existing || typeof existing !== "object" || Array.isArray(existing)) {
|
|
797
|
+
return {};
|
|
798
|
+
}
|
|
799
|
+
return existing;
|
|
800
|
+
}
|
|
801
|
+
function setServersObject(raw, servers) {
|
|
802
|
+
delete raw["mcp-servers"];
|
|
803
|
+
raw.mcpServers = servers;
|
|
804
|
+
}
|
|
805
|
+
export function writeProjectServerDisabledOverride(overridePath, cwd, serverName, disabled) {
|
|
806
|
+
const filePath = getProjectPiConfigPath(cwd);
|
|
807
|
+
let raw = {};
|
|
808
|
+
if (existsSync(filePath)) {
|
|
809
|
+
try {
|
|
810
|
+
const parsed = parseJsonConfig(readFileSync(filePath, "utf-8"));
|
|
811
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
812
|
+
throw new Error("root value must be an object");
|
|
813
|
+
}
|
|
814
|
+
raw = parsed;
|
|
815
|
+
} catch (error) {
|
|
816
|
+
throw new Error(`Failed to read project MCP override at ${filePath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
const serverKey = raw.mcpServers !== undefined ? "mcpServers" : raw["mcp-servers"] !== undefined ? "mcp-servers" : "mcpServers";
|
|
820
|
+
const rawServers = raw[serverKey];
|
|
821
|
+
if (rawServers !== undefined && (!rawServers || typeof rawServers !== "object" || Array.isArray(rawServers))) {
|
|
822
|
+
throw new Error(`Failed to update project MCP override at ${filePath}: ${serverKey} must be an object`);
|
|
823
|
+
}
|
|
824
|
+
const servers = rawServers ?? {};
|
|
825
|
+
const previous = servers[serverName];
|
|
826
|
+
if (previous !== undefined && (!previous || typeof previous !== "object" || Array.isArray(previous))) {
|
|
827
|
+
throw new Error(`Failed to update project MCP override at ${filePath}: server "${serverName}" must be an object`);
|
|
828
|
+
}
|
|
829
|
+
const existing = previous;
|
|
830
|
+
let next;
|
|
831
|
+
if (disabled) {
|
|
832
|
+
next = { ...existing, disabled: true };
|
|
833
|
+
} else {
|
|
834
|
+
next = Object.fromEntries(Object.entries(existing ?? {}).filter(([key]) => key !== "disabled"));
|
|
835
|
+
let lowerConfig = { mcpServers: {} };
|
|
836
|
+
for (const source of getConfigSources(overridePath, cwd)) {
|
|
837
|
+
if (source.readPath === filePath)
|
|
838
|
+
continue;
|
|
839
|
+
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
|
|
840
|
+
if (loaded)
|
|
841
|
+
lowerConfig = mergeConfigs(lowerConfig, expandImports(loaded, cwd));
|
|
842
|
+
}
|
|
843
|
+
if (raw.imports !== undefined) {
|
|
844
|
+
if (!Array.isArray(raw.imports) || raw.imports.some((kind) => typeof kind !== "string" || !Object.hasOwn(IMPORT_PATHS, kind))) {
|
|
845
|
+
throw new Error(`Failed to update project MCP override at ${filePath}: imports contains an unsupported config kind`);
|
|
846
|
+
}
|
|
847
|
+
lowerConfig = mergeConfigs(lowerConfig, expandImports({ mcpServers: {}, imports: raw.imports }, cwd));
|
|
848
|
+
}
|
|
849
|
+
if (isServerDisabled(lowerConfig.mcpServers[serverName]))
|
|
850
|
+
next.disabled = false;
|
|
851
|
+
}
|
|
852
|
+
if (!existing && Object.keys(next).length === 0 || JSON.stringify(existing) === JSON.stringify(next)) {
|
|
853
|
+
return { path: filePath, changed: false };
|
|
854
|
+
}
|
|
855
|
+
if (Object.keys(next).length === 0)
|
|
856
|
+
delete servers[serverName];
|
|
857
|
+
else
|
|
858
|
+
servers[serverName] = next;
|
|
859
|
+
raw[serverKey] = servers;
|
|
860
|
+
writeRawConfigObject(filePath, raw);
|
|
861
|
+
return { path: filePath, changed: true };
|
|
862
|
+
}
|
|
863
|
+
function isRepoPromptServer(name, entry) {
|
|
864
|
+
const normalizedName = name.toLowerCase();
|
|
865
|
+
if (normalizedName.includes("repoprompt") || normalizedName === "rp") {
|
|
866
|
+
return true;
|
|
867
|
+
}
|
|
868
|
+
const command = entry.command?.toLowerCase() ?? "";
|
|
869
|
+
if (command.includes("repoprompt") || command.includes("rp-mcp") || command.endsWith("repoprompt_cli")) {
|
|
870
|
+
return true;
|
|
871
|
+
}
|
|
872
|
+
return (entry.args ?? []).some((arg) => typeof arg === "string" && arg.toLowerCase().includes("repoprompt"));
|
|
873
|
+
}
|
|
874
|
+
function findProjectRoot(cwd = process.cwd()) {
|
|
875
|
+
let current = resolve(cwd);
|
|
876
|
+
while (true) {
|
|
877
|
+
if (existsSync(join(current, ".git")) || existsSync(join(current, "package.json")) || existsSync(join(current, PROJECT_CONFIG_NAME)) || existsSync(join(current, ".dm"))) {
|
|
878
|
+
return current;
|
|
879
|
+
}
|
|
880
|
+
const parent = dirname(current);
|
|
881
|
+
if (parent === current)
|
|
882
|
+
return null;
|
|
883
|
+
current = parent;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
function buildRepoPromptEntry(executablePath) {
|
|
887
|
+
return {
|
|
888
|
+
command: executablePath,
|
|
889
|
+
args: [],
|
|
890
|
+
lifecycle: "lazy"
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
function detectRepoPrompt(summary, cwd = process.cwd()) {
|
|
894
|
+
for (const source of summary.sources) {
|
|
895
|
+
if (source.kind !== "shared" || source.serverCount === 0)
|
|
896
|
+
continue;
|
|
897
|
+
const config = readValidatedConfig(source.path, `MCP config from ${source.path}`);
|
|
898
|
+
if (!config)
|
|
899
|
+
continue;
|
|
900
|
+
for (const [name, entry] of Object.entries(config.mcpServers)) {
|
|
901
|
+
if (isRepoPromptServer(name, entry)) {
|
|
902
|
+
return { configured: true, configuredPath: source.path };
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
const executablePath = REPOPROMPT_BINARY_CANDIDATES.find((candidate) => existsSync(candidate));
|
|
907
|
+
if (!executablePath) {
|
|
908
|
+
return { configured: false };
|
|
909
|
+
}
|
|
910
|
+
const projectRoot = findProjectRoot(cwd);
|
|
911
|
+
const targetPath = projectRoot ? join(projectRoot, PROJECT_CONFIG_NAME) : GENERIC_GLOBAL_CONFIG_PATH;
|
|
912
|
+
return {
|
|
913
|
+
configured: false,
|
|
914
|
+
executablePath,
|
|
915
|
+
targetPath,
|
|
916
|
+
serverName: "repoprompt",
|
|
917
|
+
entry: buildRepoPromptEntry(executablePath)
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
export function previewCompatibilityImports(importKinds, overridePath) {
|
|
921
|
+
const targetPath = getPiGlobalConfigPath(overridePath);
|
|
922
|
+
const raw = readRawConfigObject(targetPath);
|
|
923
|
+
const currentImports = Array.isArray(raw.imports) ? raw.imports.filter((value) => typeof value === "string") : [];
|
|
924
|
+
const merged = [...new Set([...currentImports, ...importKinds])];
|
|
925
|
+
const nextRaw = { ...raw, imports: merged };
|
|
926
|
+
setServersObject(nextRaw, getServersObject(nextRaw));
|
|
927
|
+
return buildConfigWritePreview(targetPath, nextRaw);
|
|
928
|
+
}
|
|
929
|
+
export function ensureCompatibilityImports(importKinds, overridePath) {
|
|
930
|
+
const targetPath = getPiGlobalConfigPath(overridePath);
|
|
931
|
+
const raw = readRawConfigObject(targetPath);
|
|
932
|
+
const currentImports = Array.isArray(raw.imports) ? raw.imports.filter((value) => typeof value === "string") : [];
|
|
933
|
+
const merged = [...new Set([...currentImports, ...importKinds])];
|
|
934
|
+
const added = merged.filter((kind) => !currentImports.includes(kind));
|
|
935
|
+
if (added.length === 0) {
|
|
936
|
+
return { path: targetPath, added: [] };
|
|
937
|
+
}
|
|
938
|
+
raw.imports = merged;
|
|
939
|
+
const servers = getServersObject(raw);
|
|
940
|
+
setServersObject(raw, servers);
|
|
941
|
+
writeRawConfigObject(targetPath, raw);
|
|
942
|
+
return { path: targetPath, added };
|
|
943
|
+
}
|
|
944
|
+
export function buildStarterProjectConfig() {
|
|
945
|
+
return {
|
|
946
|
+
mcpServers: {}
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
export function previewStarterSharedConfig(target, cwd = process.cwd()) {
|
|
950
|
+
const targetPath = getSharedConfigPath(target, cwd);
|
|
951
|
+
const nextRaw = { mcpServers: buildStarterProjectConfig().mcpServers };
|
|
952
|
+
return buildConfigWritePreview(targetPath, nextRaw);
|
|
953
|
+
}
|
|
954
|
+
export function writeStarterSharedConfig(target, cwd = process.cwd()) {
|
|
955
|
+
const targetPath = getSharedConfigPath(target, cwd);
|
|
956
|
+
const raw = { mcpServers: buildStarterProjectConfig().mcpServers };
|
|
957
|
+
writeRawConfigObject(targetPath, raw);
|
|
958
|
+
return targetPath;
|
|
959
|
+
}
|
|
960
|
+
export function previewStarterProjectConfig(cwd = process.cwd()) {
|
|
961
|
+
return previewStarterSharedConfig("project", cwd);
|
|
962
|
+
}
|
|
963
|
+
export function writeStarterProjectConfig(cwd = process.cwd()) {
|
|
964
|
+
return writeStarterSharedConfig("project", cwd);
|
|
965
|
+
}
|
|
966
|
+
export function previewSharedServerEntry(filePath, serverName, entry) {
|
|
967
|
+
const raw = readRawConfigObject(filePath);
|
|
968
|
+
const nextRaw = { ...raw };
|
|
969
|
+
const servers = getServersObject(nextRaw);
|
|
970
|
+
servers[serverName] = entry;
|
|
971
|
+
setServersObject(nextRaw, servers);
|
|
972
|
+
return buildConfigWritePreview(filePath, nextRaw);
|
|
973
|
+
}
|
|
974
|
+
export function writeSharedServerEntry(filePath, serverName, entry) {
|
|
975
|
+
const raw = readRawConfigObject(filePath);
|
|
976
|
+
const servers = getServersObject(raw);
|
|
977
|
+
servers[serverName] = entry;
|
|
978
|
+
setServersObject(raw, servers);
|
|
979
|
+
writeRawConfigObject(filePath, raw);
|
|
980
|
+
return filePath;
|
|
981
|
+
}
|
|
982
|
+
export function getServerProvenance(overridePath, cwd = process.cwd()) {
|
|
983
|
+
const provenance = new Map;
|
|
984
|
+
const userPath = getPiGlobalConfigPath(overridePath);
|
|
985
|
+
if (getConfiguredHostConfigDiscovery(overridePath, cwd) === "on") {
|
|
986
|
+
for (const importKind of Object.keys(IMPORT_PATHS)) {
|
|
987
|
+
const imported = loadImportedConfig(importKind, cwd, `Failed to inspect imported MCP config from ${importKind}:`);
|
|
988
|
+
if (!imported)
|
|
989
|
+
continue;
|
|
990
|
+
for (const name of Object.keys(extractServers(imported.value, importKind))) {
|
|
991
|
+
provenance.set(name, { path: userPath, kind: "import", importKind });
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
for (const source of getConfigSources(overridePath, cwd)) {
|
|
996
|
+
const loaded = readValidatedConfig(source.readPath, `MCP config from ${source.readPath}`);
|
|
997
|
+
if (!loaded)
|
|
998
|
+
continue;
|
|
999
|
+
if (loaded.imports?.length) {
|
|
1000
|
+
for (const importKind of loaded.imports) {
|
|
1001
|
+
const imported = loadImportedConfig(importKind, cwd, `Failed to inspect imported MCP config from ${importKind}:`);
|
|
1002
|
+
if (!imported)
|
|
1003
|
+
continue;
|
|
1004
|
+
const servers = extractServers(imported.value, importKind);
|
|
1005
|
+
for (const name of Object.keys(servers)) {
|
|
1006
|
+
if (!provenance.has(name)) {
|
|
1007
|
+
provenance.set(name, { path: userPath, kind: "import", importKind });
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
for (const name of Object.keys(loaded.mcpServers)) {
|
|
1013
|
+
provenance.set(name, {
|
|
1014
|
+
path: source.writePath,
|
|
1015
|
+
kind: source.kind,
|
|
1016
|
+
...source.importKind !== undefined ? { importKind: source.importKind } : {}
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
return provenance;
|
|
1021
|
+
}
|
|
1022
|
+
export function writeDirectToolsConfig(changes, provenance, fullConfig) {
|
|
1023
|
+
const byPath = new Map;
|
|
1024
|
+
for (const [serverName, value] of changes) {
|
|
1025
|
+
const prov = provenance.get(serverName);
|
|
1026
|
+
if (!prov)
|
|
1027
|
+
continue;
|
|
1028
|
+
const targetPath = prov.path;
|
|
1029
|
+
if (!byPath.has(targetPath))
|
|
1030
|
+
byPath.set(targetPath, []);
|
|
1031
|
+
byPath.get(targetPath).push({ name: serverName, value, prov });
|
|
1032
|
+
}
|
|
1033
|
+
for (const [filePath, entries] of byPath) {
|
|
1034
|
+
const raw = readRawConfigObject(filePath);
|
|
1035
|
+
const servers = getServersObject(raw);
|
|
1036
|
+
for (const { name, value, prov } of entries) {
|
|
1037
|
+
if (prov.kind === "import") {
|
|
1038
|
+
const fullDef = fullConfig.mcpServers[name];
|
|
1039
|
+
if (fullDef) {
|
|
1040
|
+
servers[name] = { ...fullDef, directTools: value };
|
|
1041
|
+
}
|
|
1042
|
+
} else if (servers[name]) {
|
|
1043
|
+
servers[name] = { ...servers[name], directTools: value };
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
setServersObject(raw, servers);
|
|
1047
|
+
writeRawConfigObject(filePath, raw);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
export function resolveConfiguredOAuthDir(raw, cwd = process.cwd()) {
|
|
1051
|
+
if (raw === undefined || raw === null)
|
|
1052
|
+
return;
|
|
1053
|
+
if (typeof raw !== "string") {
|
|
1054
|
+
throw new Error("settings.oauthDir must be a string");
|
|
1055
|
+
}
|
|
1056
|
+
const trimmed = raw.trim();
|
|
1057
|
+
if (!trimmed)
|
|
1058
|
+
return;
|
|
1059
|
+
return resolve(cwd, trimmed);
|
|
1060
|
+
}
|