metheus-governance-mcp-cli 0.2.21 → 0.2.22

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.
Files changed (2) hide show
  1. package/cli.mjs +80 -7
  2. package/package.json +1 -1
package/cli.mjs CHANGED
@@ -4125,11 +4125,18 @@ function tryRegister(cliBin, serverName, proxyArgs, options = {}) {
4125
4125
  cliBin === "codex" && workspaceEnv
4126
4126
  ? ["--env", `METHEUS_WORKSPACE_DIR=${workspaceEnv}`]
4127
4127
  : [];
4128
- const attempts = [];
4129
4128
  if (codexEnvArgs.length > 0) {
4130
- attempts.push([...baseAddArgs, ...codexEnvArgs, "--", process.execPath, selfPath, "proxy", ...proxyArgs]);
4131
- attempts.push([...baseAddArgs, ...codexEnvArgs, process.execPath, selfPath, "proxy", ...proxyArgs]);
4129
+ const envAttempts = [];
4130
+ envAttempts.push([...baseAddArgs, ...codexEnvArgs, "--", process.execPath, selfPath, "proxy", ...proxyArgs]);
4131
+ envAttempts.push([...baseAddArgs, ...codexEnvArgs, process.execPath, selfPath, "proxy", ...proxyArgs]);
4132
+ for (const args of envAttempts) {
4133
+ const run = runCLICommand(cliBin, args, { stdio: "inherit" });
4134
+ if (run.status === 0) return true;
4135
+ }
4136
+ // Do not silently downgrade to no-env registration when fallback env was requested.
4137
+ return false;
4132
4138
  }
4139
+ const attempts = [];
4133
4140
  attempts.push([...baseAddArgs, "--", process.execPath, selfPath, "proxy", ...proxyArgs]);
4134
4141
  attempts.push([...baseAddArgs, process.execPath, selfPath, "proxy", ...proxyArgs]);
4135
4142
  for (const args of attempts) {
@@ -4161,6 +4168,44 @@ function isRegistered(cliBin, serverName) {
4161
4168
  return run.status === 0;
4162
4169
  }
4163
4170
 
4171
+ function getRegisteredTransport(cliBin, serverName) {
4172
+ if (cliBin !== "codex") return null;
4173
+ const run = runCLICommand(cliBin, ["mcp", "get", serverName, "--json"], { stdio: "pipe" });
4174
+ if (run.status !== 0) return null;
4175
+ const parsed = tryJsonParse(String(run.stdout || ""));
4176
+ const transport = safeObject(parsed?.transport);
4177
+ if (!transport.type) return null;
4178
+ return transport;
4179
+ }
4180
+
4181
+ function extractWorkspaceDirArg(args) {
4182
+ if (!Array.isArray(args)) return "";
4183
+ for (let i = 0; i < args.length; i += 1) {
4184
+ if (String(args[i] || "").trim() !== "--workspace-dir") continue;
4185
+ return String(args[i + 1] || "").trim();
4186
+ }
4187
+ return "";
4188
+ }
4189
+
4190
+ function withWorkspaceDirArg(args, workspaceDir) {
4191
+ const next = [];
4192
+ let replaced = false;
4193
+ for (let i = 0; i < args.length; i += 1) {
4194
+ const token = String(args[i] || "").trim();
4195
+ if (token !== "--workspace-dir") {
4196
+ next.push(args[i]);
4197
+ continue;
4198
+ }
4199
+ next.push("--workspace-dir", workspaceDir);
4200
+ i += 1;
4201
+ replaced = true;
4202
+ }
4203
+ if (!replaced) {
4204
+ next.push("--workspace-dir", workspaceDir);
4205
+ }
4206
+ return next;
4207
+ }
4208
+
4164
4209
  function resolveSetupContext(flags) {
4165
4210
  const workspaceMeta = loadWorkspaceMeta(process.cwd());
4166
4211
  const projectID = String(flags["project-id"] || workspaceMeta.project_id || "").trim();
@@ -4169,6 +4214,8 @@ function resolveSetupContext(flags) {
4169
4214
  const workspaceDirRaw = String(flags["workspace-dir"] || "").trim();
4170
4215
  const workspaceFallbackDirRaw = String(flags["workspace-fallback-dir"] || "").trim();
4171
4216
  const hasWorkspaceDirFlag = Object.prototype.hasOwnProperty.call(flags, "workspace-dir");
4217
+ const hasWorkspaceFallbackDirFlag = Object.prototype.hasOwnProperty.call(flags, "workspace-fallback-dir");
4218
+ const hasAnySetupOverride = Object.keys(safeObject(flags)).length > 0;
4172
4219
  // Default to auto workspace mode for safer multi-project Codex sessions.
4173
4220
  const workspaceAutoMode = !hasWorkspaceDirFlag || isAutoWorkspaceMode(workspaceDirRaw);
4174
4221
  // Pin only when user explicitly set a non-auto workspace-dir.
@@ -4194,6 +4241,9 @@ function resolveSetupContext(flags) {
4194
4241
  baseURL,
4195
4242
  workspaceDir,
4196
4243
  workspaceFallbackDir,
4244
+ hasWorkspaceDirFlag,
4245
+ hasWorkspaceFallbackDirFlag,
4246
+ hasAnySetupOverride,
4197
4247
  shouldPinWorkspaceDir,
4198
4248
  workspaceAutoMode,
4199
4249
  serverName,
@@ -4210,13 +4260,36 @@ function runSetupInternal(flags, options = {}) {
4210
4260
  for (const cliBin of clients) {
4211
4261
  if (!commandExists(cliBin)) continue;
4212
4262
  const alreadyRegistered = isRegistered(cliBin, context.serverName);
4263
+ if (ensureOnly && alreadyRegistered && !context.hasAnySetupOverride) {
4264
+ results.push({ cliBin, ok: true, action: "kept" });
4265
+ continue;
4266
+ }
4267
+
4268
+ let proxyArgsForRegister = [...context.proxyArgs];
4269
+ let workspaceEnvForRegister = context.shouldPinWorkspaceDir
4270
+ ? context.workspaceDir
4271
+ : context.workspaceFallbackDir;
4272
+
4273
+ if (cliBin === "codex" && !context.hasWorkspaceDirFlag && !context.hasWorkspaceFallbackDirFlag) {
4274
+ const transport = getRegisteredTransport(cliBin, context.serverName);
4275
+ if (transport) {
4276
+ const existingWorkspaceDir = extractWorkspaceDirArg(transport.args);
4277
+ const existingEnv = safeObject(transport.env);
4278
+ const existingWorkspaceEnv = String(existingEnv.METHEUS_WORKSPACE_DIR || "").trim();
4279
+ if (existingWorkspaceDir && !isAutoWorkspaceMode(existingWorkspaceDir)) {
4280
+ proxyArgsForRegister = withWorkspaceDirArg(proxyArgsForRegister, existingWorkspaceDir);
4281
+ workspaceEnvForRegister = "";
4282
+ } else if (!workspaceEnvForRegister && existingWorkspaceEnv) {
4283
+ workspaceEnvForRegister = resolveWorkspaceDir(existingWorkspaceEnv);
4284
+ }
4285
+ }
4286
+ }
4287
+
4213
4288
  if (!ensureOnly || alreadyRegistered) {
4214
4289
  runRemove(cliBin, context.serverName);
4215
4290
  }
4216
- const ok = tryRegister(cliBin, context.serverName, context.proxyArgs, {
4217
- workspaceDir: context.shouldPinWorkspaceDir
4218
- ? context.workspaceDir
4219
- : context.workspaceFallbackDir,
4291
+ const ok = tryRegister(cliBin, context.serverName, proxyArgsForRegister, {
4292
+ workspaceDir: workspaceEnvForRegister,
4220
4293
  });
4221
4294
  const action = ensureOnly
4222
4295
  ? alreadyRegistered
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metheus-governance-mcp-cli",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "description": "Metheus Governance MCP CLI (setup + stdio proxy)",
5
5
  "type": "module",
6
6
  "files": [