openclaw/docs/cli/attach.md
YangManBOBO 944eba49f3
fix(cli): reject out-of-range attach grant expiry instead of crashing (#174534)
* fix(cli): reject out-of-range attach grant expiry instead of crashing

openclaw attach crashed with an unhandled RangeError (Invalid time value) from new Date(grant.expiresAtMs).toISOString() when the gateway returned a finite expiresAtMs outside JavaScript's valid Date range. The existing guard rejected non-numeric and non-finite values but not finite values past MAX_DATE_TIMESTAMP_MS (8_640_000_000_000_000). Extend the guard so out-of-range expiries fall through to the intended diagnostic and exit 1. Closes #174524.

* fix(cli): revoke the attach grant when rejecting an out-of-range expiry

The previous early-return guard rejected an out-of-range expiresAtMs before entering the grant's try/finally, so attach.revoke no longer ran for an otherwise-valid grant. Reject the malformed expiry inside the cleanup scope (where toISOString used to throw and trigger revocation) so the minted grant is still revoked, and extend the regression to assert the revoke call.

* fix(cli): validate attach responses within grant cleanup

Use the shared safe timestamp formatter in one response guard, preserve returned-token revocation on malformed grants, and cover both Date-range limits through the CLI action.

Co-authored-by: YangManBOBO <pan.xiaobo@xydigit.com>

* chore(cli): shrink attach assertion baseline after cleanup

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: YangManBOBO <pan.xiaobo@xydigit.com>
2026-10-18 13:33:03 -07:00

3.4 KiB

summary read_when title
CLI reference for `openclaw attach` (launch Claude Code with a scoped Gateway MCP grant)
You want Claude Code to use OpenClaw Gateway MCP tools
You need a temporary session-bound MCP grant for an external harness
Attach CLI

openclaw attach launches Claude Code with a strict temporary MCP config bound to one Gateway session.

openclaw attach [target]

target accepts a Control UI session URL, a compact host/agent/ref, a bare short reference, or a literal agent:... session key. A URL or host target authoritatively selects that Gateway; a bare reference uses the configured or default Gateway.

openclaw attach
openclaw attach https://gateway.example/dashboard/main/movies-a1166b81
openclaw attach movies-a1166b81
openclaw attach --session agent:main:telegram:123 --ttl 600000
openclaw attach --print-config

Options:

  • --session <key> binds the grant to a Gateway session. Defaults to the main session.
  • --url <url> selects a Gateway for a bare reference or --session key. Do not combine it with a URL target.
  • --token <token> and --password <password> provide explicit Gateway auth.
  • --tls-fingerprint <sha256> pins the Gateway TLS certificate.
  • --ttl <ms> requests a positive grant TTL in milliseconds. The Gateway applies its own ceiling.
  • --bin <path> selects the Claude Code binary. Default: claude.
  • --print-config writes the temporary .mcp.json, prints the launch command and env, and leaves the grant live until TTL expiry (it does not spawn Claude Code or revoke the grant).

Pass either a positional target or --session, not both. Short references are resolved before the scoped attach grant is minted; a missing session is never created implicitly.

A URL or host target never reuses configured credentials or OPENCLAW_GATEWAY_TOKEN / OPENCLAW_GATEWAY_PASSWORD. It uses the stored device token for that exact Gateway origin, or explicit --token/--password credentials. On first contact, pass one of those credentials once, approve the pairing request in that Gateway's Control UI, and retry; see Devices. Session URLs must stay credential-free: userinfo and sensitive query or fragment parameters such as token and password are rejected.

Target resolution uses the same session target error matrix as openclaw tui.

The bearer token is passed through environment variables, not argv. OpenClaw launches Claude Code with --strict-mcp-config --mcp-config <path> so ambient Claude MCP servers do not join the attached session. Normal launches (without --print-config) revoke the grant when the Claude Code process exits.

Malformed grant responses, including expiry timestamps outside JavaScript's valid Date range, report attach.grant returned an unexpected response from the gateway. and exit with code 1. The CLI attempts to revoke any returned token before exiting.

If an attached tool asks a structured question, answer it through the question controls in the Control UI or native app. An attach grant scopes tools to a session but does not identify an active OpenClaw run or its original caller permissions, so ordinary channel text cannot claim that question. See Ask user.

See also: Control UI URLs, Devices, Gateway CLI, MCP CLI, and ACP CLI.