pi-soly 1.9.3 → 1.11.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/ask/index.ts +12 -11
- package/ask/picker.ts +356 -76
- package/ask/prompt.ts +6 -2
- package/ask/tests/picker.test.ts +273 -82
- package/codemap.ts +276 -0
- package/hotreload.ts +239 -0
- package/init.ts +302 -0
- package/mcp/CHANGELOG.md +384 -0
- package/mcp/LICENSE +21 -0
- package/mcp/OAUTH.md +355 -0
- package/mcp/README.md +410 -0
- package/mcp/__tests__/agent-dir-paths.upstream-test.ts +80 -0
- package/mcp/__tests__/cli.upstream-test.ts +97 -0
- package/mcp/__tests__/commands-onboarding.upstream-test.ts +157 -0
- package/mcp/__tests__/config.upstream-test.ts +303 -0
- package/mcp/__tests__/consent-manager.upstream-test.ts +151 -0
- package/mcp/__tests__/direct-tools-auto-auth.upstream-test.ts +218 -0
- package/mcp/__tests__/direct-tools.upstream-test.ts +299 -0
- package/mcp/__tests__/elicitation-handler.upstream-test.ts +346 -0
- package/mcp/__tests__/elicitation-sdk-integration.upstream-test.ts +140 -0
- package/mcp/__tests__/errors.upstream-test.ts +218 -0
- package/mcp/__tests__/fixtures/elicitation-server.mjs +98 -0
- package/mcp/__tests__/host-html-template.upstream-test.ts +278 -0
- package/mcp/__tests__/index-lifecycle.upstream-test.ts +484 -0
- package/mcp/__tests__/init-elicitation.upstream-test.ts +86 -0
- package/mcp/__tests__/interactive-visualizer-server.upstream-test.ts +28 -0
- package/mcp/__tests__/logger.upstream-test.ts +175 -0
- package/mcp/__tests__/mcp-auth-flow-client-credentials.upstream-test.ts +612 -0
- package/mcp/__tests__/mcp-auth-storage.upstream-test.ts +47 -0
- package/mcp/__tests__/mcp-callback-server-unref.upstream-test.ts +264 -0
- package/mcp/__tests__/mcp-oauth-provider.upstream-test.ts +216 -0
- package/mcp/__tests__/mcp-panel-auth.upstream-test.ts +206 -0
- package/mcp/__tests__/mcp-panel-exclude-tools.upstream-test.ts +69 -0
- package/mcp/__tests__/mcp-panel-keybindings.upstream-test.ts +186 -0
- package/mcp/__tests__/npx-resolver.upstream-test.ts +54 -0
- package/mcp/__tests__/oauth-handler.upstream-test.ts +77 -0
- package/mcp/__tests__/onboarding-state.upstream-test.ts +52 -0
- package/mcp/__tests__/package-manifest.upstream-test.ts +22 -0
- package/mcp/__tests__/proxy-modes-auto-auth.upstream-test.ts +253 -0
- package/mcp/__tests__/proxy-modes-discovery.upstream-test.ts +84 -0
- package/mcp/__tests__/proxy-modes-manual-auth.upstream-test.ts +91 -0
- package/mcp/__tests__/proxy-modes-ui-messages.upstream-test.ts +72 -0
- package/mcp/__tests__/sampling-handler.upstream-test.ts +285 -0
- package/mcp/__tests__/server-manager-http-auth.upstream-test.ts +144 -0
- package/mcp/__tests__/server-manager-sampling.upstream-test.ts +236 -0
- package/mcp/__tests__/tool-metadata.upstream-test.ts +111 -0
- package/mcp/__tests__/tool-result-renderer.upstream-test.ts +147 -0
- package/mcp/__tests__/ui-integration.upstream-test.ts +551 -0
- package/mcp/__tests__/ui-resource-handler.upstream-test.ts +303 -0
- package/mcp/__tests__/ui-server.upstream-test.ts +967 -0
- package/mcp/__tests__/ui-session-messages.upstream-test.ts +72 -0
- package/mcp/__tests__/ui-streaming.upstream-test.ts +543 -0
- package/mcp/agent-dir.ts +20 -0
- package/mcp/app-bridge.bundle.js +67 -0
- package/mcp/cli.js +184 -0
- package/mcp/commands.ts +422 -0
- package/mcp/config.ts +666 -0
- package/mcp/consent-manager.ts +64 -0
- package/mcp/direct-tools.ts +439 -0
- package/mcp/elicitation-handler.ts +347 -0
- package/mcp/errors.ts +219 -0
- package/mcp/glimpse-ui.ts +80 -0
- package/mcp/host-html-template.ts +427 -0
- package/mcp/index.ts +362 -0
- package/mcp/init.ts +362 -0
- package/mcp/lifecycle.ts +93 -0
- package/mcp/logger.ts +169 -0
- package/mcp/mcp-auth-flow.ts +559 -0
- package/mcp/mcp-auth-flow.upstream-test.ts +259 -0
- package/mcp/mcp-auth.ts +302 -0
- package/mcp/mcp-auth.upstream-test.ts +373 -0
- package/mcp/mcp-callback-server.ts +372 -0
- package/mcp/mcp-callback-server.upstream-test.ts +416 -0
- package/mcp/mcp-oauth-provider.ts +369 -0
- package/mcp/mcp-oauth-provider.upstream-test.ts +518 -0
- package/mcp/mcp-panel.ts +829 -0
- package/mcp/mcp-setup-panel.ts +580 -0
- package/mcp/metadata-cache.ts +201 -0
- package/mcp/notify.ts +111 -0
- package/mcp/npx-resolver.ts +424 -0
- package/mcp/oauth-handler.ts +57 -0
- package/mcp/onboarding-state.ts +68 -0
- package/mcp/package.json +106 -0
- package/mcp/panel-keys.ts +37 -0
- package/mcp/proxy-modes.ts +949 -0
- package/mcp/resource-tools.ts +17 -0
- package/mcp/sampling-handler.ts +268 -0
- package/mcp/server-manager.ts +545 -0
- package/mcp/state.ts +41 -0
- package/mcp/tool-metadata.ts +216 -0
- package/mcp/tool-registrar.ts +46 -0
- package/mcp/tool-result-renderer.ts +161 -0
- package/mcp/types.ts +448 -0
- package/mcp/ui-resource-handler.ts +146 -0
- package/mcp/ui-server.ts +623 -0
- package/mcp/ui-session.ts +386 -0
- package/mcp/ui-stream-types.ts +89 -0
- package/mcp/utils.ts +129 -0
- package/mcp/vitest.config.ts +14 -0
- package/migrate.ts +258 -0
- package/notification.ts +218 -0
- package/notifications-log.ts +83 -0
- package/package.json +20 -3
- package/status.ts +140 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// npx-resolver.ts - Resolve npx/npm exec binaries to avoid npm parent processes
|
|
2
|
+
import { existsSync, readFileSync, realpathSync, readdirSync, statSync, writeFileSync, renameSync, mkdirSync, openSync, readSync, closeSync } from "node:fs";
|
|
3
|
+
import { join, dirname, extname, resolve, sep } from "node:path";
|
|
4
|
+
import { getAgentPath } from "./agent-dir.ts";
|
|
5
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
6
|
+
|
|
7
|
+
const CACHE_VERSION = 1;
|
|
8
|
+
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
9
|
+
|
|
10
|
+
interface NpxCacheEntry {
|
|
11
|
+
resolvedBin: string;
|
|
12
|
+
resolvedAt: number;
|
|
13
|
+
packageVersion?: string;
|
|
14
|
+
isJs: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface NpxCache {
|
|
18
|
+
version: number;
|
|
19
|
+
entries: Record<string, NpxCacheEntry>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface NpxResolution {
|
|
23
|
+
binPath: string;
|
|
24
|
+
extraArgs: string[];
|
|
25
|
+
isJs: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ParsedInvocation {
|
|
29
|
+
packageSpec: string;
|
|
30
|
+
binName?: string;
|
|
31
|
+
extraArgs: string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function resolveNpxBinary(
|
|
35
|
+
command: string,
|
|
36
|
+
args: string[]
|
|
37
|
+
): Promise<NpxResolution | null> {
|
|
38
|
+
const parsed = command === "npx"
|
|
39
|
+
? parseNpxArgs(args)
|
|
40
|
+
: command === "npm"
|
|
41
|
+
? parseNpmExecArgs(args)
|
|
42
|
+
: null;
|
|
43
|
+
|
|
44
|
+
if (!parsed) return null;
|
|
45
|
+
|
|
46
|
+
const cacheKey = JSON.stringify([command, ...args]);
|
|
47
|
+
const cache = loadCache();
|
|
48
|
+
const cached = cache?.entries?.[cacheKey];
|
|
49
|
+
|
|
50
|
+
if (cached && Date.now() - cached.resolvedAt < CACHE_TTL_MS && existsSync(cached.resolvedBin)) {
|
|
51
|
+
return { binPath: cached.resolvedBin, extraArgs: parsed.extraArgs, isJs: cached.isJs };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const resolved = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
|
|
55
|
+
if (resolved) {
|
|
56
|
+
saveCacheEntry(cacheKey, resolved);
|
|
57
|
+
return { binPath: resolved.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolved.isJs };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Slow path: force npx cache population
|
|
61
|
+
await forceNpxCache(parsed.packageSpec);
|
|
62
|
+
const resolvedAfterInstall = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
|
|
63
|
+
if (resolvedAfterInstall) {
|
|
64
|
+
saveCacheEntry(cacheKey, resolvedAfterInstall);
|
|
65
|
+
return { binPath: resolvedAfterInstall.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolvedAfterInstall.isJs };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseNpxArgs(args: string[]): ParsedInvocation | null {
|
|
72
|
+
const separatorIndex = args.indexOf("--");
|
|
73
|
+
const before = separatorIndex >= 0 ? args.slice(0, separatorIndex) : args;
|
|
74
|
+
const after = separatorIndex >= 0 ? args.slice(separatorIndex + 1) : [];
|
|
75
|
+
|
|
76
|
+
const positionals: string[] = [];
|
|
77
|
+
let packageSpec: string | undefined;
|
|
78
|
+
let sawPackageFlag = false;
|
|
79
|
+
let foundFirstPositional = false;
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < before.length; i++) {
|
|
82
|
+
const arg = before[i];
|
|
83
|
+
if (foundFirstPositional) {
|
|
84
|
+
positionals.push(arg);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (arg === "-y" || arg === "--yes") continue;
|
|
88
|
+
if (arg === "-p" || arg === "--package") {
|
|
89
|
+
const value = before[i + 1];
|
|
90
|
+
if (!value || value.startsWith("-")) return null;
|
|
91
|
+
if (!packageSpec) packageSpec = value;
|
|
92
|
+
sawPackageFlag = true;
|
|
93
|
+
i++;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (arg.startsWith("--package=")) {
|
|
97
|
+
const value = arg.slice("--package=".length);
|
|
98
|
+
if (!value) return null;
|
|
99
|
+
if (!packageSpec) packageSpec = value;
|
|
100
|
+
sawPackageFlag = true;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (arg.startsWith("-")) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
positionals.push(arg);
|
|
107
|
+
foundFirstPositional = true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (sawPackageFlag) {
|
|
111
|
+
const binName = positionals[0];
|
|
112
|
+
if (!packageSpec || !binName) return null;
|
|
113
|
+
const extraArgs = positionals.slice(1).concat(after);
|
|
114
|
+
return { packageSpec, binName, extraArgs };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const packagePositional = positionals[0];
|
|
118
|
+
if (!packagePositional) return null;
|
|
119
|
+
const extraArgs = positionals.slice(1).concat(after);
|
|
120
|
+
return { packageSpec: packagePositional, extraArgs };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function parseNpmExecArgs(args: string[]): ParsedInvocation | null {
|
|
124
|
+
if (args[0] !== "exec") return null;
|
|
125
|
+
const execArgs = args.slice(1);
|
|
126
|
+
const separatorIndex = execArgs.indexOf("--");
|
|
127
|
+
if (separatorIndex < 0) return null;
|
|
128
|
+
|
|
129
|
+
const before = execArgs.slice(0, separatorIndex);
|
|
130
|
+
const after = execArgs.slice(separatorIndex + 1);
|
|
131
|
+
|
|
132
|
+
let packageSpec: string | undefined;
|
|
133
|
+
for (let i = 0; i < before.length; i++) {
|
|
134
|
+
const arg = before[i];
|
|
135
|
+
if (arg === "-y" || arg === "--yes") continue;
|
|
136
|
+
if (arg === "--package") {
|
|
137
|
+
const value = before[i + 1];
|
|
138
|
+
if (!value || value.startsWith("-")) return null;
|
|
139
|
+
if (!packageSpec) packageSpec = value;
|
|
140
|
+
i++;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (arg.startsWith("--package=")) {
|
|
144
|
+
const value = arg.slice("--package=".length);
|
|
145
|
+
if (!value) return null;
|
|
146
|
+
if (!packageSpec) packageSpec = value;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (arg.startsWith("-")) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const binName = after[0];
|
|
155
|
+
if (!packageSpec || !binName) return null;
|
|
156
|
+
const extraArgs = after.slice(1);
|
|
157
|
+
return { packageSpec, binName, extraArgs };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function resolveFromNpmCache(packageSpec: string, binName?: string): NpxCacheEntry | null {
|
|
161
|
+
const cacheDir = getNpmCacheDir();
|
|
162
|
+
if (!cacheDir) return null;
|
|
163
|
+
|
|
164
|
+
const packageName = extractPackageName(packageSpec);
|
|
165
|
+
if (!packageName) return null;
|
|
166
|
+
|
|
167
|
+
const packageDir = findCachedPackageDir(cacheDir, packageName);
|
|
168
|
+
if (!packageDir) return null;
|
|
169
|
+
|
|
170
|
+
const packageJsonPath = join(packageDir, "package.json");
|
|
171
|
+
if (!existsSync(packageJsonPath)) return null;
|
|
172
|
+
|
|
173
|
+
let pkg: { bin?: string | Record<string, string>; version?: string } | null = null;
|
|
174
|
+
try {
|
|
175
|
+
pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as {
|
|
176
|
+
bin?: string | Record<string, string>;
|
|
177
|
+
version?: string;
|
|
178
|
+
};
|
|
179
|
+
} catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const binField = pkg?.bin;
|
|
184
|
+
if (!binField) return null;
|
|
185
|
+
|
|
186
|
+
const candidates = buildBinCandidates(packageName, binName);
|
|
187
|
+
let chosenBinName: string | undefined;
|
|
188
|
+
let binRel: string | undefined;
|
|
189
|
+
|
|
190
|
+
if (typeof binField === "string") {
|
|
191
|
+
chosenBinName = defaultBinName(packageName);
|
|
192
|
+
binRel = binField;
|
|
193
|
+
} else {
|
|
194
|
+
for (const candidate of candidates) {
|
|
195
|
+
if (binField[candidate]) {
|
|
196
|
+
chosenBinName = candidate;
|
|
197
|
+
binRel = binField[candidate];
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (!binRel) {
|
|
202
|
+
const firstEntry = Object.entries(binField)[0];
|
|
203
|
+
if (firstEntry) {
|
|
204
|
+
chosenBinName = firstEntry[0];
|
|
205
|
+
binRel = firstEntry[1];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!binRel) return null;
|
|
211
|
+
|
|
212
|
+
const nodeModulesDir = findNodeModulesDir(packageDir);
|
|
213
|
+
const binLink = chosenBinName ? join(nodeModulesDir, ".bin", chosenBinName) : null;
|
|
214
|
+
let resolvedBin = binLink && existsSync(binLink) ? safeRealpath(binLink) : "";
|
|
215
|
+
if (!resolvedBin) {
|
|
216
|
+
resolvedBin = resolve(packageDir, binRel);
|
|
217
|
+
if (!existsSync(resolvedBin)) return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const isJs = detectJsBinary(resolvedBin);
|
|
221
|
+
return {
|
|
222
|
+
resolvedBin,
|
|
223
|
+
resolvedAt: Date.now(),
|
|
224
|
+
packageVersion: pkg?.version,
|
|
225
|
+
isJs,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const FORCE_CACHE_TIMEOUT_MS = 30_000;
|
|
230
|
+
|
|
231
|
+
async function forceNpxCache(packageSpec: string): Promise<void> {
|
|
232
|
+
try {
|
|
233
|
+
await new Promise<void>((resolve, reject) => {
|
|
234
|
+
const proc = spawn(
|
|
235
|
+
"npm",
|
|
236
|
+
["exec", "--yes", "--package", packageSpec, "--", "node", "-e", "1"],
|
|
237
|
+
{ stdio: "ignore" }
|
|
238
|
+
);
|
|
239
|
+
const timer = setTimeout(() => {
|
|
240
|
+
proc.kill();
|
|
241
|
+
reject(new Error("timeout"));
|
|
242
|
+
}, FORCE_CACHE_TIMEOUT_MS);
|
|
243
|
+
timer.unref();
|
|
244
|
+
proc.on("close", () => { clearTimeout(timer); resolve(); });
|
|
245
|
+
proc.on("error", (err) => { clearTimeout(timer); reject(err); });
|
|
246
|
+
});
|
|
247
|
+
} catch {
|
|
248
|
+
// Ignore failures, resolution will fall back to original command
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function buildBinCandidates(packageName: string, explicitBin?: string): string[] {
|
|
253
|
+
const candidates: string[] = [];
|
|
254
|
+
if (explicitBin) candidates.push(explicitBin);
|
|
255
|
+
|
|
256
|
+
if (packageName.startsWith("@")) {
|
|
257
|
+
const namePart = packageName.split("/")[1] ?? "";
|
|
258
|
+
const scopePart = packageName.split("/")[0]?.replace("@", "") ?? "";
|
|
259
|
+
if (namePart) candidates.push(namePart);
|
|
260
|
+
if (scopePart && namePart) candidates.push(`${scopePart}-${namePart}`);
|
|
261
|
+
} else {
|
|
262
|
+
candidates.push(packageName);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return [...new Set(candidates.filter(Boolean))];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function extractPackageName(spec: string): string | null {
|
|
269
|
+
const trimmed = spec.trim();
|
|
270
|
+
if (!trimmed) return null;
|
|
271
|
+
if (trimmed.startsWith("@")) {
|
|
272
|
+
const slashIndex = trimmed.indexOf("/");
|
|
273
|
+
if (slashIndex < 0) return null;
|
|
274
|
+
const atIndex = trimmed.lastIndexOf("@");
|
|
275
|
+
if (atIndex > slashIndex) {
|
|
276
|
+
return trimmed.slice(0, atIndex);
|
|
277
|
+
}
|
|
278
|
+
return trimmed;
|
|
279
|
+
}
|
|
280
|
+
const atIndex = trimmed.indexOf("@");
|
|
281
|
+
return atIndex >= 0 ? trimmed.slice(0, atIndex) : trimmed;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function defaultBinName(packageName: string): string {
|
|
285
|
+
if (packageName.startsWith("@")) {
|
|
286
|
+
const parts = packageName.split("/");
|
|
287
|
+
return parts[1] ?? packageName.replace("@", "").replace("/", "-");
|
|
288
|
+
}
|
|
289
|
+
return packageName;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function findCachedPackageDir(cacheDir: string, packageName: string): string | null {
|
|
293
|
+
const npxDir = join(cacheDir, "_npx");
|
|
294
|
+
if (!existsSync(npxDir)) return null;
|
|
295
|
+
|
|
296
|
+
const packagePathParts = packageName.startsWith("@")
|
|
297
|
+
? packageName.split("/")
|
|
298
|
+
: [packageName];
|
|
299
|
+
|
|
300
|
+
const candidates = readdirSync(npxDir, { withFileTypes: true })
|
|
301
|
+
.filter(entry => entry.isDirectory())
|
|
302
|
+
.map(entry => {
|
|
303
|
+
const full = join(npxDir, entry.name);
|
|
304
|
+
const mtime = safeStatMtime(full);
|
|
305
|
+
return { name: entry.name, mtime };
|
|
306
|
+
})
|
|
307
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
308
|
+
|
|
309
|
+
for (const entry of candidates) {
|
|
310
|
+
const pkgDir = join(npxDir, entry.name, "node_modules", ...packagePathParts);
|
|
311
|
+
if (existsSync(join(pkgDir, "package.json"))) {
|
|
312
|
+
return pkgDir;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function findNodeModulesDir(packageDir: string): string {
|
|
320
|
+
const parts = packageDir.split(sep);
|
|
321
|
+
const idx = parts.lastIndexOf("node_modules");
|
|
322
|
+
if (idx >= 0) {
|
|
323
|
+
return parts.slice(0, idx + 1).join(sep);
|
|
324
|
+
}
|
|
325
|
+
return join(packageDir, "..");
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function detectJsBinary(binPath: string): boolean {
|
|
329
|
+
const ext = extname(binPath).toLowerCase();
|
|
330
|
+
if (ext === ".js" || ext === ".mjs" || ext === ".cjs") return true;
|
|
331
|
+
try {
|
|
332
|
+
const fd = openSync(binPath, "r");
|
|
333
|
+
try {
|
|
334
|
+
const buf = Buffer.alloc(256);
|
|
335
|
+
readSync(fd, buf, 0, 256, 0);
|
|
336
|
+
const firstLine = buf.toString("utf-8").split("\n")[0] ?? "";
|
|
337
|
+
return firstLine.startsWith("#!") && firstLine.includes("node");
|
|
338
|
+
} finally {
|
|
339
|
+
closeSync(fd);
|
|
340
|
+
}
|
|
341
|
+
} catch {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let npmCacheDirCached: string | null | undefined;
|
|
347
|
+
|
|
348
|
+
function getNpmCacheDir(): string | null {
|
|
349
|
+
if (npmCacheDirCached !== undefined) return npmCacheDirCached;
|
|
350
|
+
if (process.env.NPM_CONFIG_CACHE) {
|
|
351
|
+
npmCacheDirCached = process.env.NPM_CONFIG_CACHE;
|
|
352
|
+
return npmCacheDirCached;
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
const result = spawnSync("npm", ["config", "get", "cache"], { encoding: "utf-8" });
|
|
356
|
+
if (result.status === 0) {
|
|
357
|
+
const path = String(result.stdout).trim();
|
|
358
|
+
npmCacheDirCached = path || null;
|
|
359
|
+
return npmCacheDirCached;
|
|
360
|
+
}
|
|
361
|
+
} catch {
|
|
362
|
+
npmCacheDirCached = null;
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
npmCacheDirCached = null;
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function getNpxCachePath(): string {
|
|
370
|
+
return getAgentPath("mcp-npx-cache.json");
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function loadCache(): NpxCache | null {
|
|
374
|
+
const cachePath = getNpxCachePath();
|
|
375
|
+
if (!existsSync(cachePath)) return null;
|
|
376
|
+
try {
|
|
377
|
+
const raw = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
378
|
+
if (!raw || typeof raw !== "object") return null;
|
|
379
|
+
if (raw.version !== CACHE_VERSION) return null;
|
|
380
|
+
if (!raw.entries || typeof raw.entries !== "object") return null;
|
|
381
|
+
return raw as NpxCache;
|
|
382
|
+
} catch {
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function saveCacheEntry(key: string, entry: NpxCacheEntry): void {
|
|
388
|
+
const cachePath = getNpxCachePath();
|
|
389
|
+
const dir = dirname(cachePath);
|
|
390
|
+
mkdirSync(dir, { recursive: true });
|
|
391
|
+
|
|
392
|
+
let merged: NpxCache = { version: CACHE_VERSION, entries: {} };
|
|
393
|
+
try {
|
|
394
|
+
if (existsSync(cachePath)) {
|
|
395
|
+
const existing = JSON.parse(readFileSync(cachePath, "utf-8")) as NpxCache;
|
|
396
|
+
if (existing && existing.version === CACHE_VERSION && existing.entries) {
|
|
397
|
+
merged.entries = { ...existing.entries };
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
} catch {
|
|
401
|
+
// Ignore parse errors
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
merged.entries[key] = entry;
|
|
405
|
+
const tmpPath = `${cachePath}.${process.pid}.tmp`;
|
|
406
|
+
writeFileSync(tmpPath, JSON.stringify(merged, null, 2), "utf-8");
|
|
407
|
+
renameSync(tmpPath, cachePath);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function safeRealpath(path: string): string {
|
|
411
|
+
try {
|
|
412
|
+
return realpathSync(path);
|
|
413
|
+
} catch {
|
|
414
|
+
return "";
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function safeStatMtime(path: string): number {
|
|
419
|
+
try {
|
|
420
|
+
return statSync(path).mtimeMs;
|
|
421
|
+
} catch {
|
|
422
|
+
return 0;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// oauth-handler.ts - OAuth token management for MCP servers
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import type { OAuthTokens } from "@modelcontextprotocol/sdk/shared/auth.js";
|
|
4
|
+
import { getAuthEntryFilePath } from "./mcp-auth.ts";
|
|
5
|
+
|
|
6
|
+
// Token storage path for a server
|
|
7
|
+
function getTokensPath(serverName: string): string {
|
|
8
|
+
return getAuthEntryFilePath(serverName);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get stored OAuth tokens for a server (if any).
|
|
13
|
+
* Returns undefined if no tokens or tokens are expired.
|
|
14
|
+
*
|
|
15
|
+
* Token file location: $MCP_OAUTH_DIR/sha256-<server-hash>/tokens.json when set,
|
|
16
|
+
* otherwise <Pi agent dir>/mcp-oauth/sha256-<server-hash>/tokens.json
|
|
17
|
+
*
|
|
18
|
+
* Expected format:
|
|
19
|
+
* {
|
|
20
|
+
* "access_token": "...",
|
|
21
|
+
* "token_type": "bearer",
|
|
22
|
+
* "refresh_token": "...", // optional
|
|
23
|
+
* "expires_in": 3600, // optional, seconds
|
|
24
|
+
* "expiresAt": 1234567890 // optional, absolute timestamp ms
|
|
25
|
+
* }
|
|
26
|
+
*/
|
|
27
|
+
export function getStoredTokens(serverName: string): OAuthTokens | undefined {
|
|
28
|
+
const tokensPath = getTokensPath(serverName);
|
|
29
|
+
|
|
30
|
+
if (!existsSync(tokensPath)) return undefined;
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const stored = JSON.parse(readFileSync(tokensPath, "utf-8"));
|
|
34
|
+
|
|
35
|
+
// Validate required field
|
|
36
|
+
if (!stored.access_token || typeof stored.access_token !== "string") {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check expiration if expiresAt is set
|
|
41
|
+
if (stored.expiresAt && typeof stored.expiresAt === "number") {
|
|
42
|
+
if (Date.now() > stored.expiresAt) {
|
|
43
|
+
// Token expired
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
access_token: stored.access_token,
|
|
50
|
+
token_type: stored.token_type ?? "bearer",
|
|
51
|
+
refresh_token: stored.refresh_token,
|
|
52
|
+
expires_in: stored.expires_in,
|
|
53
|
+
};
|
|
54
|
+
} catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { getAgentPath } from "./agent-dir.ts";
|
|
4
|
+
|
|
5
|
+
export interface McpOnboardingState {
|
|
6
|
+
version: 1;
|
|
7
|
+
sharedConfigHintShown: boolean;
|
|
8
|
+
setupCompleted: boolean;
|
|
9
|
+
lastDiscoveryFingerprint?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEFAULT_STATE: McpOnboardingState = {
|
|
13
|
+
version: 1,
|
|
14
|
+
sharedConfigHintShown: false,
|
|
15
|
+
setupCompleted: false,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function getOnboardingStatePath(): string {
|
|
19
|
+
return getAgentPath("mcp-onboarding.json");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function loadOnboardingState(): McpOnboardingState {
|
|
23
|
+
const path = getOnboardingStatePath();
|
|
24
|
+
if (!existsSync(path)) return { ...DEFAULT_STATE };
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const raw = JSON.parse(readFileSync(path, "utf-8")) as Partial<McpOnboardingState>;
|
|
28
|
+
if (!raw || typeof raw !== "object") return { ...DEFAULT_STATE };
|
|
29
|
+
return {
|
|
30
|
+
version: 1,
|
|
31
|
+
sharedConfigHintShown: raw.sharedConfigHintShown === true,
|
|
32
|
+
setupCompleted: raw.setupCompleted === true,
|
|
33
|
+
lastDiscoveryFingerprint: typeof raw.lastDiscoveryFingerprint === "string" ? raw.lastDiscoveryFingerprint : undefined,
|
|
34
|
+
};
|
|
35
|
+
} catch {
|
|
36
|
+
return { ...DEFAULT_STATE };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function saveOnboardingState(state: McpOnboardingState): void {
|
|
41
|
+
const path = getOnboardingStatePath();
|
|
42
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
43
|
+
const tmpPath = `${path}.${process.pid}.tmp`;
|
|
44
|
+
writeFileSync(tmpPath, `${JSON.stringify(state, null, 2)}\n`, "utf-8");
|
|
45
|
+
renameSync(tmpPath, path);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function updateOnboardingState(updater: (state: McpOnboardingState) => McpOnboardingState): McpOnboardingState {
|
|
49
|
+
const next = updater(loadOnboardingState());
|
|
50
|
+
saveOnboardingState(next);
|
|
51
|
+
return next;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function markSharedConfigHintShown(fingerprint?: string): McpOnboardingState {
|
|
55
|
+
return updateOnboardingState((state) => ({
|
|
56
|
+
...state,
|
|
57
|
+
sharedConfigHintShown: true,
|
|
58
|
+
lastDiscoveryFingerprint: fingerprint ?? state.lastDiscoveryFingerprint,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function markSetupCompleted(fingerprint?: string): McpOnboardingState {
|
|
63
|
+
return updateOnboardingState((state) => ({
|
|
64
|
+
...state,
|
|
65
|
+
setupCompleted: true,
|
|
66
|
+
lastDiscoveryFingerprint: fingerprint ?? state.lastDiscoveryFingerprint,
|
|
67
|
+
}));
|
|
68
|
+
}
|
package/mcp/package.json
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-mcp-adapter",
|
|
3
|
+
"version": "2.10.0",
|
|
4
|
+
"description": "MCP (Model Context Protocol) adapter extension for Pi coding agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Nico Bailon",
|
|
8
|
+
"bin": {
|
|
9
|
+
"pi-mcp-adapter": "cli.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"test:watch": "vitest",
|
|
14
|
+
"test:coverage": "vitest run --coverage",
|
|
15
|
+
"test:oauth-provider": "node --import tsx --test mcp-oauth-provider.test.ts"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/nicobailon/pi-mcp-adapter.git"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"pi-package",
|
|
23
|
+
"pi",
|
|
24
|
+
"mcp",
|
|
25
|
+
"model-context-protocol",
|
|
26
|
+
"ai",
|
|
27
|
+
"coding-agent",
|
|
28
|
+
"extension",
|
|
29
|
+
"claude",
|
|
30
|
+
"llm"
|
|
31
|
+
],
|
|
32
|
+
"pi": {
|
|
33
|
+
"extensions": [
|
|
34
|
+
"./index.ts"
|
|
35
|
+
],
|
|
36
|
+
"video": "https://github.com/nicobailon/pi-mcp-adapter/raw/refs/heads/main/pi-mcp.mp4"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"cli.js",
|
|
40
|
+
"agent-dir.ts",
|
|
41
|
+
"index.ts",
|
|
42
|
+
"state.ts",
|
|
43
|
+
"utils.ts",
|
|
44
|
+
"tool-metadata.ts",
|
|
45
|
+
"init.ts",
|
|
46
|
+
"ui-session.ts",
|
|
47
|
+
"proxy-modes.ts",
|
|
48
|
+
"direct-tools.ts",
|
|
49
|
+
"commands.ts",
|
|
50
|
+
"onboarding-state.ts",
|
|
51
|
+
"mcp-setup-panel.ts",
|
|
52
|
+
"types.ts",
|
|
53
|
+
"ui-stream-types.ts",
|
|
54
|
+
"config.ts",
|
|
55
|
+
"server-manager.ts",
|
|
56
|
+
"sampling-handler.ts",
|
|
57
|
+
"elicitation-handler.ts",
|
|
58
|
+
"tool-registrar.ts",
|
|
59
|
+
"tool-result-renderer.ts",
|
|
60
|
+
"resource-tools.ts",
|
|
61
|
+
"lifecycle.ts",
|
|
62
|
+
"metadata-cache.ts",
|
|
63
|
+
"host-html-template.ts",
|
|
64
|
+
"ui-resource-handler.ts",
|
|
65
|
+
"consent-manager.ts",
|
|
66
|
+
"ui-server.ts",
|
|
67
|
+
"glimpse-ui.ts",
|
|
68
|
+
"npx-resolver.ts",
|
|
69
|
+
"oauth-handler.ts",
|
|
70
|
+
"mcp-auth.ts",
|
|
71
|
+
"mcp-oauth-provider.ts",
|
|
72
|
+
"mcp-callback-server.ts",
|
|
73
|
+
"mcp-auth-flow.ts",
|
|
74
|
+
"mcp-panel.ts",
|
|
75
|
+
"panel-keys.ts",
|
|
76
|
+
"logger.ts",
|
|
77
|
+
"errors.ts",
|
|
78
|
+
"app-bridge.bundle.js",
|
|
79
|
+
"banner.png",
|
|
80
|
+
"README.md",
|
|
81
|
+
"CHANGELOG.md",
|
|
82
|
+
"LICENSE"
|
|
83
|
+
],
|
|
84
|
+
"dependencies": {
|
|
85
|
+
"@earendil-works/pi-ai": "^0.74.0",
|
|
86
|
+
"@earendil-works/pi-tui": "^0.74.0",
|
|
87
|
+
"@modelcontextprotocol/ext-apps": "^1.2.2",
|
|
88
|
+
"@modelcontextprotocol/sdk": "^1.25.1",
|
|
89
|
+
"open": "^10.2.0",
|
|
90
|
+
"recheck": "^4.5.0",
|
|
91
|
+
"typebox": "^1.1.24",
|
|
92
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
93
|
+
},
|
|
94
|
+
"peerDependencies": {
|
|
95
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
96
|
+
},
|
|
97
|
+
"devDependencies": {
|
|
98
|
+
"@earendil-works/pi-coding-agent": "^0.79.1",
|
|
99
|
+
"@types/bun": "^1.0.0",
|
|
100
|
+
"@types/node": "^20.0.0",
|
|
101
|
+
"@types/open": "^6.2.1",
|
|
102
|
+
"tsx": "^4.21.0",
|
|
103
|
+
"typescript": "^5.0.0",
|
|
104
|
+
"vitest": "^3.0.0"
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { matchesKey } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
/** The `tui.select.*` keybinding ids the adapter panels resolve. */
|
|
4
|
+
export type PanelSelectKeybinding =
|
|
5
|
+
| "tui.select.up"
|
|
6
|
+
| "tui.select.down"
|
|
7
|
+
| "tui.select.confirm";
|
|
8
|
+
|
|
9
|
+
/** Structural subset of pi-tui's `KeybindingsManager` (which satisfies it). */
|
|
10
|
+
export interface PanelKeybindings {
|
|
11
|
+
matches(data: string, keybinding: PanelSelectKeybinding): boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Key matchers for list navigation: user's `tui.select.*` bindings when a
|
|
16
|
+
* manager is provided, otherwise the previous hardcoded defaults.
|
|
17
|
+
*/
|
|
18
|
+
export interface PanelKeys {
|
|
19
|
+
selectUp(data: string): boolean;
|
|
20
|
+
selectDown(data: string): boolean;
|
|
21
|
+
selectConfirm(data: string): boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createPanelKeys(keybindings?: PanelKeybindings): PanelKeys {
|
|
25
|
+
if (keybindings) {
|
|
26
|
+
return {
|
|
27
|
+
selectUp: (data) => keybindings.matches(data, "tui.select.up"),
|
|
28
|
+
selectDown: (data) => keybindings.matches(data, "tui.select.down"),
|
|
29
|
+
selectConfirm: (data) => keybindings.matches(data, "tui.select.confirm"),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
selectUp: (data) => matchesKey(data, "up"),
|
|
34
|
+
selectDown: (data) => matchesKey(data, "down"),
|
|
35
|
+
selectConfirm: (data) => matchesKey(data, "return"),
|
|
36
|
+
};
|
|
37
|
+
}
|