@bridge_gpt/mcp-server 0.2.72 → 0.2.74
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 +5 -5
- package/build/agent-launchers/claude-executor-adapter.js +15 -0
- package/build/agents.generated.js +6 -6
- package/build/bridge-config.js +7 -1
- package/build/bridge-permissions.js +30 -0
- package/build/ci-system-detection.js +50 -0
- package/build/ci-trigger-adapters.js +631 -0
- package/build/ci-trigger-install.js +106 -0
- package/build/codex-launcher-pin.js +33 -2
- package/build/codex-skill-adapter.js +3 -1
- package/build/command-host.js +33 -0
- package/build/command-provisioning.js +4 -3
- package/build/commands.generated.js +7 -6
- package/build/conductor/bridge-api-client.js +867 -35
- package/build/conductor/bring-up-facts.js +18 -5
- package/build/conductor/command-drift-production.js +42 -17
- package/build/conductor/command-drift.js +48 -1
- package/build/conductor/declare-blocked.js +554 -0
- package/build/conductor/declared-extras.js +335 -0
- package/build/conductor/doctor.js +108 -21
- package/build/conductor/driver-commands.js +141 -0
- package/build/conductor/driver-guard.js +277 -0
- package/build/conductor/driver-lapse.js +321 -0
- package/build/conductor/driver-pass-output.js +104 -0
- package/build/conductor/driver-pass.js +571 -0
- package/build/conductor/driver-register.js +176 -0
- package/build/conductor/driver-release.js +174 -0
- package/build/conductor/driver-session-state.js +315 -0
- package/build/conductor/epic-branch-ci-inspection.js +281 -43
- package/build/conductor/epic-implementer-cli.js +199 -15
- package/build/conductor/epic-server-database.js +162 -0
- package/build/conductor/epic-server-guidance.js +278 -0
- package/build/conductor/epic-server-journal.js +239 -0
- package/build/conductor/epic-server-processes.js +324 -0
- package/build/conductor/epic-server-recipe.js +397 -0
- package/build/conductor/epic-server-runtime.js +1207 -0
- package/build/conductor/epic-server-sfcc-upload.js +1567 -0
- package/build/conductor/epic-server-sfcc.js +85 -0
- package/build/conductor/epic-server-state.js +427 -0
- package/build/conductor/epic-server.js +647 -0
- package/build/conductor/gate-discovery.js +196 -11
- package/build/conductor/gate-settings-review.js +261 -4
- package/build/conductor/launch-need-probes.js +6 -53
- package/build/conductor/launch-plan.js +313 -156
- package/build/conductor/prerequisite-inputs.js +879 -45
- package/build/conductor/readiness-assessment.js +16 -1
- package/build/conductor/readiness-cli.js +20 -9
- package/build/conductor/readiness-prerequisites.js +28 -9
- package/build/conductor/readiness-sections.js +133 -126
- package/build/conductor/readiness.js +1 -1
- package/build/conductor/recovery-cli.js +13 -2
- package/build/conductor/tools.js +6 -5
- package/build/conductor/wait-run-attention.js +2 -2
- package/build/conductor/wait-run-cli.js +208 -22
- package/build/conductor/worker-database-doctor-inspector.js +33 -2
- package/build/conductor/worker-setup-tally.js +27 -0
- package/build/credential-delivery.js +280 -0
- package/build/credential-launch-safety.js +142 -0
- package/build/credential-materialization.js +91 -16
- package/build/credential-references-node-deps.js +29 -0
- package/build/credential-references.js +1394 -0
- package/build/credential-references.test-fixtures.js +211 -0
- package/build/credential-scan-conventions.js +145 -0
- package/build/credential-scan.js +517 -0
- package/build/credential-store.js +121 -1
- package/build/credentials-cli.js +252 -3
- package/build/dependency-install-declarations.js +102 -1
- package/build/docs.generated.js +10 -3
- package/build/doctor.js +95 -3
- package/build/drive-epic-help.js +21 -2
- package/build/drive-epic.js +2095 -375
- package/build/engine-paths.js +20 -0
- package/build/epic-branch-ci-summary.js +157 -0
- package/build/epic-capability-statement.js +175 -0
- package/build/epic-ci-workflow-drift.js +77 -23
- package/build/epic-ci-workflow-install.js +137 -44
- package/build/epic-ci-workflow-v2.js +274 -0
- package/build/epic-ci-workflow-v3.js +273 -0
- package/build/epic-ci-workflow.js +165 -18
- package/build/epic-implementer-bin.js +2204 -56
- package/build/epic-launch-context.js +702 -0
- package/build/epic-server-record.js +288 -0
- package/build/epic-shared/bridge-client.js +11 -3
- package/build/escalation-reachability.js +43 -0
- package/build/executor/ci-fix-test-runs.js +334 -0
- package/build/executor/completion-contract.js +8 -1
- package/build/executor/credential-delivery-step.js +168 -0
- package/build/executor/deps.js +3 -1
- package/build/executor/env.js +98 -23
- package/build/executor/epic-server-context.js +157 -0
- package/build/executor/gate-qualification-job.js +127 -26
- package/build/executor/guarded-fast-forward-push.js +13 -8
- package/build/executor/http-client.js +52 -5
- package/build/executor/job-runner.js +577 -81
- package/build/executor/observation.js +8 -0
- package/build/executor/permissions.js +85 -3
- package/build/executor/pre-pr-checks.js +363 -0
- package/build/executor/prompt-spec.js +149 -3
- package/build/executor/reconcile-overlap-job.js +5 -3
- package/build/executor/required-work-diagnostics.js +4 -2
- package/build/executor/required-work-ledger.js +936 -38
- package/build/executor/results.js +58 -0
- package/build/executor/terminal-mutation.js +8 -0
- package/build/executor/worker-command.js +72 -3
- package/build/executor/worker-database-config.js +281 -0
- package/build/executor/worker-database-target.js +16 -12
- package/build/executor/worker-database.js +320 -101
- package/build/executor/worker-finalization.js +7 -1
- package/build/executor/worker-resources.js +25 -3
- package/build/executor/worker-verification-guidance.js +18 -3
- package/build/executor/worktree.js +20 -1
- package/build/executor-launch.js +58 -4
- package/build/generated-assets-catalog.js +2 -2
- package/build/git-exclude-lock.js +187 -0
- package/build/index.js +824 -366
- package/build/init.js +26 -11
- package/build/install-bridge-conductor-answers.js +528 -0
- package/build/install-bridge-conductor-apply.js +288 -0
- package/build/install-bridge-conductor-assessment.js +1222 -0
- package/build/install-bridge-conductor-compact.js +186 -0
- package/build/install-bridge-conductor-contracts.js +72 -0
- package/build/install-bridge-conductor-gate.js +1312 -198
- package/build/install-bridge-conductor-setup.js +577 -83
- package/build/install-bridge-conductor.js +3381 -544
- package/build/install-bridge.js +280 -25
- package/build/install-doctor.js +38 -8
- package/build/install-maintenance.js +26 -3
- package/build/install-sfcc-detection.js +120 -0
- package/build/install-sfcc-setup.js +185 -0
- package/build/launcher-config-inspection.js +162 -1
- package/build/mcp-grant-candidates.js +779 -0
- package/build/mcp-grant-doctor.js +63 -0
- package/build/mcp-grant.js +288 -0
- package/build/mcp-host-targets.js +8 -0
- package/build/mcp-identity.js +23 -0
- package/build/mcp-invoke.js +292 -15
- package/build/mcp-presets.js +107 -0
- package/build/mcp-provisioning.js +155 -6
- package/build/mcp-registration-doctor.js +31 -2
- package/build/mcp-server-invocation.js +19 -1
- package/build/merge-workflow-install.js +243 -110
- package/build/pipelines.generated.js +7 -6
- package/build/plan-discovery.js +97 -4
- package/build/plan-execution-ledger.js +100 -7
- package/build/plan-generation-io.js +262 -0
- package/build/plan-generation.js +424 -0
- package/build/plane/build-freshness.js +6 -1
- package/build/plane/cli.js +75 -9
- package/build/plane/plane-freshness.js +17 -0
- package/build/plane/preflight.js +50 -10
- package/build/plane/shutdown.js +19 -0
- package/build/plane/status.js +8 -2
- package/build/plane/terminal-run-cleanup.js +18 -4
- package/build/printed-remedy-commands.js +211 -12
- package/build/project-root.js +40 -0
- package/build/readiness-check.js +77 -0
- package/build/readiness-prerequisites.generated.js +122 -14
- package/build/readme.generated.js +1 -1
- package/build/repository-state-paths.js +41 -0
- package/build/repository-toolchains.js +6 -3
- package/build/run-unit-tests-launcher.js +8 -1
- package/build/setup-epic.js +1305 -210
- package/build/setup-progress.js +44 -0
- package/build/setup-question.js +122 -4
- package/build/sfcc/credentials.js +3 -2
- package/build/sfcc/hostname.js +98 -2
- package/build/sfcc/tool-wrapper.js +6 -95
- package/build/start-tickets.js +193 -29
- package/build/test-support/executor-source-files.js +200 -0
- package/build/testing-config-client.js +25 -2
- package/build/third-party-mcp-targets.js +63 -2
- package/build/ticket-delivery-enrollment.js +9 -0
- package/build/upgrade-cli.js +85 -2
- package/build/v2-readiness-requirements.js +28 -1
- package/build/version.generated.js +3 -3
- package/build/with-credentials.js +208 -0
- package/build/worktree-credential-delivery.js +340 -0
- package/docs/EPIC-IMPLEMENTER-INSTALL.md +9 -1
- package/docs/epic-implementer/getting-started.md +3 -1
- package/docs/epic-implementer/self-hosting.md +14 -1
- package/package.json +2 -2
|
@@ -0,0 +1,779 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP grant candidates: discovery, normalization, and publication (BAPI-1545).
|
|
3
|
+
*
|
|
4
|
+
* The grant setup item offers the owner a set of extra MCP servers to approve
|
|
5
|
+
* for every Epic Implementer worker in one repository. The choices come from:
|
|
6
|
+
*
|
|
7
|
+
* - the owner's host config: user-scope `mcpServers` in `~/.claude.json` and
|
|
8
|
+
* Cursor's `~/.cursor/mcp.json` (project-scope host entries are ignored);
|
|
9
|
+
* - the checkout's own MCP configs, `.mcp.json` and `.cursor/mcp.json`
|
|
10
|
+
* (BAPI-1636; `.vscode/mcp.json` is not read);
|
|
11
|
+
* - the repository's `.bridge/config` `[[mcp]]` entries, read from the
|
|
12
|
+
* checkout the owner runs install in (trusted at that moment only, because
|
|
13
|
+
* the owner is present);
|
|
14
|
+
* - the built-in Playwright preset (`mcp-presets.ts`).
|
|
15
|
+
*
|
|
16
|
+
* Bridge itself is never a candidate: it is always provisioned.
|
|
17
|
+
*
|
|
18
|
+
* BAPI-1636: every entry from every origin passes through ONE pure classifier,
|
|
19
|
+
* {@link classifyMcpGrantEntry}, and ends in exactly one state: offered,
|
|
20
|
+
* excluded with a fixed reason, or unclassifiable. A server that targets a
|
|
21
|
+
* Bridge API under any name, or carries another repository's credential, is
|
|
22
|
+
* never offered. The same classifier checks an existing grant (reported, never
|
|
23
|
+
* changed) and re-runs at publication through a fresh rediscovery, so a
|
|
24
|
+
* selection that went stale or unsafe is refused before anything is staged.
|
|
25
|
+
*
|
|
26
|
+
* Secrets never reach a grant, and discovery never retains one (BAPI-1648).
|
|
27
|
+
* The `env` values of an imported host or project entry are inspected only
|
|
28
|
+
* transiently, inside the classifier, to spot a Bridge URL; a candidate keeps
|
|
29
|
+
* the declared key NAMES and launch metadata, nothing else. Publication does
|
|
30
|
+
* not copy values anywhere: a host, project, or bundle-less registry server is
|
|
31
|
+
* published reference-backed, with neither `secret_bundle` nor
|
|
32
|
+
* `secret_env_keys`, and the owner's approved credential-reference record
|
|
33
|
+
* (`credential-references.ts`) decides which keys a worker receives. A
|
|
34
|
+
* declared `.bridge/config` bundle keeps its legacy form, because that path
|
|
35
|
+
* already references its bundle instead of creating a copy. An entry whose
|
|
36
|
+
* ARGUMENTS look like a credential is not offered at all, and a non-`stdio`
|
|
37
|
+
* entry is not offered because its headers could carry secrets.
|
|
38
|
+
*
|
|
39
|
+
* The candidate fingerprint is a SHA-256 over the canonical inventory. It
|
|
40
|
+
* changes when a command, an argument, a source, a declared key-name set, a
|
|
41
|
+
* non-secret value, or a declared bundle reference changes, and it does NOT
|
|
42
|
+
* change when only an imported value rotates, because no value is part of it.
|
|
43
|
+
* Package-read evidence, destination evidence, and existing-grant findings are
|
|
44
|
+
* never part of it, so none of them causes a repeated prompt.
|
|
45
|
+
*/
|
|
46
|
+
import { createHash } from "crypto";
|
|
47
|
+
import path from "path";
|
|
48
|
+
import { readBridgeConfig } from "./bridge-config.js";
|
|
49
|
+
import { computeLaunchFingerprint, readCredentialReferenceRecord, } from "./credential-references.js";
|
|
50
|
+
import { createNodeCredentialReferenceDeps } from "./credential-references-node-deps.js";
|
|
51
|
+
import { resolveClaudeUserConfigPath } from "./claude-user-config-doctor.js";
|
|
52
|
+
import { bridgeUrlOrigin, launcherTargetFor, parseLauncherPin, } from "./launcher-config-inspection.js";
|
|
53
|
+
import { resolveCursorUserConfigPath } from "./mcp-host-targets.js";
|
|
54
|
+
import { isLocalBuildLauncher, MCP_PACKAGE_NAME } from "./mcp-identity.js";
|
|
55
|
+
import { BRIDGE_RESERVED_SERVER_NAMES, isGrantableServerName, MCP_GRANT_MAX_SERVERS, MCP_GRANT_VERSION, writeMcpGrant, } from "./mcp-grant.js";
|
|
56
|
+
import { PLAYWRIGHT_PRESET_NAME, PLAYWRIGHT_PRESET_UNAVAILABLE_TEXT, playwrightPresetEntry } from "./mcp-presets.js";
|
|
57
|
+
import { getThirdPartyTargetDefinition } from "./third-party-mcp-targets.js";
|
|
58
|
+
/** Project origins map to the existing `repository` source, so grant schema version 1 is unchanged. */
|
|
59
|
+
const ORIGIN_SOURCE = {
|
|
60
|
+
claude: "host",
|
|
61
|
+
cursor: "host",
|
|
62
|
+
"project-claude": "repository",
|
|
63
|
+
"project-cursor": "repository",
|
|
64
|
+
repository: "repository",
|
|
65
|
+
preset: "preset",
|
|
66
|
+
};
|
|
67
|
+
/** Plain-language label for an origin. Never a path. */
|
|
68
|
+
export const CANDIDATE_ORIGIN_LABEL = {
|
|
69
|
+
claude: "Claude user config",
|
|
70
|
+
cursor: "Cursor user config",
|
|
71
|
+
"project-claude": "this repository's .mcp.json",
|
|
72
|
+
"project-cursor": "this repository's .cursor/mcp.json",
|
|
73
|
+
repository: "this repository's .bridge/config",
|
|
74
|
+
preset: "built-in preset",
|
|
75
|
+
};
|
|
76
|
+
/** The project MCP configs read for candidates, in order. Root keys come from the launcher target list. */
|
|
77
|
+
const PROJECT_CONFIG_ORIGINS = [
|
|
78
|
+
{ relPath: ".mcp.json", origin: "project-claude" },
|
|
79
|
+
{ relPath: ".cursor/mcp.json", origin: "project-cursor" },
|
|
80
|
+
];
|
|
81
|
+
/** The fixed text shown for each exclusion. Never includes a value. */
|
|
82
|
+
export const CANDIDATE_EXCLUSION_TEXT = {
|
|
83
|
+
not_stdio: "Not supported in this version",
|
|
84
|
+
secret_arg: "Not offered: an argument may contain a credential",
|
|
85
|
+
reserved_name: "Not offered: the name is reserved for the built-in Playwright preset",
|
|
86
|
+
invalid_name: "Not offered: the name is not a usable server name",
|
|
87
|
+
invalid_definition: "Not offered: the definition is incomplete",
|
|
88
|
+
bundle_keys_unknown: "Not offered: the credential keys its bundle supplies cannot be identified",
|
|
89
|
+
bridge_target: "Not offered: this is a Bridge server; workers already get Bridge for this repository.",
|
|
90
|
+
other_repository_credential: "Not offered: it carries a credential for a different repository.",
|
|
91
|
+
unclassifiable: "Not offered: setup cannot tell what this server connects to.",
|
|
92
|
+
};
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Credential-looking arguments
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
const SECRET_FLAG_WORDS = ["key", "token", "secret", "password"];
|
|
97
|
+
function urlHasUserInfo(value) {
|
|
98
|
+
if (!/^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(value))
|
|
99
|
+
return false;
|
|
100
|
+
try {
|
|
101
|
+
const url = new URL(value);
|
|
102
|
+
return url.username.length > 0 || url.password.length > 0;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// An unparseable URL-looking value with an `@` before the path is treated as user info.
|
|
106
|
+
return /^[A-Za-z][A-Za-z0-9+.-]*:\/\/[^/]*@/.test(value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** True when a URL's query names a parameter like `password`, `sslpassword`, `api_key`, or `token`. */
|
|
110
|
+
function urlQueryNamesSecret(value) {
|
|
111
|
+
if (!/^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(value))
|
|
112
|
+
return false;
|
|
113
|
+
const query = value.indexOf("?");
|
|
114
|
+
if (query === -1)
|
|
115
|
+
return false;
|
|
116
|
+
return value
|
|
117
|
+
.slice(query + 1)
|
|
118
|
+
.split(/[&;#]/)
|
|
119
|
+
.some((pair) => {
|
|
120
|
+
const name = pair.split("=")[0].toLowerCase();
|
|
121
|
+
return SECRET_FLAG_WORDS.some((word) => name.includes(word));
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/** User info, or a secret-named query parameter. */
|
|
125
|
+
function urlLooksLikeCredential(value) {
|
|
126
|
+
return urlHasUserInfo(value) || urlQueryNamesSecret(value);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* True when an argument looks like it carries a credential: a flag whose name
|
|
130
|
+
* contains `key`, `token`, `secret`, or `password` (any case, split or `=`
|
|
131
|
+
* form), or a URL with user info or with a query parameter so named (for
|
|
132
|
+
* example a DSN's `?password=`). The value is never returned or printed.
|
|
133
|
+
*/
|
|
134
|
+
export function argumentLooksLikeCredential(arg) {
|
|
135
|
+
if (arg.startsWith("-")) {
|
|
136
|
+
const flag = arg.replace(/^-+/, "").split("=")[0].toLowerCase();
|
|
137
|
+
if (SECRET_FLAG_WORDS.some((word) => flag.includes(word)))
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
if (urlLooksLikeCredential(arg))
|
|
141
|
+
return true;
|
|
142
|
+
const eq = arg.indexOf("=");
|
|
143
|
+
return eq !== -1 && urlLooksLikeCredential(arg.slice(eq + 1));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Env values held for one entry during classification only. Value-free from
|
|
147
|
+
* outside: only {@link anyValueMatches} answers, and it refuses to serialize.
|
|
148
|
+
* It never leaves classification: discovery projects names, not this holder.
|
|
149
|
+
*/
|
|
150
|
+
export class EntryEnvValues {
|
|
151
|
+
#values;
|
|
152
|
+
constructor(values) {
|
|
153
|
+
this.#values = { ...values };
|
|
154
|
+
}
|
|
155
|
+
anyValueMatches(test) {
|
|
156
|
+
return Object.values(this.#values).some(test);
|
|
157
|
+
}
|
|
158
|
+
toJSON() {
|
|
159
|
+
return "[redacted]";
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** Build the known-Bridge-URL test from the production URL and the destination evidence. */
|
|
163
|
+
export function knownBridgeUrlMatcher(productionBaseUrl, destination) {
|
|
164
|
+
const production = bridgeUrlOrigin(productionBaseUrl);
|
|
165
|
+
return (value) => {
|
|
166
|
+
const origin = bridgeUrlOrigin(value);
|
|
167
|
+
if (origin === null)
|
|
168
|
+
return false;
|
|
169
|
+
return origin === production || (destination?.matchesUrl(value) ?? false);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** True when an argument, bare or as a `--flag=<url>` value, names a known Bridge URL. */
|
|
173
|
+
function argumentNamesBridgeUrl(arg, isKnownBridgeUrl) {
|
|
174
|
+
if (isKnownBridgeUrl(arg))
|
|
175
|
+
return true;
|
|
176
|
+
const eq = arg.indexOf("=");
|
|
177
|
+
return eq !== -1 && isKnownBridgeUrl(arg.slice(eq + 1));
|
|
178
|
+
}
|
|
179
|
+
/** The repository a credential-store target is scoped to, or null for any other target. */
|
|
180
|
+
function credentialTargetRepository(value) {
|
|
181
|
+
const bapi = /^bapi:([^:\s]+)$/.exec(value);
|
|
182
|
+
if (bapi)
|
|
183
|
+
return bapi[1];
|
|
184
|
+
const grant = /^mcp-grant:([^:\s]+):/.exec(value);
|
|
185
|
+
return grant ? grant[1] : null;
|
|
186
|
+
}
|
|
187
|
+
const SHELL_COMMANDS = new Set(["sh", "bash", "zsh", "dash", "ksh", "fish"]);
|
|
188
|
+
/** True for `sh -c`, `bash -c`, `zsh -c`, `cmd /c`, and the like: the target hides in one string. */
|
|
189
|
+
function isShellWrapper(command, args) {
|
|
190
|
+
const base = command.replace(/\\/g, "/").split("/").pop().toLowerCase().replace(/\.exe$/, "");
|
|
191
|
+
if (SHELL_COMMANDS.has(base))
|
|
192
|
+
return args.some((a) => /^-[A-Za-z]*c[A-Za-z]*$/.test(a));
|
|
193
|
+
if (base === "cmd")
|
|
194
|
+
return args.some((a) => /^\/[ck]$/i.test(a));
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Classify one normalized entry. Pure and deterministic: no I/O, no logging,
|
|
199
|
+
* and the result never carries a command, an argument, or a value. The order is
|
|
200
|
+
* fixed: Bridge's own names are skipped, then `invalid_name`,
|
|
201
|
+
* `invalid_definition`, `not_stdio`, `bridge_target`,
|
|
202
|
+
* `other_repository_credential`, `reserved_name`, `secret_arg`,
|
|
203
|
+
* `bundle_keys_unknown`, and last `unclassifiable`.
|
|
204
|
+
*/
|
|
205
|
+
export function classifyMcpGrantEntry(entry, context) {
|
|
206
|
+
if (BRIDGE_RESERVED_SERVER_NAMES.includes(entry.name))
|
|
207
|
+
return { state: "skipped" };
|
|
208
|
+
const excluded = (reason) => ({ state: "excluded", reason });
|
|
209
|
+
if (!isGrantableServerName(entry.name))
|
|
210
|
+
return excluded("invalid_name");
|
|
211
|
+
if (!entry.definitionValid)
|
|
212
|
+
return excluded("invalid_definition");
|
|
213
|
+
if (!entry.stdio)
|
|
214
|
+
return excluded("not_stdio");
|
|
215
|
+
// Each signal alone is enough.
|
|
216
|
+
const namesBridgeUrl = (value) => argumentNamesBridgeUrl(value, context.isKnownBridgeUrl);
|
|
217
|
+
if (parseLauncherPin(entry.args) !== null ||
|
|
218
|
+
entry.packageIdentity === "bridge" ||
|
|
219
|
+
entry.envKeys.some((k) => k.startsWith("BAPI_")) ||
|
|
220
|
+
entry.args.some(namesBridgeUrl) ||
|
|
221
|
+
Object.values(entry.plainEnv).some(context.isKnownBridgeUrl) ||
|
|
222
|
+
(entry.secretEnv?.anyValueMatches(context.isKnownBridgeUrl) ?? false)) {
|
|
223
|
+
return excluded("bridge_target");
|
|
224
|
+
}
|
|
225
|
+
// A reference may stand alone or be a `--flag=<target>` value. Secret env
|
|
226
|
+
// values are tested only for a Bridge URL (above), never read for this rule.
|
|
227
|
+
const flagValues = entry.args.flatMap((a) => (a.indexOf("=") === -1 ? [] : [a.slice(a.indexOf("=") + 1)]));
|
|
228
|
+
const references = [entry.declaredBundle, ...entry.args, ...flagValues, ...Object.values(entry.plainEnv)];
|
|
229
|
+
if (references.some((value) => {
|
|
230
|
+
if (value === undefined)
|
|
231
|
+
return false;
|
|
232
|
+
const repo = credentialTargetRepository(value);
|
|
233
|
+
return repo !== null && repo !== context.repoName;
|
|
234
|
+
})) {
|
|
235
|
+
return excluded("other_repository_credential");
|
|
236
|
+
}
|
|
237
|
+
if (entry.name === PLAYWRIGHT_PRESET_NAME && context.allowReservedName !== true)
|
|
238
|
+
return excluded("reserved_name");
|
|
239
|
+
if (entry.args.some(argumentLooksLikeCredential))
|
|
240
|
+
return excluded("secret_arg");
|
|
241
|
+
if (entry.declaredBundle !== undefined && entry.declaredBundleKeys === undefined)
|
|
242
|
+
return excluded("bundle_keys_unknown");
|
|
243
|
+
if (isShellWrapper(entry.command, entry.args))
|
|
244
|
+
return { state: "unclassifiable", reason: "unclassifiable" };
|
|
245
|
+
if (entry.packageIdentity === "unknown")
|
|
246
|
+
return { state: "unclassifiable", reason: "unclassifiable" };
|
|
247
|
+
return { state: "offered" };
|
|
248
|
+
}
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// Read-only evidence collection and normalization
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
const ENV_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
253
|
+
function isPlainObject(value) {
|
|
254
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
255
|
+
}
|
|
256
|
+
async function readJsonIfPresent(filePath, deps) {
|
|
257
|
+
try {
|
|
258
|
+
return JSON.parse(await deps.readFile(filePath));
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
// Missing or unreadable config offers nothing; it never fails setup.
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Read the package beside `node <abs-root>/build/index.js`, read-only, with the
|
|
267
|
+
* POSIX or Windows path rules the launcher path itself uses. Never throws; the
|
|
268
|
+
* package file's content never leaves this function.
|
|
269
|
+
*/
|
|
270
|
+
export async function collectLocalBuildPackageIdentity(entryPoint, readFile) {
|
|
271
|
+
const paths = entryPoint.startsWith("/") ? path.posix : path.win32;
|
|
272
|
+
const root = paths.dirname(paths.dirname(entryPoint));
|
|
273
|
+
try {
|
|
274
|
+
const parsed = JSON.parse(await readFile(paths.join(root, "package.json")));
|
|
275
|
+
const name = isPlainObject(parsed) ? parsed.name : undefined;
|
|
276
|
+
if (typeof name !== "string" || name.trim().length === 0)
|
|
277
|
+
return "unknown";
|
|
278
|
+
return name === MCP_PACKAGE_NAME ? "bridge" : "other";
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return "unknown";
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/** Normalize one host or project config entry. Values stay in an {@link EntryEnvValues}. */
|
|
285
|
+
function normalizeConfigEntry(name, origin, raw) {
|
|
286
|
+
const invalid = {
|
|
287
|
+
name,
|
|
288
|
+
origin,
|
|
289
|
+
definitionValid: false,
|
|
290
|
+
stdio: true,
|
|
291
|
+
command: "",
|
|
292
|
+
args: [],
|
|
293
|
+
plainEnv: {},
|
|
294
|
+
envKeys: [],
|
|
295
|
+
};
|
|
296
|
+
if (!isPlainObject(raw))
|
|
297
|
+
return invalid;
|
|
298
|
+
const transport = raw.type;
|
|
299
|
+
if ((transport !== undefined && transport !== "stdio") || (raw.command === undefined && raw.url !== undefined)) {
|
|
300
|
+
return { ...invalid, definitionValid: true, stdio: false };
|
|
301
|
+
}
|
|
302
|
+
const command = raw.command;
|
|
303
|
+
const args = raw.args ?? [];
|
|
304
|
+
const env = raw.env ?? {};
|
|
305
|
+
if (typeof command !== "string" ||
|
|
306
|
+
command.trim().length === 0 ||
|
|
307
|
+
!Array.isArray(args) ||
|
|
308
|
+
args.some((a) => typeof a !== "string") ||
|
|
309
|
+
!isPlainObject(env) ||
|
|
310
|
+
Object.entries(env).some(([k, v]) => !ENV_KEY_RE.test(k) || typeof v !== "string")) {
|
|
311
|
+
return invalid;
|
|
312
|
+
}
|
|
313
|
+
const values = env;
|
|
314
|
+
return {
|
|
315
|
+
name,
|
|
316
|
+
origin,
|
|
317
|
+
definitionValid: true,
|
|
318
|
+
stdio: true,
|
|
319
|
+
command,
|
|
320
|
+
args: [...args],
|
|
321
|
+
plainEnv: {},
|
|
322
|
+
envKeys: Object.keys(values).sort(),
|
|
323
|
+
...(Object.keys(values).length > 0 ? { secretEnv: new EntryEnvValues(values) } : {}),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
/** Normalize the `mcpServers` of one host or project config document. */
|
|
327
|
+
export function normalizeServersDocument(doc, origin, rootKey) {
|
|
328
|
+
if (!isPlainObject(doc))
|
|
329
|
+
return [];
|
|
330
|
+
const servers = doc[rootKey];
|
|
331
|
+
if (!isPlainObject(servers))
|
|
332
|
+
return [];
|
|
333
|
+
return Object.entries(servers).map(([name, raw]) => normalizeConfigEntry(name, origin, raw));
|
|
334
|
+
}
|
|
335
|
+
export async function normalizeRepositoryEntries(deps) {
|
|
336
|
+
const read = await readBridgeConfig(deps.cwd, { readFile: deps.readFile });
|
|
337
|
+
if (!read.ok)
|
|
338
|
+
return [];
|
|
339
|
+
return normalizeBridgeConfigManifest(read.manifest);
|
|
340
|
+
}
|
|
341
|
+
/** Normalize the `[[mcp]]` entries of a parsed `.bridge/config` manifest. */
|
|
342
|
+
export function normalizeBridgeConfigManifest(manifest) {
|
|
343
|
+
return manifest.mcp.map((entry) => {
|
|
344
|
+
const name = entry.target;
|
|
345
|
+
const command = entry.command ?? "";
|
|
346
|
+
const registry = getThirdPartyTargetDefinition(name);
|
|
347
|
+
const normalized = {
|
|
348
|
+
name,
|
|
349
|
+
origin: "repository",
|
|
350
|
+
definitionValid: command.trim().length > 0,
|
|
351
|
+
stdio: true,
|
|
352
|
+
command,
|
|
353
|
+
args: [...(entry.args ?? [])],
|
|
354
|
+
plainEnv: {},
|
|
355
|
+
envKeys: [],
|
|
356
|
+
};
|
|
357
|
+
if (entry.secretBundle !== undefined) {
|
|
358
|
+
// The `.bridge/config` subset has no way to name the keys a bundle
|
|
359
|
+
// supplies, so only a registry target's keys are known. Copying an
|
|
360
|
+
// unrestricted bundle is never safe.
|
|
361
|
+
normalized.declaredBundle = entry.secretBundle;
|
|
362
|
+
if (registry !== undefined)
|
|
363
|
+
normalized.declaredBundleKeys = [...registry.requiredEnvKeys].sort();
|
|
364
|
+
}
|
|
365
|
+
else if (registry !== undefined && registry.requiredEnvKeys.length > 0) {
|
|
366
|
+
// BAPI-1648: a bundle-less registry entry is offered reference-backed. Its
|
|
367
|
+
// required key NAMES are declared so the server can be tied to an approval.
|
|
368
|
+
normalized.envKeys = [...registry.requiredEnvKeys].sort();
|
|
369
|
+
}
|
|
370
|
+
return normalized;
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
/** Attach local-build package evidence, read through the injected seam. */
|
|
374
|
+
export async function withPackageEvidence(entries, readFile) {
|
|
375
|
+
const out = [];
|
|
376
|
+
for (const entry of entries) {
|
|
377
|
+
if (entry.definitionValid && entry.stdio && isLocalBuildLauncher(entry.command, entry.args)) {
|
|
378
|
+
out.push({ ...entry, packageIdentity: await collectLocalBuildPackageIdentity(entry.args[0], readFile) });
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
out.push(entry);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return out;
|
|
385
|
+
}
|
|
386
|
+
function canonicalDefinition(def) {
|
|
387
|
+
return JSON.stringify({
|
|
388
|
+
command: def.command,
|
|
389
|
+
args: def.args,
|
|
390
|
+
env: Object.keys(def.env).sort().map((k) => [k, def.env[k]]),
|
|
391
|
+
stagedEnvKeys: def.stagedEnvKeys,
|
|
392
|
+
declaredBundle: def.declaredBundle ?? null,
|
|
393
|
+
declaredBundleKeys: def.declaredBundleKeys ?? null,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
function sha256(text) {
|
|
397
|
+
return createHash("sha256").update(text).digest("hex");
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Names-only projection of an offered normalized entry: launch metadata, origin,
|
|
401
|
+
* declared key names, and any declared bundle metadata. It never reads an env
|
|
402
|
+
* value; a declared bundle supplies its own keys, so no key list is added then.
|
|
403
|
+
*/
|
|
404
|
+
function offeredDefinition(entry) {
|
|
405
|
+
const declaredNames = entry.declaredBundle === undefined ? [...entry.envKeys] : [];
|
|
406
|
+
const plain = { ...entry.plainEnv };
|
|
407
|
+
return {
|
|
408
|
+
name: entry.name,
|
|
409
|
+
origin: entry.origin,
|
|
410
|
+
command: entry.command,
|
|
411
|
+
args: [...entry.args],
|
|
412
|
+
env: plain,
|
|
413
|
+
stagedEnvKeys: declaredNames.filter((k) => !(k in plain)),
|
|
414
|
+
...(entry.declaredBundle !== undefined
|
|
415
|
+
? { declaredBundle: entry.declaredBundle, declaredBundleKeys: [...(entry.declaredBundleKeys ?? [])] }
|
|
416
|
+
: {}),
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/** Normalize one published grant entry. Its metadata is trusted for key names, never resolved. */
|
|
420
|
+
function normalizeGrantEntry(server) {
|
|
421
|
+
const plainEnv = { ...(server.env ?? {}) };
|
|
422
|
+
return {
|
|
423
|
+
name: server.name,
|
|
424
|
+
origin: server.source === "preset" ? "preset" : server.source === "host" ? "claude" : "repository",
|
|
425
|
+
definitionValid: true,
|
|
426
|
+
stdio: server.transport === "stdio",
|
|
427
|
+
command: server.command,
|
|
428
|
+
args: [...server.args],
|
|
429
|
+
plainEnv,
|
|
430
|
+
envKeys: [...new Set([...Object.keys(plainEnv), ...(server.secret_env_keys ?? [])])].sort(),
|
|
431
|
+
...(server.secret_bundle !== undefined
|
|
432
|
+
? {
|
|
433
|
+
declaredBundle: server.secret_bundle,
|
|
434
|
+
// An already-published bundle's key names are declared in the grant itself,
|
|
435
|
+
// so the discovery-only registry rule does not apply.
|
|
436
|
+
...(server.secret_env_keys !== undefined ? { declaredBundleKeys: [...server.secret_env_keys] } : {}),
|
|
437
|
+
}
|
|
438
|
+
: {}),
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Check every server in a validated grant with the same rules. Read-only:
|
|
443
|
+
* bundle values are never resolved and nothing is written. Returns per-server
|
|
444
|
+
* findings (name plus fixed reason); a safe server yields none.
|
|
445
|
+
*/
|
|
446
|
+
export async function inspectExistingMcpGrant(grant, context, readFile) {
|
|
447
|
+
const entries = await withPackageEvidence(grant.servers.map(normalizeGrantEntry), readFile);
|
|
448
|
+
const findings = [];
|
|
449
|
+
for (const entry of entries) {
|
|
450
|
+
const result = classifyMcpGrantEntry(entry, { ...context, allowReservedName: entry.origin === "preset" });
|
|
451
|
+
if (result.state === "excluded" || result.state === "unclassifiable") {
|
|
452
|
+
findings.push({ name: entry.name, reason: result.reason });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return findings.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
456
|
+
}
|
|
457
|
+
async function readExistingGrant(deps, context) {
|
|
458
|
+
if (deps.readGrant === undefined)
|
|
459
|
+
return { state: "absent" };
|
|
460
|
+
let read;
|
|
461
|
+
try {
|
|
462
|
+
read = await deps.readGrant();
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
return { state: "unusable" };
|
|
466
|
+
}
|
|
467
|
+
if (read.state === "absent")
|
|
468
|
+
return { state: "absent" };
|
|
469
|
+
if (read.state === "unusable")
|
|
470
|
+
return { state: "unusable" };
|
|
471
|
+
return { state: "ok", findings: await inspectExistingMcpGrant(read.grant, context, deps.readFile) };
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Discover the candidate inventory. Deterministic: candidates sort by name, a
|
|
475
|
+
* name's definitions by id, and identical definitions collapse with every origin
|
|
476
|
+
* kept. Differing definitions under one name stay as alternatives. Read-only.
|
|
477
|
+
*/
|
|
478
|
+
export async function discoverMcpGrantCandidates(deps) {
|
|
479
|
+
const home = deps.homedir();
|
|
480
|
+
const context = {
|
|
481
|
+
repoName: deps.repoName,
|
|
482
|
+
isKnownBridgeUrl: knownBridgeUrlMatcher(deps.productionBaseUrl, deps.destination),
|
|
483
|
+
};
|
|
484
|
+
const entries = [
|
|
485
|
+
...normalizeServersDocument(await readJsonIfPresent(resolveClaudeUserConfigPath(home, deps.platform), deps), "claude", "mcpServers"),
|
|
486
|
+
...normalizeServersDocument(await readJsonIfPresent(resolveCursorUserConfigPath(home), deps), "cursor", "mcpServers"),
|
|
487
|
+
];
|
|
488
|
+
for (const project of PROJECT_CONFIG_ORIGINS) {
|
|
489
|
+
const rootKey = launcherTargetFor(project.relPath)?.topLevelKey ?? "mcpServers";
|
|
490
|
+
entries.push(...normalizeServersDocument(await readJsonIfPresent(path.join(deps.cwd, ...project.relPath.split("/")), deps), project.origin, rootKey));
|
|
491
|
+
}
|
|
492
|
+
entries.push(...(await normalizeRepositoryEntries(deps)));
|
|
493
|
+
const preset = playwrightPresetEntry();
|
|
494
|
+
if (preset !== null) {
|
|
495
|
+
entries.push({
|
|
496
|
+
name: preset.name,
|
|
497
|
+
origin: "preset",
|
|
498
|
+
definitionValid: true,
|
|
499
|
+
stdio: true,
|
|
500
|
+
command: preset.command,
|
|
501
|
+
args: [...preset.args],
|
|
502
|
+
plainEnv: {},
|
|
503
|
+
envKeys: [],
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
const definitions = [];
|
|
507
|
+
const exclusionList = [];
|
|
508
|
+
for (const entry of await withPackageEvidence(entries, deps.readFile)) {
|
|
509
|
+
const result = classifyMcpGrantEntry(entry, { ...context, allowReservedName: entry.origin === "preset" });
|
|
510
|
+
if (result.state === "skipped")
|
|
511
|
+
continue;
|
|
512
|
+
if (result.state === "offered")
|
|
513
|
+
definitions.push(offeredDefinition(entry));
|
|
514
|
+
else
|
|
515
|
+
exclusionList.push({ name: entry.name, origin: entry.origin, reason: result.reason });
|
|
516
|
+
}
|
|
517
|
+
const byName = new Map();
|
|
518
|
+
for (const raw of definitions) {
|
|
519
|
+
const canonical = canonicalDefinition(raw);
|
|
520
|
+
const source = ORIGIN_SOURCE[raw.origin];
|
|
521
|
+
// The source is part of identity: a host and a repository entry are different approvals.
|
|
522
|
+
const id = `${raw.name}#${sha256(`${source}\n${canonical}`).slice(0, 16)}`;
|
|
523
|
+
const defs = byName.get(raw.name) ?? new Map();
|
|
524
|
+
byName.set(raw.name, defs);
|
|
525
|
+
const existing = defs.get(id);
|
|
526
|
+
if (existing !== undefined) {
|
|
527
|
+
if (!existing.origins.includes(raw.origin))
|
|
528
|
+
existing.origins.push(raw.origin);
|
|
529
|
+
existing.origins.sort();
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
defs.set(id, {
|
|
533
|
+
id,
|
|
534
|
+
source,
|
|
535
|
+
origins: [raw.origin],
|
|
536
|
+
command: raw.command,
|
|
537
|
+
args: [...raw.args],
|
|
538
|
+
env: { ...raw.env },
|
|
539
|
+
stagedEnvKeys: [...raw.stagedEnvKeys],
|
|
540
|
+
...(raw.declaredBundle !== undefined ? { declaredBundle: raw.declaredBundle, declaredBundleKeys: [...(raw.declaredBundleKeys ?? [])] } : {}),
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
const candidates = [...byName.entries()]
|
|
545
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
546
|
+
.map(([name, defs]) => ({ name, definitions: [...defs.values()].sort((a, b) => (a.id < b.id ? -1 : 1)) }));
|
|
547
|
+
const exclusions = [...exclusionList].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : a.origin < b.origin ? -1 : a.origin > b.origin ? 1 : a.reason < b.reason ? -1 : 1));
|
|
548
|
+
const presetUnavailable = preset === null;
|
|
549
|
+
return {
|
|
550
|
+
candidates,
|
|
551
|
+
exclusions,
|
|
552
|
+
presetUnavailable,
|
|
553
|
+
fingerprint: computeCandidateFingerprint(candidates, exclusions, presetUnavailable),
|
|
554
|
+
existingGrant: await readExistingGrant(deps, context),
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
/** The canonical fingerprint of an inventory. It holds no imported value, so a rotated value never changes it. */
|
|
558
|
+
export function computeCandidateFingerprint(candidates, exclusions, presetUnavailable) {
|
|
559
|
+
const canonical = {
|
|
560
|
+
version: 1,
|
|
561
|
+
candidates: candidates.map((c) => ({
|
|
562
|
+
name: c.name,
|
|
563
|
+
definitions: c.definitions.map((d) => ({
|
|
564
|
+
source: d.source,
|
|
565
|
+
origins: d.origins,
|
|
566
|
+
transport: "stdio",
|
|
567
|
+
command: d.command,
|
|
568
|
+
args: d.args,
|
|
569
|
+
env: Object.keys(d.env).sort().map((k) => [k, d.env[k]]),
|
|
570
|
+
staged_env_keys: d.stagedEnvKeys,
|
|
571
|
+
declared_bundle: d.declaredBundle ?? null,
|
|
572
|
+
declared_bundle_keys: d.declaredBundleKeys ?? null,
|
|
573
|
+
})),
|
|
574
|
+
})),
|
|
575
|
+
exclusions: exclusions.map((e) => [e.name, e.origin, e.reason]),
|
|
576
|
+
preset_unavailable: presetUnavailable,
|
|
577
|
+
};
|
|
578
|
+
return sha256(JSON.stringify(canonical));
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* The plain-language exclusion lines for display. Names and fixed text only.
|
|
582
|
+
* BAPI-1636: an already-granted server that fails the current rules is listed
|
|
583
|
+
* too, as still active; this never changes the grant.
|
|
584
|
+
*/
|
|
585
|
+
export function describeInventoryExclusions(inventory) {
|
|
586
|
+
const lines = inventory.exclusions.map((e) => `${e.name} (${CANDIDATE_ORIGIN_LABEL[e.origin]}): ${CANDIDATE_EXCLUSION_TEXT[e.reason]}`);
|
|
587
|
+
if (inventory.existingGrant?.state === "ok") {
|
|
588
|
+
for (const finding of inventory.existingGrant.findings) {
|
|
589
|
+
lines.push(`${finding.name} (already approved and still active for workers): ${CANDIDATE_EXCLUSION_TEXT[finding.reason]}`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (inventory.presetUnavailable)
|
|
593
|
+
lines.push(PLAYWRIGHT_PRESET_UNAVAILABLE_TEXT);
|
|
594
|
+
return lines;
|
|
595
|
+
}
|
|
596
|
+
// ---------------------------------------------------------------------------
|
|
597
|
+
// Selection (BAPI-1636: moved from the conductor; Enter now means none)
|
|
598
|
+
// ---------------------------------------------------------------------------
|
|
599
|
+
/** The grant selection prompt. The default it shows is the default it applies. */
|
|
600
|
+
export const GRANT_SELECTION_PROMPT = ' Servers to approve (numbers separated by commas, "all", or "none") [none]: ';
|
|
601
|
+
/**
|
|
602
|
+
* Parse a selection answer into offered-list positions. Null when unusable.
|
|
603
|
+
* Blank and `none` select nothing; only an explicit `all` selects everything.
|
|
604
|
+
*/
|
|
605
|
+
export function parseGrantSelection(answer, count) {
|
|
606
|
+
const text = answer.trim().toLowerCase();
|
|
607
|
+
if (text === "none" || text === "")
|
|
608
|
+
return [];
|
|
609
|
+
if (text === "all")
|
|
610
|
+
return Array.from({ length: count }, (_, i) => i);
|
|
611
|
+
const picked = new Set();
|
|
612
|
+
for (const part of text.split(",")) {
|
|
613
|
+
const n = Number(part.trim());
|
|
614
|
+
if (!Number.isInteger(n) || n < 1 || n > count)
|
|
615
|
+
return null;
|
|
616
|
+
picked.add(n - 1);
|
|
617
|
+
}
|
|
618
|
+
return [...picked].sort((a, b) => a - b);
|
|
619
|
+
}
|
|
620
|
+
function indexDefinitions(inventory) {
|
|
621
|
+
const byId = new Map();
|
|
622
|
+
for (const candidate of inventory.candidates) {
|
|
623
|
+
for (const def of candidate.definitions)
|
|
624
|
+
byId.set(def.id, { name: candidate.name, def });
|
|
625
|
+
}
|
|
626
|
+
return byId;
|
|
627
|
+
}
|
|
628
|
+
function sameDefinition(a, b) {
|
|
629
|
+
return a.source === b.source && canonicalDefinition(a) === canonicalDefinition(b);
|
|
630
|
+
}
|
|
631
|
+
/** A refusal naming a safe server name and the fixed reason. Never an id or a value. */
|
|
632
|
+
function refusalFor(name, fresh) {
|
|
633
|
+
const excluded = fresh.exclusions.find((e) => e.name === name);
|
|
634
|
+
return excluded !== undefined
|
|
635
|
+
? `${name} can no longer be approved: ${CANDIDATE_EXCLUSION_TEXT[excluded.reason]}`
|
|
636
|
+
: `${name} is no longer offered`;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Check a selection against a FRESH inventory before anything is staged. The
|
|
640
|
+
* fresh inventory is a new discovery, so the classifier has already run again
|
|
641
|
+
* on every entry with refreshed package and destination evidence. A selection is
|
|
642
|
+
* refused when an id is no longer offered, a definition's canonical launch
|
|
643
|
+
* metadata changed (compared in full, not by the truncated id hash), a server is
|
|
644
|
+
* named twice, or the selection is over the limit. Errors carry only a safe
|
|
645
|
+
* server name and fixed text.
|
|
646
|
+
*/
|
|
647
|
+
export function revalidateMcpGrantSelection(fresh, selectedDefinitionIds, approved) {
|
|
648
|
+
const approvedById = indexDefinitions(approved);
|
|
649
|
+
const freshById = indexDefinitions(fresh);
|
|
650
|
+
const chosen = [];
|
|
651
|
+
const names = new Set();
|
|
652
|
+
for (const id of selectedDefinitionIds) {
|
|
653
|
+
const shown = approvedById.get(id);
|
|
654
|
+
if (shown === undefined) {
|
|
655
|
+
// An id that was never offered. Name the server only when the fresh
|
|
656
|
+
// inventory itself reports it, so a submitted id is never echoed.
|
|
657
|
+
const hash = id.indexOf("#");
|
|
658
|
+
const excluded = hash > 0 ? fresh.exclusions.find((e) => e.name === id.slice(0, hash)) : undefined;
|
|
659
|
+
return {
|
|
660
|
+
ok: false,
|
|
661
|
+
error: excluded !== undefined
|
|
662
|
+
? `${excluded.name} cannot be approved: ${CANDIDATE_EXCLUSION_TEXT[excluded.reason]}`
|
|
663
|
+
: "the selection names a server that is not offered",
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
const current = freshById.get(id);
|
|
667
|
+
if (current === undefined || current.name !== shown.name)
|
|
668
|
+
return { ok: false, error: refusalFor(shown.name, fresh) };
|
|
669
|
+
if (!sameDefinition(shown.def, current.def)) {
|
|
670
|
+
return { ok: false, error: `${shown.name} changed after it was shown; re-run setup to review it` };
|
|
671
|
+
}
|
|
672
|
+
if (names.has(current.name))
|
|
673
|
+
return { ok: false, error: "the selection names a server twice" };
|
|
674
|
+
names.add(current.name);
|
|
675
|
+
chosen.push(current);
|
|
676
|
+
}
|
|
677
|
+
if (chosen.length > MCP_GRANT_MAX_SERVERS)
|
|
678
|
+
return { ok: false, error: `at most ${MCP_GRANT_MAX_SERVERS} servers can be approved` };
|
|
679
|
+
return { ok: true, chosen };
|
|
680
|
+
}
|
|
681
|
+
/** The fixed advisory for a credential-requiring server without an approved reference. */
|
|
682
|
+
export function missingReferenceAdvisory(serverName) {
|
|
683
|
+
return (`Advisory: ${serverName} needs credentials but has no approved credential reference. ` +
|
|
684
|
+
`It was published anyway; rerun /install-bridge to approve its credentials.`);
|
|
685
|
+
}
|
|
686
|
+
async function defaultReadReferences(repoName, deps) {
|
|
687
|
+
const base = createNodeCredentialReferenceDeps(deps.grant.env);
|
|
688
|
+
return readCredentialReferenceRecord(repoName, {
|
|
689
|
+
...base,
|
|
690
|
+
homedir: deps.grant.homedir,
|
|
691
|
+
platform: deps.grant.platform,
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Servers the approved record names as `mcp_server` consumers. A consumer that
|
|
696
|
+
* carries a launch fingerprint approves only the definition with that exact
|
|
697
|
+
* launcher. Approval comes from the consumer naming the server, never from
|
|
698
|
+
* whether its source is currently present.
|
|
699
|
+
*/
|
|
700
|
+
function approvedConsumerMatcher(read) {
|
|
701
|
+
if (read === null || read.state !== "ok")
|
|
702
|
+
return () => false;
|
|
703
|
+
const consumers = [];
|
|
704
|
+
for (const entry of read.record.credentials) {
|
|
705
|
+
for (const consumer of entry.consumers) {
|
|
706
|
+
if (consumer.kind === "mcp_server")
|
|
707
|
+
consumers.push(consumer);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return (name, def) => {
|
|
711
|
+
const fingerprint = computeLaunchFingerprint(def.command, def.args);
|
|
712
|
+
return consumers.some((c) => c.server === name && (c.launch_fingerprint === undefined || c.launch_fingerprint === fingerprint));
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Publish an approved selection: rediscover and revalidate it, then write the
|
|
717
|
+
* grant durably. Host, project, and bundle-less registry servers are published
|
|
718
|
+
* reference-backed, with no `secret_bundle` and no `secret_env_keys`; a declared
|
|
719
|
+
* `.bridge/config` bundle keeps its legacy form. Nothing is copied, no
|
|
720
|
+
* credential-store target is created, and the reference record is never written.
|
|
721
|
+
* A selected credential-requiring server without an approved reference still
|
|
722
|
+
* publishes, with one fixed stderr advisory. Any refusal or failure stops before
|
|
723
|
+
* the grant is written. Errors are fixed and name no value.
|
|
724
|
+
*/
|
|
725
|
+
export async function publishMcpGrant(repoName, inventory, selectedDefinitionIds, now, deps) {
|
|
726
|
+
let fresh;
|
|
727
|
+
try {
|
|
728
|
+
fresh = await deps.rediscover();
|
|
729
|
+
}
|
|
730
|
+
catch {
|
|
731
|
+
return { ok: false, error: "the servers could not be checked again before saving" };
|
|
732
|
+
}
|
|
733
|
+
const revalidated = revalidateMcpGrantSelection(fresh, selectedDefinitionIds, inventory);
|
|
734
|
+
if (!revalidated.ok)
|
|
735
|
+
return revalidated;
|
|
736
|
+
const chosen = [...revalidated.chosen].sort((a, b) => (a.name < b.name ? -1 : 1));
|
|
737
|
+
const servers = [];
|
|
738
|
+
const needsApproval = [];
|
|
739
|
+
for (const selected of chosen) {
|
|
740
|
+
const { name, def } = selected;
|
|
741
|
+
const entry = { name, source: def.source, transport: "stdio", command: def.command, args: [...def.args] };
|
|
742
|
+
if (Object.keys(def.env).length > 0)
|
|
743
|
+
entry.env = { ...def.env };
|
|
744
|
+
if (def.declaredBundle !== undefined) {
|
|
745
|
+
entry.secret_bundle = def.declaredBundle;
|
|
746
|
+
entry.secret_env_keys = [...(def.declaredBundleKeys ?? [])];
|
|
747
|
+
}
|
|
748
|
+
else if (def.stagedEnvKeys.length > 0) {
|
|
749
|
+
needsApproval.push(selected);
|
|
750
|
+
}
|
|
751
|
+
servers.push(entry);
|
|
752
|
+
}
|
|
753
|
+
if (needsApproval.length > 0) {
|
|
754
|
+
let read = null;
|
|
755
|
+
try {
|
|
756
|
+
read = await (deps.readReferences ?? ((repo) => defaultReadReferences(repo, deps)))(repoName);
|
|
757
|
+
}
|
|
758
|
+
catch {
|
|
759
|
+
read = null;
|
|
760
|
+
}
|
|
761
|
+
const approved = approvedConsumerMatcher(read);
|
|
762
|
+
const advise = deps.advise ?? ((line) => process.stderr.write(`${line}\n`));
|
|
763
|
+
for (const { name, def } of needsApproval) {
|
|
764
|
+
if (!approved(name, def))
|
|
765
|
+
advise(missingReferenceAdvisory(name));
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
const grant = {
|
|
769
|
+
version: MCP_GRANT_VERSION,
|
|
770
|
+
repo_name: repoName,
|
|
771
|
+
granted_at: now.toISOString(),
|
|
772
|
+
candidate_fingerprint: fresh.fingerprint,
|
|
773
|
+
servers,
|
|
774
|
+
};
|
|
775
|
+
const written = await (deps.writeGrant ?? writeMcpGrant)(grant, deps.grant);
|
|
776
|
+
if (!written.ok)
|
|
777
|
+
return { ok: false, error: written.error };
|
|
778
|
+
return { ok: true, grant, path: written.path };
|
|
779
|
+
}
|