passport-bridge 0.7.1 → 0.8.1
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 +63 -12
- package/dist/activeProfile.js +122 -0
- package/dist/bridge.js +2 -9
- package/dist/cli.js +3 -1
- package/dist/cliArgs.js +8 -1
- package/dist/cliConnect.js +161 -0
- package/dist/cliDescribe.js +18 -0
- package/dist/cliLogin.js +96 -24
- package/dist/cliOffline.js +106 -19
- package/dist/cliOutput.js +14 -2
- package/dist/cliReady.js +56 -0
- package/dist/cliResult.js +29 -5
- package/dist/cliSupport.js +79 -7
- package/dist/completion.js +4 -1
- package/dist/credentialStorage.js +293 -0
- package/dist/credentials.js +67 -291
- package/dist/doctor.js +73 -6
- package/dist/passport.js +100 -129
- package/dist/tokenWorkspace.js +5 -0
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,20 +17,39 @@ Node.js 22.23.2 or newer is required.
|
|
|
17
17
|
Sign in once, then search and run the tools your workspace permits:
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
|
-
passport login --cloud https://passport.example.com --workspace ws_example
|
|
20
|
+
passport login --cloud https://passport.example.com --workspace ws_example --profile cli@ws_example
|
|
21
21
|
passport status
|
|
22
22
|
passport search "open issues"
|
|
23
23
|
passport describe github_search_issues
|
|
24
24
|
passport run github_search_issues '{"query":"SSO"}'
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
A successful login makes the new profile active. On a machine with more than
|
|
28
|
+
one stored profile, inspect or switch the active profile without a network
|
|
29
|
+
request:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
passport profiles
|
|
33
|
+
passport use cli@ws_example
|
|
34
|
+
passport status
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For Passport Cloud, `passport login --workspace <id>` can omit `--cloud`.
|
|
38
|
+
Self-hosted deployments pass their origin explicitly. Add `--no-open` to print
|
|
39
|
+
the device URL without launching a browser. This is the required form for SSH,
|
|
40
|
+
CI, and other headless sessions:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
passport login --workspace ws_example --profile cli@ws_example --no-open
|
|
44
|
+
```
|
|
45
|
+
|
|
27
46
|
`passport help [command]` documents every option. `passport help` and `passport version` work without a profile or network connection.
|
|
28
47
|
|
|
29
48
|
Remote Passport servers must use HTTPS. Plain HTTP is accepted only for local
|
|
30
49
|
development on `localhost`, `127.0.0.0/8`, or `::1`. Self-hosted deployments
|
|
31
50
|
should terminate HTTPS before using interactive profiles or agent keys.
|
|
32
51
|
|
|
33
|
-
Passport CLI 0.
|
|
52
|
+
Passport CLI 0.8.1 requires gateway MCP serverInfo `passport` version 0.5.0 or
|
|
34
53
|
newer and the exact four-tool governed CLI protocol. Older servers fail clearly
|
|
35
54
|
before the CLI makes a tool call.
|
|
36
55
|
|
|
@@ -42,8 +61,9 @@ before the CLI makes a tool call.
|
|
|
42
61
|
| `logout` | Revoke the credential and remove its local profile. |
|
|
43
62
|
| `status` | Show the workspace, member, client, and visible MCP connection states. |
|
|
44
63
|
| `doctor` | Check runtime, credential-store safety, configuration, reachability, authentication, and CLI protocol compatibility. |
|
|
64
|
+
| `ready` | Report whether the local CLI is ready for the requested operation. |
|
|
45
65
|
| `search [query]` | Search permitted tool definitions. An empty query lists the complete catalog. |
|
|
46
|
-
| `describe <tool>` | Print one exact definition and its full parameter schema.
|
|
66
|
+
| `describe <tool>` | Print one exact definition and its full parameter schema. JSON also exposes `name` and `inputSchema` aliases. |
|
|
47
67
|
| `run <tool> [json]` | Run a tool through Passport governance. |
|
|
48
68
|
| `connect [app...]` | Show secure member connection links and optionally wait for completion. |
|
|
49
69
|
| `resources [uri]` | List or read MCP resources. |
|
|
@@ -51,8 +71,10 @@ before the CLI makes a tool call.
|
|
|
51
71
|
| `discover` | Scan supported global MCP configuration and submit a privacy-minimized fleet report. |
|
|
52
72
|
| `session` | Process NDJSON commands through one authenticated MCP session. |
|
|
53
73
|
| `completion <shell>` | Generate completion for Bash, Zsh, or Fish without a profile or network. |
|
|
74
|
+
| `profiles` | List stored profiles and show the active profile without a network request. |
|
|
75
|
+
| `use <name>` | Select the active stored profile without a network request. |
|
|
54
76
|
| `help [command]` | Show general or command-specific help. |
|
|
55
|
-
| `version` | Show the package version and locally resolvable profile context.
|
|
77
|
+
| `version` | Show the package version and locally resolvable profile context. `--json` includes readiness fields. |
|
|
56
78
|
|
|
57
79
|
Every command accepts `--profile <name>`, `--json`, `--timeout <seconds>`, `--quiet`, and `--caller <id>` where applicable.
|
|
58
80
|
|
|
@@ -72,6 +94,18 @@ passport doctor --profile cli@ws_example
|
|
|
72
94
|
passport doctor --profile cli@ws_example --json
|
|
73
95
|
```
|
|
74
96
|
|
|
97
|
+
`passport version --json` works without a profile or network and returns
|
|
98
|
+
`version`, `profileCount`, `profileSelected`, `selectionSource`, `next`, and
|
|
99
|
+
optional endpoint/profile fields. `passport ready --json` runs the local,
|
|
100
|
+
authentication, server, and exact CLI-protocol preflight and returns `ready`,
|
|
101
|
+
`ok`, auth/profile context, `server`, and `checks`; use its `ready` value and
|
|
102
|
+
exit code as the automation gate rather than parsing human output.
|
|
103
|
+
|
|
104
|
+
When profile selection is unclear, start with `passport profiles` and choose
|
|
105
|
+
one with `passport use <name>`. Use `--profile <name>` for a one-off command or
|
|
106
|
+
`PASSPORT_PROFILE=<name>` for a shell scope; explicit selectors take precedence
|
|
107
|
+
over the active profile.
|
|
108
|
+
|
|
75
109
|
`doctor` checks Node.js, the credential file's shape/ownership/mode, auth
|
|
76
110
|
configuration, HTTPS policy, `/healthz`, the authenticated MCP handshake,
|
|
77
111
|
serverInfo compatibility, and the exact four-tool CLI protocol. It never prints
|
|
@@ -160,7 +194,7 @@ The CLI never retries a call after a network failure or timeout because the exec
|
|
|
160
194
|
For an MCP that needs member sign-in:
|
|
161
195
|
|
|
162
196
|
```sh
|
|
163
|
-
passport connect github --
|
|
197
|
+
passport connect github --wait 120
|
|
164
198
|
```
|
|
165
199
|
|
|
166
200
|
Without app names, `connect` uses one aggregate link when several visible MCPs need sign-in. Agent-key principals receive status and member guidance but cannot mint browser links.
|
|
@@ -184,12 +218,15 @@ With `--json`, stdout contains exactly one JSON value and no progress text. Hint
|
|
|
184
218
|
|
|
185
219
|
| Command | JSON value |
|
|
186
220
|
| ------------ | --------------------------------------------------------------------------------------------------------- |
|
|
187
|
-
| `login` | `{profile, endpoint, client, surface}`
|
|
221
|
+
| `login` | `{profile, endpoint, client, surface, active, next}` |
|
|
222
|
+
| `profiles` | `{profiles:[{name,active,client?}],active?}` |
|
|
223
|
+
| `use` | `{profile,active:true}` |
|
|
188
224
|
| `logout` | `{profile, removed, revoked}` |
|
|
189
|
-
| `status` | `{workspace, member, endpoint, auth, profile?, client, mcps}`
|
|
225
|
+
| `status` | `{workspace, member, endpoint, auth, profile?, client, mcps, next?}` |
|
|
190
226
|
| `doctor` | `{version,ok,auth,profile?,profileCount,endpointOrigin?,server?,checks}` (also emitted when checks fail). |
|
|
227
|
+
| `ready` | `{ready,version,ok,auth,profile?,profileCount,endpointOrigin?,server?,checks}` |
|
|
191
228
|
| `search` | The gateway search payload, including `results` or `catalog` and `note`. |
|
|
192
|
-
| `describe` | One tool definition.
|
|
229
|
+
| `describe` | One tool definition; JSON keeps `tool`/`parameters` and adds `name`/`inputSchema` compatibility aliases. |
|
|
193
230
|
| `run` | The complete governed MCP tool result, preserving every content block and upstream `structuredContent`. |
|
|
194
231
|
| `connect` | The gateway connection payload, including `connections` and optional `connectAllUrl`. |
|
|
195
232
|
| `resources` | The MCP list or read result. |
|
|
@@ -198,9 +235,11 @@ With `--json`, stdout contains exactly one JSON value and no progress text. Hint
|
|
|
198
235
|
| `session` | NDJSON envelopes: `{id?,ok,exitCode,result?}` or `{id?,ok:false,error,cause,exitCode,...}`. |
|
|
199
236
|
| `completion` | `{shell,script}`. Human mode emits the raw completion script. |
|
|
200
237
|
| `help` | `{command, help}` |
|
|
201
|
-
| `version` | `{version,
|
|
238
|
+
| `version` | `{version,profileCount,profileSelected,selectionSource,next,requiredInput?,endpoint?,profile?}` |
|
|
202
239
|
|
|
203
|
-
JSON errors have a stable shape
|
|
240
|
+
JSON errors have a stable base shape and may add safe remediation fields such
|
|
241
|
+
as `hint`, `profiles`, `next` (`argv` plus a description), `remediation`,
|
|
242
|
+
`retry`, `requiredInput`, or `connectUrl`:
|
|
204
243
|
|
|
205
244
|
```json
|
|
206
245
|
{ "error": "...", "cause": "blocked", "exitCode": 5, "app": "github" }
|
|
@@ -224,9 +263,19 @@ Credentials are named slots in one file, `~/.passport/credentials.json`, with mo
|
|
|
224
263
|
|
|
225
264
|
1. `--profile <name>`
|
|
226
265
|
2. `PASSPORT_PROFILE`
|
|
227
|
-
3. the
|
|
266
|
+
3. the active profile selected by `passport use <name>`
|
|
267
|
+
4. the sole stored profile
|
|
268
|
+
|
|
269
|
+
If several profiles exist and none is active, select one explicitly. The
|
|
270
|
+
credential store is shared with `passport-bridge` and refresh exchanges are
|
|
271
|
+
coordinated across processes. The active-profile pointer contains no
|
|
272
|
+
credential and is local to this Passport credential store.
|
|
228
273
|
|
|
229
|
-
|
|
274
|
+
When no explicit, environment, or active selector is present, CLI 0.8.1
|
|
275
|
+
detects exactly one CLI-owned profile (`client: "cli"` or the exact legacy
|
|
276
|
+
`cli@<workspace>` shape) and makes that existing profile active. If more than
|
|
277
|
+
one CLI candidate remains, run `passport profiles`, choose with `passport use <name>`,
|
|
278
|
+
or pass an explicit `--profile`; Passport refuses to guess.
|
|
230
279
|
|
|
231
280
|
Profile names are 1-80 letters, numbers, dots, underscores, `@` signs, or hyphens, and must start with a letter or number. Credential updates are atomically replaced at mode `0600`, including when several profiles rotate concurrently.
|
|
232
281
|
|
|
@@ -258,6 +307,8 @@ Supply an existing Passport agent key from the job's secret manager:
|
|
|
258
307
|
export PASSPORT_ENDPOINT=https://passport.example.com/mcp
|
|
259
308
|
export PASSPORT_AGENT_KEY="$CI_PASSPORT_AGENT_KEY"
|
|
260
309
|
passport search "deployment status" --json
|
|
310
|
+
passport version --json
|
|
311
|
+
passport ready --json
|
|
261
312
|
```
|
|
262
313
|
|
|
263
314
|
Environment authentication wins over stored profiles, persists nothing, and sends the agent key directly to the gateway. Do not combine it with `--profile`. `PASSPORT_PROFILE` is only for stored interactive credentials.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { closeSync, constants, fchmodSync, fstatSync, lstatSync, openSync, readFileSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { credentialDir, enforceOwner, ensurePrivateDirectory, isMissing, syncDirectory, writePrivateAtomic } from "./credentialStorage.js";
|
|
4
|
+
const MAX_ACTIVE_PROFILE_BYTES = 512;
|
|
5
|
+
function activeFile() {
|
|
6
|
+
return join(credentialDir(), ".active-profile");
|
|
7
|
+
}
|
|
8
|
+
export function activeProfilePath() {
|
|
9
|
+
return activeFile();
|
|
10
|
+
}
|
|
11
|
+
export function readActiveProfileDocument(assertName) {
|
|
12
|
+
if (!ensurePrivateDirectory(false))
|
|
13
|
+
return { kind: "missing" };
|
|
14
|
+
let pathStat;
|
|
15
|
+
try {
|
|
16
|
+
pathStat = lstatSync(activeFile());
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (isMissing(error))
|
|
20
|
+
return { kind: "missing" };
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
if (pathStat.isSymbolicLink() || !pathStat.isFile()) {
|
|
24
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
25
|
+
}
|
|
26
|
+
enforceOwner(pathStat, "active profile");
|
|
27
|
+
if (pathStat.size > MAX_ACTIVE_PROFILE_BYTES) {
|
|
28
|
+
throw new Error("Passport active profile is unexpectedly large; refusing to read it.");
|
|
29
|
+
}
|
|
30
|
+
let descriptor;
|
|
31
|
+
try {
|
|
32
|
+
descriptor = openSync(activeFile(), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
33
|
+
const stat = fstatSync(descriptor);
|
|
34
|
+
if (!stat.isFile())
|
|
35
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
36
|
+
if (stat.dev !== pathStat.dev || stat.ino !== pathStat.ino) {
|
|
37
|
+
throw new Error("Passport active profile changed while it was being opened; refusing to read it.");
|
|
38
|
+
}
|
|
39
|
+
enforceOwner(stat, "active profile");
|
|
40
|
+
if (stat.size > MAX_ACTIVE_PROFILE_BYTES) {
|
|
41
|
+
throw new Error("Passport active profile is unexpectedly large; refusing to read it.");
|
|
42
|
+
}
|
|
43
|
+
if (process.platform !== "win32")
|
|
44
|
+
fchmodSync(descriptor, 0o600);
|
|
45
|
+
const profile = readFileSync(descriptor, "utf8").trim();
|
|
46
|
+
if (!profile)
|
|
47
|
+
return { kind: "missing" };
|
|
48
|
+
try {
|
|
49
|
+
assertName(profile);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
throw new Error("Passport active profile is invalid; choose a profile with `passport use <name>`.");
|
|
53
|
+
}
|
|
54
|
+
return { kind: "present", profile };
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (isMissing(error))
|
|
58
|
+
return { kind: "missing" };
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
if (descriptor !== undefined)
|
|
63
|
+
closeSync(descriptor);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Best-effort read for command resolution. A stale or damaged pointer must
|
|
67
|
+
* never make the CLI guess a credential; callers can fall back to the normal
|
|
68
|
+
* sole-profile rule or report ambiguity. */
|
|
69
|
+
export function readActiveProfile(assertName) {
|
|
70
|
+
try {
|
|
71
|
+
const result = readActiveProfileDocument(assertName);
|
|
72
|
+
return result.kind === "missing" ? undefined : result.profile;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export function writeActiveProfileAtomic(name, assertName) {
|
|
79
|
+
assertName(name);
|
|
80
|
+
writePrivateAtomic(activeFile(), ".active-profile", `${name}\n`, "active profile");
|
|
81
|
+
}
|
|
82
|
+
/** The active-profile pointer contains no credential material. If an old or
|
|
83
|
+
* damaged installation left a symlink at that exact path, selecting a profile
|
|
84
|
+
* may safely unlink the symlink itself (never its target) before recreating the
|
|
85
|
+
* owner-only regular file. Other non-regular targets still fail closed. */
|
|
86
|
+
export function prepareActiveProfileForSelection() {
|
|
87
|
+
let stat;
|
|
88
|
+
try {
|
|
89
|
+
stat = lstatSync(activeFile());
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (isMissing(error))
|
|
93
|
+
return;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
if (stat.isSymbolicLink()) {
|
|
97
|
+
unlinkSync(activeFile());
|
|
98
|
+
syncDirectory();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!stat.isFile()) {
|
|
102
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
103
|
+
}
|
|
104
|
+
enforceOwner(stat, "active profile");
|
|
105
|
+
}
|
|
106
|
+
export function clearActiveProfileAtomic() {
|
|
107
|
+
let stat;
|
|
108
|
+
try {
|
|
109
|
+
stat = lstatSync(activeFile());
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (isMissing(error))
|
|
113
|
+
return;
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
117
|
+
throw new Error("Passport active profile must be a regular file, not a symlink.");
|
|
118
|
+
}
|
|
119
|
+
enforceOwner(stat, "active profile");
|
|
120
|
+
unlinkSync(activeFile());
|
|
121
|
+
syncDirectory();
|
|
122
|
+
}
|
package/dist/bridge.js
CHANGED
|
@@ -9,6 +9,8 @@ import { VERSION } from "./version.js";
|
|
|
9
9
|
import { bridgePollIntervalMs } from "./bridgeConfig.js";
|
|
10
10
|
import { readBoundedJson } from "./boundedResponse.js";
|
|
11
11
|
import { createBoundedBridgeShutdown } from "./bridgeShutdown.js";
|
|
12
|
+
import { workspaceOfToken } from "./tokenWorkspace.js";
|
|
13
|
+
export { workspaceOfToken } from "./tokenWorkspace.js";
|
|
12
14
|
/** Everything but the MCP protocol goes to stderr — stdout is the transport. */
|
|
13
15
|
export function log(...args) {
|
|
14
16
|
process.stderr.write(`[passport-bridge] ${args.join(" ")}\n`);
|
|
@@ -21,15 +23,6 @@ function safeHostname() {
|
|
|
21
23
|
return "unknown";
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
|
-
/**
|
|
25
|
-
* The workspace a Passport token belongs to. Every token the server mints is
|
|
26
|
-
* workspace-prefixed (`<wsId>~...`, see server store.mintToken), so the tenant
|
|
27
|
-
* is recoverable from the string itself. Returns "" for an env-supplied or
|
|
28
|
-
* malformed token with no recognizable prefix.
|
|
29
|
-
*/
|
|
30
|
-
export function workspaceOfToken(token) {
|
|
31
|
-
return token.match(/^(ws_[A-Za-z0-9][A-Za-z0-9_-]{0,124})~/)?.[1] ?? "";
|
|
32
|
-
}
|
|
33
26
|
/**
|
|
34
27
|
* Shown to the AI client (and logged) when the bridge refuses to switch tenants:
|
|
35
28
|
* the on-disk credential now belongs to a DIFFERENT workspace than this process
|
package/dist/cli.js
CHANGED
|
@@ -290,7 +290,9 @@ async function login() {
|
|
|
290
290
|
}
|
|
291
291
|
const refreshToken = credential.refreshToken;
|
|
292
292
|
const endpoint = legacyCredentialEndpoint(credential.endpoint, cloud);
|
|
293
|
-
await withProfileLock(profile, async () =>
|
|
293
|
+
await withProfileLock(profile, async () => {
|
|
294
|
+
writeProfile(profile, { refreshToken, endpoint, instanceId, client, target });
|
|
295
|
+
});
|
|
294
296
|
out("");
|
|
295
297
|
out(` ✓ Signed in. Saved profile "${profile}" to ${credsPath()}`);
|
|
296
298
|
out(` Now point your AI app at: passport-bridge serve --profile ${profile}`);
|
package/dist/cliArgs.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export const COMMANDS = [
|
|
2
2
|
"login",
|
|
3
3
|
"logout",
|
|
4
|
+
"profiles",
|
|
5
|
+
"use",
|
|
4
6
|
"status",
|
|
5
7
|
"doctor",
|
|
8
|
+
"ready",
|
|
6
9
|
"search",
|
|
7
10
|
"describe",
|
|
8
11
|
"run",
|
|
@@ -25,6 +28,7 @@ const FLAGS = {
|
|
|
25
28
|
cloud: { kind: "value", commands: ["login"] },
|
|
26
29
|
workspace: { kind: "value", commands: ["login"] },
|
|
27
30
|
client: { kind: "value", commands: ["login"] },
|
|
31
|
+
"no-open": { kind: "boolean", commands: ["login"] },
|
|
28
32
|
app: { kind: "value", commands: ["search"] },
|
|
29
33
|
args: { kind: "value", commands: ["run"] },
|
|
30
34
|
set: { kind: "repeat", commands: ["run", "prompts"] },
|
|
@@ -123,8 +127,11 @@ function validateShape(command, positionals) {
|
|
|
123
127
|
const max = {
|
|
124
128
|
login: 0,
|
|
125
129
|
logout: 0,
|
|
130
|
+
profiles: 0,
|
|
131
|
+
use: 1,
|
|
126
132
|
status: 0,
|
|
127
133
|
doctor: 0,
|
|
134
|
+
ready: 0,
|
|
128
135
|
describe: 1,
|
|
129
136
|
run: 2,
|
|
130
137
|
resources: 1,
|
|
@@ -135,7 +142,7 @@ function validateShape(command, positionals) {
|
|
|
135
142
|
help: 1,
|
|
136
143
|
version: 0
|
|
137
144
|
};
|
|
138
|
-
const minimum = { describe: 1, run: 1, completion: 1 };
|
|
145
|
+
const minimum = { describe: 1, run: 1, use: 1, completion: 1 };
|
|
139
146
|
if (positionals.length < (minimum[command] ?? 0)) {
|
|
140
147
|
throw new ArgError(`${command} requires ${command === "run" ? "a tool name" : command === "completion" ? "bash, zsh, or fish" : "a name"}.`);
|
|
141
148
|
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { bool, value } from "./cliArgs.js";
|
|
2
|
+
import { SERVER_TOO_OLD } from "./cliProtocol.js";
|
|
3
|
+
import { firstText, payloadOf, requireSuccess } from "./cliResult.js";
|
|
4
|
+
import { failure, openUrl, sleep } from "./cliSupport.js";
|
|
5
|
+
export function managePayload(result) {
|
|
6
|
+
requireSuccess(result);
|
|
7
|
+
const payload = payloadOf(result, "connection");
|
|
8
|
+
if (!payload.workspace ||
|
|
9
|
+
typeof payload.workspace !== "object" ||
|
|
10
|
+
!payload.member ||
|
|
11
|
+
typeof payload.member !== "object" ||
|
|
12
|
+
typeof payload.client !== "string" ||
|
|
13
|
+
payload.surface !== "cli" ||
|
|
14
|
+
typeof payload.auth !== "string" ||
|
|
15
|
+
!Array.isArray(payload.connections)) {
|
|
16
|
+
return failure(SERVER_TOO_OLD, 1, "server_too_old");
|
|
17
|
+
}
|
|
18
|
+
return payload;
|
|
19
|
+
}
|
|
20
|
+
function assertConnectionCanStart(auth, needSignin, urls, human, shouldOpen, shouldAct) {
|
|
21
|
+
if (auth.kind === "agent-key" && needSignin.length && shouldAct) {
|
|
22
|
+
return failure("Agent-key authentication cannot create a person's sign-in link.", 3, "member_profile_required", {
|
|
23
|
+
app: needSignin.length === 1 ? String(needSignin[0].app ?? "") || undefined : undefined,
|
|
24
|
+
hint: "Use an interactive member profile, delegate the agent to a member, or configure a company-managed connection.",
|
|
25
|
+
remediation: {
|
|
26
|
+
action: "use_member_profile_or_managed_connection",
|
|
27
|
+
description: "Use an interactive member profile, delegate the agent to a member, or configure a company-managed connection."
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (shouldOpen && needSignin.length && !urls.length) {
|
|
32
|
+
return failure(human.join("\n"), 3, "connection_action_required", {
|
|
33
|
+
app: needSignin.length === 1 ? String(needSignin[0].app ?? "") || undefined : undefined,
|
|
34
|
+
hint: "Complete the connection action reported by Passport, then retry.",
|
|
35
|
+
remediation: {
|
|
36
|
+
action: "complete_reported_connection_action",
|
|
37
|
+
description: "Complete the connection action reported by Passport, then retry."
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export async function connect(args, runtime, withGateway) {
|
|
43
|
+
const alias = value(args, "alias");
|
|
44
|
+
if (alias !== undefined && !/^[A-Za-z0-9 -]{1,32}$/.test(alias.trim()))
|
|
45
|
+
return failure("--alias must contain 1-32 letters, numbers, spaces, or hyphens.", 6, "usage");
|
|
46
|
+
const requested = args.positionals;
|
|
47
|
+
return withGateway(args, runtime, async (gateway, auth) => {
|
|
48
|
+
let managed = managePayload(await gateway.callTool("passport_manage_connections", {
|
|
49
|
+
...(alias !== undefined ? { alias: alias.trim() } : {}),
|
|
50
|
+
...(requested.length ? { apps: requested } : {})
|
|
51
|
+
}));
|
|
52
|
+
const rows = requested.length
|
|
53
|
+
? requested.map((app) => findConnection(managed.connections, app))
|
|
54
|
+
: managed.connections;
|
|
55
|
+
const human = connectionLines(managed, rows, requested.length > 0);
|
|
56
|
+
const urls = connectionUrls(managed, rows, requested.length > 0);
|
|
57
|
+
const needSignin = rows.filter((row) => row.status === "needs_your_signin");
|
|
58
|
+
const waitSeconds = value(args, "wait") ? Number(value(args, "wait")) : 0;
|
|
59
|
+
assertConnectionCanStart(auth, needSignin, urls, human, bool(args, "open"), bool(args, "open") || waitSeconds > 0);
|
|
60
|
+
if (bool(args, "open")) {
|
|
61
|
+
if (urls.length > 1)
|
|
62
|
+
return failure("--open requires a single connect URL. Name one MCP, or use the aggregate connect flow.", 6, "usage");
|
|
63
|
+
if (urls[0]) {
|
|
64
|
+
if (!bool(args, "quiet"))
|
|
65
|
+
runtime.stderr.write("Opening the connection page in your browser...\n");
|
|
66
|
+
if (!openUrl(urls[0]))
|
|
67
|
+
runtime.stderr.write(`Could not open a browser automatically. Open: ${urls[0]}\n`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (!waitSeconds)
|
|
71
|
+
return { json: managed, human: human.join("\n") };
|
|
72
|
+
const targets = rows.filter((row) => row.status === "needs_your_signin").map((row) => String(row.app));
|
|
73
|
+
if (!targets.length)
|
|
74
|
+
return { json: managed, human: human.join("\n") };
|
|
75
|
+
const aggregateFlow = !requested.length && Boolean(managed.connectAllUrl);
|
|
76
|
+
const allTargetsHaveFlow = aggregateFlow || needSignin.every((row) => typeof row.connectUrl === "string");
|
|
77
|
+
if (!allTargetsHaveFlow) {
|
|
78
|
+
return failure(human.join("\n"), 3, "connection_action_required", {
|
|
79
|
+
app: targets.length === 1 ? targets[0] : undefined,
|
|
80
|
+
hint: "Complete the connection action reported by Passport, then retry the command.",
|
|
81
|
+
remediation: {
|
|
82
|
+
action: "complete_reported_connection_action",
|
|
83
|
+
description: "Complete the connection action reported by Passport, then retry."
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (!bool(args, "json") && (!bool(args, "quiet") || !bool(args, "open")))
|
|
88
|
+
runtime.stdout.write(`${human.join("\n")}\n`);
|
|
89
|
+
if (!bool(args, "quiet")) {
|
|
90
|
+
runtime.stderr.write(`Waiting up to ${waitSeconds} seconds for ${targets.length === 1 ? targets[0] : "the connections"}...\n`);
|
|
91
|
+
}
|
|
92
|
+
const waitResult = await waitForConnections(gateway, targets, waitSeconds, runtime.signal);
|
|
93
|
+
if (!waitResult.connected) {
|
|
94
|
+
const note = typeof waitResult.note === "string" ? waitResult.note : "Timed out waiting for the MCP connection.";
|
|
95
|
+
return failure(note, 1, "connection_timeout", { app: targets.length === 1 ? targets[0] : undefined });
|
|
96
|
+
}
|
|
97
|
+
managed = managePayload(await gateway.callTool("passport_manage_connections", { includeLinks: false }));
|
|
98
|
+
return { json: managed, human: typeof waitResult.note === "string" ? waitResult.note : "Connection ready." };
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function findConnection(rows, requested) {
|
|
102
|
+
const needle = requested.trim().toLowerCase();
|
|
103
|
+
const row = rows.find((entry) => String(entry.app ?? "").toLowerCase() === needle || String(entry.name ?? "").toLowerCase() === needle);
|
|
104
|
+
if (!row)
|
|
105
|
+
return failure(`Unknown MCP "${requested}". Run passport status to see visible MCPs.`, 6, "unknown_app", {
|
|
106
|
+
app: requested,
|
|
107
|
+
hint: "Run: passport status",
|
|
108
|
+
next: [{ argv: ["passport", "status"], description: "List visible MCP connections." }]
|
|
109
|
+
});
|
|
110
|
+
return row;
|
|
111
|
+
}
|
|
112
|
+
function connectionUrls(managed, rows, explicitlyNamed) {
|
|
113
|
+
const needSignin = rows.filter((row) => row.status === "needs_your_signin");
|
|
114
|
+
if (!explicitlyNamed && needSignin.length >= 2 && managed.connectAllUrl)
|
|
115
|
+
return [managed.connectAllUrl];
|
|
116
|
+
return needSignin.map((row) => row.connectUrl).filter((url) => typeof url === "string");
|
|
117
|
+
}
|
|
118
|
+
function connectionLines(managed, rows, explicitlyNamed) {
|
|
119
|
+
const urls = connectionUrls(managed, rows, explicitlyNamed);
|
|
120
|
+
if (!explicitlyNamed && urls.length === 1 && rows.filter((row) => row.status === "needs_your_signin").length >= 2) {
|
|
121
|
+
return [`Connect your MCPs: ${urls[0]}`];
|
|
122
|
+
}
|
|
123
|
+
const lines = [];
|
|
124
|
+
for (const row of rows) {
|
|
125
|
+
const name = String(row.name ?? row.app ?? "MCP");
|
|
126
|
+
if (row.status === "needs_your_signin" && typeof row.connectUrl === "string")
|
|
127
|
+
lines.push(`${name}: ${row.connectUrl}`);
|
|
128
|
+
else if (row.status === "connected" || row.status === "org_managed")
|
|
129
|
+
lines.push(`${name}: already connected`);
|
|
130
|
+
else if (typeof row.action === "string")
|
|
131
|
+
lines.push(`${name}: ${row.action}`);
|
|
132
|
+
else
|
|
133
|
+
lines.push(`${name}: ${String(row.status ?? "unknown")}`);
|
|
134
|
+
}
|
|
135
|
+
if (!lines.length || (!explicitlyNamed && !rows.some((row) => row.status === "needs_your_signin")))
|
|
136
|
+
lines.push(managed.note || "Nothing needs connecting.");
|
|
137
|
+
return lines;
|
|
138
|
+
}
|
|
139
|
+
async function waitForConnections(gateway, apps, seconds, signal) {
|
|
140
|
+
const deadlineAt = Date.now() + seconds * 1000;
|
|
141
|
+
let latest = { connected: false };
|
|
142
|
+
do {
|
|
143
|
+
const remaining = Math.max(1, Math.ceil((deadlineAt - Date.now()) / 1000));
|
|
144
|
+
const chunk = Math.min(120, remaining);
|
|
145
|
+
const result = requireSuccess(await gateway.callTool("passport_wait_for_connection", apps.length === 1 ? { app: apps[0], timeoutSeconds: chunk } : { apps, timeoutSeconds: chunk }, (chunk + 5) * 1000));
|
|
146
|
+
const text = firstText(result);
|
|
147
|
+
try {
|
|
148
|
+
latest = JSON.parse(text);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
return failure("Passport returned an invalid connection wait payload.", 1, "invalid_server_response");
|
|
152
|
+
}
|
|
153
|
+
if (latest.connected === true || latest.status === "connected")
|
|
154
|
+
return { ...latest, connected: true };
|
|
155
|
+
if (latest.status === "needs_setup")
|
|
156
|
+
return { ...latest, connected: false };
|
|
157
|
+
if (Date.now() < deadlineAt)
|
|
158
|
+
await sleep(Math.min(250, deadlineAt - Date.now()), signal);
|
|
159
|
+
} while (Date.now() < deadlineAt);
|
|
160
|
+
return { ...latest, connected: false };
|
|
161
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize a search result for the CLI's describe contract.
|
|
3
|
+
*
|
|
4
|
+
* `tool` and `parameters` are the original Passport fields. `name` and
|
|
5
|
+
* `inputSchema` are compatibility aliases for clients that consume the
|
|
6
|
+
* conventional MCP vocabulary. Keep all server-provided fields intact.
|
|
7
|
+
*/
|
|
8
|
+
export function describedToolPayload(exact, fallbackName) {
|
|
9
|
+
const tool = typeof exact.tool === "string" && exact.tool ? exact.tool : fallbackName;
|
|
10
|
+
const parameters = exact.parameters ?? {};
|
|
11
|
+
return {
|
|
12
|
+
...exact,
|
|
13
|
+
tool,
|
|
14
|
+
parameters,
|
|
15
|
+
name: tool,
|
|
16
|
+
inputSchema: parameters
|
|
17
|
+
};
|
|
18
|
+
}
|