Skip to content

fix(cli): validate MCP registration before side effects - #9057

Merged
gensokyo-dev merged 4 commits into
morluto:mainfrom
2SinceJuly:fix/mcp-add-reject-unexpected-arguments
Nov 6, 2026
Merged

gensokyo-dev merged 4 commits into
morluto:mainfrom
2SinceJuly:fix/mcp-add-reject-unexpected-arguments

Conversation

@2SinceJuly

@2SinceJuly 2SinceJuly commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #7375. Fixes #7608.

mcp add extra --agent amp and misspelled options such as --agents claude-code reported success, wrote Amp settings and, for the latter, started registration without the requested selector. Invalid registrations now exit 1 before client-settings inspection, backups or runner startup, with the offending argument identified. Missing/empty values keep their correction field and priority.

One shared argument reader owns token consumption, agent selection and tagged validation results. It replaces the separate missing-value scans, agent scanner and proposed unexpected-token scanner. Both the launcher scope guard and dispatch use those facts. Declared sensitive literals are redacted from diagnostics; ordinary evidence text remains intact. The declared -c alias now accepts equals form consistently with --command.

Evidence

  • Fresh main a93eb3969: original regression fails (1 failed, 2 passed). Seven independent compiled-CLI calls confirm malformed arguments write settings/backups; a misspelled selector also starts a recording runner.
  • Six focused files: 33 passed, 1 platform skip, including actual CLI admission, missing-value priority, unchanged existing files/mtime, absent backups and runner calls, environment preservation, recovery, scope and discovery controls. Valid registrations assert the exact saved command, arguments and retained environment, rather than file existence.
  • Independent seven-call before/after matrix uses isolated existing Amp configurations and recording package runners; no package downloads, target execution or live client application. Normal spaced/equals registrations retain the original backup and environment; valid non-Amp selection reaches only the recording runner. Invalid requests leave settings and timestamps unchanged and create no backup or runner record.
  • check:fast and docs:check pass (including nine documentation consistency tests).
  • Resource bounds: two CPU cores; focused tests 77.913s wall / 133.502s CPU / 1644.6 MiB sampled process-family RSS; checks 27.855s / 38.440s / 5471.1 MiB. Independent CLI matrix: before 10.064s / 10.632s / 228.7 MiB, after 9.835s / 9.977s / 228.1 MiB; fresh processes with uncontrolled OS caches. No guard stops. No analysis cache, token saving or speedup claim; model tokens were not measured.

Linux local verification exercises the real CLI and Amp file writer; package-runner behavior is simulated, and Windows/live-client behavior is unverified. Existing expanded discovery behavior is preserved, including --command=--help. Completion generation is unchanged; the issue's secondary surplus-completion observation is outside this registration fix. This change adds no provider or target authority.

Technical CI passes on the final head, including all eight test shards and required aggregates. Greptile reviews this head at 5/5 with no unresolved threads. No completed Codex review of this head is available; the bot reports exhausted quota. The original author readiness declarations are preserved verbatim; no new human-review attestation is added.

Author readiness

Human owner: @2SinceJuly

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

`rea mcp add extra --agent amp --command 'node --version'` exited 0,
reported a successful registration, and wrote $HOME/.config/amp/settings.json
even though `extra` was silently dropped. The command takes options only, and
Incur skips any other token, so the caller had no way to see that part of the
invocation had no effect.

The neighbouring guards already reject a valued option with no value, a value
position occupied by another flag, and an empty `--flag=` form; an unexpected
positional was the one shape left. Classify the expanded tokens, since a
non-empty equals form is valid syntax, and report the stray word through the
same VALIDATION_ERROR document the other guards use.

Refs morluto#7375

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-06T01:26:02.687497Z
We couldn’t complete this review. Comment @codex review to try again.
f7e910b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issue was found.

Summary

The PR checks mcp add arguments before reading client settings, creating backups, or starting registration.

  • rea mcp add rejects malformed options before registration can change settings.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["mcp add arguments"] --> B{"Discovery request?"}
  B -->|Yes| C["Existing discovery route"]
  B -->|No| D["readMcpAddArguments"]
  D --> E{"Arguments valid?"}
  E -->|No| F["Redact error and exit 1"]
  E -->|Yes| G{"Amp selected?"}
  G -->|Yes| H["Check settings and create backup"]
  H --> I["Run registration and verify result"]
  G -->|No| J["Run selected client registration"]
Loading

Reviews (3) · Last reviewed commit: "fix(cli): validate MCP registration befo..." · Reviewed by Greptile

Comment thread src/cli.ts Resolved
`rea mcp add extra --command --json` reported "Missing value for flag:
--command" but set `fieldErrors[0].path` to `agent`, because the field was
chosen from the stray argument while the message came from the missing
value. Scripts read that path to find which option to fix, so they were
pointed at the wrong one.

Derive the field from the same error the message describes, and cover the
combined shape — a stray word plus a missing value — for both `--agent`
and `--command`/`-c`.

Refs morluto#9057
@2SinceJuly

Copy link
Copy Markdown
Contributor Author

Fixed in 51d76be64.

The field and the message were chosen from different errors: the message came from missingValue ?? emptyValue but path was forced to agent whenever any stray argument existed. So mcp add extra --command said "Missing value for flag: --command" while fieldErrors[0].path pointed at agent.

The field now derives from the same error the message describes — command when the missing flag is --command/-c, agent for --agent, and agent only for a genuinely unexpected argument on its own.

Pinned by mcp add names the option the reported error is about, which covers a stray word combined with each of --command, -c and --agent, and asserts the message and the field agree. It fails on the previous head.

tests/boundary/cli/mcpAddOptionValidation.test.ts — 3 passed.

2SinceJuly and others added 2 commits November 6, 2026 13:00
Expanding --command=--help yields --command followed by --help, and the
stray-argument pass skipped a value that starts with a dash, so it
reported the value as an unexpected argument. The registration was
rejected even though the caller asked for it. Only an absent value
belongs to the missing-value guard.
Fixes morluto#7375. Fixes morluto#7608.

Share argument consumption and agent facts between admission and scope checks; reject invalid registrations before settings, backups or runner startup. Preserve missing-value priority, explicit redaction, discovery and valid registration controls.

Validated 33 focused tests (one platform skip), current-main RED and independent compiled-CLI before/after workflows; check:fast passes.

Assisted-by: Reimu (gensokyo-skills:reimu-incident-triage)
@gensokyo-dev gensokyo-dev changed the title fix(cli): reject an argument mcp add cannot honor fix(cli): validate MCP registration before side effects Nov 6, 2026
@gensokyo-dev
gensokyo-dev merged commit e9ea9d6 into morluto:main Nov 6, 2026
45 checks passed
@morluto morluto added area: cli CLI adapter and one-shot commands bug Something isn't working labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli CLI adapter and one-shot commands bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] mcp add ignores undeclared flags and applies registration [Bug] mcp add writes client configuration despite an unexpected positional argument

3 participants