Skip to content

feat(cli): add @uid and URL shorthands - #2897

Closed
OrKoN wants to merge 3 commits into
mainfrom
cli-uid-url-shorthands
Closed

OrKoN wants to merge 3 commits into
mainfrom
cli-uid-url-shorthands

Conversation

@OrKoN

@OrKoN OrKoN commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Adds CLI-only shorthands for element uids and URLs. The MCP server, tool schemas, pageId handling, and snapshot output are unchanged.

  • @<uid> is accepted for uid params:
    • In a required uid slot, the @ is stripped: click 1 @1_5 is the same as click 1 1_5.
    • An extra @<uid> fills the first optional uid param that is not set yet: take_screenshot 1 @1_5 becomes take_screenshot 1 --uid 1_5.
    • For evaluate_script, extra @<uid> args are appended to the element --args.
    • --uid @1_5 also works.
  • URLs passed as an extra positional fill an optional url param: navigate_page 1 https://web.dev becomes navigate_page 1 --url https://web.dev. Only absolute URLs with http, https, file, chrome, chrome-extension, about, or data schemes count; example.com does not.

Implementation: expandShorthandArgs() in src/config/cli-commands.ts rewrites argv before yargs parses it, so yargs config and strict mode are unchanged. It:

  • Skips flag values, using arg types to tell booleans and arrays apart.
  • Never overrides an explicit flag: take_screenshot 1 --uid 1_4 @1_5 still fails with "Unknown argument".
  • Only strips @ in uid slots, so fill 1 @1_5 @handle keeps the value @handle.

Usage lines show the shorthands (e.g. take_screenshot <pageId> ... [@uid], navigate_page <pageId> ... [url]), and docs/cli.md has a new "Shorthands" section.

The agent tip printed on argument errors now asks for exact command names from --help, uses click 1 @1_2 as the correct example, and lists the @<uid> and URL shorthands for optional parameters. Unknown commands are not rerouted or auto-corrected.

The chrome-devtools-cli skill (skills/chrome-devtools-cli/SKILL.md) now uses the shorthands in its workflow, usage notes, and examples (e.g. click 1 @1_4, take_screenshot 1 @1_4, navigate_page 1 "https://example.com"). I ran each changed example through expandShorthandArgs to check it expands to the expected flags.

Manually verified with the built CLI on wikipedia.org: navigate_page 1 <url>, take_screenshot 1 @uid, evaluate_script ... @uid, fill 1 @uid "@devtools", and click 1 @uid.

Comment thread docs/cli.md Resolved
@OrKoN
OrKoN force-pushed the cli-uid-url-shorthands branch 2 times, most recently from ea18215 to 64cee66 Compare October 15, 2026 06:56

@zyzyzyryxy zyzyzyryxy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM codewise, I sent you the eval links to review.

@OrKoN

OrKoN commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Closing for now (might revisit later).

@OrKoN OrKoN closed this Nov 10, 2026
zjdCander pushed a commit to zjdCander/chrome-devtools-mcp that referenced this pull request Nov 16, 2026
…romeDevTools#3437)

`--browserUrl localhost:9222` (no `http://`) passes validation, so the
server starts normally. The first tool call then fails:

```
Could not connect to Chrome. …
Cause: Failed to fetch browser webSocket URL from localhost:9222: fetch failed
Cause: unknown scheme
```

The equivalent `--browserUrl 127.0.0.1:9222` is rejected at startup with
`Provided browserUrl 127.0.0.1:9222 is not valid URL.` The two inputs
are equally wrong but fail at different times and in different ways.

## Root cause

`src/config/browser-options.ts`, `browserUrl.coerce`, only checks that
the value parses as a URL:

```ts
try {
  new URL(url);
} catch {
  throw new Error(`Provided browserUrl ${url} is not valid URL.`);
}
return url;
```

`new URL('localhost:9222')` succeeds with `protocol === 'localhost:'`.
`127.0.0.1:9222` throws only because a scheme can't start with a digit.
The browser URL is fetched over HTTP (`<browserUrl>/json/version`) to
discover the WebSocket endpoint, so only `http:` and `https:` can ever
work.

## Reproduction (1.12.0, Node 24.22.2)

Coerce results before the fix:

```
localhost:9222 => localhost:9222
127.0.0.1:9222 ERR Provided browserUrl 127.0.0.1:9222 is not valid URL.
http://localhost:9222 => http://localhost:9222
new URL(localhost:9222).protocol = localhost:
```

## Fix

After parsing, require `http:` or `https:`. This mirrors the
`ws:`/`wss:` check that `--wsEndpoint` already has a few lines below.
With the fix, the server rejects the value at startup and exits 1:

```
$ node out/Default/gen/src/bin/chrome-devtools-mcp.js --browserUrl localhost:9222
Provided browserUrl localhost:9222 must use http:// or https:// protocol.
```

| `--browserUrl` value | Before | After |
|---|---|---|
| unset / `''` | `undefined` | `undefined` |
| `http://127.0.0.1:9222`, `https://example.com:9222`,
`http://localhost:9222/` | accepted | accepted |
| `localhost:9222` | accepted, fails at first tool call | rejected: must
use http(s) |
| `ws://127.0.0.1:9222`, `file:///tmp/chrome`, `chrome://inspect` |
accepted, fail at first tool call | rejected: must use http(s) |
| `127.0.0.1:9222` | rejected: not valid URL | rejected: not valid URL
(unchanged) |

## Tests

- Added `rejects browser urls without an http or https scheme` to
`tests/cli.test.ts`, next to the existing `browserUrl` tests. It calls
the option's `coerce` directly, like the viewport test does.
- It fails on `main` with `AssertionError: Missing expected exception`
(for `localhost:9222`) and passes with the fix.

On macOS (Node 24.22.2, `.nvmrc`):

- `npm run check-format` passes.
- `npm run test:no-build`: 1804 passed, 0 failed, 3 skipped. On `main`
it was 1800 passed, 0 failed, 3 skipped.
- The difference is the new test plus the 3-test `McpPage logpoints`
suite, which ran in this run but not in the `main` run.

The change doesn't touch the import block, and it merges cleanly onto
ChromeDevTools#2897, which also edits `tests/cli.test.ts`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Natasha Gorshunova <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants