Skip to content

fix(mcp): forward only server-wide globals and refuse per-command flags at startup - #52

Merged
gshost1 merged 6 commits into
mainfrom
fix/mcp-forwarded-globals
Nov 8, 2026
Merged

gshost1 merged 6 commits into
mainfrom
fix/mcp-forwarded-globals

Conversation

@gshost1

@gshost1 gshost1 commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

agent-desktop mcp forwarded every global flag given on its command line to every per-call child. The visual-debug flags (--debug, --screenshot=PATH) and the post-action wait flags (--wait-for, --wait-for-gone, --wait-timeout) belong to one command, not to a server. With mcp --debug --screenshot x.html, every tool other than snapshot and click failed from the first call (INVALID_ARGS "--debug supports snapshot and click only", including desktop_skills_get, which the server's instructions tell clients to read first), and every snapshot or click after the first failed with "Cannot create debug output", because the path is created once. With --wait-for, every tool without a post-action wait failed. This makes each global's scope explicit and has mcp refuse per-command flags before serving. Ledger rows STATE-4 and BINARY-9.

State before and after

before: every global on the command line --> prepended to every child
  --debug --screenshot P : non-snapshot/click tools fail on call 1;
                           first snapshot/click creates P, every later one fails
  --wait-for ...         : every tool without a post-action wait fails
  bad --agent-id/--session: every call fails

after: GlobalScope::of(flag)          (checked by id, before the long name)
  ServerWide  --> validated once (session, agent id, UTF-8), forwarded to every child
  ServerOnly  --> applies to the server itself, not forwarded (--verbose)
  PerCommand  --> refused: INVALID_ARGS envelope on stderr, nothing on stdout, exit 2, before serving
  none        --> refused: INTERNAL "not classified for mcp", exit 1
  a test fails when a new global flag has no scope

Scope of each global

Flag Scope Why
--session server-wide, validated at startup the snapshot namespace for the whole server
--agent-id server-wide, validated at startup the subagent identity within that session
--headed server-wide the delivery mode; LeaseRole::of_call reads it
--trace server-wide the trace file, opened in append mode by every call
--trace-strict server-wide the trace error policy that goes with --trace
--verbose server-only turns on the server's own debug log on stderr; not forwarded (a child's stderr is discarded when it prints an envelope)
--debug, --screenshot per-command, refused snapshot or click only; the file can be created once
--wait-for, --wait-for-gone, --wait-timeout per-command, refused snapshot and ref actions only

What changed

  • src/mcp/global_scope.rs (new): GlobalScope { ServerWide, ServerOnly, PerCommand } and the table above.
  • src/mcp/global_forward.rs (new, moved from mod.rs): collect_forwarded_globals (scope by id, startup validation of --session with resolve_active_session and --agent-id with validate_agent_id, UTF-8 check) and refuse_start.
  • src/main.rs: emit_response split so the envelope writer (write_response) can target stderr; the refusal reuses it.
  • src/mcp/mod.rs: module declarations and the call site; the server instructions no longer list --wait-for.
  • src/tests/mcp_process.rs: mcp_server_global_flags_reach_every_call asserted the old behavior (it expected "--debug supports snapshot and click only"); it now runs a call with a valid --trace and --session and checks the child's trace records that session. New refusal test keeps stdin open and requires the server to exit by itself.
  • apps/docs/content/docs/guides/mcp.mdx, docs/json-output.md (exit codes), README.md, one sentence in AGENTS.md.

Prior art: none upstream; item 11 of the open upstream issue #602 register. Upstream #587 (open, read-only server mode) also edits src/mcp/mod.rs; the logic lives in new files to keep that diff small.

How tested

Each test fails with its rule broken and passes with it:

  • visual_debug_flags_are_refused_and_never_forwarded (audit sketch; on main, forwarded_globals returned ["--debug", "--screenshot=/tmp/x.html"]) and post_action_wait_flags_are_refused_before_or_after_mcp.
  • Process test: with stdin left open, mcp with the debug flags, the wait flags, or an invalid --agent-id exits by itself within 20 s with code 2, empty stdout and the envelope on stderr. (Envelope back on stdout with exit 1: fails on the code, then on "stdout is JSON-RPC only"; reading stdin before refusing: times out.)
  • Non-UTF-8 value refused (Unix only); --verbose not forwarded; identity validation; short-only and unclassified flags refused with their own codes; --session reaches the child's trace.

Gates on head bab3f4d5 (main 42b97e61 merged in): fmt, clippy -D warnings, source-rule and architecture scripts, core Windows check, Win32 leakage check, cargo tree, host-set tests 4833 passed, 0 failed. Re-cycled onto main 3fce1ebb (PRs #47, #49, #51 changed Cargo.lock, crates/core/src/lib.rs, docs/json-output.md) as 24c0ac10: gates green on a re-run (4900 passed, 0 failed; the first run failed only the batch CLI flake, now root-caused as the batch's 5 s permission preflight), CI 13/13 green.

Review: a high code review and a security review. The security review found nothing exploitable; its non-UTF-8 note and the code review's stdout, exit-code, short-flag, verbose and validation findings are applied here.

Security

The refusal happens before any file is opened or stdin is read, and the error names the flags, never their values, so a screenshot path does not appear. Forwarded values keep the --flag=value form (one argv element each), tool-call arguments cannot set globals, and non-UTF-8 values are refused instead of being altered.

Performance

One table lookup per global at startup.

Risks

  • mcp --wait-for ... (and the other per-command flags) now fails at startup with exit 2 instead of on each call; it never worked except for snapshot and ref actions. --wait-for is not available through MCP at all until it becomes a per-call argument.
  • mcp --verbose now logs the server's own activity instead of being passed to children.
  • AGENT_DESKTOP_SESSION / AGENT_DESKTOP_AGENT_ID in the server's environment are still checked only by each child.
  • Already on main, not changed: the server reads each child's stderr with no size limit.

What's left

--wait-for as a per-call argument on the snapshot and ref-action tools; version --debug --screenshot x.html still fails on the CLI because visual debug is validated before the version arm (BINARY-9 remainder). Merge rule: rules 1 and 2 (head has main 3fce1ebb merged in, gates and CI green on it, GitHub reports CLEAN, main did not move during CI).

`mcp` forwarded every global flag given on its command line to each tool
call's child. Flags that describe one command broke the server:
`mcp --debug --screenshot x.html` failed every tool other than snapshot and
click from the first call (including `desktop_skills_get`, which the
server instructions say to read first), and every snapshot or click after
the first, because the debug file is create-once. `mcp --wait-for` failed
every tool without a post-action wait the same way.

Each global now has an explicit scope. Server-wide flags (--session,
--agent-id, --headed, --trace, --trace-strict, --verbose) are forwarded as
before. Per-command flags (--debug, --screenshot, --wait-for,
--wait-for-gone, --wait-timeout) and any global without a scope make `mcp`
print an INVALID_ARGS envelope with a suggestion and exit 1 before it
serves anything. A test fails when a new global flag has no scope.

The process test that used --debug to show globals reach every call now
uses an invalid --agent-id, and the server instructions no longer list
--wait-for as a server flag. Item 11 of the upstream register `vercel-labs#602`.
gshost1 added a commit that referenced this pull request Nov 8, 2026
…warding --verbose

Review of the first change found gaps in how `mcp` refuses its flags:

- The refusal envelope went to stdout, which an MCP client reads as
  JSON-RPC, so the user never saw why the server exited. It now goes to
  stderr through the same JSON writer the CLI uses, and an argument error
  exits 2 like every other argument error.
- An invalid --session or --agent-id was forwarded and failed every call.
  `mcp` now checks both once with the child's own validators.
- A non-UTF-8 value was forwarded after a lossy conversion, which could
  point a child at a different --trace file. It is now refused.
- The long-name check ran before the scope check, so a short-only global
  would have been dropped silently, and a global missing from the scope
  table got the per-command message. Both are now an internal error that
  says the flag is not classified for mcp.
- --verbose only produced child stderr that the server buffers and then
  discards. It is now server-only: it turns on the server's own debug log
  and is not forwarded.

The tests now check the refusal while stdin is still open, prove valid
values reach the child through the trace it writes, and clean up their
scratch directory. The module is renamed to global_forward and its entry
point to collect_forwarded_globals. main.rs gains a small write_response
helper so the refusal reuses the envelope writer instead of copying it.
@gshost1
gshost1 merged commit 9ea5d14 into main Nov 8, 2026
13 checks passed
@gshost1
gshost1 deleted the fix/mcp-forwarded-globals branch November 8, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant