pi-mcp-extension 1.16.2 → 1.16.3
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/README.md +2 -0
- package/package.json +1 -1
- package/src/commands.ts +40 -0
- package/src/connection-coordinator.ts +21 -9
- package/src/errors.ts +6 -1
- package/src/index.ts +16 -1
- package/src/register-commands.ts +7 -2
package/README.md
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
|
|
41
41
|
> **Prerequisite:** Node.js 22+ and the [Pi coding agent](https://pi.dev).
|
|
42
42
|
|
|
43
|
+
> **Disable MCP for one run:** `pi --no-mcp` also disables this installed extension, even when it replaces `builtin:mcp`. No MCP config is loaded, no servers connect, and no MCP commands or tools are registered. As with Pi, `--no-mcp` has no effect when it appears after `--` or is passed as another option's value.
|
|
44
|
+
|
|
43
45
|
1. **Install the extension** (or run once without installing via `pi -e npm:pi-mcp-extension`):
|
|
44
46
|
```bash
|
|
45
47
|
pi install npm:pi-mcp-extension
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-mcp-extension",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.3",
|
|
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/commands.ts
CHANGED
|
@@ -135,4 +135,44 @@ export function authArgumentCompletions(argumentPrefix: string, servers: readonl
|
|
|
135
135
|
return completions.length > 0 ? completions : null;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
// WHY: Pi 1.3.0 only reports --no-mcp through its own parsed arguments; extensions cannot
|
|
139
|
+
// read that setting from ExtensionAPI. Mirror its value-taking flags to avoid treating
|
|
140
|
+
// an option's value as the --no-mcp switch.
|
|
141
|
+
const PI_VALUE_TAKING_FLAGS: ReadonlySet<string> = new Set([
|
|
142
|
+
"--provider",
|
|
143
|
+
"--model",
|
|
144
|
+
"--api-key",
|
|
145
|
+
"--system-prompt",
|
|
146
|
+
"--append-system-prompt",
|
|
147
|
+
"--name",
|
|
148
|
+
"-n",
|
|
149
|
+
"--session",
|
|
150
|
+
"--session-id",
|
|
151
|
+
"--fork",
|
|
152
|
+
"--session-dir",
|
|
153
|
+
"--models",
|
|
154
|
+
"--tools",
|
|
155
|
+
"-t",
|
|
156
|
+
"--exclude-tools",
|
|
157
|
+
"-xt",
|
|
158
|
+
"--thinking",
|
|
159
|
+
"--export",
|
|
160
|
+
"--extension",
|
|
161
|
+
"-e",
|
|
162
|
+
"--skill",
|
|
163
|
+
"--prompt-template",
|
|
164
|
+
"--theme",
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
/** Return true only when Pi receives --no-mcp as a CLI flag, not as a value or positional argument. */
|
|
168
|
+
export function hasNoMcpFlag(argv: readonly string[]): boolean {
|
|
169
|
+
for (let i = 0; i < argv.length; i++) {
|
|
170
|
+
const arg = argv[i] ?? "";
|
|
171
|
+
if (arg === "--") break;
|
|
172
|
+
if (arg === "--no-mcp") return true;
|
|
173
|
+
if (PI_VALUE_TAKING_FLAGS.has(arg)) i++;
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
138
178
|
export { collectRegisteredServers, mergeWithRegisteredServers } from "./session-lifecycle.js";
|
|
@@ -10,7 +10,7 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
10
10
|
import type { McpConfig, ServerConfig } from "./config.js";
|
|
11
11
|
import type { ManagedServer, ServerManager } from "./server-manager.js";
|
|
12
12
|
import type { ToolBridge } from "./tool-bridge.js";
|
|
13
|
-
import { McpError, AuthCancelledError, AuthRequiredError } from "./errors.js";
|
|
13
|
+
import { McpError, AuthCancelledError, AuthRequiredError, type CancellationPhase } from "./errors.js";
|
|
14
14
|
import {
|
|
15
15
|
formatErrorMessage,
|
|
16
16
|
promptStartupAuth,
|
|
@@ -39,6 +39,7 @@ type ConnectAttemptEntry = {
|
|
|
39
39
|
controller: AbortController;
|
|
40
40
|
reset: boolean;
|
|
41
41
|
waiters: number;
|
|
42
|
+
phase: CancellationPhase;
|
|
42
43
|
};
|
|
43
44
|
|
|
44
45
|
type ActiveStartupTurn = {
|
|
@@ -417,9 +418,9 @@ export class ConnectionCoordinator {
|
|
|
417
418
|
}
|
|
418
419
|
entry.controller.abort();
|
|
419
420
|
stopPromise = this.deps.manager.stopServer(serverName);
|
|
420
|
-
void stopPromise.finally(() => reject(new AuthCancelledError(serverName)));
|
|
421
|
+
void stopPromise.finally(() => reject(new AuthCancelledError(serverName, entry.phase)));
|
|
421
422
|
} else {
|
|
422
|
-
reject(new AuthCancelledError(serverName));
|
|
423
|
+
reject(new AuthCancelledError(serverName, entry.phase));
|
|
423
424
|
}
|
|
424
425
|
};
|
|
425
426
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
@@ -441,7 +442,7 @@ export class ConnectionCoordinator {
|
|
|
441
442
|
): ManagedServer {
|
|
442
443
|
const server = this.deps.manager.getServer(serverName);
|
|
443
444
|
if (!server) throw new McpError(`Unknown server "${serverName}"`, serverName, "config");
|
|
444
|
-
if (signal?.aborted) throw new AuthCancelledError(serverName);
|
|
445
|
+
if (signal?.aborted) throw new AuthCancelledError(serverName, reset ? "auth" : "connection");
|
|
445
446
|
if (reset && !server.config.auth) {
|
|
446
447
|
throw new McpError(`Server "${serverName}" has no OAuth configuration`, serverName, "config");
|
|
447
448
|
}
|
|
@@ -471,9 +472,11 @@ export class ConnectionCoordinator {
|
|
|
471
472
|
entry: ConnectAttemptEntry,
|
|
472
473
|
): Promise<void> {
|
|
473
474
|
const { serverName, controller } = params;
|
|
474
|
-
const task = this._executeConnectTask(params)
|
|
475
|
+
const task = this._executeConnectTask(params, entry)
|
|
475
476
|
.catch((err: unknown) => {
|
|
476
|
-
if (controller.signal.aborted
|
|
477
|
+
if (controller.signal.aborted || err instanceof AuthCancelledError) {
|
|
478
|
+
throw new AuthCancelledError(serverName, entry.phase);
|
|
479
|
+
}
|
|
477
480
|
throw err;
|
|
478
481
|
})
|
|
479
482
|
.finally(() => {
|
|
@@ -485,7 +488,10 @@ export class ConnectionCoordinator {
|
|
|
485
488
|
return task;
|
|
486
489
|
}
|
|
487
490
|
|
|
488
|
-
private async _executeConnectTask(
|
|
491
|
+
private async _executeConnectTask(
|
|
492
|
+
params: ConnectAttemptParams,
|
|
493
|
+
entry: ConnectAttemptEntry,
|
|
494
|
+
): Promise<void> {
|
|
489
495
|
const { serverName, ctx, controller, existing, reset, alreadyQueued } = params;
|
|
490
496
|
if (existing) {
|
|
491
497
|
// Wait for a superseded connect to release its callback listener and auth lock.
|
|
@@ -504,7 +510,8 @@ export class ConnectionCoordinator {
|
|
|
504
510
|
return;
|
|
505
511
|
}
|
|
506
512
|
if (!ctx.hasUI) throw new AuthRequiredError(serverName);
|
|
507
|
-
|
|
513
|
+
entry.phase = "auth";
|
|
514
|
+
const runInteractiveAuth = (): Promise<void> => this._authorizeAndRestart(params, entry);
|
|
508
515
|
if (alreadyQueued || params.inheritedStartupTurn) {
|
|
509
516
|
await runInteractiveAuth();
|
|
510
517
|
} else {
|
|
@@ -530,11 +537,15 @@ export class ConnectionCoordinator {
|
|
|
530
537
|
}
|
|
531
538
|
}
|
|
532
539
|
|
|
533
|
-
private async _authorizeAndRestart(
|
|
540
|
+
private async _authorizeAndRestart(
|
|
541
|
+
params: InteractiveAuthParams,
|
|
542
|
+
entry: ConnectAttemptEntry,
|
|
543
|
+
): Promise<void> {
|
|
534
544
|
const { serverName, ctx, controller, reset } = params;
|
|
535
545
|
this._ensureActive(controller.signal, serverName);
|
|
536
546
|
await this.deps.authorize(serverName, ctx, controller.signal, reset);
|
|
537
547
|
this._ensureActive(controller.signal, serverName);
|
|
548
|
+
entry.phase = "connection";
|
|
538
549
|
try {
|
|
539
550
|
await this._startAndGuardAbort(serverName, ctx, controller.signal);
|
|
540
551
|
} catch (err) {
|
|
@@ -618,6 +629,7 @@ export class ConnectionCoordinator {
|
|
|
618
629
|
controller,
|
|
619
630
|
reset,
|
|
620
631
|
waiters: 0,
|
|
632
|
+
phase: reset ? "auth" : "connection",
|
|
621
633
|
};
|
|
622
634
|
entry.promise = this._startConnectTask(
|
|
623
635
|
{
|
package/src/errors.ts
CHANGED
|
@@ -36,10 +36,15 @@ export class McpError extends Error {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
export type CancellationPhase = "auth" | "connection";
|
|
40
|
+
|
|
39
41
|
export class AuthCancelledError extends Error {
|
|
40
|
-
|
|
42
|
+
public readonly phase: CancellationPhase;
|
|
43
|
+
|
|
44
|
+
constructor(serverName: string, phase: CancellationPhase = "auth") {
|
|
41
45
|
super(`Authentication cancelled for ${serverName}`);
|
|
42
46
|
this.name = "AuthCancelledError";
|
|
47
|
+
this.phase = phase;
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
50
|
|
package/src/index.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
loadBootstrapConfigState,
|
|
20
20
|
} from "./session-lifecycle.js";
|
|
21
21
|
import { registerCommands } from "./register-commands.js";
|
|
22
|
+
import { hasNoMcpFlag } from "./commands.js";
|
|
22
23
|
import { registerManagementTools } from "./register-tools.js";
|
|
23
24
|
import { ServerManager, type ManagedServer } from "./server-manager.js";
|
|
24
25
|
import { ToolBridge } from "./tool-bridge.js";
|
|
@@ -48,7 +49,21 @@ export {
|
|
|
48
49
|
startupAuthLaterOption,
|
|
49
50
|
} from "./startup-auth.js";
|
|
50
51
|
|
|
51
|
-
export default async function (
|
|
52
|
+
export default async function (
|
|
53
|
+
pi: ExtensionAPI,
|
|
54
|
+
paths: {
|
|
55
|
+
bootstrapCwd?: string;
|
|
56
|
+
globalConfigPath?: string;
|
|
57
|
+
authStorageDir?: string;
|
|
58
|
+
browserOpen?: (url: string) => void | Promise<void>;
|
|
59
|
+
callbackServer?: CallbackServer;
|
|
60
|
+
argv?: readonly string[];
|
|
61
|
+
} = {},
|
|
62
|
+
): Promise<void> {
|
|
63
|
+
// WHY: Pi's --no-mcp only disables builtin:mcp, not installed replacements.
|
|
64
|
+
// Exit before logs, config reads, server setup, event handlers, commands, or tools.
|
|
65
|
+
if (hasNoMcpFlag(paths.argv ?? process.argv.slice(2))) return;
|
|
66
|
+
|
|
52
67
|
// ── 0. Opt-in diagnostic log ───────────────────────────────────────────────
|
|
53
68
|
// WHY: The debug log is created before anything else so bootstrap failures
|
|
54
69
|
// are captured too. The file is only touched when the first event is written.
|
package/src/register-commands.ts
CHANGED
|
@@ -99,7 +99,8 @@ async function handleMcpStart(
|
|
|
99
99
|
ctx.ui.notify("Usage: /mcp:start <server-name>", "error");
|
|
100
100
|
return;
|
|
101
101
|
}
|
|
102
|
-
|
|
102
|
+
const server = manager.getServer(serverName);
|
|
103
|
+
if (!server) {
|
|
103
104
|
ctx.ui.notify(`pi-mcp: No server named "${serverName}"`, "error");
|
|
104
105
|
return;
|
|
105
106
|
}
|
|
@@ -108,7 +109,11 @@ async function handleMcpStart(
|
|
|
108
109
|
ctx.ui.notify(`pi-mcp: Started ${serverName}`, "info");
|
|
109
110
|
} catch (err) {
|
|
110
111
|
if (err instanceof AuthCancelledError) {
|
|
111
|
-
|
|
112
|
+
const message =
|
|
113
|
+
server.config.auth && err.phase === "auth"
|
|
114
|
+
? err.message
|
|
115
|
+
: `Connection cancelled for ${serverName}`;
|
|
116
|
+
ctx.ui.notify(`pi-mcp: ${sanitizeTerminalText(message)}`, "info");
|
|
112
117
|
return;
|
|
113
118
|
}
|
|
114
119
|
const msg = formatErrorMessage(err);
|