pi-web-ui 0.113.1 → 0.114.0
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/CHANGELOG.md +43 -1
- package/README.md +117 -246
- package/README.zh-CN.md +145 -217
- package/README.zh-TW.md +145 -217
- package/bin/device-hub-cli.mjs +577 -0
- package/bin/pi-device-hub.mjs +9 -0
- package/bin/pi-web-ui.mjs +47 -15
- package/dist/server/agent-service-daemon.js +102 -24
- package/dist/server/agent-service.js +2289 -1120
- package/dist/server/approval-rules.js +29 -3
- package/dist/server/ask-user-question-tool.js +8 -8
- package/dist/server/atomic-file.js +35 -14
- package/dist/server/attachment-store.js +71 -35
- package/dist/server/attachments.js +18 -8
- package/dist/server/canvas/canvas-agent-tool.js +65 -15
- package/dist/server/canvas/canvas-dispatch.js +61 -20
- package/dist/server/canvas/canvas-orchestrator.js +516 -199
- package/dist/server/canvas/canvas-scheduler.js +99 -0
- package/dist/server/canvas/canvas-store.js +107 -54
- package/dist/server/canvas/canvas-validation.js +131 -0
- package/dist/server/canvas/canvas-workspace.js +284 -0
- package/dist/server/client-id-utils.js +8 -0
- package/dist/server/client-state.js +104 -22
- package/dist/server/communicate-tool.js +76 -0
- package/dist/server/compact-context-tool.js +2 -3
- package/dist/server/compaction-instructions.js +75 -0
- package/dist/server/compaction-markers.js +7 -2
- package/dist/server/compaction-state.js +84 -0
- package/dist/server/composer-drafts.js +43 -40
- package/dist/server/context-evidence.js +346 -0
- package/dist/server/conversation-communication.js +368 -0
- package/dist/server/conversation-read-tool.js +3 -4
- package/dist/server/conversation-tool-names.js +14 -0
- package/dist/server/conversation-tool.js +57 -0
- package/dist/server/delegate-mode.js +17 -4
- package/dist/server/device-center/bot-catalog.js +88 -0
- package/dist/server/device-center/bot-execution.js +106 -0
- package/dist/server/device-center/bots.js +170 -0
- package/dist/server/device-center/collab-service.js +153 -0
- package/dist/server/device-center/collab-tool.js +184 -0
- package/dist/server/device-center/connector.js +332 -0
- package/dist/server/device-center/desktop-linux.js +372 -0
- package/dist/server/device-center/desktop-native.js +413 -0
- package/dist/server/device-center/desktop-page.js +232 -0
- package/dist/server/device-center/desktop-setup.js +30 -0
- package/dist/server/device-center/desktop.js +387 -0
- package/dist/server/device-center/device-portals.js +241 -0
- package/dist/server/device-center/execution-failure.js +70 -0
- package/dist/server/device-center/executor.js +371 -0
- package/dist/server/device-center/file-sync.js +493 -0
- package/dist/server/device-center/hub-auth.js +69 -0
- package/dist/server/device-center/hub-page.js +68 -0
- package/dist/server/device-center/hub-setup.js +214 -0
- package/dist/server/device-center/hub.js +829 -0
- package/dist/server/device-center/local-page.js +17 -0
- package/dist/server/device-center/local.js +476 -0
- package/dist/server/device-center/main.js +118 -0
- package/dist/server/device-center/native-desktop/macos.js +192 -0
- package/dist/server/device-center/native-desktop/windows.js +118 -0
- package/dist/server/device-center/path-portal.js +120 -0
- package/dist/server/device-center/relay.js +959 -0
- package/dist/server/device-center/runtime.js +253 -0
- package/dist/server/device-center/smoke-preview.js +3 -0
- package/dist/server/device-center/store.js +122 -0
- package/dist/server/device-center/task-error.js +14 -0
- package/dist/server/device-center/transfers.js +293 -0
- package/dist/server/device-center/workspaces.js +45 -0
- package/dist/server/dispatch-domain-handlers.js +83 -27
- package/dist/server/dsh/dsh-agent-service.js +277 -158
- package/dist/server/dsh/dsh-client.js +12 -2
- package/dist/server/durable/durable-agent-service.js +1 -1
- package/dist/server/evidence-tool-hooks.js +49 -0
- package/dist/server/file-save.js +95 -0
- package/dist/server/file-transfer-routes.js +97 -47
- package/dist/server/files-service.js +38 -26
- package/dist/server/goal-review-gate.js +16 -6
- package/dist/server/goal-service.js +54 -30
- package/dist/server/http-proxy.js +120 -0
- package/dist/server/index.js +229 -223
- package/dist/server/load-timing.js +31 -0
- package/dist/server/mcp-bridge.js +219 -94
- package/dist/server/mcp-hot-reload.js +31 -5
- package/dist/server/mcp-manager.js +192 -93
- package/dist/server/mcp-oauth-login.js +169 -0
- package/dist/server/mcp-oauth-ui.js +137 -0
- package/dist/server/mcp-oauth.js +395 -0
- package/dist/server/mcp-project-runtime.js +207 -0
- package/dist/server/message-window.js +74 -21
- package/dist/server/net-guard.js +122 -20
- package/dist/server/payload-limits.js +17 -0
- package/dist/server/pending-question.js +82 -0
- package/dist/server/plan-mode.js +338 -137
- package/dist/server/plugin-assets.js +8 -4
- package/dist/server/plugin-chat-attachments.js +48 -0
- package/dist/server/plugin-facilities.js +67 -220
- package/dist/server/plugin-installer.js +2 -1
- package/dist/server/plugin-removal.js +49 -0
- package/dist/server/plugin-secrets.js +179 -0
- package/dist/server/plugins.js +262 -92
- package/dist/server/preset-fields.js +49 -0
- package/dist/server/preset-plugin-dependencies.js +57 -0
- package/dist/server/preset-share.js +112 -72
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/queue-modes.js +10 -0
- package/dist/server/remote-ssh-service.js +160 -67
- package/dist/server/request-context.js +164 -0
- package/dist/server/sandbox-data.js +655 -0
- package/dist/server/sandbox-engines.js +7 -0
- package/dist/server/sandbox-runtime-gate.js +41 -0
- package/dist/server/sandbox-service.js +139 -698
- package/dist/server/sandbox-tool.js +21 -4
- package/dist/server/scheduler-tasks.js +63 -48
- package/dist/server/serialize.js +48 -19
- package/dist/server/session-branch.js +25 -3
- package/dist/server/session-parser.js +28 -18
- package/dist/server/session-scan.js +130 -0
- package/dist/server/session-search.js +51 -37
- package/dist/server/settings-service.js +267 -111
- package/dist/server/setup-config.js +58 -0
- package/dist/server/skill-maintenance.js +112 -24
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagent-notify.js +4 -1
- package/dist/server/subagent-security-gate.js +52 -11
- package/dist/server/subagent-task.js +27 -0
- package/dist/server/subagents.js +217 -46
- package/dist/server/tabs.js +9 -1
- package/dist/server/terminals.js +467 -363
- package/dist/server/tool-guards.js +42 -6
- package/dist/server/tool-manager.js +47 -20
- package/dist/server/tool-overrides.js +13 -11
- package/dist/server/tool-prompt-overrides.js +32 -2
- package/dist/server/tool-prompts.js +1 -1
- package/dist/server/turn-timing.js +11 -0
- package/dist/server/universal/session-store.js +48 -6
- package/dist/server/universal/universal-service.js +1 -1
- package/dist/server/universal/universal-session.js +334 -231
- package/dist/server/uploads.js +64 -5
- package/dist/server/worktree-service.js +32 -24
- package/dist/server/ws-pending-queue.js +73 -27
- package/docs/device-center.md +142 -0
- package/docs/device-desktop.md +48 -0
- package/package.json +4 -1
- package/plugin-sdk/index.d.ts +28 -6
- package/plugin-sdk/ssh-host-trust.mjs +225 -0
- package/plugins/catalog.json +1 -1
- package/themes/aetheris.css +1 -107
- package/themes/ayu-light.css +5 -29
- package/themes/bases/flow-base.css +0 -6
- package/themes/bases/statusbar-flat-base.css +112 -0
- package/themes/bases/zhujian-base.css +1 -0
- package/themes/bases/zhupi-base.css +1 -0
- package/themes/catppuccin-latte.css +4 -27
- package/themes/catppuccin.css +4 -35
- package/themes/claude-code-dark.css +1 -0
- package/themes/codex.css +4 -28
- package/themes/cyberpunk.css +3 -61
- package/themes/dark-teal.css +0 -1
- package/themes/dazzle.css +3 -73
- package/themes/everforest-light.css +4 -27
- package/themes/geist.css +4 -28
- package/themes/gruvbox-light.css +4 -27
- package/themes/kanagawa-lotus.css +4 -27
- package/themes/md-preview.css +3 -213
- package/themes/mist.css +4 -24
- package/themes/nord.css +4 -35
- package/themes/one-dark.css +4 -37
- package/themes/paper.css +4 -16
- package/themes/rose-pine-dawn.css +4 -28
- package/themes/sakura.css +4 -24
- package/themes/solarized-light.css +4 -27
- package/themes/tokyo-night.css +3 -34
- package/themes/white.css +4 -26
- package/themes/zhujian-dark.css +1 -0
- package/themes/zhupi-dark.css +1 -0
- package/web/dist/.build-assets.json +1 -0
- package/web/dist/assets/BgTasksModal-CS7UNzcw.js +1 -0
- package/web/dist/assets/BgTasksModal-Dk-tp78W.js +1 -0
- package/web/dist/assets/CanvasStudio-BEJZE5BI.js +1579 -0
- package/web/dist/assets/CanvasStudio-CgUNHqos.js +1579 -0
- package/web/dist/assets/DeviceCenter-C8_CbxQ3.js +3 -0
- package/web/dist/assets/DeviceCenter-CVblvhNX.js +3 -0
- package/web/dist/assets/Dropdown-Oug0DDte.js +1 -0
- package/web/dist/assets/FilePreview-CvdX6nr6.js +2 -0
- package/web/dist/assets/FilePreview-kCZhfBAF.js +2 -0
- package/web/dist/assets/GlobalSearchModal-BLH63yYP.js +1 -0
- package/web/dist/assets/GlobalSearchModal-DAkvlvp6.js +1 -0
- package/web/dist/assets/GroupConversation-Cgo4xbpa.js +1 -0
- package/web/dist/assets/GroupConversation-DZ71jEi8.js +1 -0
- package/web/dist/assets/ModelConfigModal-BpWdFdQC.js +2 -0
- package/web/dist/assets/PiSetupModal-mkWPSxMj.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-VK_jqXYS.js +1 -0
- package/web/dist/assets/SCMPanel-BP1TUT-V.js +2 -0
- package/web/dist/assets/SCMPanel-BscPm9kf.js +2 -0
- package/web/dist/assets/SettingsModal-C8UgMjVM.js +20 -0
- package/web/dist/assets/SettingsModal-VynB7jNb.js +20 -0
- package/web/dist/assets/TerminalPanel-D6cPRFei.js +3 -0
- package/web/dist/assets/TerminalPanel-DL20uKgU.js +3 -0
- package/web/dist/assets/TuiOverlayModal-Dst3BmT8.js +1 -0
- package/web/dist/assets/TuiOverlayModal-NCkpNtTp.js +1 -0
- package/web/dist/assets/UniversalPathPicker-CfQFc_c3.js +1 -0
- package/web/dist/assets/UniversalPathPicker-DwxwNcFw.js +1 -0
- package/web/dist/assets/app-globals-CnO6KlEM.js +325 -0
- package/web/dist/assets/desktop-DPyTy4M3.js +1 -0
- package/web/dist/assets/es-DWMIuDU2.js +2 -0
- package/web/dist/assets/index-CPgAH5q7.js +83 -0
- package/web/dist/assets/index-Cje8iugL.js +83 -0
- package/web/dist/assets/index-DnKeiDbj.css +1 -0
- package/web/dist/assets/markdown-BggnuQ4H.js +40 -0
- package/web/dist/assets/markdown-C8LL_u4z.css +1 -0
- package/web/dist/assets/math-1uRJCsA9.js +257 -0
- package/web/dist/assets/math-BorAY7qD.css +1 -0
- package/web/dist/assets/plugin-host-CqA4yW6w.js +1 -0
- package/web/dist/assets/shortcut-stack-hhrDyRBn.js +1 -0
- package/web/dist/assets/theme-BqdAjEjF.js +1 -0
- package/web/dist/assets/uuid-BzDIGCwP.js +1 -0
- package/web/dist/index.html +15 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/TerminalPanel-Ci55JtJL.js +0 -3
- package/web/dist/assets/index-ByiO9f-N.js +0 -2008
- package/web/dist/assets/index-ycGLh4a4.css +0 -1
- package/web/dist/assets/markdown-BJZlfvdP.js +0 -296
- package/web/dist/assets/markdown-DSNUiDzp.css +0 -1
|
@@ -4,56 +4,55 @@
|
|
|
4
4
|
* 负责 Pi 全局 (~/.pi/agent/mcp.json 或兼容 ~/.pi-web/mcp.json)
|
|
5
5
|
* 与工作区项目级 (.pi/mcp.json) MCP 服务器配置的读写、合并与生命周期。
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { readFileSync, unlinkSync } from "node:fs";
|
|
8
|
+
import { resolve } from "node:path";
|
|
9
|
+
import { isDeepStrictEqual } from "node:util";
|
|
10
|
+
import { writeAtomicSync } from "./atomic-file.js";
|
|
9
11
|
import { getGlobalMcpPath, getProjectMcpPath, parseMcpConfig, } from "./mcp-bridge.js";
|
|
10
12
|
export { getGlobalMcpPath, getProjectMcpPath };
|
|
11
13
|
/** 读取指定文件的 MCP 服务器配置 */
|
|
12
14
|
export function readMcpConfigFile(filePath) {
|
|
13
|
-
if (!existsSync(filePath)) {
|
|
14
|
-
return { servers: {}, raw: null, legacy: false, exists: false };
|
|
15
|
-
}
|
|
16
15
|
try {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
catch {
|
|
26
|
-
/* invalid json */
|
|
27
|
-
}
|
|
28
|
-
const legacy = !!raw && raw.servers !== undefined && raw.mcpServers === undefined;
|
|
29
|
-
const parsedConfig = parseMcpConfig(text);
|
|
16
|
+
const raw = readWritableConfig(filePath);
|
|
17
|
+
if (!raw)
|
|
18
|
+
return { servers: {}, raw: null, legacy: false, exists: false };
|
|
19
|
+
const legacy = raw.servers !== undefined && raw.mcpServers === undefined;
|
|
20
|
+
return { servers: parseMcpConfig(JSON.stringify(raw))?.servers ?? {}, raw, legacy, exists: true };
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
30
23
|
return {
|
|
31
|
-
servers:
|
|
32
|
-
raw,
|
|
33
|
-
legacy,
|
|
24
|
+
servers: {},
|
|
25
|
+
raw: null,
|
|
26
|
+
legacy: false,
|
|
34
27
|
exists: true,
|
|
28
|
+
error: `MCP 配置 ${filePath}: ${mutationError(error).error}`,
|
|
35
29
|
};
|
|
36
30
|
}
|
|
37
|
-
catch {
|
|
38
|
-
return { servers: {}, raw: null, legacy: false, exists: true };
|
|
39
|
-
}
|
|
40
31
|
}
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
raw = parsed;
|
|
32
|
+
/** Only ENOENT is an empty configuration. All other read/shape errors stop mutations. */
|
|
33
|
+
function readWritableConfig(filePath) {
|
|
34
|
+
try {
|
|
35
|
+
const raw = JSON.parse(readFileSync(filePath, "utf8"));
|
|
36
|
+
if (!isRecord(raw))
|
|
37
|
+
throw new Error("MCP 配置必须是 JSON 对象");
|
|
38
|
+
for (const key of ["servers", "mcpServers"]) {
|
|
39
|
+
if (raw[key] !== undefined && (!isRecord(raw[key]) || Object.values(raw[key]).some((v) => !isRecord(v)))) {
|
|
40
|
+
throw new Error(`MCP 配置 ${key} 格式无效`);
|
|
51
41
|
}
|
|
52
42
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
43
|
+
if (raw.servers !== undefined && raw.mcpServers !== undefined)
|
|
44
|
+
throw new Error("MCP 配置包含两份服务器清单,禁止覆盖");
|
|
45
|
+
return raw;
|
|
56
46
|
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error.code === "ENOENT")
|
|
49
|
+
return null;
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** 写入指定文件的 MCP 服务器配置(保留除服务器清单以外的其它字段) */
|
|
54
|
+
export function writeMcpConfigFile(filePath, servers, preferLegacy = false) {
|
|
55
|
+
const raw = readWritableConfig(filePath) ?? {};
|
|
57
56
|
const legacy = raw.servers !== undefined && raw.mcpServers === undefined ? true : preferLegacy;
|
|
58
57
|
const next = { ...raw };
|
|
59
58
|
if (legacy) {
|
|
@@ -64,7 +63,27 @@ export function writeMcpConfigFile(filePath, servers, preferLegacy = false) {
|
|
|
64
63
|
delete next.servers;
|
|
65
64
|
next.mcpServers = servers;
|
|
66
65
|
}
|
|
67
|
-
|
|
66
|
+
atomicWrite(filePath, next);
|
|
67
|
+
}
|
|
68
|
+
function isRecord(value) {
|
|
69
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
70
|
+
}
|
|
71
|
+
/** MCP configs and recovery intents may contain credentials. */
|
|
72
|
+
function atomicWrite(filePath, value) {
|
|
73
|
+
writeAtomicSync(filePath, JSON.stringify(value, null, 2) + "\n", { mode: 0o600, fsync: true });
|
|
74
|
+
}
|
|
75
|
+
/** Keep unrecognized server fields intact when editing a different entry. */
|
|
76
|
+
function editableConfig(filePath) {
|
|
77
|
+
const cfg = readMcpConfigFile(filePath);
|
|
78
|
+
if (cfg.error)
|
|
79
|
+
throw new Error(cfg.error);
|
|
80
|
+
return {
|
|
81
|
+
...cfg,
|
|
82
|
+
servers: { ...cfg.raw?.[cfg.legacy ? "servers" : "mcpServers"] },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function mutationError(error) {
|
|
86
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
68
87
|
}
|
|
69
88
|
/** 校验 MCP 服务器名称合法性(字母、数字、下划线、短横线) */
|
|
70
89
|
export function isValidMcpServerName(name) {
|
|
@@ -77,12 +96,21 @@ export function listAllMcpServers(opts) {
|
|
|
77
96
|
const globalCfg = readMcpConfigFile(globalPath);
|
|
78
97
|
const projectCfg = readMcpConfigFile(projectPath);
|
|
79
98
|
const result = [];
|
|
99
|
+
for (const [scope, path, cfg] of [
|
|
100
|
+
["global", globalPath, globalCfg],
|
|
101
|
+
["project", projectPath, projectCfg],
|
|
102
|
+
]) {
|
|
103
|
+
if (cfg.error)
|
|
104
|
+
result.push({ name: path, scope, enabled: false, status: "error", error: cfg.error });
|
|
105
|
+
}
|
|
80
106
|
// 全局服务器
|
|
81
107
|
for (const [name, spec] of Object.entries(globalCfg.servers)) {
|
|
82
|
-
const
|
|
108
|
+
const overridden = Object.hasOwn(projectCfg.servers, name);
|
|
109
|
+
const status = getRuntimeStatus(name, spec, overridden ? undefined : opts.bridge);
|
|
83
110
|
result.push({
|
|
84
111
|
name,
|
|
85
112
|
scope: "global",
|
|
113
|
+
...(overridden ? { overriddenBy: "project" } : {}),
|
|
86
114
|
command: spec.command,
|
|
87
115
|
args: spec.args,
|
|
88
116
|
cwd: spec.cwd,
|
|
@@ -94,9 +122,7 @@ export function listAllMcpServers(opts) {
|
|
|
94
122
|
protocolVersion: spec.protocolVersion,
|
|
95
123
|
enabled: spec.enabled !== false,
|
|
96
124
|
description: spec.description,
|
|
97
|
-
status
|
|
98
|
-
error: status.error,
|
|
99
|
-
tools: status.tools,
|
|
125
|
+
...status,
|
|
100
126
|
});
|
|
101
127
|
}
|
|
102
128
|
// 项目级服务器
|
|
@@ -116,9 +142,7 @@ export function listAllMcpServers(opts) {
|
|
|
116
142
|
protocolVersion: spec.protocolVersion,
|
|
117
143
|
enabled: spec.enabled !== false,
|
|
118
144
|
description: spec.description,
|
|
119
|
-
status
|
|
120
|
-
error: status.error,
|
|
121
|
-
tools: status.tools,
|
|
145
|
+
...status,
|
|
122
146
|
});
|
|
123
147
|
}
|
|
124
148
|
return { servers: result, globalPath, projectPath };
|
|
@@ -142,38 +166,103 @@ export function saveMcpServer(opts) {
|
|
|
142
166
|
const globalPath = getGlobalMcpPath(opts.agentDir, opts.dataDir);
|
|
143
167
|
const projectPath = getProjectMcpPath(opts.cwd);
|
|
144
168
|
const targetPath = targetScope === "global" ? globalPath : projectPath;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
169
|
+
try {
|
|
170
|
+
const targetCfg = editableConfig(targetPath);
|
|
171
|
+
const spec = {
|
|
172
|
+
command: opts.server.command?.trim() || undefined,
|
|
173
|
+
args: Array.isArray(opts.server.args) && opts.server.args.length > 0 ? opts.server.args.map(String) : undefined,
|
|
174
|
+
cwd: opts.server.cwd?.trim() || undefined,
|
|
175
|
+
env: opts.server.env && Object.keys(opts.server.env).length > 0
|
|
176
|
+
? opts.server.env
|
|
177
|
+
: undefined,
|
|
178
|
+
url: opts.server.url?.trim() || undefined,
|
|
179
|
+
headers: opts.server.headers && Object.keys(opts.server.headers).length > 0
|
|
180
|
+
? opts.server.headers
|
|
181
|
+
: undefined,
|
|
182
|
+
type: opts.server.type,
|
|
183
|
+
timeout: typeof opts.server.timeout === "number" ? opts.server.timeout : undefined,
|
|
184
|
+
protocolVersion: opts.server.protocolVersion?.trim() || undefined,
|
|
185
|
+
enabled: opts.server.enabled !== false,
|
|
186
|
+
description: opts.server.description?.trim() || undefined,
|
|
187
|
+
};
|
|
188
|
+
const prevName = opts.prevName?.trim();
|
|
189
|
+
const oldPath = opts.prevScope === "global" ? globalPath : projectPath;
|
|
190
|
+
const moving = prevName && opts.prevScope && (prevName !== name || resolve(oldPath) !== resolve(targetPath));
|
|
191
|
+
if (moving) {
|
|
192
|
+
if (resolve(oldPath) !== resolve(targetPath))
|
|
193
|
+
return migrateMcpServer(oldPath, targetPath, prevName, name, spec);
|
|
194
|
+
if (!Object.hasOwn(targetCfg.servers, prevName))
|
|
195
|
+
throw new Error(`源服务器 ${prevName} 不存在`);
|
|
196
|
+
if (Object.hasOwn(targetCfg.servers, name))
|
|
197
|
+
throw new Error(`目标服务器 ${name} 已存在`);
|
|
198
|
+
delete targetCfg.servers[prevName];
|
|
155
199
|
}
|
|
200
|
+
targetCfg.servers[name] = spec;
|
|
201
|
+
writeMcpConfigFile(targetPath, targetCfg.servers, targetCfg.legacy);
|
|
202
|
+
return { ok: true };
|
|
156
203
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
204
|
+
catch (error) {
|
|
205
|
+
return mutationError(error);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/** Durable intent distinguishes a partially committed move from a name collision.
|
|
209
|
+
* Retry the same save after restart to finish. Never roll back a committed target,
|
|
210
|
+
* or delete an entry edited since the intent was recorded.
|
|
211
|
+
*/
|
|
212
|
+
function migrateMcpServer(sourcePath, targetPath, sourceName, targetName, spec) {
|
|
213
|
+
const journalPath = `${sourcePath}.migration.json`;
|
|
214
|
+
const source = editableConfig(sourcePath);
|
|
215
|
+
const target = editableConfig(targetPath);
|
|
216
|
+
const desired = JSON.parse(JSON.stringify(spec));
|
|
217
|
+
let journal;
|
|
218
|
+
try {
|
|
219
|
+
journal = JSON.parse(readFileSync(journalPath, "utf8"));
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
if (error.code !== "ENOENT")
|
|
223
|
+
throw error;
|
|
224
|
+
if (!Object.hasOwn(source.servers, sourceName))
|
|
225
|
+
throw new Error(`源服务器 ${sourceName} 不存在`);
|
|
226
|
+
if (Object.hasOwn(target.servers, targetName))
|
|
227
|
+
throw new Error(`目标服务器 ${targetName} 已存在`);
|
|
228
|
+
journal = {
|
|
229
|
+
version: 1,
|
|
230
|
+
sourcePath: resolve(sourcePath),
|
|
231
|
+
targetPath: resolve(targetPath),
|
|
232
|
+
sourceName,
|
|
233
|
+
targetName,
|
|
234
|
+
sourceSpec: source.servers[sourceName],
|
|
235
|
+
targetSpec: desired,
|
|
236
|
+
};
|
|
237
|
+
atomicWrite(journalPath, journal);
|
|
238
|
+
}
|
|
239
|
+
if (!isRecord(journal) ||
|
|
240
|
+
journal.version !== 1 ||
|
|
241
|
+
journal.sourcePath !== resolve(sourcePath) ||
|
|
242
|
+
journal.targetPath !== resolve(targetPath) ||
|
|
243
|
+
journal.sourceName !== sourceName ||
|
|
244
|
+
journal.targetName !== targetName ||
|
|
245
|
+
!isRecord(journal.sourceSpec) ||
|
|
246
|
+
!isDeepStrictEqual(journal.targetSpec, desired)) {
|
|
247
|
+
throw new Error(`存在不同或损坏的 MCP 迁移事务:${journalPath}`);
|
|
248
|
+
}
|
|
249
|
+
const hasSource = Object.hasOwn(source.servers, sourceName);
|
|
250
|
+
const hasTarget = Object.hasOwn(target.servers, targetName);
|
|
251
|
+
if ((hasSource && !isDeepStrictEqual(source.servers[sourceName], journal.sourceSpec)) ||
|
|
252
|
+
(hasTarget && !isDeepStrictEqual(target.servers[targetName], desired))) {
|
|
253
|
+
throw new Error(`MCP 迁移期间配置已变更,保留两端并停止:${journalPath}`);
|
|
254
|
+
}
|
|
255
|
+
if (!hasSource && !hasTarget)
|
|
256
|
+
throw new Error(`MCP 迁移两端记录均不存在:${journalPath}`);
|
|
257
|
+
if (!hasTarget) {
|
|
258
|
+
target.servers[targetName] = desired;
|
|
259
|
+
writeMcpConfigFile(targetPath, target.servers, target.legacy);
|
|
260
|
+
}
|
|
261
|
+
if (hasSource) {
|
|
262
|
+
delete source.servers[sourceName];
|
|
263
|
+
writeMcpConfigFile(sourcePath, source.servers, source.legacy);
|
|
264
|
+
}
|
|
265
|
+
unlinkSync(journalPath);
|
|
177
266
|
return { ok: true };
|
|
178
267
|
}
|
|
179
268
|
/** 删除指定作用域下的 MCP 服务器 */
|
|
@@ -182,13 +271,18 @@ export function deleteMcpServer(opts) {
|
|
|
182
271
|
const globalPath = getGlobalMcpPath(opts.agentDir, opts.dataDir);
|
|
183
272
|
const projectPath = getProjectMcpPath(opts.cwd);
|
|
184
273
|
const targetPath = opts.scope === "global" ? globalPath : projectPath;
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
274
|
+
try {
|
|
275
|
+
const targetCfg = editableConfig(targetPath);
|
|
276
|
+
if (!targetCfg.servers[name]) {
|
|
277
|
+
return { ok: false, error: `服务器 ${name} 在 ${opts.scope === "global" ? "全局" : "项目"} 配置中不存在` };
|
|
278
|
+
}
|
|
279
|
+
delete targetCfg.servers[name];
|
|
280
|
+
writeMcpConfigFile(targetPath, targetCfg.servers, targetCfg.legacy);
|
|
281
|
+
return { ok: true };
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
return mutationError(error);
|
|
188
285
|
}
|
|
189
|
-
delete targetCfg.servers[name];
|
|
190
|
-
writeMcpConfigFile(targetPath, targetCfg.servers, targetCfg.legacy);
|
|
191
|
-
return { ok: true };
|
|
192
286
|
}
|
|
193
287
|
/** 切换指定作用域下的 MCP 服务器启用/停用状态 */
|
|
194
288
|
export function toggleMcpServer(opts) {
|
|
@@ -196,22 +290,27 @@ export function toggleMcpServer(opts) {
|
|
|
196
290
|
const globalPath = getGlobalMcpPath(opts.agentDir, opts.dataDir);
|
|
197
291
|
const projectPath = getProjectMcpPath(opts.cwd);
|
|
198
292
|
const targetPath = opts.scope === "global" ? globalPath : projectPath;
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
targetCfg.servers[name]
|
|
202
|
-
|
|
203
|
-
return { ok: true };
|
|
204
|
-
}
|
|
205
|
-
// 若在项目级切换全局服务器的启用状态(项目级覆盖):写入仅带 enabled 的配置项
|
|
206
|
-
if (opts.scope === "project") {
|
|
207
|
-
const globalCfg = readMcpConfigFile(globalPath);
|
|
208
|
-
if (globalCfg.servers[name]) {
|
|
209
|
-
targetCfg.servers[name] = { enabled: opts.enabled };
|
|
293
|
+
try {
|
|
294
|
+
const targetCfg = editableConfig(targetPath);
|
|
295
|
+
if (targetCfg.servers[name]) {
|
|
296
|
+
targetCfg.servers[name].enabled = opts.enabled;
|
|
210
297
|
writeMcpConfigFile(targetPath, targetCfg.servers, targetCfg.legacy);
|
|
211
298
|
return { ok: true };
|
|
212
299
|
}
|
|
300
|
+
// 若在项目级切换全局服务器的启用状态(项目级覆盖):写入仅带 enabled 的配置项
|
|
301
|
+
if (opts.scope === "project") {
|
|
302
|
+
const globalCfg = editableConfig(globalPath);
|
|
303
|
+
if (globalCfg.servers[name]) {
|
|
304
|
+
targetCfg.servers[name] = { enabled: opts.enabled };
|
|
305
|
+
writeMcpConfigFile(targetPath, targetCfg.servers, targetCfg.legacy);
|
|
306
|
+
return { ok: true };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return { ok: false, error: `服务器 ${name} 不存在` };
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
return mutationError(error);
|
|
213
313
|
}
|
|
214
|
-
return { ok: false, error: `服务器 ${name} 不存在` };
|
|
215
314
|
}
|
|
216
315
|
/** 从 npm 包名、URL 或命令自动推导合法的 MCP 服务器名称 */
|
|
217
316
|
export function deriveMcpServerName(raw) {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { authorizeMcp, McpOAuthProvider, MemoryOAuthStateStore } from "@earendil-works/pi-mcp/oauth";
|
|
4
|
+
import { getMcpAuthStorePath, mcpAuthStoreKey, readMcpAuthStore, withMcpAuthStoreLock, writeMcpAuthStore, } from "./mcp-oauth.js";
|
|
5
|
+
const LOGIN_TTL_MS = 10 * 60_000;
|
|
6
|
+
const attempts = new Map();
|
|
7
|
+
/** Ephemeral PKCE state stays server-side; only a completed grant enters pi's shared store. */
|
|
8
|
+
export async function startMcpOAuthLogin(options) {
|
|
9
|
+
const url = new URL(options.serverUrl);
|
|
10
|
+
if (url.protocol !== "https:" &&
|
|
11
|
+
!(url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)))
|
|
12
|
+
throw new Error("MCP OAuth requires HTTPS (HTTP is allowed only on loopback).");
|
|
13
|
+
if (attempts.size >= 32)
|
|
14
|
+
throw new Error("Too many pending MCP sign-ins. Close an existing sign-in and retry.");
|
|
15
|
+
const id = randomBytes(24).toString("hex");
|
|
16
|
+
const callbackPath = `/mcp/oauth/${randomBytes(16).toString("hex")}`;
|
|
17
|
+
const controller = new AbortController();
|
|
18
|
+
const store = new MemoryOAuthStateStore();
|
|
19
|
+
const server = createServer((req, res) => {
|
|
20
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
21
|
+
res.setHeader("Cache-Control", "no-store");
|
|
22
|
+
res.setHeader("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'");
|
|
23
|
+
const attempt = attempts.get(id);
|
|
24
|
+
if (!attempt || req.method !== "GET" || !req.url?.startsWith(`${callbackPath}?`)) {
|
|
25
|
+
res.writeHead(404).end("Not found");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
void finishMcpOAuthLogin({ id, redirectUrl: new URL(req.url, attempt.redirectUrl).href }).then(() => res.end("MCP sign-in complete. You can close this tab and return to pi-web-ui."), () => res.writeHead(400).end("MCP sign-in failed. Return to pi-web-ui and try again."));
|
|
29
|
+
});
|
|
30
|
+
await new Promise((resolve, reject) => {
|
|
31
|
+
server.once("error", reject);
|
|
32
|
+
server.listen(0, "127.0.0.1", () => {
|
|
33
|
+
server.removeListener("error", reject);
|
|
34
|
+
resolve();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
server.unref();
|
|
38
|
+
const address = server.address();
|
|
39
|
+
if (!address || typeof address === "string") {
|
|
40
|
+
server.close();
|
|
41
|
+
throw new Error("Cannot open MCP OAuth callback listener.");
|
|
42
|
+
}
|
|
43
|
+
const redirectUrl = `http://127.0.0.1:${address.port}${callbackPath}`;
|
|
44
|
+
const provider = new McpOAuthProvider({
|
|
45
|
+
serverUrl: options.serverUrl,
|
|
46
|
+
redirectUrl,
|
|
47
|
+
clientMetadata: {
|
|
48
|
+
client_name: "pi-web-ui",
|
|
49
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
50
|
+
response_types: ["code"],
|
|
51
|
+
token_endpoint_auth_method: "none",
|
|
52
|
+
},
|
|
53
|
+
store,
|
|
54
|
+
onRedirect: (authorizationUrl) => {
|
|
55
|
+
attempt.authorizationUrl = authorizationUrl.href;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const attempt = {
|
|
59
|
+
controller,
|
|
60
|
+
server,
|
|
61
|
+
redirectUrl,
|
|
62
|
+
authorizationUrl: "",
|
|
63
|
+
provider,
|
|
64
|
+
store,
|
|
65
|
+
options: { ...options },
|
|
66
|
+
status: { status: "pending" },
|
|
67
|
+
finishing: undefined,
|
|
68
|
+
timer: setTimeout(() => cancelMcpOAuthLogin(id), LOGIN_TTL_MS),
|
|
69
|
+
};
|
|
70
|
+
attempt.timer.unref();
|
|
71
|
+
attempts.set(id, attempt);
|
|
72
|
+
try {
|
|
73
|
+
await authorizeMcp(provider, {
|
|
74
|
+
serverUrl: options.serverUrl,
|
|
75
|
+
skipRefresh: true,
|
|
76
|
+
signal: AbortSignal.any([controller.signal, AbortSignal.timeout(30_000)]),
|
|
77
|
+
});
|
|
78
|
+
controller.signal.throwIfAborted();
|
|
79
|
+
if (!attempt.authorizationUrl)
|
|
80
|
+
throw new Error("No OAuth authorization URL returned.");
|
|
81
|
+
return { id, authorizationUrl: attempt.authorizationUrl, redirectUrl };
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
cancelMcpOAuthLogin(id);
|
|
85
|
+
throw new Error("Cannot start MCP OAuth sign-in. Check that the server supports OAuth discovery and client registration.");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function getAttempt(id) {
|
|
89
|
+
const attempt = attempts.get(id);
|
|
90
|
+
if (!attempt)
|
|
91
|
+
throw new Error("MCP sign-in expired or was cancelled. Start sign-in again.");
|
|
92
|
+
return attempt;
|
|
93
|
+
}
|
|
94
|
+
export function getMcpOAuthLoginStatus(id) {
|
|
95
|
+
return { ...getAttempt(id).status };
|
|
96
|
+
}
|
|
97
|
+
export async function finishMcpOAuthLogin({ id, redirectUrl }) {
|
|
98
|
+
const attempt = getAttempt(id);
|
|
99
|
+
if (attempt.status.status === "complete")
|
|
100
|
+
return;
|
|
101
|
+
if (attempt.status.status === "error")
|
|
102
|
+
throw new Error(attempt.status.error);
|
|
103
|
+
// Validate before changing state: a stray or forged callback must not consume the real login.
|
|
104
|
+
let callback;
|
|
105
|
+
try {
|
|
106
|
+
callback = new URL(redirectUrl);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
throw new Error("Paste the full OAuth redirect URL.");
|
|
110
|
+
}
|
|
111
|
+
const expected = new URL(attempt.redirectUrl);
|
|
112
|
+
const state = attempt.store.load()?.oauthState;
|
|
113
|
+
if (callback.origin !== expected.origin ||
|
|
114
|
+
callback.pathname !== expected.pathname ||
|
|
115
|
+
!state ||
|
|
116
|
+
callback.searchParams.getAll("state").length !== 1 ||
|
|
117
|
+
callback.searchParams.get("state") !== state)
|
|
118
|
+
throw new Error("The OAuth redirect does not match this sign-in request.");
|
|
119
|
+
if (attempt.finishing)
|
|
120
|
+
return attempt.finishing;
|
|
121
|
+
if (callback.searchParams.has("error")) {
|
|
122
|
+
attempt.status = { status: "error", error: "OAuth authorization was denied. Start sign-in again." };
|
|
123
|
+
attempt.server.close();
|
|
124
|
+
throw new Error(attempt.status.error);
|
|
125
|
+
}
|
|
126
|
+
const code = callback.searchParams.get("code");
|
|
127
|
+
if (!code || callback.searchParams.getAll("code").length !== 1)
|
|
128
|
+
throw new Error("The OAuth redirect URL is missing an authorization code.");
|
|
129
|
+
attempt.finishing = completeLogin(attempt, code, callback.searchParams.get("iss") ?? undefined);
|
|
130
|
+
return attempt.finishing;
|
|
131
|
+
}
|
|
132
|
+
async function completeLogin(attempt, code, iss) {
|
|
133
|
+
try {
|
|
134
|
+
const result = await authorizeMcp(attempt.provider, {
|
|
135
|
+
serverUrl: attempt.options.serverUrl,
|
|
136
|
+
authorizationCode: code,
|
|
137
|
+
iss,
|
|
138
|
+
signal: AbortSignal.any([attempt.controller.signal, AbortSignal.timeout(30_000)]),
|
|
139
|
+
});
|
|
140
|
+
if (result !== "AUTHORIZED" || !attempt.store.load()?.tokens?.access_token)
|
|
141
|
+
throw new Error("No OAuth grant returned.");
|
|
142
|
+
await withMcpAuthStoreLock(getMcpAuthStorePath(attempt.options), () => {
|
|
143
|
+
attempt.controller.signal.throwIfAborted();
|
|
144
|
+
const saved = { ...attempt.store.load() };
|
|
145
|
+
delete saved.codeVerifier;
|
|
146
|
+
delete saved.oauthState;
|
|
147
|
+
const credentials = readMcpAuthStore(attempt.options);
|
|
148
|
+
credentials[mcpAuthStoreKey(attempt.options.name, attempt.options.serverUrl)] = saved;
|
|
149
|
+
writeMcpAuthStore(credentials, attempt.options);
|
|
150
|
+
}, attempt.controller.signal);
|
|
151
|
+
attempt.status = { status: "complete" };
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
attempt.status = { status: "error", error: "Cannot complete MCP OAuth sign-in. Start sign-in again." };
|
|
155
|
+
throw new Error(attempt.status.error);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
attempt.server.close();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export function cancelMcpOAuthLogin(id) {
|
|
162
|
+
const attempt = attempts.get(id);
|
|
163
|
+
if (!attempt)
|
|
164
|
+
return;
|
|
165
|
+
attempt.controller.abort();
|
|
166
|
+
attempt.server.close();
|
|
167
|
+
clearTimeout(attempt.timer);
|
|
168
|
+
attempts.delete(id);
|
|
169
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import * as login from "./mcp-oauth-login.js";
|
|
2
|
+
/** One controller per requesting ClientSession; IDs never authorize another client. */
|
|
3
|
+
export class McpOAuthUi {
|
|
4
|
+
service;
|
|
5
|
+
attempts = new Map();
|
|
6
|
+
closed = false;
|
|
7
|
+
constructor(service = login) {
|
|
8
|
+
this.service = service;
|
|
9
|
+
}
|
|
10
|
+
async handle(msg, context) {
|
|
11
|
+
const send = (status, extra = {}) => {
|
|
12
|
+
if (!this.closed)
|
|
13
|
+
context.send({ type: "mcp_oauth_result", reqId: msg.reqId, status, ...extra });
|
|
14
|
+
};
|
|
15
|
+
let ownedAttempt;
|
|
16
|
+
try {
|
|
17
|
+
if (msg.type === "mcp_oauth_start") {
|
|
18
|
+
for (const [key, attempt] of this.attempts) {
|
|
19
|
+
if (attempt.completed) {
|
|
20
|
+
if (attempt.id)
|
|
21
|
+
this.service.cancelMcpOAuthLogin(attempt.id);
|
|
22
|
+
this.attempts.delete(key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (this.closed || this.attempts.has(msg.reqId) || this.attempts.size >= 8) {
|
|
26
|
+
send("error", { error: "Unable to start MCP authorization. Please try again." });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const serverUrl = context.resolveServer(msg.name, msg.scope);
|
|
30
|
+
if (!serverUrl || !["https:", "http:"].includes(new URL(serverUrl).protocol))
|
|
31
|
+
throw new Error();
|
|
32
|
+
const attempt = { cwd: context.cwd, cancelled: false };
|
|
33
|
+
this.attempts.set(msg.reqId, attempt);
|
|
34
|
+
try {
|
|
35
|
+
const result = await this.service.startMcpOAuthLogin({
|
|
36
|
+
agentDir: context.agentDir,
|
|
37
|
+
name: msg.name,
|
|
38
|
+
serverUrl,
|
|
39
|
+
});
|
|
40
|
+
attempt.id = result.id;
|
|
41
|
+
if (attempt.cancelled || this.closed) {
|
|
42
|
+
this.service.cancelMcpOAuthLogin(result.id);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const url = new URL(result.authorizationUrl);
|
|
46
|
+
if (!["https:", "http:"].includes(url.protocol))
|
|
47
|
+
throw new Error();
|
|
48
|
+
send("pending", result);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
if (attempt.id)
|
|
52
|
+
this.service.cancelMcpOAuthLogin(attempt.id);
|
|
53
|
+
this.attempts.delete(msg.reqId);
|
|
54
|
+
if (!attempt.cancelled)
|
|
55
|
+
send("error", { error: "Unable to start MCP authorization. Please try again." });
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const attempt = this.attempts.get(msg.reqId);
|
|
60
|
+
if (!attempt || (msg.type !== "mcp_oauth_cancel" && attempt.id !== msg.id)) {
|
|
61
|
+
send("error", { error: "MCP authorization failed. Please start a new login." });
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
ownedAttempt = attempt;
|
|
65
|
+
if (msg.type === "mcp_oauth_cancel") {
|
|
66
|
+
attempt.cancelled = true;
|
|
67
|
+
if (attempt.id)
|
|
68
|
+
this.service.cancelMcpOAuthLogin(attempt.id);
|
|
69
|
+
this.attempts.delete(msg.reqId);
|
|
70
|
+
send("cancelled");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (!attempt.id || attempt.cancelled)
|
|
74
|
+
throw new Error();
|
|
75
|
+
if (attempt.completed) {
|
|
76
|
+
send("complete", { id: attempt.id });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!attempt.finishing) {
|
|
80
|
+
if (msg.type === "mcp_oauth_finish") {
|
|
81
|
+
if (!msg.redirectUrl || msg.redirectUrl.length > 16384)
|
|
82
|
+
throw new Error();
|
|
83
|
+
attempt.finishing = (async () => {
|
|
84
|
+
await this.service.finishMcpOAuthLogin({ id: attempt.id, redirectUrl: msg.redirectUrl });
|
|
85
|
+
if (!attempt.cancelled && !this.closed)
|
|
86
|
+
await context.reload(attempt.cwd);
|
|
87
|
+
})();
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const status = this.service.getMcpOAuthLoginStatus(attempt.id);
|
|
91
|
+
if (status.status === "error")
|
|
92
|
+
throw new Error();
|
|
93
|
+
if (status.status === "pending") {
|
|
94
|
+
send("pending", { id: attempt.id });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
attempt.finishing = context.reload(attempt.cwd);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
await attempt.finishing;
|
|
101
|
+
if (attempt.cancelled || this.closed)
|
|
102
|
+
return;
|
|
103
|
+
attempt.completed = true;
|
|
104
|
+
send("complete", { id: attempt.id });
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
const attempt = this.attempts.get(msg.reqId);
|
|
108
|
+
if (ownedAttempt?.cancelled || this.closed)
|
|
109
|
+
return;
|
|
110
|
+
let recoverable = false;
|
|
111
|
+
try {
|
|
112
|
+
recoverable = !!attempt?.id && this.service.getMcpOAuthLoginStatus(attempt.id).status === "pending";
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
/* Expired attempts are terminal. */
|
|
116
|
+
}
|
|
117
|
+
if ((msg.type === "mcp_oauth_finish" || msg.type === "mcp_oauth_status") && attempt?.id && recoverable) {
|
|
118
|
+
attempt.finishing = undefined;
|
|
119
|
+
send("pending", { id: attempt.id, error: "Invalid redirect URL. Paste the full callback URL and try again." });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (attempt?.id)
|
|
123
|
+
this.service.cancelMcpOAuthLogin(attempt.id);
|
|
124
|
+
this.attempts.delete(msg.reqId);
|
|
125
|
+
send("error", { error: "MCP authorization failed. Please start a new login." });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
dispose() {
|
|
129
|
+
this.closed = true;
|
|
130
|
+
for (const attempt of this.attempts.values()) {
|
|
131
|
+
attempt.cancelled = true;
|
|
132
|
+
if (attempt.id)
|
|
133
|
+
this.service.cancelMcpOAuthLogin(attempt.id);
|
|
134
|
+
}
|
|
135
|
+
this.attempts.clear();
|
|
136
|
+
}
|
|
137
|
+
}
|