pi-mcp-extension 1.16.1 → 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 +88 -1
- package/src/connection-coordinator.ts +21 -9
- package/src/errors.ts +6 -1
- package/src/index.ts +16 -1
- package/src/register-commands.ts +9 -3
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
|
@@ -64,7 +64,7 @@ export interface ServerCompletionEntry {
|
|
|
64
64
|
|
|
65
65
|
export function serverNameCompletions(
|
|
66
66
|
argumentPrefix: string,
|
|
67
|
-
servers: ServerCompletionEntry[],
|
|
67
|
+
servers: readonly ServerCompletionEntry[],
|
|
68
68
|
options: { authOnly?: boolean } = {},
|
|
69
69
|
) {
|
|
70
70
|
const prefix = argumentPrefix.trimStart();
|
|
@@ -88,4 +88,91 @@ export function serverNameCompletions(
|
|
|
88
88
|
return completions.length > 0 ? completions : null;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
const RESET_FLAG = "--reset";
|
|
92
|
+
const RESET_FLAG_DESCRIPTION = "Discard stored credentials and re-authorize";
|
|
93
|
+
|
|
94
|
+
function resetFlagCompletion(prefix: string, servers: readonly ServerCompletionEntry[]) {
|
|
95
|
+
if (!prefix.startsWith("-") || !RESET_FLAG.startsWith(prefix)) return null;
|
|
96
|
+
if (!servers.some((server) => Boolean(server.config.auth))) return null;
|
|
97
|
+
return { value: `${RESET_FLAG} `, label: RESET_FLAG, description: RESET_FLAG_DESCRIPTION };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function leadingResetServerCompletions(
|
|
101
|
+
resetSeparator: string,
|
|
102
|
+
serverPrefix: string,
|
|
103
|
+
servers: readonly ServerCompletionEntry[],
|
|
104
|
+
) {
|
|
105
|
+
const resetMatches = serverNameCompletions(serverPrefix, servers, { authOnly: true });
|
|
106
|
+
if (!resetMatches) return [];
|
|
107
|
+
|
|
108
|
+
const serverNames = servers.map((server) => server.name);
|
|
109
|
+
const completions = [];
|
|
110
|
+
for (const item of resetMatches) {
|
|
111
|
+
// WHY: Pi replaces the entire argument prefix, so the completion must reinsert the flag.
|
|
112
|
+
const value = `${resetSeparator}${item.value}`;
|
|
113
|
+
// Exact configured names and trailing flags can take precedence over a leading flag.
|
|
114
|
+
const parsed = parseAuthArguments(value, serverNames);
|
|
115
|
+
if (parsed.reset && parsed.name === item.value) completions.push({ ...item, value });
|
|
116
|
+
}
|
|
117
|
+
return completions;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function authArgumentCompletions(argumentPrefix: string, servers: readonly ServerCompletionEntry[]) {
|
|
121
|
+
const prefix = argumentPrefix.trimStart();
|
|
122
|
+
const literalMatches = serverNameCompletions(prefix, servers, { authOnly: true }) ?? [];
|
|
123
|
+
const resetPrefix = /^--reset\s+/.exec(prefix);
|
|
124
|
+
if (!resetPrefix) {
|
|
125
|
+
const flagCompletion = resetFlagCompletion(prefix, servers);
|
|
126
|
+
const completions = flagCompletion ? [...literalMatches, flagCompletion] : literalMatches;
|
|
127
|
+
return completions.length > 0 ? completions : null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const serverPrefix = prefix.slice(resetPrefix[0].length);
|
|
131
|
+
const completions = [
|
|
132
|
+
...literalMatches,
|
|
133
|
+
...leadingResetServerCompletions(resetPrefix[0], serverPrefix, servers),
|
|
134
|
+
];
|
|
135
|
+
return completions.length > 0 ? completions : null;
|
|
136
|
+
}
|
|
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
|
+
|
|
91
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
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
} from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
10
10
|
import {
|
|
11
|
+
authArgumentCompletions,
|
|
11
12
|
parseAuthArguments,
|
|
12
13
|
parseResourceArguments,
|
|
13
14
|
serverNameCompletions,
|
|
@@ -98,7 +99,8 @@ async function handleMcpStart(
|
|
|
98
99
|
ctx.ui.notify("Usage: /mcp:start <server-name>", "error");
|
|
99
100
|
return;
|
|
100
101
|
}
|
|
101
|
-
|
|
102
|
+
const server = manager.getServer(serverName);
|
|
103
|
+
if (!server) {
|
|
102
104
|
ctx.ui.notify(`pi-mcp: No server named "${serverName}"`, "error");
|
|
103
105
|
return;
|
|
104
106
|
}
|
|
@@ -107,7 +109,11 @@ async function handleMcpStart(
|
|
|
107
109
|
ctx.ui.notify(`pi-mcp: Started ${serverName}`, "info");
|
|
108
110
|
} catch (err) {
|
|
109
111
|
if (err instanceof AuthCancelledError) {
|
|
110
|
-
|
|
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");
|
|
111
117
|
return;
|
|
112
118
|
}
|
|
113
119
|
const msg = formatErrorMessage(err);
|
|
@@ -331,7 +337,7 @@ export function registerCommands(pi: ExtensionAPI, deps: RegisterCommandsDeps):
|
|
|
331
337
|
pi.registerCommand("mcp:auth", {
|
|
332
338
|
description: "Connect with OAuth; use /mcp:auth <name> --reset to discard stored credentials.",
|
|
333
339
|
handler: (args, ctx) => handleMcpAuth(args, ctx, deps),
|
|
334
|
-
getArgumentCompletions: (prefix) =>
|
|
340
|
+
getArgumentCompletions: (prefix) => authArgumentCompletions(prefix, manager.getAllServers()),
|
|
335
341
|
});
|
|
336
342
|
|
|
337
343
|
// ── 7. /mcp:resources — list or read MCP resources ───────────────────────
|