browser-ref-mcp 0.0.0-stage → 0.1.0
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.
- package/LICENSE +202 -0
- package/README.md +174 -2
- package/README.zh-CN.md +157 -0
- package/dist/actions.js +381 -0
- package/dist/browserManager.js +410 -0
- package/dist/config.js +125 -0
- package/dist/errors.js +52 -0
- package/dist/index.js +68 -0
- package/dist/session.js +176 -0
- package/dist/snapshot.js +707 -0
- package/dist/tools.js +498 -0
- package/dist/util.js +113 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,175 @@
|
|
|
1
|
-
#
|
|
1
|
+
# browser-ref-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/browser-ref-mcp)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
|
|
7
|
+
**A reliable browser-control MCP server for AI agents.** Click by `ref`, not by brittle CSS selectors — and when the browser dies, the next call just brings it back.
|
|
8
|
+
|
|
9
|
+
Built for the two things that actually break agent-driven browsing:
|
|
10
|
+
|
|
11
|
+
| The problem | What happens with most browser tools | What this server does |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| **The agent can't click what it sees** | Selectors go stale between snapshot and click, shadow DOM and cross-origin iframes are invisible, the agent retries blindly | Semantic snapshot with `[eN]` refs, shadow DOM and cross-origin iframes flattened into one tree, four-level resolution fallback |
|
|
14
|
+
| **Sessions keep dying** | Browser closed by hand, process crash, port conflict — and the agent is stuck until you restart everything | Resident process, lazy restart on next call, tab session restored, orphan cleanup, timeout guardrails |
|
|
15
|
+
|
|
16
|
+
No browser download. It drives the Chrome or Edge you already have, via `playwright-core`.
|
|
17
|
+
|
|
18
|
+
Works with Claude Code, Claude Desktop, Cursor, Cline, Codex CLI, Windsurf, OpenClaw, and any client that speaks stdio MCP.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npx -y browser-ref-mcp
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Or add it to your client config:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"mcpServers": {
|
|
33
|
+
"browser-ref-mcp": {
|
|
34
|
+
"command": "npx",
|
|
35
|
+
"args": ["-y", "browser-ref-mcp"]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Restart the client **completely** (on macOS quit from the menu bar — closing the window does not reload config).
|
|
42
|
+
|
|
43
|
+
<details>
|
|
44
|
+
<summary>Install from source instead</summary>
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
git clone https://github.com/zy2755/browser-ref-mcp.git
|
|
48
|
+
cd browser-ref-mcp
|
|
49
|
+
npm install && npm run build
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Then point `command` at `node` and `args` at the **absolute** path of `dist/index.js` — GUI apps often don't inherit your shell's working directory, so relative paths tend to fail:
|
|
53
|
+
|
|
54
|
+
```json
|
|
55
|
+
{
|
|
56
|
+
"mcpServers": {
|
|
57
|
+
"browser-ref-mcp": {
|
|
58
|
+
"command": "node",
|
|
59
|
+
"args": ["/absolute/path/to/browser-ref-mcp/dist/index.js"]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Config file locations:
|
|
66
|
+
|
|
67
|
+
- **Claude Desktop** — macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`, Windows: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
68
|
+
- **Cursor** — `~/.cursor/mcp.json`, or Settings → Features → MCP
|
|
69
|
+
- **Claude Code** — `claude mcp add`, or the same `mcpServers` shape
|
|
70
|
+
|
|
71
|
+
</details>
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## The 14 tools
|
|
76
|
+
|
|
77
|
+
| Tool | What it does |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `browser_navigate(url)` | Navigate — automatically returns an interactive-element snapshot |
|
|
80
|
+
| `browser_snapshot` | Semantic snapshot. `filter: "interactive"` for interactive elements only, `rootRef` to scope to one subtree |
|
|
81
|
+
| `browser_click(ref)` | Click by ref, returns a fresh snapshot. Stale refs are re-resolved automatically |
|
|
82
|
+
| `browser_type(ref, text)` | Type into a field (clears first). `submit: true` presses Enter |
|
|
83
|
+
| `browser_press_key(key)` | Key press, e.g. `Enter` / `Escape` / `Control+a` |
|
|
84
|
+
| `browser_select_option(ref, values)` | Pick `<select>` options by value or label, two-pass match |
|
|
85
|
+
| `browser_hover(ref)` | Hover to reveal hover-only menus |
|
|
86
|
+
| `browser_scroll(direction, amount, ref?)` | Scroll the page, or a specific scroll container |
|
|
87
|
+
| `browser_screenshot` | Screenshot — returns the image and writes it to disk |
|
|
88
|
+
| `browser_evaluate(js)` | Escape hatch: run JS in the page |
|
|
89
|
+
| `browser_wait` | Wait for a duration, text, element, URL change, or network idle |
|
|
90
|
+
| `browser_tabs` | Multi-tab: `list` / `new` / `select` / `close` |
|
|
91
|
+
| `browser_get_info` | Health state, tab list, restart count, last error |
|
|
92
|
+
| `browser_close` | Close the browser (profile logins are kept) |
|
|
93
|
+
|
|
94
|
+
### Using refs
|
|
95
|
+
|
|
96
|
+
Every interactive element in a snapshot carries an `[eN]` label:
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
[e38] link "Sign in"
|
|
100
|
+
[e42] textbox "Email"
|
|
101
|
+
[e51] button "Continue"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
So the agent calls `browser_click({ "ref": "e42" })` — no selector guessing, no DOM archaeology. Elements inside iframes are flattened into the same tree with the same numbering, including across origins.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## How clicking actually works
|
|
109
|
+
|
|
110
|
+
Most "click by selector" failures come from the page changing between the moment the agent reads it and the moment it clicks. This server separates *reading* from *locating*:
|
|
111
|
+
|
|
112
|
+
1. **Semantic snapshot** — an in-page DOM walk (accessibility role + accessible name + `cursor: pointer` heuristics) that pierces **open shadow DOM** and flattens **cross-origin iframe** content into a single tree.
|
|
113
|
+
2. **Ref → locator recipe** — each ref stores both a CSS path (id-anchored + `nth-of-type`, unique within its frame) *and* a role+name semantic description.
|
|
114
|
+
3. **Resolution fallback chain** — `cssPath` → `getByRole` exact → `getByRole` fuzzy → `getByText`. Anything that hits is validated before use.
|
|
115
|
+
4. **Never a dead end** — if a ref is truly dead, the error message tells the agent what to do next (re-snapshot, or wait first) instead of leaving it stuck in a retry loop.
|
|
116
|
+
|
|
117
|
+
## How reconnection works
|
|
118
|
+
|
|
119
|
+
- **Process model** — the MCP server itself is a resident daemon (stdio clients hold it open). The browser launches over a **CDP pipe**, not a websocket port, so there are no port conflicts.
|
|
120
|
+
- **Lazy restart** — a health check runs before every tool call (`browser.isConnected()`). If the browser was closed by hand or crashed, the next call relaunches it and **restores all tabs**, explicitly telling the agent that old refs are invalid.
|
|
121
|
+
- **Orphan cleanup** — if a leftover process is holding the profile, it is killed by profile path before retrying.
|
|
122
|
+
- **Hang guardrails** — every Playwright call is wrapped in `withTimeout`, so a frozen browser returns a timeout instead of hanging forever.
|
|
123
|
+
- **Full-chain logging** — `~/.browser-mcp/logs/server-YYYYMMDD.log` (2 MB rotation, 3 files kept). `browser_get_info` exposes the last error and restart count.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Browser modes
|
|
128
|
+
|
|
129
|
+
**`launch` mode (default)** — starts a dedicated Chrome with its profile at `~/.browser-mcp/profile`. Logins persist across restarts. You sign in to a given site once in this profile.
|
|
130
|
+
|
|
131
|
+
**`connect` mode** — attach to an already-running browser that has a debugging port open; it is not killed on exit:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# start Chrome with a debugging port first
|
|
135
|
+
chrome.exe --remote-debugging-port=9222
|
|
136
|
+
|
|
137
|
+
node dist/index.js --connect # defaults to http://127.0.0.1:9222
|
|
138
|
+
node dist/index.js --cdp-url http://127.0.0.1:9223
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Configuration
|
|
142
|
+
|
|
143
|
+
Precedence: CLI args > env vars (`BROWSER_MCP_*`) > `~/.browser-mcp/config.json` > built-in defaults.
|
|
144
|
+
|
|
145
|
+
Common flags: `--headless` / `--profile <dir>` / `--channels chrome,msedge` / `--executable <path>` / `--viewport 1440x900` / `--start-url <url>` / `--dialogs accept|dismiss` (default `accept`).
|
|
146
|
+
|
|
147
|
+
Append them after the package name:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
"args": ["-y", "browser-ref-mcp", "--headless", "--channels", "msedge"]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Full list: `npx -y browser-ref-mcp --help`.
|
|
154
|
+
|
|
155
|
+
> **Don't set `executablePath` unless you have to.** Once it is set, the `channels` fallback chain (`chrome` → `msedge`) is skipped and only that single candidate remains — which lowers robustness across machines.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Security notes
|
|
160
|
+
|
|
161
|
+
- The dedicated profile holds live logins (cookies) — it *is* your browser identity. Never commit `~/.browser-mcp/profile` to any repository.
|
|
162
|
+
- Page dialogs are auto-accepted by default. The agent cannot handle CAPTCHAs or payment flows; those still need a human.
|
|
163
|
+
- Logs may contain page URLs including query parameters — clean them up after debugging.
|
|
164
|
+
|
|
165
|
+
## Roadmap
|
|
166
|
+
|
|
167
|
+
File upload, richer download management, request interception, cookie/storage management, PDF export, a CLI front end.
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
[Apache-2.0](./LICENSE)
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
[中文说明](./README.zh-CN.md)
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# browser-ref-mcp
|
|
2
|
+
|
|
3
|
+
给 AI agent 用的可靠浏览器控制 MCP server。针对现有浏览器工具的两大痛点专门设计:
|
|
4
|
+
|
|
5
|
+
- **点不了网页内容** → 语义快照 + ref 编号定位 + 多级兜底(见下文"点击是怎么解决的")
|
|
6
|
+
- **频繁断联出错** → 常驻进程 + 崩溃懒重启 + 会话恢复(见下文"断联是怎么解决的")
|
|
7
|
+
|
|
8
|
+
技术栈:Node + TypeScript + playwright-core(免下载浏览器,直接驱动系统已装的 Chrome/Edge)。
|
|
9
|
+
|
|
10
|
+
## 接入 MCP 客户端
|
|
11
|
+
|
|
12
|
+
server 走 stdio,任何支持 stdio MCP 的客户端都能接。先 `npm install && npm run build`,再把 `dist/index.js` 的**绝对路径**填进客户端配置(GUI 应用常不继承 shell 的工作目录,相对路径容易解析失败)。
|
|
13
|
+
|
|
14
|
+
### 通用配置
|
|
15
|
+
|
|
16
|
+
```json
|
|
17
|
+
{
|
|
18
|
+
"mcpServers": {
|
|
19
|
+
"browser-ref-mcp": {
|
|
20
|
+
"command": "node",
|
|
21
|
+
"args": ["/absolute/path/to/browser-ref-mcp/dist/index.js"]
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
若客户端启动时拿不到 shell 的 `PATH`,把 `command` 换成 node 可执行文件的完整路径,如 `/usr/local/bin/node` 或 `C:\\Program Files\\nodejs\\node.exe`。
|
|
28
|
+
|
|
29
|
+
### Claude Desktop
|
|
30
|
+
|
|
31
|
+
配置文件位置:
|
|
32
|
+
|
|
33
|
+
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
34
|
+
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"mcpServers": {
|
|
39
|
+
"browser-ref-mcp": {
|
|
40
|
+
"command": "node",
|
|
41
|
+
"args": ["C:\\path\\to\\browser-ref-mcp\\dist\\index.js"]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
写完后**完全退出并重启**客户端(macOS 需从菜单栏退出,只关窗口不会重载配置)。Claude Code / Cline / Cursor / OpenClaw 等同样用这份 `mcpServers` 结构,只是配置文件位置不同。
|
|
48
|
+
|
|
49
|
+
### 启动参数
|
|
50
|
+
|
|
51
|
+
需要调整浏览器行为时,直接往 `args` 追加命令行参数:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
"args": ["/absolute/path/to/dist/index.js", "--headless", "--channels", "msedge"]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
完整参数见 `node dist/index.js --help`。更推荐把常用项固化到 `~/.browser-mcp/config.json`(见下文「配置」),这样多个客户端共用一份配置。
|
|
58
|
+
|
|
59
|
+
## 构建
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install
|
|
63
|
+
npm run build # 产物在 dist/
|
|
64
|
+
npm run smoke # 冒烟测试(40 项,会真实启动 Chrome)
|
|
65
|
+
npm run serve:fixtures # 本地测试页(手动调试用)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 工具集(14 个)
|
|
69
|
+
|
|
70
|
+
| 工具 | 说明 |
|
|
71
|
+
|---|---|
|
|
72
|
+
| `browser_navigate(url)` | 导航,**自动附交互元素快照** |
|
|
73
|
+
| `browser_snapshot` | 语义快照;`filter:"interactive"` 只看交互元素,`rootRef` 只看某元素子树 |
|
|
74
|
+
| `browser_click(ref)` | 点击;点击后自动附新快照;失效 ref 自动重解析 |
|
|
75
|
+
| `browser_type(ref, text)` | 输入(先清空);`submit:true` 回车提交 |
|
|
76
|
+
| `browser_press_key(key)` | 按键,如 `Enter` / `Escape` / `Control+a` |
|
|
77
|
+
| `browser_select_option(ref, values)` | 下拉选择,value/label 自动两轮匹配 |
|
|
78
|
+
| `browser_hover(ref)` | 悬停(触发悬浮菜单) |
|
|
79
|
+
| `browser_scroll(direction, amount, ref?)` | 滚动页面或指定滚动容器 |
|
|
80
|
+
| `browser_screenshot` | 截屏,返回图片 + 落盘 |
|
|
81
|
+
| `browser_evaluate(js)` | 逃生舱:页面内执行 JS |
|
|
82
|
+
| `browser_wait` | 等待:时长 / 文本出现 / 元素出现 / URL 变化 / 网络空闲 |
|
|
83
|
+
| `browser_tabs` | 多标签管理:list / new / select / close |
|
|
84
|
+
| `browser_get_info` | 健康状态、标签列表、重启次数、最近错误 |
|
|
85
|
+
| `browser_close` | 关闭浏览器(profile 登录态保留) |
|
|
86
|
+
|
|
87
|
+
**ref 的用法**:快照里每个可交互元素带 `[eN]` 编号,`browser_click({ ref: "e42" })` 直接引用。iframe 内元素拍平进同一棵树,编号同样可用。
|
|
88
|
+
|
|
89
|
+
## 点击是怎么解决的
|
|
90
|
+
|
|
91
|
+
1. **语义快照**:in-page DOM 遍历(无障碍角色 + 可访问名 + cursor:pointer 启发式),穿透 **open shadow DOM**,跨源 **iframe 内容拍平**进同一棵树;
|
|
92
|
+
2. **ref → 定位配方**:每个 ref 同时记录 css 路径(有 id 锚定 + nth-of-type,frame 内唯一)和 role+name 语义描述;
|
|
93
|
+
3. **解析兜底链**:cssPath → getByRole 精确 → getByRole 模糊 → getByText,任何一层命中都验证后使用;
|
|
94
|
+
4. **绝不死路**:ref 彻底失效时报错信息里直接给出行动建议(重新快照 / 先等待),不会让 agent 卡死。
|
|
95
|
+
|
|
96
|
+
## 断联是怎么解决的
|
|
97
|
+
|
|
98
|
+
- **进程模型**:MCP server 本身就是常驻 daemon(stdio 客户端会长期持有),浏览器经 **CDP pipe**(非 websocket 端口)启动,无端口冲突;
|
|
99
|
+
- **懒重启**:每次工具调用前健康检查(`browser.isConnected()`);浏览器被手动关闭/崩溃后,下一次调用自动重启并**恢复所有标签页**,结果中明确提示"旧 ref 已失效";
|
|
100
|
+
- **孤儿清理**:profile 被残留进程占用时,启动前按 profile 路径定向 taskkill,再重试;
|
|
101
|
+
- **悬挂护栏**:所有 Playwright 调用套 `withTimeout`,浏览器僵死时调用能超时返回而不是永久卡住;
|
|
102
|
+
- **全链路日志**:`~/.browser-mcp/logs/server-YYYYMMDD.log`(2MB 轮转,保留 3 份),`browser_get_info` 可查最近错误与重启次数。
|
|
103
|
+
|
|
104
|
+
## 浏览器来源(双模式)
|
|
105
|
+
|
|
106
|
+
- **launch 模式(默认)**:启动专用 Chrome,profile 在 `~/.browser-mcp/profile` —— 登录态持久保存,重启不丢。首次用某网站需在此 profile 登录一次。
|
|
107
|
+
- **connect 模式**:接管已开调试端口的现有浏览器,退出时不杀它:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# 先手动带调试端口启动 Chrome
|
|
111
|
+
chrome.exe --remote-debugging-port=9222
|
|
112
|
+
# server 端
|
|
113
|
+
node dist/index.js --connect # 默认 http://127.0.0.1:9222
|
|
114
|
+
node dist/index.js --cdp-url http://127.0.0.1:9223
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 配置
|
|
118
|
+
|
|
119
|
+
优先级:命令行参数 > 环境变量(`BROWSER_MCP_*`)> `~/.browser-mcp/config.json` > 内置默认。
|
|
120
|
+
|
|
121
|
+
常用参数:`--headless`(无头)/ `--profile <dir>` / `--channels chrome,msedge` / `--executable <path>` / `--viewport 1440x900` / `--start-url <url>` / `--dialogs accept|dismiss`(对话框自动处理策略,默认 accept)。
|
|
122
|
+
|
|
123
|
+
完整参数见 `node dist/index.js --help`。
|
|
124
|
+
|
|
125
|
+
## 目录结构
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
src/
|
|
129
|
+
index.ts 入口:stdio server、优雅退出
|
|
130
|
+
config.ts 配置解析(文件/环境变量/命令行)
|
|
131
|
+
browserManager.ts 浏览器生命周期:启动/健康检查/懒重启/会话恢复/弹窗对话框下载
|
|
132
|
+
session.ts 标签页登记与持久化(原子写)
|
|
133
|
+
snapshot.ts in-page DOM walker + ref 注册表 + frame 拼接 + 渲染
|
|
134
|
+
actions.ts ref 解析兜底链 + 各类动作实现
|
|
135
|
+
tools.ts 14 个 MCP 工具定义与接线
|
|
136
|
+
errors.ts 错误整形(结构化 code + 行动建议)
|
|
137
|
+
util.ts 日志(轮转)/超时护栏/截断
|
|
138
|
+
test/
|
|
139
|
+
fixtures/ 本地测试页(基础/shadow/跨源iframe/对话框/懒加载)
|
|
140
|
+
serve.mjs fixture 服务器
|
|
141
|
+
smoke.mjs 40 项冒烟测试(含杀进程恢复、server 重启恢复)
|
|
142
|
+
e2e-baidu.mjs 真实互联网端到端(百度搜索全流程)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## 安全提示
|
|
146
|
+
|
|
147
|
+
- 专用 profile 存有登录态(Cookies),等同于你的浏览器身份,注意不要把 `~/.browser-mcp/profile` 提交到任何仓库;
|
|
148
|
+
- 工具会自动接受页面对话框(默认策略),agent 无法处理验证码/支付类强交互,这些仍需人工;
|
|
149
|
+
- 日志可能包含页面 URL(含查询参数),排查问题后注意清理。
|
|
150
|
+
|
|
151
|
+
## 二期 backlog
|
|
152
|
+
|
|
153
|
+
文件上传、下载管理增强、请求拦截、Cookie/存储管理、PDF 导出、CLI 前端。
|
|
154
|
+
|
|
155
|
+
## 许可证
|
|
156
|
+
|
|
157
|
+
Apache-2.0,见 [LICENSE](LICENSE)。
|