pi-mcp-extension 1.16.4 → 1.16.5
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/package.json +1 -1
- package/src/config-compat.ts +61 -17
- package/src/config.ts +284 -6
- package/src/session-lifecycle.ts +77 -40
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-mcp-extension",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.5",
|
|
4
4
|
"description": "Full-featured, lightweight MCP extension for the Pi coding agent: stdio, SSE & Streamable HTTP transports, OAuth 2.1 (PKCE + DCR), lazy/eager lifecycles — minimal dependencies, zero bloat.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
package/src/config-compat.ts
CHANGED
|
@@ -188,28 +188,52 @@ function invalidProjectConfig(projectPath: string, detail: string): McpError {
|
|
|
188
188
|
return new McpError(`Invalid mcp.json at ${projectPath}:\n ${detail}`, "<config>", "config");
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
191
|
+
export type ProjectOverrideResolution =
|
|
192
|
+
| { ok: true; value: Record<string, unknown> }
|
|
193
|
+
| { ok: false; error: McpError };
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Return a config `McpError` when a partial project override sets keys outside
|
|
197
|
+
* `PROJECT_OVERRIDE_KEYS`, or `undefined` when all keys are allowed.
|
|
198
|
+
*/
|
|
199
|
+
export function validateProjectOverrideKeys(
|
|
200
|
+
name: string,
|
|
201
|
+
override: Record<string, unknown>,
|
|
202
|
+
projectPath: string,
|
|
203
|
+
): McpError | undefined {
|
|
204
|
+
const unsupported = Object.keys(override).filter(
|
|
205
|
+
(key) => !(PROJECT_OVERRIDE_KEYS as readonly string[]).includes(key),
|
|
206
|
+
);
|
|
207
|
+
if (unsupported.length === 0) return undefined;
|
|
208
|
+
return invalidProjectConfig(
|
|
209
|
+
projectPath,
|
|
210
|
+
`mcpServers.${name}: a project override can only set ${PROJECT_OVERRIDE_KEYS.join(", ")} (found ${unsupported.join(", ")})`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Resolve a partial project override onto the raw global entry, returning either the merged
|
|
216
|
+
* candidate or a config `McpError` without throwing.
|
|
217
|
+
*/
|
|
218
|
+
export function resolveProjectOverride(
|
|
193
219
|
name: string,
|
|
194
220
|
override: Record<string, unknown>,
|
|
195
221
|
globalServers: Record<string, unknown>,
|
|
196
222
|
projectPath: string,
|
|
197
|
-
):
|
|
223
|
+
): ProjectOverrideResolution {
|
|
198
224
|
const base = Object.hasOwn(globalServers, name) ? globalServers[name] : undefined;
|
|
199
225
|
if (!isPlainObject(base)) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
226
|
+
return {
|
|
227
|
+
ok: false,
|
|
228
|
+
error: invalidProjectConfig(
|
|
229
|
+
projectPath,
|
|
230
|
+
`mcpServers.${name}: a project override needs "command" or "url" in this file, or a global server of that name`,
|
|
231
|
+
),
|
|
232
|
+
};
|
|
204
233
|
}
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (unsupported.length > 0) {
|
|
209
|
-
throw invalidProjectConfig(
|
|
210
|
-
projectPath,
|
|
211
|
-
`mcpServers.${name}: a project override can only set ${PROJECT_OVERRIDE_KEYS.join(", ")} (found ${unsupported.join(", ")})`,
|
|
212
|
-
);
|
|
234
|
+
const keyError = validateProjectOverrideKeys(name, override, projectPath);
|
|
235
|
+
if (keyError) {
|
|
236
|
+
return { ok: false, error: keyError };
|
|
213
237
|
}
|
|
214
238
|
const merged: Record<string, unknown> = { ...base };
|
|
215
239
|
if (override.lifecycle !== undefined && override.enabled === undefined) {
|
|
@@ -220,7 +244,21 @@ function mergeProjectOverride(
|
|
|
220
244
|
merged[key] = override[key];
|
|
221
245
|
}
|
|
222
246
|
}
|
|
223
|
-
return merged;
|
|
247
|
+
return { ok: true, value: merged };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Merge a partial project override onto the raw global entry, keeping the global credentials. */
|
|
251
|
+
function mergeProjectOverride(
|
|
252
|
+
name: string,
|
|
253
|
+
override: Record<string, unknown>,
|
|
254
|
+
globalServers: Record<string, unknown>,
|
|
255
|
+
projectPath: string,
|
|
256
|
+
): Record<string, unknown> {
|
|
257
|
+
const resolution = resolveProjectOverride(name, override, globalServers, projectPath);
|
|
258
|
+
if (!resolution.ok) {
|
|
259
|
+
throw resolution.error;
|
|
260
|
+
}
|
|
261
|
+
return resolution.value;
|
|
224
262
|
}
|
|
225
263
|
|
|
226
264
|
/**
|
|
@@ -236,9 +274,15 @@ export function applyProjectOverrides(projectRaw: unknown, globalRaw: unknown, p
|
|
|
236
274
|
: {};
|
|
237
275
|
const servers: Record<string, unknown> = {};
|
|
238
276
|
for (const [name, value] of Object.entries(projectRaw.mcpServers)) {
|
|
239
|
-
|
|
277
|
+
const resolved = isProjectOverrideEntry(value)
|
|
240
278
|
? mergeProjectOverride(name, value, globalServers, projectPath)
|
|
241
279
|
: value;
|
|
280
|
+
Object.defineProperty(servers, name, {
|
|
281
|
+
value: resolved,
|
|
282
|
+
enumerable: true,
|
|
283
|
+
writable: true,
|
|
284
|
+
configurable: true,
|
|
285
|
+
});
|
|
242
286
|
}
|
|
243
287
|
return { ...projectRaw, mcpServers: servers };
|
|
244
288
|
}
|
package/src/config.ts
CHANGED
|
@@ -19,8 +19,12 @@ import { z } from "zod";
|
|
|
19
19
|
import { callbackServerConfigFromRedirectUrl } from "./callback-url.js";
|
|
20
20
|
import {
|
|
21
21
|
applyProjectOverrides,
|
|
22
|
+
isPlainObject,
|
|
23
|
+
isProjectOverrideEntry,
|
|
22
24
|
normalizeAuthConfigAliases,
|
|
23
25
|
normalizeServerConfigAliases,
|
|
26
|
+
resolveProjectOverride,
|
|
27
|
+
validateProjectOverrideKeys,
|
|
24
28
|
withRegisteredServerLifecycle,
|
|
25
29
|
} from "./config-compat.js";
|
|
26
30
|
import { McpError } from "./errors.js";
|
|
@@ -249,15 +253,18 @@ const SettingsSchema = z.object({
|
|
|
249
253
|
maxRetries: z.number().int().min(0).max(10).default(DEFAULT_SETTINGS.maxRetries),
|
|
250
254
|
});
|
|
251
255
|
|
|
252
|
-
const
|
|
256
|
+
const McpConfigEnvelopeSchema = z.object({
|
|
253
257
|
settings: SettingsSchema.default(() => ({ ...DEFAULT_SETTINGS })),
|
|
254
|
-
mcpServers: z.record(z.string(),
|
|
258
|
+
mcpServers: z.record(z.string(), z.unknown()).default({}),
|
|
255
259
|
/**
|
|
256
260
|
* Pi 1.3.0 top-level switch (default `true` when omitted): activate the built-in `codemode`
|
|
257
261
|
* tool automatically when a server with `"codemode"` exposure connects.
|
|
258
262
|
*/
|
|
259
263
|
autoEnableCodemode: z.boolean().optional(),
|
|
260
264
|
});
|
|
265
|
+
const McpConfigSchema = McpConfigEnvelopeSchema.extend({
|
|
266
|
+
mcpServers: z.record(z.string(), ServerConfigSchema).default({}),
|
|
267
|
+
});
|
|
261
268
|
|
|
262
269
|
/**
|
|
263
270
|
* Project-level file: `settings` keys are optional and carry NO defaults, so a project file
|
|
@@ -272,13 +279,26 @@ const ProjectSettingsSchema = z.object({
|
|
|
272
279
|
requestTimeoutMs: z.number().positive().optional(),
|
|
273
280
|
maxRetries: z.number().int().min(0).max(10).optional(),
|
|
274
281
|
});
|
|
275
|
-
const
|
|
282
|
+
const ProjectConfigEnvelopeSchema = z.object({
|
|
276
283
|
settings: ProjectSettingsSchema.default({}),
|
|
277
|
-
mcpServers: z.record(z.string(),
|
|
284
|
+
mcpServers: z.record(z.string(), z.unknown()).default({}),
|
|
278
285
|
autoEnableCodemode: z.boolean().optional(),
|
|
279
286
|
});
|
|
287
|
+
const ProjectConfigSchema = ProjectConfigEnvelopeSchema.extend({
|
|
288
|
+
mcpServers: z.record(z.string(), ServerConfigSchema).default({}),
|
|
289
|
+
});
|
|
280
290
|
type ProjectConfig = z.output<typeof ProjectConfigSchema>;
|
|
281
291
|
|
|
292
|
+
const ProjectOverrideFieldsSchema = z.preprocess(
|
|
293
|
+
normalizeServerConfigAliases,
|
|
294
|
+
ServerConfigObjectSchema.pick({
|
|
295
|
+
enabled: true,
|
|
296
|
+
lifecycle: true,
|
|
297
|
+
exposure: true,
|
|
298
|
+
toolExposure: true,
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
|
|
282
302
|
// ─── Public Types ─────────────────────────────────────────────────────────────
|
|
283
303
|
|
|
284
304
|
export type AuthConfig = z.output<typeof AuthConfigSchema>;
|
|
@@ -310,6 +330,20 @@ export interface LoadConfigOptions {
|
|
|
310
330
|
includeProjectConfig?: boolean;
|
|
311
331
|
}
|
|
312
332
|
|
|
333
|
+
/** Result of fault-tolerant session config loading with isolated per-server errors. */
|
|
334
|
+
export interface SessionConfigLoadResult {
|
|
335
|
+
loaded: LoadedConfig;
|
|
336
|
+
serverErrors: McpError[];
|
|
337
|
+
/**
|
|
338
|
+
* Server names rejected from `mcp.json` that must not fall back to global or
|
|
339
|
+
* extension-registered entries of the same name (failed global entries, broken
|
|
340
|
+
* project overrides of global entries, and broken full project entries).
|
|
341
|
+
* Partial project overrides targeting a name absent from global `mcp.json` are
|
|
342
|
+
* excluded so an extension-registered server of that name stays active.
|
|
343
|
+
*/
|
|
344
|
+
suppressedServers: ReadonlySet<string>;
|
|
345
|
+
}
|
|
346
|
+
|
|
313
347
|
// ─── Loader ───────────────────────────────────────────────────────────────────
|
|
314
348
|
|
|
315
349
|
async function readJsonFile(path: string): Promise<unknown> {
|
|
@@ -420,21 +454,265 @@ export function agentDir(env: NodeJS.ProcessEnv = process.env): string {
|
|
|
420
454
|
return join(homedir(), ".pi", "agent");
|
|
421
455
|
}
|
|
422
456
|
|
|
457
|
+
const EMPTY_STRING_SET: ReadonlySet<string> = new Set();
|
|
458
|
+
|
|
459
|
+
function setOwnProperty<T>(target: Record<string, T>, key: string, value: T): void {
|
|
460
|
+
Object.defineProperty(target, key, {
|
|
461
|
+
value,
|
|
462
|
+
enumerable: true,
|
|
463
|
+
writable: true,
|
|
464
|
+
configurable: true,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
423
468
|
/** Winning file origin per server name: a project entry replaces a global entry of the same name. */
|
|
424
469
|
function originsOf(
|
|
425
470
|
globalCfg: McpConfig,
|
|
426
471
|
projectCfg: ProjectConfig | undefined,
|
|
472
|
+
droppedNames: ReadonlySet<string> = EMPTY_STRING_SET,
|
|
427
473
|
): Record<string, FileConfigOrigin> {
|
|
428
474
|
const origins: Record<string, FileConfigOrigin> = {};
|
|
429
475
|
for (const name of Object.keys(globalCfg.mcpServers)) {
|
|
430
|
-
|
|
476
|
+
if (!droppedNames.has(name)) {
|
|
477
|
+
setOwnProperty(origins, name, "global");
|
|
478
|
+
}
|
|
431
479
|
}
|
|
432
480
|
for (const name of Object.keys(projectCfg?.mcpServers ?? {})) {
|
|
433
|
-
origins
|
|
481
|
+
setOwnProperty(origins, name, "project");
|
|
434
482
|
}
|
|
435
483
|
return origins;
|
|
436
484
|
}
|
|
437
485
|
|
|
486
|
+
function extractRawServers(raw: unknown): Record<string, unknown> {
|
|
487
|
+
return isPlainObject(raw) && isPlainObject(raw.mcpServers) ? raw.mcpServers : {};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function formatServerValidationError(
|
|
491
|
+
name: string,
|
|
492
|
+
issues: z.ZodError["issues"],
|
|
493
|
+
sourcePath: string,
|
|
494
|
+
): McpError {
|
|
495
|
+
const formatted = issues
|
|
496
|
+
.map((i) => ` mcpServers.${name}${i.path.length > 0 ? `.${i.path.join(".")}` : ""}: ${i.message}`)
|
|
497
|
+
.join("\n");
|
|
498
|
+
return new McpError(`Invalid mcp.json at ${sourcePath}:\n${formatted}`, "<config>", "config");
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function parseGlobalServersTolerant(
|
|
502
|
+
rawServers: Record<string, unknown>,
|
|
503
|
+
sourcePath: string,
|
|
504
|
+
): { servers: Record<string, ServerConfig>; invalidNames: Set<string>; errors: McpError[] } {
|
|
505
|
+
const servers: Record<string, ServerConfig> = {};
|
|
506
|
+
const invalidNames = new Set<string>();
|
|
507
|
+
const errors: McpError[] = [];
|
|
508
|
+
for (const [name, entry] of Object.entries(rawServers)) {
|
|
509
|
+
const result = ServerConfigSchema.safeParse(entry);
|
|
510
|
+
if (result.success) {
|
|
511
|
+
setOwnProperty(servers, name, result.data);
|
|
512
|
+
} else {
|
|
513
|
+
invalidNames.add(name);
|
|
514
|
+
errors.push(formatServerValidationError(name, result.error.issues, sourcePath));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return { servers, invalidNames, errors };
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
type ProjectServerEntryResult =
|
|
521
|
+
| { ok: true; value: ServerConfig }
|
|
522
|
+
| { ok: false; error?: McpError; suppressFallback: boolean };
|
|
523
|
+
|
|
524
|
+
function parseValidatedProjectCandidate(
|
|
525
|
+
name: string,
|
|
526
|
+
candidate: unknown,
|
|
527
|
+
sourcePath: string,
|
|
528
|
+
): ProjectServerEntryResult {
|
|
529
|
+
const parsed = ServerConfigSchema.safeParse(candidate);
|
|
530
|
+
if (!parsed.success) {
|
|
531
|
+
return {
|
|
532
|
+
ok: false,
|
|
533
|
+
error: formatServerValidationError(name, parsed.error.issues, sourcePath),
|
|
534
|
+
suppressFallback: true,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
return { ok: true, value: parsed.data };
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function parseOverrideForInvalidGlobal(
|
|
541
|
+
name: string,
|
|
542
|
+
override: Record<string, unknown>,
|
|
543
|
+
sourcePath: string,
|
|
544
|
+
): ProjectServerEntryResult {
|
|
545
|
+
const keyError = validateProjectOverrideKeys(name, override, sourcePath);
|
|
546
|
+
if (keyError) {
|
|
547
|
+
return { ok: false, error: keyError, suppressFallback: true };
|
|
548
|
+
}
|
|
549
|
+
const fields = ProjectOverrideFieldsSchema.safeParse(override);
|
|
550
|
+
if (!fields.success) {
|
|
551
|
+
return {
|
|
552
|
+
ok: false,
|
|
553
|
+
error: formatServerValidationError(name, fields.error.issues, sourcePath),
|
|
554
|
+
suppressFallback: true,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
// WHY: global validation already reported the broken base server against the global file;
|
|
558
|
+
// drop the server without emitting a duplicate error falsely attributed to .pi/mcp.json.
|
|
559
|
+
return { ok: false, suppressFallback: true };
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function parseProjectServerEntry(
|
|
563
|
+
name: string,
|
|
564
|
+
entry: unknown,
|
|
565
|
+
globalContext: {
|
|
566
|
+
rawServers: Record<string, unknown>;
|
|
567
|
+
invalidNames: ReadonlySet<string>;
|
|
568
|
+
},
|
|
569
|
+
sourcePath: string,
|
|
570
|
+
): ProjectServerEntryResult {
|
|
571
|
+
if (!isProjectOverrideEntry(entry)) {
|
|
572
|
+
return parseValidatedProjectCandidate(name, entry, sourcePath);
|
|
573
|
+
}
|
|
574
|
+
if (globalContext.invalidNames.has(name)) {
|
|
575
|
+
return parseOverrideForInvalidGlobal(name, entry, sourcePath);
|
|
576
|
+
}
|
|
577
|
+
const resolved = resolveProjectOverride(name, entry, globalContext.rawServers, sourcePath);
|
|
578
|
+
if (!resolved.ok) {
|
|
579
|
+
return {
|
|
580
|
+
ok: false,
|
|
581
|
+
error: resolved.error,
|
|
582
|
+
suppressFallback: Object.hasOwn(globalContext.rawServers, name),
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
return parseValidatedProjectCandidate(name, resolved.value, sourcePath);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function parseProjectServersTolerant(
|
|
589
|
+
rawServers: Record<string, unknown>,
|
|
590
|
+
globalContext: {
|
|
591
|
+
rawServers: Record<string, unknown>;
|
|
592
|
+
invalidNames: ReadonlySet<string>;
|
|
593
|
+
},
|
|
594
|
+
sourcePath: string,
|
|
595
|
+
): {
|
|
596
|
+
servers: Record<string, ServerConfig>;
|
|
597
|
+
droppedNames: Set<string>;
|
|
598
|
+
suppressedNames: Set<string>;
|
|
599
|
+
errors: McpError[];
|
|
600
|
+
} {
|
|
601
|
+
const servers: Record<string, ServerConfig> = {};
|
|
602
|
+
const droppedNames = new Set<string>();
|
|
603
|
+
const suppressedNames = new Set<string>();
|
|
604
|
+
const errors: McpError[] = [];
|
|
605
|
+
for (const [name, entry] of Object.entries(rawServers)) {
|
|
606
|
+
const parsed = parseProjectServerEntry(name, entry, globalContext, sourcePath);
|
|
607
|
+
if (parsed.ok) {
|
|
608
|
+
setOwnProperty(servers, name, parsed.value);
|
|
609
|
+
} else {
|
|
610
|
+
droppedNames.add(name);
|
|
611
|
+
if (parsed.suppressFallback) suppressedNames.add(name);
|
|
612
|
+
if (parsed.error) errors.push(parsed.error);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return { servers, droppedNames, suppressedNames, errors };
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function filterDroppedServers(
|
|
619
|
+
servers: Record<string, ServerConfig>,
|
|
620
|
+
droppedNames: ReadonlySet<string>,
|
|
621
|
+
): Record<string, ServerConfig> {
|
|
622
|
+
if (droppedNames.size === 0) return servers;
|
|
623
|
+
const filtered: Record<string, ServerConfig> = {};
|
|
624
|
+
for (const [name, cfg] of Object.entries(servers)) {
|
|
625
|
+
if (!droppedNames.has(name)) {
|
|
626
|
+
setOwnProperty(filtered, name, cfg);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return filtered;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function mergeSessionConfigs(
|
|
633
|
+
globalCfg: McpConfig,
|
|
634
|
+
projectCfg: ProjectConfig,
|
|
635
|
+
droppedNames: ReadonlySet<string>,
|
|
636
|
+
): McpConfig {
|
|
637
|
+
// WHY: a broken project override or replacement must drop the global server of that name
|
|
638
|
+
// rather than silently falling back to un-overridden global settings or credentials.
|
|
639
|
+
const keptGlobal: McpConfig = droppedNames.size === 0
|
|
640
|
+
? globalCfg
|
|
641
|
+
: { ...globalCfg, mcpServers: filterDroppedServers(globalCfg.mcpServers, droppedNames) };
|
|
642
|
+
return mergeConfigs(keptGlobal, projectCfg);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function applyProjectSuppressedNames(
|
|
646
|
+
suppressedServers: Set<string>,
|
|
647
|
+
validProjectServers: Record<string, ServerConfig>,
|
|
648
|
+
projectSuppressedNames: ReadonlySet<string>,
|
|
649
|
+
): void {
|
|
650
|
+
for (const name of Object.keys(validProjectServers)) {
|
|
651
|
+
suppressedServers.delete(name);
|
|
652
|
+
}
|
|
653
|
+
for (const name of projectSuppressedNames) {
|
|
654
|
+
suppressedServers.add(name);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Load and merge global and project configs for a session, isolating per-server validation errors
|
|
660
|
+
* and invalid project overrides so healthy sibling servers stay loaded. Whole-file errors (I/O,
|
|
661
|
+
* JSON syntax, non-object root/`mcpServers`, invalid `settings` or `autoEnableCodemode`) still
|
|
662
|
+
* throw `McpError("config")`.
|
|
663
|
+
*/
|
|
664
|
+
export async function loadSessionConfigWithOrigins(
|
|
665
|
+
cwd: string,
|
|
666
|
+
globalPath = join(agentDir(), "mcp.json"),
|
|
667
|
+
options?: LoadConfigOptions,
|
|
668
|
+
): Promise<SessionConfigLoadResult> {
|
|
669
|
+
const projectConfigIncluded = options?.includeProjectConfig !== false;
|
|
670
|
+
const projectConfigPath = join(cwd, ".pi", "mcp.json");
|
|
671
|
+
|
|
672
|
+
const [globalRaw, projectRaw] = await Promise.all([
|
|
673
|
+
readJsonFile(globalPath),
|
|
674
|
+
projectConfigIncluded ? readJsonFile(projectConfigPath) : Promise.resolve(undefined),
|
|
675
|
+
]);
|
|
676
|
+
|
|
677
|
+
const serverErrors: McpError[] = [];
|
|
678
|
+
const globalEnvelope = globalRaw !== undefined
|
|
679
|
+
? parseWith(McpConfigEnvelopeSchema, globalRaw, globalPath)
|
|
680
|
+
: McpConfigEnvelopeSchema.parse({});
|
|
681
|
+
const globalRawServers = extractRawServers(globalRaw);
|
|
682
|
+
const globalParsed = parseGlobalServersTolerant(globalRawServers, globalPath);
|
|
683
|
+
serverErrors.push(...globalParsed.errors);
|
|
684
|
+
const globalCfg: McpConfig = { ...globalEnvelope, mcpServers: globalParsed.servers };
|
|
685
|
+
const suppressedServers = new Set<string>(globalParsed.invalidNames);
|
|
686
|
+
|
|
687
|
+
let projectCfg: ProjectConfig | undefined;
|
|
688
|
+
let droppedNames: ReadonlySet<string> = EMPTY_STRING_SET;
|
|
689
|
+
if (projectRaw !== undefined) {
|
|
690
|
+
const projectEnvelope = parseWith(ProjectConfigEnvelopeSchema, projectRaw, projectConfigPath);
|
|
691
|
+
const projectParsed = parseProjectServersTolerant(
|
|
692
|
+
extractRawServers(projectRaw),
|
|
693
|
+
{ rawServers: globalRawServers, invalidNames: globalParsed.invalidNames },
|
|
694
|
+
projectConfigPath,
|
|
695
|
+
);
|
|
696
|
+
serverErrors.push(...projectParsed.errors);
|
|
697
|
+
droppedNames = projectParsed.droppedNames;
|
|
698
|
+
applyProjectSuppressedNames(suppressedServers, projectParsed.servers, projectParsed.suppressedNames);
|
|
699
|
+
projectCfg = { ...projectEnvelope, mcpServers: projectParsed.servers };
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
return {
|
|
703
|
+
loaded: {
|
|
704
|
+
config: projectCfg === undefined
|
|
705
|
+
? globalCfg
|
|
706
|
+
: mergeSessionConfigs(globalCfg, projectCfg, droppedNames),
|
|
707
|
+
origins: originsOf(globalCfg, projectCfg, droppedNames),
|
|
708
|
+
projectConfigPath,
|
|
709
|
+
projectConfigIncluded,
|
|
710
|
+
},
|
|
711
|
+
serverErrors,
|
|
712
|
+
suppressedServers,
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
|
|
438
716
|
/**
|
|
439
717
|
* Load and merge global and project configs, reporting where every server entry came from.
|
|
440
718
|
* Same validation, precedence, and error behavior as `loadConfig`.
|
package/src/session-lifecycle.ts
CHANGED
|
@@ -13,11 +13,11 @@ import type {
|
|
|
13
13
|
} from "@earendil-works/pi-coding-agent";
|
|
14
14
|
import { join } from "node:path";
|
|
15
15
|
import {
|
|
16
|
-
|
|
16
|
+
loadSessionConfigWithOrigins,
|
|
17
17
|
parseRegisteredServerConfig,
|
|
18
|
-
type LoadedConfig,
|
|
19
18
|
type McpConfig,
|
|
20
19
|
type ServerConfig,
|
|
20
|
+
type SessionConfigLoadResult,
|
|
21
21
|
} from "./config.js";
|
|
22
22
|
import {
|
|
23
23
|
withExtensionServers,
|
|
@@ -46,6 +46,8 @@ export const DEFAULT_SETTINGS = {
|
|
|
46
46
|
maxRetries: 5,
|
|
47
47
|
} as const;
|
|
48
48
|
|
|
49
|
+
const EMPTY_STRING_SET: ReadonlySet<string> = new Set();
|
|
50
|
+
|
|
49
51
|
export type SessionProvenance = ConfigProvenance;
|
|
50
52
|
|
|
51
53
|
export interface BootstrapConfigPaths {
|
|
@@ -56,6 +58,7 @@ export interface BootstrapConfigPaths {
|
|
|
56
58
|
export interface BootstrapConfigState {
|
|
57
59
|
fileConfig: McpConfig;
|
|
58
60
|
fileProvenance: ConfigProvenanceInput;
|
|
61
|
+
suppressedFileServers?: ReadonlySet<string> | undefined;
|
|
59
62
|
extensionServers: Record<string, ServerConfig>;
|
|
60
63
|
config: McpConfig;
|
|
61
64
|
}
|
|
@@ -95,11 +98,16 @@ export function collectRegisteredServers(
|
|
|
95
98
|
entries: readonly RegisteredMcpServer[],
|
|
96
99
|
fileServers: Record<string, ServerConfig>,
|
|
97
100
|
onError?: (err: unknown) => void,
|
|
101
|
+
suppressedFileServers?: ReadonlySet<string>,
|
|
98
102
|
): Record<string, ServerConfig> {
|
|
99
103
|
const registered: Record<string, ServerConfig> = {};
|
|
100
104
|
for (const entry of entries) {
|
|
101
|
-
// Servers configured in mcp.json
|
|
102
|
-
|
|
105
|
+
// Servers configured in mcp.json (including broken file entries that must not fall back
|
|
106
|
+
// to extension registrations) take precedence over extension-registered servers.
|
|
107
|
+
if (
|
|
108
|
+
Object.prototype.hasOwnProperty.call(fileServers, entry.name) ||
|
|
109
|
+
suppressedFileServers?.has(entry.name)
|
|
110
|
+
) {
|
|
103
111
|
continue;
|
|
104
112
|
}
|
|
105
113
|
try {
|
|
@@ -155,12 +163,17 @@ export async function loadBootstrapConfigState(
|
|
|
155
163
|
projectConfigPath: join(bootstrapCwd, ".pi", "mcp.json"),
|
|
156
164
|
projectConfigIncluded: false,
|
|
157
165
|
};
|
|
166
|
+
let suppressedFileServers: ReadonlySet<string> = EMPTY_STRING_SET;
|
|
158
167
|
try {
|
|
159
|
-
const
|
|
168
|
+
const result = await loadSessionConfigWithOrigins(bootstrapCwd, paths.globalConfigPath, {
|
|
160
169
|
includeProjectConfig: false,
|
|
161
170
|
});
|
|
162
|
-
fileConfig = loaded.config;
|
|
163
|
-
fileProvenance = loaded;
|
|
171
|
+
fileConfig = result.loaded.config;
|
|
172
|
+
fileProvenance = result.loaded;
|
|
173
|
+
suppressedFileServers = result.suppressedServers;
|
|
174
|
+
for (const err of result.serverErrors) {
|
|
175
|
+
reportBootstrapConfigError(err);
|
|
176
|
+
}
|
|
164
177
|
} catch (err) {
|
|
165
178
|
// Can't notify yet (no ctx), so the diagnostic goes to stderr (and to the
|
|
166
179
|
// debug log when it is enabled). The session_start handler will re-try with
|
|
@@ -173,11 +186,13 @@ export async function loadBootstrapConfigState(
|
|
|
173
186
|
initialEntries,
|
|
174
187
|
fileConfig.mcpServers,
|
|
175
188
|
reportBootstrapConfigError,
|
|
189
|
+
suppressedFileServers,
|
|
176
190
|
);
|
|
177
191
|
const config = mergeWithRegisteredServers(fileConfig, extensionServers);
|
|
178
192
|
return {
|
|
179
193
|
fileConfig,
|
|
180
194
|
fileProvenance,
|
|
195
|
+
suppressedFileServers,
|
|
181
196
|
extensionServers,
|
|
182
197
|
config,
|
|
183
198
|
};
|
|
@@ -210,9 +225,15 @@ export function abortSupersededEagerStarts(
|
|
|
210
225
|
registeredEntries: readonly RegisteredMcpServer[],
|
|
211
226
|
fileServers: Record<string, ServerConfig>,
|
|
212
227
|
abortEagerStart: (name: string) => void,
|
|
228
|
+
suppressedFileServers?: ReadonlySet<string>,
|
|
213
229
|
): void {
|
|
214
230
|
if (inFlightEager.size === 0) return;
|
|
215
|
-
const nextPreview = collectRegisteredServers(
|
|
231
|
+
const nextPreview = collectRegisteredServers(
|
|
232
|
+
registeredEntries,
|
|
233
|
+
fileServers,
|
|
234
|
+
undefined,
|
|
235
|
+
suppressedFileServers,
|
|
236
|
+
);
|
|
216
237
|
for (const name of findRemovedOrChangedServers(
|
|
217
238
|
Object.fromEntries(inFlightEager),
|
|
218
239
|
nextPreview,
|
|
@@ -266,6 +287,7 @@ export class SessionLifecycle {
|
|
|
266
287
|
private readonly hasRegisteredServersSource: boolean;
|
|
267
288
|
private fileConfig: McpConfig;
|
|
268
289
|
private fileProvenance: ConfigProvenanceInput;
|
|
290
|
+
private suppressedFileServers: ReadonlySet<string>;
|
|
269
291
|
private extensionServers: Record<string, ServerConfig>;
|
|
270
292
|
private config: McpConfig;
|
|
271
293
|
private latestEventEntries: RegisteredMcpServer[] = [];
|
|
@@ -292,6 +314,7 @@ export class SessionLifecycle {
|
|
|
292
314
|
this.hasRegisteredServersSource = hasRegisteredServersApi(deps.pi);
|
|
293
315
|
this.fileConfig = deps.initialState.fileConfig;
|
|
294
316
|
this.fileProvenance = deps.initialState.fileProvenance;
|
|
317
|
+
this.suppressedFileServers = deps.initialState.suppressedFileServers ?? EMPTY_STRING_SET;
|
|
295
318
|
this.extensionServers = deps.initialState.extensionServers;
|
|
296
319
|
this.config = deps.initialState.config;
|
|
297
320
|
}
|
|
@@ -330,6 +353,7 @@ export class SessionLifecycle {
|
|
|
330
353
|
this.getRegisteredEntries(),
|
|
331
354
|
this.fileConfig.mcpServers,
|
|
332
355
|
(name) => this.abortEagerStart(name),
|
|
356
|
+
this.suppressedFileServers,
|
|
333
357
|
);
|
|
334
358
|
return this.enqueueServerReconcile(ctx);
|
|
335
359
|
}
|
|
@@ -378,17 +402,19 @@ export class SessionLifecycle {
|
|
|
378
402
|
return epoch === this.sessionEpoch && this.changeEventCount !== this.appliedChangeCount;
|
|
379
403
|
}
|
|
380
404
|
|
|
381
|
-
private setSessionConfigState(
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
this.
|
|
405
|
+
private setSessionConfigState(next: {
|
|
406
|
+
fileConfig: McpConfig;
|
|
407
|
+
fileProvenance: ConfigProvenanceInput;
|
|
408
|
+
suppressedFileServers: ReadonlySet<string>;
|
|
409
|
+
extensionServers: Record<string, ServerConfig>;
|
|
410
|
+
config: McpConfig;
|
|
411
|
+
}): void {
|
|
412
|
+
this.fileConfig = next.fileConfig;
|
|
413
|
+
this.fileProvenance = next.fileProvenance;
|
|
414
|
+
this.suppressedFileServers = next.suppressedFileServers;
|
|
389
415
|
this.appliedChangeCount = this.changeEventCount;
|
|
390
|
-
this.extensionServers =
|
|
391
|
-
this.config =
|
|
416
|
+
this.extensionServers = next.extensionServers;
|
|
417
|
+
this.config = next.config;
|
|
392
418
|
}
|
|
393
419
|
|
|
394
420
|
private syncConfiguredDiscoveryTools(servers: Record<string, ServerConfig>): void {
|
|
@@ -452,6 +478,7 @@ export class SessionLifecycle {
|
|
|
452
478
|
this.getRegisteredEntries(),
|
|
453
479
|
this.fileConfig.mcpServers,
|
|
454
480
|
(name) => this.abortEagerStart(name),
|
|
481
|
+
this.suppressedFileServers,
|
|
455
482
|
);
|
|
456
483
|
}
|
|
457
484
|
if (this.inFlightEagerBatch.length > 0) {
|
|
@@ -517,12 +544,13 @@ export class SessionLifecycle {
|
|
|
517
544
|
(regErr) => notifySessionConfigError(ctx, regErr),
|
|
518
545
|
);
|
|
519
546
|
const nextConfig = mergeWithRegisteredServers(fallbackFileConfig, nextExtensionServers);
|
|
520
|
-
this.setSessionConfigState(
|
|
521
|
-
fallbackFileConfig,
|
|
522
|
-
fallbackProvenance,
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
547
|
+
this.setSessionConfigState({
|
|
548
|
+
fileConfig: fallbackFileConfig,
|
|
549
|
+
fileProvenance: fallbackProvenance,
|
|
550
|
+
suppressedFileServers: EMPTY_STRING_SET,
|
|
551
|
+
extensionServers: nextExtensionServers,
|
|
552
|
+
config: nextConfig,
|
|
553
|
+
});
|
|
526
554
|
this.deps.bridge.updateSettings(nextConfig.settings);
|
|
527
555
|
this.deps.bridge.setAutoEnableCodemode(nextConfig.autoEnableCodemode);
|
|
528
556
|
this.deps.manager.rebuildServers(nextConfig);
|
|
@@ -532,13 +560,15 @@ export class SessionLifecycle {
|
|
|
532
560
|
: [];
|
|
533
561
|
}
|
|
534
562
|
|
|
535
|
-
private hasSessionConfigChanged(
|
|
563
|
+
private hasSessionConfigChanged(result: SessionConfigLoadResult): boolean {
|
|
536
564
|
const previewExtensionServers = collectRegisteredServers(
|
|
537
565
|
this.getRegisteredEntries(),
|
|
538
|
-
|
|
566
|
+
result.loaded.config.mcpServers,
|
|
567
|
+
undefined,
|
|
568
|
+
result.suppressedServers,
|
|
539
569
|
);
|
|
540
570
|
const previewConfig = mergeWithRegisteredServers(
|
|
541
|
-
|
|
571
|
+
result.loaded.config,
|
|
542
572
|
previewExtensionServers,
|
|
543
573
|
);
|
|
544
574
|
return JSON.stringify(previewConfig) !== JSON.stringify(this.config);
|
|
@@ -546,21 +576,24 @@ export class SessionLifecycle {
|
|
|
546
576
|
|
|
547
577
|
private commitLoadedConfig(
|
|
548
578
|
ctx: ExtensionContext,
|
|
549
|
-
|
|
579
|
+
result: SessionConfigLoadResult,
|
|
550
580
|
rebuild: boolean,
|
|
551
581
|
): Array<[string, ServerConfig]> {
|
|
582
|
+
const nextLoaded = result.loaded;
|
|
552
583
|
const nextExtensionServers = collectRegisteredServers(
|
|
553
584
|
this.getRegisteredEntries(),
|
|
554
585
|
nextLoaded.config.mcpServers,
|
|
555
586
|
(err) => notifySessionConfigError(ctx, err),
|
|
587
|
+
result.suppressedServers,
|
|
556
588
|
);
|
|
557
589
|
const sessionConfig = mergeWithRegisteredServers(nextLoaded.config, nextExtensionServers);
|
|
558
|
-
this.setSessionConfigState(
|
|
559
|
-
nextLoaded.config,
|
|
560
|
-
nextLoaded,
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
590
|
+
this.setSessionConfigState({
|
|
591
|
+
fileConfig: nextLoaded.config,
|
|
592
|
+
fileProvenance: nextLoaded,
|
|
593
|
+
suppressedFileServers: result.suppressedServers,
|
|
594
|
+
extensionServers: nextExtensionServers,
|
|
595
|
+
config: rebuild ? sessionConfig : this.config,
|
|
596
|
+
});
|
|
564
597
|
this.deps.bridge.setAutoEnableCodemode(sessionConfig.autoEnableCodemode);
|
|
565
598
|
if (rebuild) {
|
|
566
599
|
this.deps.bridge.updateSettings(sessionConfig.settings);
|
|
@@ -594,10 +627,10 @@ export class SessionLifecycle {
|
|
|
594
627
|
|
|
595
628
|
private async applyLoadedSessionConfig(
|
|
596
629
|
ctx: ExtensionContext,
|
|
597
|
-
|
|
630
|
+
result: SessionConfigLoadResult,
|
|
598
631
|
epoch: number,
|
|
599
632
|
): Promise<void> {
|
|
600
|
-
const configChanged = this.hasSessionConfigChanged(
|
|
633
|
+
const configChanged = this.hasSessionConfigChanged(result);
|
|
601
634
|
if (configChanged) {
|
|
602
635
|
reportDiagnostic({
|
|
603
636
|
level: "info",
|
|
@@ -608,7 +641,7 @@ export class SessionLifecycle {
|
|
|
608
641
|
await this.deps.coordinator.teardown(ctx);
|
|
609
642
|
if (!this.isEpochCurrent(epoch)) return;
|
|
610
643
|
}
|
|
611
|
-
const eagerServers = this.commitLoadedConfig(ctx,
|
|
644
|
+
const eagerServers = this.commitLoadedConfig(ctx, result, configChanged);
|
|
612
645
|
if (!this.isEpochCurrent(epoch) || eagerServers.length === 0) return;
|
|
613
646
|
await this.runEagerStarts(eagerServers, ctx);
|
|
614
647
|
}
|
|
@@ -630,9 +663,9 @@ export class SessionLifecycle {
|
|
|
630
663
|
});
|
|
631
664
|
const includeProjectConfig = isProjectTrusted(ctx);
|
|
632
665
|
|
|
633
|
-
let
|
|
666
|
+
let result: SessionConfigLoadResult;
|
|
634
667
|
try {
|
|
635
|
-
|
|
668
|
+
result = await loadSessionConfigWithOrigins(ctx.cwd, this.deps.globalConfigPath, {
|
|
636
669
|
includeProjectConfig,
|
|
637
670
|
});
|
|
638
671
|
} catch (err) {
|
|
@@ -640,7 +673,10 @@ export class SessionLifecycle {
|
|
|
640
673
|
return;
|
|
641
674
|
}
|
|
642
675
|
if (!this.isEpochCurrent(epoch)) return;
|
|
643
|
-
|
|
676
|
+
for (const err of result.serverErrors) {
|
|
677
|
+
notifySessionConfigError(ctx, err);
|
|
678
|
+
}
|
|
679
|
+
await this.applyLoadedSessionConfig(ctx, result, epoch);
|
|
644
680
|
}
|
|
645
681
|
|
|
646
682
|
private handleReconcileError(err: unknown, epoch: number, ctx: ExtensionContext): void {
|
|
@@ -697,6 +733,7 @@ export class SessionLifecycle {
|
|
|
697
733
|
(err) => {
|
|
698
734
|
configErrors.push(formatErrorMessage(err));
|
|
699
735
|
},
|
|
736
|
+
this.suppressedFileServers,
|
|
700
737
|
);
|
|
701
738
|
|
|
702
739
|
const removedOrChanged = findRemovedOrChangedServers(
|