camoucore-mcp 0.0.0-stage → 0.3.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/README.md +165 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +107 -0
- package/dist/secrets.d.ts +50 -0
- package/dist/secrets.js +193 -0
- package/dist/server.d.ts +55 -0
- package/dist/server.js +659 -0
- package/dist/tree.d.ts +10 -0
- package/dist/tree.js +36 -0
- package/package.json +59 -4
package/README.md
CHANGED
|
@@ -1,3 +1,166 @@
|
|
|
1
|
-
#
|
|
1
|
+
# camoucore-mcp
|
|
2
|
+
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io) server that lets an agent drive a
|
|
4
|
+
Camoufox or Camouwolf browser. The agent reads each page as an accessibility
|
|
5
|
+
tree with a ref on every element and acts on those refs; input goes through
|
|
6
|
+
the launcher's `humanize` engines, so it moves, types and scrolls like a
|
|
7
|
+
person. It types passwords and one-time codes it is never shown, and hands
|
|
8
|
+
the window to the person at the keyboard for anything else it must not do
|
|
9
|
+
itself, such as a payment.
|
|
10
|
+
|
|
11
|
+
## Running it
|
|
12
|
+
|
|
13
|
+
The server takes the launcher package to drive. That package must export
|
|
14
|
+
`PRODUCT` and `NewBrowser`, as camouwolf does from 0.1.0, and its browser must
|
|
15
|
+
be installed:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npx camouwolf fetch
|
|
19
|
+
npx -y -p camoucore-mcp -p camouwolf camoucore-mcp camouwolf
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
In Claude Code:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
claude mcp add camouwolf -- npx -y -p camoucore-mcp -p camouwolf camoucore-mcp camouwolf
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Any other MCP client takes the same command:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"mcpServers": {
|
|
33
|
+
"camouwolf": {
|
|
34
|
+
"command": "npx",
|
|
35
|
+
"args": ["-y", "-p", "camoucore-mcp", "-p", "camouwolf", "camoucore-mcp", "camouwolf"]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The launcher reads its usual environment, so `CAMOUWOLF_EXECUTABLE_PATH` points
|
|
42
|
+
the server at a build of your own. Node.js 22.15 or newer.
|
|
43
|
+
|
|
44
|
+
## Tools
|
|
45
|
+
|
|
46
|
+
| Tool | What it does |
|
|
47
|
+
|---|---|
|
|
48
|
+
| `session_open { os?, visible?, humanize?, passthrough?, profile? }` | Launches one browser with a fresh identity. `visible` opens a window a person can take over; `humanize` defaults to true. `passthrough` launches with this machine's own fingerprint instead, on a launcher that exports `PASSTHROUGH`. `profile` runs on a copy of [your own profile](#your-own-profile). |
|
|
49
|
+
| `navigate { session_id, url }` | http and https only. |
|
|
50
|
+
| `snapshot { session_id, ref?, depth?, find? }` | The accessibility tree with refs. Refs last until the next snapshot or navigation. On a large page, read only what you need: `ref` is one element's subtree, `depth` stops that many levels down, and `find` keeps the lines containing its text (ignoring case) under their named ancestors. `find` searches the redacted tree, so it cannot test for a secret. |
|
|
51
|
+
| `click`, `hover { session_id, ref }` | |
|
|
52
|
+
| `type { session_id, ref, text, submit? }` | Key by key; `submit` presses Enter. |
|
|
53
|
+
| `type_secret { session_id, ref, name, submit? }` | Types a [secret](#secrets) by name. Only when secrets are configured. |
|
|
54
|
+
| `select_option { session_id, ref, options }` | Chooses options in a `<select>` by label or value. |
|
|
55
|
+
| `press_key { session_id, key }` | A key or chord on the focused element: `Enter`, `Escape`, `Control+a`. |
|
|
56
|
+
| `go_back { session_id }` | One page back in the tab's history. |
|
|
57
|
+
| `tabs`, `switch_tab { session_id, index, close? }` | Lists the session's tabs, and moves to one (closing the current one with `close`). |
|
|
58
|
+
| `scroll { session_id, ref, delta_y? }` | Into view, then a wheel of `delta_y` pixels. |
|
|
59
|
+
| `screenshot { session_id }` | A PNG of the viewport, refused while the page holds a secret. |
|
|
60
|
+
| `upload_file { session_id, ref, files }` | Attaches files [from the uploads directory](#uploads), by name, to a file input or to the button that opens a file picker. Only with `--uploads`. |
|
|
61
|
+
| `hand_off { session_id, reason }` | Brings a visible session's window to the front. The agent stops and waits for the person. |
|
|
62
|
+
| `solve_captcha { session_id }` | Only when the [captchakraken](https://www.npmjs.com/package/captchakraken) package is installed beside the server; it uses the key that package is configured with. |
|
|
63
|
+
| `session_close { session_id }` | Closes the browser. |
|
|
64
|
+
|
|
65
|
+
One session is one browser and one identity: sessions never share cookies or a
|
|
66
|
+
fingerprint. Closing the client closes every browser it opened.
|
|
67
|
+
|
|
68
|
+
## Secrets
|
|
69
|
+
|
|
70
|
+
Passwords and one-time codes go in a JSON file that only you can read, scoped
|
|
71
|
+
to the sites they belong to, after browser-use's `sensitive_data`:
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"https://accounts.example.com": {
|
|
76
|
+
"email": "me@example.com",
|
|
77
|
+
"password": "correct horse battery staple",
|
|
78
|
+
"otp": { "totp": "JBSWY3DPEHPK3PXP" }
|
|
79
|
+
},
|
|
80
|
+
"*.example.org": { "password": "another one" }
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
chmod 600 ~/.config/camoucore-mcp/secrets.json
|
|
86
|
+
claude mcp add camouwolf -- npx -y -p camoucore-mcp -p camouwolf camoucore-mcp camouwolf \
|
|
87
|
+
--secrets ~/.config/camoucore-mcp/secrets.json
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- **The agent sees names, never values.** The server's instructions list each
|
|
91
|
+
pattern and its names, and `snapshot` lists those usable on the current page.
|
|
92
|
+
`type_secret` types the value key by key, like `type`.
|
|
93
|
+
- **A secret is typed only on its site.** The check is on the URL of the frame
|
|
94
|
+
the field is in, so a login form in a third-party iframe on the right page is
|
|
95
|
+
refused. A pattern is `[scheme://][*.]host[:port][/path]`: the scheme is
|
|
96
|
+
`https` unless it says `http` or `http*` (both), `*.` adds subdomains, and a
|
|
97
|
+
path may end in `*`. Every secret has a pattern; there is no secret for any
|
|
98
|
+
site.
|
|
99
|
+
- **Values are redacted from everything the server returns**: snapshots, page
|
|
100
|
+
titles, URLs and errors, as `<secret:name>`, including their HTML, JSON, URL,
|
|
101
|
+
base64 and hex encodings and any change of case. A snapshot holds every
|
|
102
|
+
field's value, masked or not, so this is what keeps a typed password out of
|
|
103
|
+
the conversation.
|
|
104
|
+
- **Pixels cannot be redacted,** so `screenshot` is refused while the page
|
|
105
|
+
holds a secret anywhere in its tree.
|
|
106
|
+
- **One-time codes:** `{ "totp": "<base32 seed>" }` types the current 6-digit
|
|
107
|
+
code (RFC 6238, SHA-1, 30 seconds), the seed an authenticator app is set up
|
|
108
|
+
with. The seed is never typed or shown.
|
|
109
|
+
- The file is refused if anyone but its owner can read it, and a value under 4
|
|
110
|
+
characters is refused, since redacting it would mangle ordinary text.
|
|
111
|
+
|
|
112
|
+
What this does not cover: the browser's own memory, and anything you paste into
|
|
113
|
+
the chat yourself. A secret you have not configured, or a payment, is still a
|
|
114
|
+
`hand_off`.
|
|
115
|
+
|
|
116
|
+
## Your own profile
|
|
117
|
+
|
|
118
|
+
`--profile <dir>` lets `session_open { profile: true }` start a browser signed
|
|
119
|
+
in wherever you are, from your own browser's profile directory (Firefox's is
|
|
120
|
+
under `~/.mozilla/firefox/`, or `~/snap/firefox/common/.mozilla/firefox/` for
|
|
121
|
+
the snap; `about:profiles` shows it):
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
camoucore-mcp camoufox --profile ~/.mozilla/firefox/abcd1234.default-release
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
- **Each session runs on a private copy,** made when it opens and deleted when
|
|
128
|
+
it closes. The browser writes to its profile, Playwright's prefs included, so
|
|
129
|
+
the original is never launched, and nothing the session does is saved back.
|
|
130
|
+
Your own browser can stay open.
|
|
131
|
+
- **The agent gets your signed-in sessions,** for every site the profile is
|
|
132
|
+
signed in to. Pair it with `--allowed-domains` to keep it to the ones the task
|
|
133
|
+
needs.
|
|
134
|
+
- **Use `passthrough` with it.** Sites know that profile with this machine's
|
|
135
|
+
fingerprint; a drawn one on the same cookies is a mismatch they can see.
|
|
136
|
+
- The profile must be the launcher's engine: a Firefox profile for camoufox,
|
|
137
|
+
a Chromium one for camouwolf.
|
|
138
|
+
|
|
139
|
+
## Uploads
|
|
140
|
+
|
|
141
|
+
`--uploads <dir>` is the one directory `upload_file` may attach files from.
|
|
142
|
+
Without it there is no `upload_file`, since a page could otherwise be sent any
|
|
143
|
+
file the server can read. A name that leads out of the directory, by `..`, an
|
|
144
|
+
absolute path or a link, is refused, and refused before it is looked at, so the
|
|
145
|
+
agent never learns what exists elsewhere.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
camoucore-mcp camoufox --uploads ~/agent-uploads
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Allowed domains
|
|
152
|
+
|
|
153
|
+
`--allowed-domains <pattern,...>` keeps the agent on those sites, with the same
|
|
154
|
+
patterns as secrets:
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
camoucore-mcp camouwolf --allowed-domains "*.example.com,accounts.example.org"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`navigate` refuses any other URL, and a tab whose page goes elsewhere (a link,
|
|
161
|
+
a redirect, a script) is sent back to `about:blank`, with the tool call that
|
|
162
|
+
saw it refused. No tool result ever shows such a page. It is a guard for an
|
|
163
|
+
agent that a page may try to steer, not a network filter: the request that left
|
|
164
|
+
the allowed sites has already been sent, and a page's own images, scripts and
|
|
165
|
+
iframes may come from anywhere.
|
|
2
166
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** camoucore-mcp <launcher>: serve a launcher's browser over MCP on stdio. */
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { Writable } from "node:stream";
|
|
6
|
+
import { parseArgs } from "node:util";
|
|
7
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
|
+
import { Secrets } from "./secrets.js";
|
|
9
|
+
import { createServer, loadLauncher } from "./server.js";
|
|
10
|
+
// stdout is the protocol, and launchers print to it, so everything but the
|
|
11
|
+
// transport goes to stderr. This comes before the launcher is imported, since
|
|
12
|
+
// importing it can print too.
|
|
13
|
+
const protocolWrite = process.stdout.write.bind(process.stdout);
|
|
14
|
+
process.stdout.write = process.stderr.write.bind(process.stderr);
|
|
15
|
+
const protocolStream = new Writable({
|
|
16
|
+
write: (chunk, encoding, done) => void protocolWrite(chunk, encoding, done),
|
|
17
|
+
});
|
|
18
|
+
const USAGE = "usage: camoucore-mcp <launcher> [--secrets <file>] [--allowed-domains <pattern,...>] [--profile <dir>] [--uploads <dir>]\n" +
|
|
19
|
+
" <launcher> the package to drive: camoufox or camouwolf\n" +
|
|
20
|
+
" --profile your own browser profile, which session_open { profile: true } runs a copy of\n" +
|
|
21
|
+
" --uploads the one directory upload_file may attach files from";
|
|
22
|
+
let parsed;
|
|
23
|
+
function parse() {
|
|
24
|
+
return parseArgs({
|
|
25
|
+
allowPositionals: true,
|
|
26
|
+
options: {
|
|
27
|
+
secrets: { type: "string" },
|
|
28
|
+
"allowed-domains": { type: "string" },
|
|
29
|
+
profile: { type: "string" },
|
|
30
|
+
uploads: { type: "string" },
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
parsed = parse();
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`${error.message}\n${USAGE}`);
|
|
39
|
+
process.exit(2);
|
|
40
|
+
}
|
|
41
|
+
const [specifier] = parsed.positionals;
|
|
42
|
+
if (!specifier || parsed.positionals.length > 1) {
|
|
43
|
+
console.error(USAGE);
|
|
44
|
+
process.exit(2);
|
|
45
|
+
}
|
|
46
|
+
// Read before anything launches, so a bad file stops the server at start.
|
|
47
|
+
const secrets = parsed.values.secrets
|
|
48
|
+
? Secrets.load(parsed.values.secrets)
|
|
49
|
+
: undefined;
|
|
50
|
+
const profile = parsed.values.profile
|
|
51
|
+
? path.resolve(parsed.values.profile)
|
|
52
|
+
: undefined;
|
|
53
|
+
if (profile &&
|
|
54
|
+
!fs.statSync(profile, { throwIfNoEntry: false })?.isDirectory()) {
|
|
55
|
+
console.error(`--profile ${profile} is not a directory.`);
|
|
56
|
+
process.exit(2);
|
|
57
|
+
}
|
|
58
|
+
const uploads = parsed.values.uploads
|
|
59
|
+
? path.resolve(parsed.values.uploads)
|
|
60
|
+
: undefined;
|
|
61
|
+
if (uploads &&
|
|
62
|
+
!fs.statSync(uploads, { throwIfNoEntry: false })?.isDirectory()) {
|
|
63
|
+
console.error(`--uploads ${uploads} is not a directory.`);
|
|
64
|
+
process.exit(2);
|
|
65
|
+
}
|
|
66
|
+
const allowedDomains = parsed.values["allowed-domains"]
|
|
67
|
+
?.split(",")
|
|
68
|
+
.map((pattern) => pattern.trim())
|
|
69
|
+
.filter(Boolean);
|
|
70
|
+
/**
|
|
71
|
+
* The optional captchakraken package's solver. Not installed, the server
|
|
72
|
+
* offers no solve_captcha; installed, any failure to load it is loud, so a
|
|
73
|
+
* broken install never passes for a missing one.
|
|
74
|
+
*/
|
|
75
|
+
async function loadSolver() {
|
|
76
|
+
const solverPackage = "captchakraken";
|
|
77
|
+
let resolved;
|
|
78
|
+
try {
|
|
79
|
+
resolved = import.meta.resolve(solverPackage);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const { CaptchaKrakenSolver } = await import(resolved);
|
|
85
|
+
return new CaptchaKrakenSolver();
|
|
86
|
+
}
|
|
87
|
+
const { launcher, browserType } = await loadLauncher(specifier);
|
|
88
|
+
const { server, closeAll } = createServer(launcher, browserType, {
|
|
89
|
+
solver: await loadSolver(),
|
|
90
|
+
secrets,
|
|
91
|
+
allowedDomains,
|
|
92
|
+
profile,
|
|
93
|
+
uploads,
|
|
94
|
+
});
|
|
95
|
+
/** A client going away must not leave its browsers running. */
|
|
96
|
+
let shuttingDown = false;
|
|
97
|
+
async function shutdown() {
|
|
98
|
+
if (shuttingDown)
|
|
99
|
+
return;
|
|
100
|
+
shuttingDown = true;
|
|
101
|
+
await closeAll();
|
|
102
|
+
process.exit(0);
|
|
103
|
+
}
|
|
104
|
+
process.on("SIGINT", shutdown);
|
|
105
|
+
process.on("SIGTERM", shutdown);
|
|
106
|
+
process.stdin.on("close", shutdown);
|
|
107
|
+
await server.connect(new StdioServerTransport(process.stdin, protocolStream));
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A URL pattern: `[scheme://]host[:port][/path]`. The scheme is `https`,
|
|
3
|
+
* `http` or `http*` (both), and `https` when left out. `*.example.com` is
|
|
4
|
+
* example.com and its subdomains. A port must match when given. A path is a
|
|
5
|
+
* glob on the URL's path, where `*` is any run of characters.
|
|
6
|
+
*/
|
|
7
|
+
export declare class UrlPattern {
|
|
8
|
+
readonly source: string;
|
|
9
|
+
private readonly schemes;
|
|
10
|
+
private readonly host;
|
|
11
|
+
private readonly subdomains;
|
|
12
|
+
private readonly port;
|
|
13
|
+
private readonly path;
|
|
14
|
+
constructor(source: string);
|
|
15
|
+
matches(url: string): boolean;
|
|
16
|
+
}
|
|
17
|
+
/** RFC 6238 with the defaults every authenticator app uses: SHA-1, 30 s, 6 digits. */
|
|
18
|
+
export declare function totp(seed: string, at?: number): string;
|
|
19
|
+
type Value = string | {
|
|
20
|
+
totp: string;
|
|
21
|
+
};
|
|
22
|
+
export declare class Secrets {
|
|
23
|
+
private readonly entries;
|
|
24
|
+
/** Each literal value with its name, longest first, for redaction. */
|
|
25
|
+
private readonly literals;
|
|
26
|
+
/**
|
|
27
|
+
* `byPattern` maps a URL pattern to that domain's secrets: a name to its
|
|
28
|
+
* value, or to `{ totp: "<base32 seed>" }` for a one-time code.
|
|
29
|
+
*/
|
|
30
|
+
constructor(byPattern: Record<string, Record<string, Value>>);
|
|
31
|
+
/** Reads a JSON secrets file that only its owner can read. */
|
|
32
|
+
static load(file: string): Secrets;
|
|
33
|
+
get empty(): boolean;
|
|
34
|
+
/** Every pattern and the names under it; neither is secret. */
|
|
35
|
+
names(): Record<string, string[]>;
|
|
36
|
+
/** The names usable on a page at `url`. */
|
|
37
|
+
namesFor(url: string): string[];
|
|
38
|
+
/** The text to type for `name` in a frame at `url`; refuses anywhere else. */
|
|
39
|
+
valueFor(name: string, url: string): string;
|
|
40
|
+
/** Whether `text` holds any secret value, in any case. */
|
|
41
|
+
contains(text: string): boolean;
|
|
42
|
+
/** `text` with every secret value, in every form, as `<secret:name>`. */
|
|
43
|
+
redact(text: string): string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `text` with `value`, and the escaped and encoded forms it takes in markup,
|
|
47
|
+
* JSON, URLs and headers, in any case, replaced by `replacement`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function redactValue(text: string, value: string, replacement: string): string;
|
|
50
|
+
export {};
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets the agent can type but never read, after browser-use's
|
|
3
|
+
* `sensitive_data`: the model names a secret, the server types its value into
|
|
4
|
+
* a field whose frame is on the secret's domain, and every value is replaced
|
|
5
|
+
* by `<secret:name>` in whatever goes back to the model.
|
|
6
|
+
*/
|
|
7
|
+
import { createHmac } from "node:crypto";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
/**
|
|
10
|
+
* A URL pattern: `[scheme://]host[:port][/path]`. The scheme is `https`,
|
|
11
|
+
* `http` or `http*` (both), and `https` when left out. `*.example.com` is
|
|
12
|
+
* example.com and its subdomains. A port must match when given. A path is a
|
|
13
|
+
* glob on the URL's path, where `*` is any run of characters.
|
|
14
|
+
*/
|
|
15
|
+
export class UrlPattern {
|
|
16
|
+
source;
|
|
17
|
+
schemes;
|
|
18
|
+
host;
|
|
19
|
+
subdomains;
|
|
20
|
+
port;
|
|
21
|
+
path;
|
|
22
|
+
constructor(source) {
|
|
23
|
+
this.source = source;
|
|
24
|
+
const parts = /^(?:(https|http|http\*):\/\/)?(\*\.)?([^/:*?#]+)(?::(\d+))?(\/[^?#]*)?$/i.exec(source);
|
|
25
|
+
if (!parts) {
|
|
26
|
+
throw new Error(`${source} is not a URL pattern: [https|http|http*://][*.]host[:port][/path]`);
|
|
27
|
+
}
|
|
28
|
+
const [, scheme = "https", wildcard, host, port, path] = parts;
|
|
29
|
+
this.schemes =
|
|
30
|
+
scheme === "http*" ? ["http:", "https:"] : [`${scheme.toLowerCase()}:`];
|
|
31
|
+
// The URL parser's hostname: lowercase, IDN as punycode, as a URL's is.
|
|
32
|
+
this.host = new URL(`https://${host}`).hostname;
|
|
33
|
+
this.subdomains = Boolean(wildcard);
|
|
34
|
+
this.port = port;
|
|
35
|
+
this.path =
|
|
36
|
+
path === undefined
|
|
37
|
+
? undefined
|
|
38
|
+
: new RegExp(`^${path
|
|
39
|
+
.split("*")
|
|
40
|
+
.map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
|
|
41
|
+
.join(".*")}$`);
|
|
42
|
+
}
|
|
43
|
+
matches(url) {
|
|
44
|
+
let parsed;
|
|
45
|
+
try {
|
|
46
|
+
parsed = new URL(url);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const defaultPort = parsed.protocol === "https:" ? "443" : "80";
|
|
52
|
+
return (this.schemes.includes(parsed.protocol) &&
|
|
53
|
+
(parsed.hostname === this.host ||
|
|
54
|
+
(this.subdomains && parsed.hostname.endsWith(`.${this.host}`))) &&
|
|
55
|
+
(this.port === undefined || (parsed.port || defaultPort) === this.port) &&
|
|
56
|
+
(this.path === undefined || this.path.test(parsed.pathname)));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function base32Decode(text) {
|
|
60
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
61
|
+
const clean = text.replace(/[\s=-]/g, "").toUpperCase();
|
|
62
|
+
let bits = "";
|
|
63
|
+
for (const char of clean) {
|
|
64
|
+
const value = alphabet.indexOf(char);
|
|
65
|
+
if (value < 0)
|
|
66
|
+
throw new Error("a TOTP seed is base32");
|
|
67
|
+
bits += value.toString(2).padStart(5, "0");
|
|
68
|
+
}
|
|
69
|
+
const bytes = bits.match(/.{8}/g) ?? [];
|
|
70
|
+
return Buffer.from(bytes.map((byte) => Number.parseInt(byte, 2)));
|
|
71
|
+
}
|
|
72
|
+
/** RFC 6238 with the defaults every authenticator app uses: SHA-1, 30 s, 6 digits. */
|
|
73
|
+
export function totp(seed, at = Date.now()) {
|
|
74
|
+
const counter = Buffer.alloc(8);
|
|
75
|
+
counter.writeBigUInt64BE(BigInt(Math.floor(at / 30_000)));
|
|
76
|
+
const hmac = createHmac("sha1", base32Decode(seed)).update(counter).digest();
|
|
77
|
+
const offset = hmac.at(-1) & 0xf;
|
|
78
|
+
const code = (hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000;
|
|
79
|
+
return code.toString().padStart(6, "0");
|
|
80
|
+
}
|
|
81
|
+
/** Below this a value would be redacted out of ordinary text too often. */
|
|
82
|
+
const MIN_LENGTH = 4;
|
|
83
|
+
export class Secrets {
|
|
84
|
+
entries = [];
|
|
85
|
+
/** Each literal value with its name, longest first, for redaction. */
|
|
86
|
+
literals;
|
|
87
|
+
/**
|
|
88
|
+
* `byPattern` maps a URL pattern to that domain's secrets: a name to its
|
|
89
|
+
* value, or to `{ totp: "<base32 seed>" }` for a one-time code.
|
|
90
|
+
*/
|
|
91
|
+
constructor(byPattern) {
|
|
92
|
+
for (const [source, secrets] of Object.entries(byPattern)) {
|
|
93
|
+
if (typeof secrets !== "object" || secrets === null) {
|
|
94
|
+
throw new Error(`Secrets are scoped to a URL pattern: give ${source} an object of name to value.`);
|
|
95
|
+
}
|
|
96
|
+
const pattern = new UrlPattern(source);
|
|
97
|
+
for (const [name, value] of Object.entries(secrets)) {
|
|
98
|
+
const literal = typeof value === "string" ? value : value?.totp;
|
|
99
|
+
if (typeof literal !== "string") {
|
|
100
|
+
throw new Error(`Secret ${name} for ${source} is a string or { "totp": "<base32 seed>" }.`);
|
|
101
|
+
}
|
|
102
|
+
if (literal.length < MIN_LENGTH) {
|
|
103
|
+
throw new Error(`Secret ${name} for ${source} is shorter than ${MIN_LENGTH} characters, too short to keep out of page text.`);
|
|
104
|
+
}
|
|
105
|
+
if (typeof value !== "string")
|
|
106
|
+
totp(value.totp);
|
|
107
|
+
this.entries.push({ pattern, name, value });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
this.literals = this.entries
|
|
111
|
+
.map((entry) => [
|
|
112
|
+
typeof entry.value === "string" ? entry.value : entry.value.totp,
|
|
113
|
+
entry.name,
|
|
114
|
+
])
|
|
115
|
+
.sort(([a], [b]) => b.length - a.length);
|
|
116
|
+
}
|
|
117
|
+
/** Reads a JSON secrets file that only its owner can read. */
|
|
118
|
+
static load(file) {
|
|
119
|
+
if (process.platform !== "win32" && fs.statSync(file).mode & 0o077) {
|
|
120
|
+
throw new Error(`${file} can be read by other users. \`chmod 600 ${file}\` makes it the owner's alone.`);
|
|
121
|
+
}
|
|
122
|
+
return new Secrets(JSON.parse(fs.readFileSync(file, "utf8")));
|
|
123
|
+
}
|
|
124
|
+
get empty() {
|
|
125
|
+
return this.entries.length === 0;
|
|
126
|
+
}
|
|
127
|
+
/** Every pattern and the names under it; neither is secret. */
|
|
128
|
+
names() {
|
|
129
|
+
const names = {};
|
|
130
|
+
for (const { pattern, name } of this.entries) {
|
|
131
|
+
names[pattern.source] ??= [];
|
|
132
|
+
names[pattern.source].push(name);
|
|
133
|
+
}
|
|
134
|
+
return names;
|
|
135
|
+
}
|
|
136
|
+
/** The names usable on a page at `url`. */
|
|
137
|
+
namesFor(url) {
|
|
138
|
+
return [
|
|
139
|
+
...new Set(this.entries.filter((e) => e.pattern.matches(url)).map((e) => e.name)),
|
|
140
|
+
];
|
|
141
|
+
}
|
|
142
|
+
/** The text to type for `name` in a frame at `url`; refuses anywhere else. */
|
|
143
|
+
valueFor(name, url) {
|
|
144
|
+
const named = this.entries.filter((entry) => entry.name === name);
|
|
145
|
+
if (!named.length)
|
|
146
|
+
throw new Error(`There is no secret named ${name}.`);
|
|
147
|
+
const here = named.filter((entry) => entry.pattern.matches(url));
|
|
148
|
+
if (!here.length) {
|
|
149
|
+
throw new Error(`Secret ${name} is for ${named.map((e) => e.pattern.source).join(", ")}, and this field's frame is at ${url}.`);
|
|
150
|
+
}
|
|
151
|
+
const values = new Set(here.map((entry) => JSON.stringify(entry.value)));
|
|
152
|
+
if (values.size > 1) {
|
|
153
|
+
throw new Error(`Secret ${name} has different values for patterns that all match ${url}: ${here.map((e) => e.pattern.source).join(", ")}.`);
|
|
154
|
+
}
|
|
155
|
+
const { value } = here[0];
|
|
156
|
+
return typeof value === "string" ? value : totp(value.totp);
|
|
157
|
+
}
|
|
158
|
+
/** Whether `text` holds any secret value, in any case. */
|
|
159
|
+
contains(text) {
|
|
160
|
+
const lower = text.toLowerCase();
|
|
161
|
+
return this.literals.some(([value]) => lower.includes(value.toLowerCase()));
|
|
162
|
+
}
|
|
163
|
+
/** `text` with every secret value, in every form, as `<secret:name>`. */
|
|
164
|
+
redact(text) {
|
|
165
|
+
return this.literals.reduce((redacted, [value, name]) => redactValue(redacted, value, `<secret:${name}>`), text);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* `text` with `value`, and the escaped and encoded forms it takes in markup,
|
|
170
|
+
* JSON, URLs and headers, in any case, replaced by `replacement`.
|
|
171
|
+
*/
|
|
172
|
+
export function redactValue(text, value, replacement) {
|
|
173
|
+
const forms = new Set([
|
|
174
|
+
value,
|
|
175
|
+
JSON.stringify(value).slice(1, -1),
|
|
176
|
+
encodeURIComponent(value),
|
|
177
|
+
value
|
|
178
|
+
.replace(/&/g, "&")
|
|
179
|
+
.replace(/</g, "<")
|
|
180
|
+
.replace(/>/g, ">")
|
|
181
|
+
.replace(/"/g, """)
|
|
182
|
+
.replace(/'/g, "'"),
|
|
183
|
+
Buffer.from(value).toString("base64"),
|
|
184
|
+
Buffer.from(value).toString("base64url"),
|
|
185
|
+
Buffer.from(value).toString("hex"),
|
|
186
|
+
]);
|
|
187
|
+
let redacted = text;
|
|
188
|
+
for (const form of forms) {
|
|
189
|
+
const escaped = form.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
190
|
+
redacted = redacted.replace(new RegExp(escaped, "gi"), replacement);
|
|
191
|
+
}
|
|
192
|
+
return redacted;
|
|
193
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { Browser, BrowserContext, BrowserType, Page } from "playwright-core";
|
|
3
|
+
import { Secrets } from "./secrets.js";
|
|
4
|
+
/** What a launcher package exports for this server to drive it. */
|
|
5
|
+
export interface Launcher {
|
|
6
|
+
PRODUCT: {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly title: string;
|
|
9
|
+
readonly engine: {
|
|
10
|
+
readonly name: "firefox" | "chromium";
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* True when NewBrowser takes `passthrough`. A launcher without it would
|
|
15
|
+
* ignore the option and launch with an injected fingerprint, so the server
|
|
16
|
+
* asks rather than finding out from the page.
|
|
17
|
+
*/
|
|
18
|
+
PASSTHROUGH?: boolean;
|
|
19
|
+
/** A Browser, or with `persistent_context` the profile's BrowserContext. */
|
|
20
|
+
NewBrowser(browserType: BrowserType, options: Record<string, unknown>): Promise<Browser | BrowserContext>;
|
|
21
|
+
}
|
|
22
|
+
/** The `captchakraken` solver, which finds the captcha on a page itself. */
|
|
23
|
+
export interface Solver {
|
|
24
|
+
solve(page: Page): Promise<{
|
|
25
|
+
isSolved?: boolean;
|
|
26
|
+
} | undefined>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Imports a launcher by package name or path, and the Playwright browser type
|
|
30
|
+
* it launches, from the Playwright the launcher itself resolves.
|
|
31
|
+
*/
|
|
32
|
+
export declare function loadLauncher(specifier: string): Promise<{
|
|
33
|
+
launcher: Launcher;
|
|
34
|
+
browserType: BrowserType;
|
|
35
|
+
}>;
|
|
36
|
+
export interface ServerOptions {
|
|
37
|
+
solver?: Solver;
|
|
38
|
+
/**
|
|
39
|
+
* The person's own browser profile, which a session opened with
|
|
40
|
+
* `profile: true` runs on a copy of; without one, that is refused.
|
|
41
|
+
*/
|
|
42
|
+
profile?: string;
|
|
43
|
+
secrets?: Secrets;
|
|
44
|
+
/** URL patterns, as for secrets, that pages may load; empty allows any. */
|
|
45
|
+
allowedDomains?: string[];
|
|
46
|
+
/**
|
|
47
|
+
* The one directory upload_file may attach files from; without one there
|
|
48
|
+
* is no upload_file, since a page could otherwise be sent any file here.
|
|
49
|
+
*/
|
|
50
|
+
uploads?: string;
|
|
51
|
+
}
|
|
52
|
+
export declare function createServer(launcher: Launcher, browserType: BrowserType, { solver, secrets, allowedDomains, profile, uploads, }?: ServerOptions): {
|
|
53
|
+
server: McpServer;
|
|
54
|
+
closeAll(): Promise<void>;
|
|
55
|
+
};
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A launcher's browser as MCP tools: open a session, read the page as an
|
|
3
|
+
* accessibility tree with refs, act on those refs, type secrets the agent
|
|
4
|
+
* never sees, and hand the window to a person for the rest.
|
|
5
|
+
*
|
|
6
|
+
* One session is one launch, so one identity. Nothing is pooled behind a
|
|
7
|
+
* session id: two tasks sharing a browser would share its fingerprint and its
|
|
8
|
+
* cookies. Input goes through the launcher's `humanize` engines, which plan the
|
|
9
|
+
* pointer paths, key timing and scrolling in this process.
|
|
10
|
+
*/
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import { createRequire } from "node:module";
|
|
14
|
+
import os from "node:os";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
import { redactValue, Secrets, UrlPattern } from "./secrets.js";
|
|
19
|
+
import { findInTree } from "./tree.js";
|
|
20
|
+
const VERSION = createRequire(import.meta.url)("../package.json").version;
|
|
21
|
+
/**
|
|
22
|
+
* Imports a launcher by package name or path, and the Playwright browser type
|
|
23
|
+
* it launches, from the Playwright the launcher itself resolves.
|
|
24
|
+
*/
|
|
25
|
+
export async function loadLauncher(specifier) {
|
|
26
|
+
const module = await import(specifier);
|
|
27
|
+
if (!module.PRODUCT?.engine || typeof module.NewBrowser !== "function") {
|
|
28
|
+
throw new Error(`${specifier} does not export PRODUCT and NewBrowser, so it is not a launcher this server can drive.`);
|
|
29
|
+
}
|
|
30
|
+
const launcher = module;
|
|
31
|
+
const playwright = createRequire(import.meta.resolve(specifier))("playwright-core");
|
|
32
|
+
return { launcher, browserType: playwright[launcher.PRODUCT.engine.name] };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A file at the top of each engine's profile directory, so a Chromium profile
|
|
36
|
+
* given to a Firefox launcher (or no profile at all) is refused before launch.
|
|
37
|
+
*/
|
|
38
|
+
const PROFILE_MARKER = {
|
|
39
|
+
firefox: "prefs.js",
|
|
40
|
+
chromium: "Local State",
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* What a running browser leaves in its profile to keep a second one out, and
|
|
44
|
+
* caches the copy does not need. Skipping the locks is what lets a session
|
|
45
|
+
* open a profile the person's own browser has open.
|
|
46
|
+
*/
|
|
47
|
+
const PROFILE_SKIP = new Set([
|
|
48
|
+
"lock",
|
|
49
|
+
".parentlock",
|
|
50
|
+
"parent.lock",
|
|
51
|
+
"SingletonLock",
|
|
52
|
+
"SingletonSocket",
|
|
53
|
+
"SingletonCookie",
|
|
54
|
+
"cache2",
|
|
55
|
+
"startupCache",
|
|
56
|
+
"Cache",
|
|
57
|
+
"Code Cache",
|
|
58
|
+
"GPUCache",
|
|
59
|
+
]);
|
|
60
|
+
/**
|
|
61
|
+
* A private copy of the person's profile for one session. The browser writes
|
|
62
|
+
* to its profile (Playwright adds its own prefs), so the original is never
|
|
63
|
+
* launched; what the session changes is thrown away with the copy.
|
|
64
|
+
*/
|
|
65
|
+
function copyProfile(source, engine) {
|
|
66
|
+
const marker = PROFILE_MARKER[engine];
|
|
67
|
+
if (!fs.existsSync(path.join(source, marker))) {
|
|
68
|
+
throw new Error(`${source} has no ${marker}, so it is not a ${engine} profile directory.`);
|
|
69
|
+
}
|
|
70
|
+
// mkdtemp makes the directory 0700: the copy holds the person's cookies.
|
|
71
|
+
const copy = fs.mkdtempSync(path.join(os.tmpdir(), "camoucore-mcp-profile-"));
|
|
72
|
+
fs.cpSync(source, copy, {
|
|
73
|
+
recursive: true,
|
|
74
|
+
filter: (from) => !PROFILE_SKIP.has(path.basename(from)),
|
|
75
|
+
});
|
|
76
|
+
return copy;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The file `name` names inside `dir`, refused if it is anything else: a path
|
|
80
|
+
* out of it, a link out of it, or not a file. A path out of it is refused
|
|
81
|
+
* before it is looked at, so the refusal never says what exists elsewhere.
|
|
82
|
+
*/
|
|
83
|
+
function uploadable(dir, name) {
|
|
84
|
+
const root = fs.realpathSync(dir);
|
|
85
|
+
const outside = (file) => {
|
|
86
|
+
const inside = path.relative(root, file);
|
|
87
|
+
return (!inside ||
|
|
88
|
+
inside === ".." ||
|
|
89
|
+
inside.startsWith(`..${path.sep}`) ||
|
|
90
|
+
path.isAbsolute(inside));
|
|
91
|
+
};
|
|
92
|
+
const refuseOutside = () => new Error(`${name} is outside the uploads directory.`);
|
|
93
|
+
const named = path.resolve(root, name);
|
|
94
|
+
if (outside(named))
|
|
95
|
+
throw refuseOutside();
|
|
96
|
+
let file;
|
|
97
|
+
try {
|
|
98
|
+
file = fs.realpathSync(named);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
throw new Error(`There is no ${name} in the uploads directory.`);
|
|
102
|
+
}
|
|
103
|
+
if (outside(file))
|
|
104
|
+
throw refuseOutside();
|
|
105
|
+
if (!fs.statSync(file).isFile())
|
|
106
|
+
throw new Error(`${name} is not a file.`);
|
|
107
|
+
return file;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The page's accessibility tree, with what any password field holds hidden: a
|
|
111
|
+
* field's value is in the tree, masked or not, and one the person typed during
|
|
112
|
+
* a hand_off, or the browser filled in, is no configured secret to redact.
|
|
113
|
+
*/
|
|
114
|
+
async function treeOf(page, secrets, { ref, depth } = {}) {
|
|
115
|
+
const root = ref ? page.locator(`aria-ref=${ref}`) : page;
|
|
116
|
+
let tree = await root.ariaSnapshot({
|
|
117
|
+
mode: "ai",
|
|
118
|
+
...(depth === undefined ? {} : { depth }),
|
|
119
|
+
});
|
|
120
|
+
for (const frame of page.frames()) {
|
|
121
|
+
for (const field of await frame.locator("input[type=password]").all()) {
|
|
122
|
+
const value = await field.inputValue().catch(() => "");
|
|
123
|
+
// A configured secret keeps its name, from the redaction of every result.
|
|
124
|
+
if (value && !secrets.contains(value)) {
|
|
125
|
+
tree = redactValue(tree, value, "<hidden password>");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return tree;
|
|
130
|
+
}
|
|
131
|
+
export function createServer(launcher, browserType, { solver, secrets = new Secrets({}), allowedDomains = [], profile, uploads, } = {}) {
|
|
132
|
+
const { PRODUCT } = launcher;
|
|
133
|
+
const sessions = new Map();
|
|
134
|
+
const allowed = allowedDomains.map((pattern) => new UrlPattern(pattern));
|
|
135
|
+
const isAllowed = (url) => !allowed.length ||
|
|
136
|
+
url === "about:blank" ||
|
|
137
|
+
allowed.some((pattern) => pattern.matches(url));
|
|
138
|
+
// Every result passes through redaction, so a secret the page shows back
|
|
139
|
+
// (a field's value in the snapshot, an echo in an error) reaches the model
|
|
140
|
+
// as <secret:name>.
|
|
141
|
+
const answer = (value, extra = []) => ({
|
|
142
|
+
content: [
|
|
143
|
+
{ type: "text", text: secrets.redact(JSON.stringify(value, null, 2)) },
|
|
144
|
+
...extra,
|
|
145
|
+
],
|
|
146
|
+
});
|
|
147
|
+
const refuse = (error) => {
|
|
148
|
+
const failure = error;
|
|
149
|
+
const text = JSON.stringify({ error: failure.name, message: failure.message }, null, 2);
|
|
150
|
+
return {
|
|
151
|
+
content: [{ type: "text", text: secrets.redact(text) }],
|
|
152
|
+
isError: true,
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
/** Tracks a tab and keeps its main frame inside the allowed domains. */
|
|
156
|
+
function watch(session, page) {
|
|
157
|
+
session.pages.push(page);
|
|
158
|
+
page.on("close", () => {
|
|
159
|
+
session.pages = session.pages.filter((open) => open !== page);
|
|
160
|
+
if (session.page === page && session.pages.length) {
|
|
161
|
+
session.page = session.pages[session.pages.length - 1];
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
page.on("framenavigated", (frame) => {
|
|
165
|
+
if (frame !== page.mainFrame() || isAllowed(frame.url()))
|
|
166
|
+
return;
|
|
167
|
+
session.blocked = frame.url();
|
|
168
|
+
page.goto("about:blank").catch(() => { });
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/** A tool on an open session: an unknown id and any failure are refusals. */
|
|
172
|
+
function onSession(work) {
|
|
173
|
+
return async (args) => {
|
|
174
|
+
const session = sessions.get(args.session_id);
|
|
175
|
+
if (!session) {
|
|
176
|
+
return refuse(new Error(`No open session ${args.session_id}. session_open starts one.`));
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
// Before and after, since a click's navigation can commit after the
|
|
180
|
+
// click returns: no tool reads or acts on a page outside the domains.
|
|
181
|
+
await keepInside(session);
|
|
182
|
+
const result = await work(session, args);
|
|
183
|
+
await keepInside(session);
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
return refuse(error);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async function keepInside(session) {
|
|
192
|
+
const url = session.page.url();
|
|
193
|
+
const blocked = session.blocked ?? (isAllowed(url) ? undefined : url);
|
|
194
|
+
if (!blocked)
|
|
195
|
+
return;
|
|
196
|
+
session.blocked = undefined;
|
|
197
|
+
if (!isAllowed(url))
|
|
198
|
+
await session.page.goto("about:blank");
|
|
199
|
+
throw new Error(`The page went to ${blocked}, outside the allowed domains, and was sent back to about:blank.`);
|
|
200
|
+
}
|
|
201
|
+
const locate = (session, ref) => session.page.locator(`aria-ref=${ref}`);
|
|
202
|
+
const sessionId = z.string().describe("From session_open.");
|
|
203
|
+
const ref = z
|
|
204
|
+
.string()
|
|
205
|
+
.describe("An element ref, like e12, from the latest snapshot.");
|
|
206
|
+
const server = new McpServer({ name: `${PRODUCT.name}-mcp`, version: VERSION }, {
|
|
207
|
+
instructions: `A ${PRODUCT.title} browser. session_open, then navigate, snapshot to read the page and ` +
|
|
208
|
+
"get refs, and click, type, select, scroll and hover by ref. On a large page, snapshot " +
|
|
209
|
+
"with `find` or `ref` to read only the part you need. Type a password or " +
|
|
210
|
+
"one-time code only with type_secret, by name: you never see the values, and the " +
|
|
211
|
+
"person must never be asked to paste one into the chat. For a secret that is not " +
|
|
212
|
+
"configured, payment details, or anything you should not do yourself, hand_off the " +
|
|
213
|
+
"window to the person. Always session_close when done." +
|
|
214
|
+
(secrets.empty
|
|
215
|
+
? " No secrets are configured."
|
|
216
|
+
: ` Secrets, by the URLs they may be typed on: ${JSON.stringify(secrets.names())}.`) +
|
|
217
|
+
(allowed.length
|
|
218
|
+
? ` Pages may load only: ${allowedDomains.join(", ")}.`
|
|
219
|
+
: "") +
|
|
220
|
+
(uploads
|
|
221
|
+
? ` upload_file attaches files from ${uploads}, by name.`
|
|
222
|
+
: ""),
|
|
223
|
+
});
|
|
224
|
+
server.registerTool("session_open", {
|
|
225
|
+
title: "Open a browser session",
|
|
226
|
+
description: "Launch one browser with a fresh identity. `os` is the identity's OS; left out, the " +
|
|
227
|
+
"launcher draws one. `visible` opens a window a person can see and take over with " +
|
|
228
|
+
"hand_off. `humanize` (default true) moves, types and scrolls like a person; false " +
|
|
229
|
+
"sends each action as Playwright does, which is faster and easier to detect. " +
|
|
230
|
+
"`passthrough` launches with this machine's own fingerprint instead of a drawn one." +
|
|
231
|
+
(profile
|
|
232
|
+
? " `profile` runs on a copy of the person's own browser profile, signed in where " +
|
|
233
|
+
"they are; nothing the session changes is saved back. Use passthrough with it, " +
|
|
234
|
+
"since the sites know that profile with this machine's fingerprint."
|
|
235
|
+
: ""),
|
|
236
|
+
inputSchema: {
|
|
237
|
+
os: z.enum(["windows", "macos", "linux"]).optional(),
|
|
238
|
+
visible: z.boolean().optional(),
|
|
239
|
+
humanize: z.boolean().optional(),
|
|
240
|
+
passthrough: z.boolean().optional(),
|
|
241
|
+
...(profile ? { profile: z.boolean().optional() } : {}),
|
|
242
|
+
},
|
|
243
|
+
annotations: { title: "Open a browser session", openWorldHint: true },
|
|
244
|
+
}, async (args) => {
|
|
245
|
+
const { os: identityOs, visible = false, humanize = true, passthrough = false, } = args;
|
|
246
|
+
let copy;
|
|
247
|
+
let closing = false;
|
|
248
|
+
/**
|
|
249
|
+
* Never throws: it runs from the browser's own events, where a throw
|
|
250
|
+
* would take the server down. A copy left behind holds the person's
|
|
251
|
+
* cookies, so it is named.
|
|
252
|
+
*/
|
|
253
|
+
const removeCopy = async () => {
|
|
254
|
+
const dir = copy;
|
|
255
|
+
if (!dir)
|
|
256
|
+
return;
|
|
257
|
+
copy = undefined;
|
|
258
|
+
await fs.promises
|
|
259
|
+
.rm(dir, {
|
|
260
|
+
recursive: true,
|
|
261
|
+
force: true,
|
|
262
|
+
maxRetries: 5,
|
|
263
|
+
retryDelay: 200,
|
|
264
|
+
})
|
|
265
|
+
.catch((error) => console.error(`camoucore-mcp: could not remove the profile copy ${dir}: ${error.message}`));
|
|
266
|
+
};
|
|
267
|
+
try {
|
|
268
|
+
if (passthrough && !launcher.PASSTHROUGH) {
|
|
269
|
+
throw new Error(`This ${PRODUCT.title} launcher does not take passthrough, so it would launch ` +
|
|
270
|
+
"with an injected fingerprint. A release that exports PASSTHROUGH does.");
|
|
271
|
+
}
|
|
272
|
+
if (passthrough && identityOs !== undefined) {
|
|
273
|
+
throw new Error("passthrough launches with this machine's own OS, so it takes no `os`.");
|
|
274
|
+
}
|
|
275
|
+
if (args.profile) {
|
|
276
|
+
if (!profile)
|
|
277
|
+
throw new Error("No profile is configured.");
|
|
278
|
+
copy = copyProfile(profile, PRODUCT.engine.name);
|
|
279
|
+
}
|
|
280
|
+
const launched = await launcher.NewBrowser(browserType, {
|
|
281
|
+
headless: !visible,
|
|
282
|
+
humanize,
|
|
283
|
+
...(identityOs === undefined ? {} : { os: identityOs }),
|
|
284
|
+
...(passthrough ? { passthrough: true } : {}),
|
|
285
|
+
...(copy ? { persistent_context: true, user_data_dir: copy } : {}),
|
|
286
|
+
});
|
|
287
|
+
// A persistent launch is its profile's context, and opens with a tab.
|
|
288
|
+
const context = copy
|
|
289
|
+
? launched
|
|
290
|
+
: undefined;
|
|
291
|
+
const browser = context ? undefined : launched;
|
|
292
|
+
const page = context
|
|
293
|
+
? (context.pages()[0] ?? (await context.newPage()))
|
|
294
|
+
: await browser.newPage();
|
|
295
|
+
const session = {
|
|
296
|
+
id: randomUUID(),
|
|
297
|
+
// The copy goes once close() returns, when the browser has
|
|
298
|
+
// stopped writing to it.
|
|
299
|
+
close: async () => {
|
|
300
|
+
closing = true;
|
|
301
|
+
try {
|
|
302
|
+
await (context ?? browser).close();
|
|
303
|
+
}
|
|
304
|
+
finally {
|
|
305
|
+
await removeCopy();
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
pages: [],
|
|
309
|
+
page,
|
|
310
|
+
visible,
|
|
311
|
+
};
|
|
312
|
+
watch(session, page);
|
|
313
|
+
// A link or script that opens a tab adds it; switch_tab moves to it.
|
|
314
|
+
page.context().on("page", (opened) => watch(session, opened));
|
|
315
|
+
sessions.set(session.id, session);
|
|
316
|
+
const ended = () => {
|
|
317
|
+
sessions.delete(session.id);
|
|
318
|
+
if (!closing)
|
|
319
|
+
void removeCopy();
|
|
320
|
+
};
|
|
321
|
+
if (context)
|
|
322
|
+
context.on("close", ended);
|
|
323
|
+
else
|
|
324
|
+
browser.on("disconnected", ended);
|
|
325
|
+
return answer({ session_id: session.id });
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
await removeCopy();
|
|
329
|
+
return refuse(error);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
// A web page only: file:, about:, data: and javascript: URLs would hand the
|
|
333
|
+
// agent the machine's files and the browser's own internals.
|
|
334
|
+
const webUrl = z
|
|
335
|
+
.string()
|
|
336
|
+
.url()
|
|
337
|
+
.refine((url) => ["http:", "https:"].includes(new URL(url).protocol), {
|
|
338
|
+
message: "navigate takes an http or https URL",
|
|
339
|
+
});
|
|
340
|
+
server.registerTool("navigate", {
|
|
341
|
+
title: "Navigate",
|
|
342
|
+
description: "Go to an http or https URL and wait for it to load.",
|
|
343
|
+
inputSchema: { session_id: sessionId, url: webUrl },
|
|
344
|
+
annotations: { title: "Navigate", openWorldHint: true },
|
|
345
|
+
}, onSession(async (session, { url }) => {
|
|
346
|
+
if (!isAllowed(url)) {
|
|
347
|
+
throw new Error(`${url} is outside the allowed domains: ${allowedDomains.join(", ")}.`);
|
|
348
|
+
}
|
|
349
|
+
const response = await session.page.goto(url);
|
|
350
|
+
return answer({
|
|
351
|
+
url: session.page.url(),
|
|
352
|
+
title: await session.page.title(),
|
|
353
|
+
status: response?.status() ?? null,
|
|
354
|
+
});
|
|
355
|
+
}));
|
|
356
|
+
server.registerTool("snapshot", {
|
|
357
|
+
title: "Snapshot",
|
|
358
|
+
description: "The page as an accessibility tree, with a ref on every element: read this rather " +
|
|
359
|
+
"than a screenshot. Refs last until the next snapshot or navigation. Read only what " +
|
|
360
|
+
"you need: `ref` gives one element's subtree, `depth` stops that many levels down, " +
|
|
361
|
+
"and `find` keeps only the lines containing its text (ignoring case), under their " +
|
|
362
|
+
"named ancestors.",
|
|
363
|
+
inputSchema: {
|
|
364
|
+
session_id: sessionId,
|
|
365
|
+
ref: z
|
|
366
|
+
.string()
|
|
367
|
+
.optional()
|
|
368
|
+
.describe("Only this element and what is inside it."),
|
|
369
|
+
depth: z.number().int().min(1).optional(),
|
|
370
|
+
find: z.string().min(1).optional(),
|
|
371
|
+
},
|
|
372
|
+
annotations: {
|
|
373
|
+
title: "Snapshot",
|
|
374
|
+
readOnlyHint: true,
|
|
375
|
+
openWorldHint: true,
|
|
376
|
+
},
|
|
377
|
+
}, onSession(async (session, args) => {
|
|
378
|
+
const url = session.page.url();
|
|
379
|
+
// Searched once redacted, so `find` cannot test for a secret's value.
|
|
380
|
+
let snapshot = secrets.redact(await treeOf(session.page, secrets, args));
|
|
381
|
+
if (args.find !== undefined) {
|
|
382
|
+
snapshot =
|
|
383
|
+
findInTree(snapshot, args.find) ||
|
|
384
|
+
`Nothing here contains ${JSON.stringify(args.find)}.`;
|
|
385
|
+
}
|
|
386
|
+
return answer({
|
|
387
|
+
url,
|
|
388
|
+
title: await session.page.title(),
|
|
389
|
+
...(session.pages.length > 1 ? { tabs: session.pages.length } : {}),
|
|
390
|
+
...(secrets.empty ? {} : { secrets_here: secrets.namesFor(url) }),
|
|
391
|
+
snapshot,
|
|
392
|
+
});
|
|
393
|
+
}));
|
|
394
|
+
server.registerTool("click", {
|
|
395
|
+
title: "Click",
|
|
396
|
+
description: "Click an element by ref.",
|
|
397
|
+
inputSchema: { session_id: sessionId, ref },
|
|
398
|
+
annotations: { title: "Click", openWorldHint: true },
|
|
399
|
+
}, onSession(async (session, args) => {
|
|
400
|
+
await locate(session, args.ref).click();
|
|
401
|
+
return answer({ clicked: args.ref, url: session.page.url() });
|
|
402
|
+
}));
|
|
403
|
+
server.registerTool("type", {
|
|
404
|
+
title: "Type",
|
|
405
|
+
description: "Type into an element by ref, key by key. `submit` presses Enter afterwards.",
|
|
406
|
+
inputSchema: {
|
|
407
|
+
session_id: sessionId,
|
|
408
|
+
ref,
|
|
409
|
+
text: z.string(),
|
|
410
|
+
submit: z.boolean().optional(),
|
|
411
|
+
},
|
|
412
|
+
annotations: { title: "Type", openWorldHint: true },
|
|
413
|
+
}, onSession(async (session, args) => {
|
|
414
|
+
if (/<secret[:>]/.test(args.text)) {
|
|
415
|
+
throw new Error("type sends its text as written. type_secret types a secret by name.");
|
|
416
|
+
}
|
|
417
|
+
const target = locate(session, args.ref);
|
|
418
|
+
await target.click();
|
|
419
|
+
await target.pressSequentially(args.text);
|
|
420
|
+
if (args.submit)
|
|
421
|
+
await target.press("Enter");
|
|
422
|
+
return answer({ typed: args.ref, url: session.page.url() });
|
|
423
|
+
}));
|
|
424
|
+
if (!secrets.empty) {
|
|
425
|
+
server.registerTool("type_secret", {
|
|
426
|
+
title: "Type a secret",
|
|
427
|
+
description: "Type a configured secret into an element by ref, key by key, without seeing it. " +
|
|
428
|
+
"Only on a field whose frame is at a URL the secret is configured for; a one-time " +
|
|
429
|
+
"code secret types the current code. `submit` presses Enter afterwards.",
|
|
430
|
+
inputSchema: {
|
|
431
|
+
session_id: sessionId,
|
|
432
|
+
ref,
|
|
433
|
+
name: z.string().describe("The secret's name, as configured."),
|
|
434
|
+
submit: z.boolean().optional(),
|
|
435
|
+
},
|
|
436
|
+
annotations: { title: "Type a secret", openWorldHint: true },
|
|
437
|
+
}, onSession(async (session, args) => {
|
|
438
|
+
const target = locate(session, args.ref);
|
|
439
|
+
// The frame the keys go to, which for a field in an iframe is
|
|
440
|
+
// not the page's URL.
|
|
441
|
+
const handle = await target.elementHandle();
|
|
442
|
+
const frame = await handle.ownerFrame();
|
|
443
|
+
await handle.dispose();
|
|
444
|
+
if (!frame)
|
|
445
|
+
throw new Error(`${args.ref} is no longer in the page.`);
|
|
446
|
+
const value = secrets.valueFor(args.name, frame.url());
|
|
447
|
+
await target.click();
|
|
448
|
+
await target.pressSequentially(value);
|
|
449
|
+
if (args.submit)
|
|
450
|
+
await target.press("Enter");
|
|
451
|
+
return answer({ typed_secret: args.name, ref: args.ref });
|
|
452
|
+
}));
|
|
453
|
+
}
|
|
454
|
+
server.registerTool("select_option", {
|
|
455
|
+
title: "Select an option",
|
|
456
|
+
description: "Choose options in a <select> by ref, by their label or value. Several for a multiple select.",
|
|
457
|
+
inputSchema: {
|
|
458
|
+
session_id: sessionId,
|
|
459
|
+
ref,
|
|
460
|
+
options: z.array(z.string()).min(1),
|
|
461
|
+
},
|
|
462
|
+
annotations: { title: "Select an option", openWorldHint: true },
|
|
463
|
+
}, onSession(async (session, args) => {
|
|
464
|
+
const selected = await locate(session, args.ref).selectOption(args.options);
|
|
465
|
+
return answer({ selected });
|
|
466
|
+
}));
|
|
467
|
+
server.registerTool("press_key", {
|
|
468
|
+
title: "Press a key",
|
|
469
|
+
description: "Press a key or chord on the focused element, such as Enter, Escape, ArrowDown, Tab or Control+a.",
|
|
470
|
+
inputSchema: { session_id: sessionId, key: z.string() },
|
|
471
|
+
annotations: { title: "Press a key", openWorldHint: true },
|
|
472
|
+
}, onSession(async (session, args) => {
|
|
473
|
+
await session.page.keyboard.press(args.key);
|
|
474
|
+
return answer({ pressed: args.key, url: session.page.url() });
|
|
475
|
+
}));
|
|
476
|
+
server.registerTool("go_back", {
|
|
477
|
+
title: "Go back",
|
|
478
|
+
description: "Go back one page in the tab's history.",
|
|
479
|
+
inputSchema: { session_id: sessionId },
|
|
480
|
+
annotations: { title: "Go back", openWorldHint: true },
|
|
481
|
+
}, onSession(async (session) => {
|
|
482
|
+
await session.page.goBack();
|
|
483
|
+
return answer({
|
|
484
|
+
url: session.page.url(),
|
|
485
|
+
title: await session.page.title(),
|
|
486
|
+
});
|
|
487
|
+
}));
|
|
488
|
+
server.registerTool("tabs", {
|
|
489
|
+
title: "List tabs",
|
|
490
|
+
description: "The session's open tabs, by index, and which one the tools act on.",
|
|
491
|
+
inputSchema: { session_id: sessionId },
|
|
492
|
+
annotations: { title: "List tabs", readOnlyHint: true },
|
|
493
|
+
}, onSession(async (session) => answer({
|
|
494
|
+
tabs: await Promise.all(session.pages.map(async (page, index) => ({
|
|
495
|
+
index,
|
|
496
|
+
url: page.url(),
|
|
497
|
+
title: await page.title(),
|
|
498
|
+
current: page === session.page,
|
|
499
|
+
}))),
|
|
500
|
+
})));
|
|
501
|
+
server.registerTool("switch_tab", {
|
|
502
|
+
title: "Switch tab",
|
|
503
|
+
description: "Act on another tab, by its index from tabs; `close` closes the current one first.",
|
|
504
|
+
inputSchema: {
|
|
505
|
+
session_id: sessionId,
|
|
506
|
+
index: z.number().int().min(0),
|
|
507
|
+
close: z.boolean().optional(),
|
|
508
|
+
},
|
|
509
|
+
annotations: { title: "Switch tab" },
|
|
510
|
+
}, onSession(async (session, args) => {
|
|
511
|
+
const target = session.pages[args.index];
|
|
512
|
+
if (!target) {
|
|
513
|
+
throw new Error(`There is no tab ${args.index}; the session has ${session.pages.length}.`);
|
|
514
|
+
}
|
|
515
|
+
if (args.close && target !== session.page)
|
|
516
|
+
await session.page.close();
|
|
517
|
+
session.page = target;
|
|
518
|
+
if (session.visible)
|
|
519
|
+
await target.bringToFront();
|
|
520
|
+
return answer({ url: target.url(), title: await target.title() });
|
|
521
|
+
}));
|
|
522
|
+
server.registerTool("scroll", {
|
|
523
|
+
title: "Scroll",
|
|
524
|
+
description: "Scroll an element into view by ref; with `delta_y`, then wheel that many pixels " +
|
|
525
|
+
"from it (positive is down).",
|
|
526
|
+
inputSchema: {
|
|
527
|
+
session_id: sessionId,
|
|
528
|
+
ref,
|
|
529
|
+
delta_y: z.number().optional(),
|
|
530
|
+
},
|
|
531
|
+
annotations: { title: "Scroll", openWorldHint: true },
|
|
532
|
+
}, onSession(async (session, args) => {
|
|
533
|
+
const target = locate(session, args.ref);
|
|
534
|
+
await target.scrollIntoViewIfNeeded();
|
|
535
|
+
if (args.delta_y !== undefined) {
|
|
536
|
+
await target.hover();
|
|
537
|
+
await session.page.mouse.wheel(0, args.delta_y);
|
|
538
|
+
}
|
|
539
|
+
return answer({ scrolled: args.ref });
|
|
540
|
+
}));
|
|
541
|
+
server.registerTool("hover", {
|
|
542
|
+
title: "Hover",
|
|
543
|
+
description: "Move the pointer over an element by ref.",
|
|
544
|
+
inputSchema: { session_id: sessionId, ref },
|
|
545
|
+
annotations: { title: "Hover", openWorldHint: true },
|
|
546
|
+
}, onSession(async (session, args) => {
|
|
547
|
+
await locate(session, args.ref).hover();
|
|
548
|
+
return answer({ hovered: args.ref });
|
|
549
|
+
}));
|
|
550
|
+
server.registerTool("screenshot", {
|
|
551
|
+
title: "Screenshot",
|
|
552
|
+
description: "A PNG of the viewport. Use snapshot unless the pixels themselves matter.",
|
|
553
|
+
inputSchema: { session_id: sessionId },
|
|
554
|
+
annotations: {
|
|
555
|
+
title: "Screenshot",
|
|
556
|
+
readOnlyHint: true,
|
|
557
|
+
openWorldHint: true,
|
|
558
|
+
},
|
|
559
|
+
}, onSession(async (session) => {
|
|
560
|
+
// Pixels cannot be redacted, so no screenshot while the page holds a
|
|
561
|
+
// secret: the tree has every field's value, masked ones included.
|
|
562
|
+
if (!secrets.empty &&
|
|
563
|
+
secrets.contains(await session.page.ariaSnapshot({ mode: "ai" }))) {
|
|
564
|
+
throw new Error("The page holds a secret, so it cannot be shown as pixels. snapshot shows it redacted.");
|
|
565
|
+
}
|
|
566
|
+
const image = await session.page.screenshot({ type: "png" });
|
|
567
|
+
return answer({ url: session.page.url() }, [
|
|
568
|
+
{
|
|
569
|
+
type: "image",
|
|
570
|
+
data: image.toString("base64"),
|
|
571
|
+
mimeType: "image/png",
|
|
572
|
+
},
|
|
573
|
+
]);
|
|
574
|
+
}));
|
|
575
|
+
if (uploads) {
|
|
576
|
+
server.registerTool("upload_file", {
|
|
577
|
+
title: "Upload files",
|
|
578
|
+
description: "Attach files from the uploads directory, by name, to a file input by ref, or to " +
|
|
579
|
+
"the button or field that opens the page's file picker. Several for a multiple input.",
|
|
580
|
+
inputSchema: {
|
|
581
|
+
session_id: sessionId,
|
|
582
|
+
ref,
|
|
583
|
+
files: z
|
|
584
|
+
.array(z.string())
|
|
585
|
+
.min(1)
|
|
586
|
+
.describe("Names inside the uploads directory."),
|
|
587
|
+
},
|
|
588
|
+
annotations: { title: "Upload files", openWorldHint: true },
|
|
589
|
+
}, onSession(async (session, args) => {
|
|
590
|
+
const paths = args.files.map((name) => uploadable(uploads, name));
|
|
591
|
+
const target = locate(session, args.ref);
|
|
592
|
+
const isInput = await target.evaluate((element) => element instanceof HTMLInputElement && element.type === "file");
|
|
593
|
+
if (isInput) {
|
|
594
|
+
await target.setInputFiles(paths);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
// Listening takes the picker over, so no dialog opens.
|
|
598
|
+
const [chooser] = await Promise.all([
|
|
599
|
+
session.page.waitForEvent("filechooser", { timeout: 10_000 }),
|
|
600
|
+
target.click(),
|
|
601
|
+
]);
|
|
602
|
+
await chooser.setFiles(paths);
|
|
603
|
+
}
|
|
604
|
+
return answer({ uploaded: args.files, ref: args.ref });
|
|
605
|
+
}));
|
|
606
|
+
}
|
|
607
|
+
server.registerTool("hand_off", {
|
|
608
|
+
title: "Hand the window to the person",
|
|
609
|
+
description: "Bring a visible session's window to the front for the person to act in: a secret " +
|
|
610
|
+
"that is not configured, a payment, a captcha. Then stop, tell them `reason`, and wait until " +
|
|
611
|
+
"they say they are done; snapshot again before acting, since the page has changed.",
|
|
612
|
+
inputSchema: {
|
|
613
|
+
session_id: sessionId,
|
|
614
|
+
reason: z.string().describe("What the person needs to do."),
|
|
615
|
+
},
|
|
616
|
+
annotations: { title: "Hand the window to the person" },
|
|
617
|
+
}, onSession(async (session, args) => {
|
|
618
|
+
if (!session.visible) {
|
|
619
|
+
throw new Error("This session has no window. session_open with visible: true opens one a person can use.");
|
|
620
|
+
}
|
|
621
|
+
await session.page.bringToFront();
|
|
622
|
+
return answer({ handed_off: true, reason: args.reason });
|
|
623
|
+
}));
|
|
624
|
+
if (solver) {
|
|
625
|
+
server.registerTool("solve_captcha", {
|
|
626
|
+
title: "Solve captcha",
|
|
627
|
+
description: "Find and solve the captcha on the page with CaptchaKraken, on the key its " +
|
|
628
|
+
"package is configured with.",
|
|
629
|
+
inputSchema: { session_id: sessionId },
|
|
630
|
+
annotations: { title: "Solve captcha", openWorldHint: true },
|
|
631
|
+
}, onSession(async (session) => {
|
|
632
|
+
// The solver returns nothing when it finds no captcha on the page.
|
|
633
|
+
const result = await solver.solve(session.page);
|
|
634
|
+
return answer(result
|
|
635
|
+
? { found: true, solved: Boolean(result.isSolved) }
|
|
636
|
+
: { found: false, solved: false });
|
|
637
|
+
}));
|
|
638
|
+
}
|
|
639
|
+
server.registerTool("session_close", {
|
|
640
|
+
title: "Close a browser session",
|
|
641
|
+
description: "Close the browser.",
|
|
642
|
+
inputSchema: { session_id: sessionId },
|
|
643
|
+
annotations: {
|
|
644
|
+
title: "Close a browser session",
|
|
645
|
+
destructiveHint: true,
|
|
646
|
+
openWorldHint: true,
|
|
647
|
+
},
|
|
648
|
+
}, onSession(async (session) => {
|
|
649
|
+
sessions.delete(session.id);
|
|
650
|
+
await session.close();
|
|
651
|
+
return answer({ closed: true });
|
|
652
|
+
}));
|
|
653
|
+
return {
|
|
654
|
+
server,
|
|
655
|
+
closeAll: async () => {
|
|
656
|
+
await Promise.allSettled([...sessions.values()].map((session) => session.close()));
|
|
657
|
+
},
|
|
658
|
+
};
|
|
659
|
+
}
|
package/dist/tree.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrowing an accessibility tree to what the agent asked for, so a large page
|
|
3
|
+
* is not read whole for every question about it.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The lines of `tree` that contain `needle`, ignoring case, each under its
|
|
7
|
+
* ancestors so it is clear where it sits. Ancestors that are only containers
|
|
8
|
+
* are left out, since they say nothing and a deep page has many of them.
|
|
9
|
+
*/
|
|
10
|
+
export declare function findInTree(tree: string, needle: string): string;
|
package/dist/tree.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrowing an accessibility tree to what the agent asked for, so a large page
|
|
3
|
+
* is not read whole for every question about it.
|
|
4
|
+
*/
|
|
5
|
+
/** Containers that only nest other elements: no name, no value, no text. */
|
|
6
|
+
const BARE = /^- (generic|group|none|presentation|list|listitem|rowgroup)( \[ref=\w+\])?:?$/;
|
|
7
|
+
const indentOf = (line) => line.length - line.trimStart().length;
|
|
8
|
+
/**
|
|
9
|
+
* The lines of `tree` that contain `needle`, ignoring case, each under its
|
|
10
|
+
* ancestors so it is clear where it sits. Ancestors that are only containers
|
|
11
|
+
* are left out, since they say nothing and a deep page has many of them.
|
|
12
|
+
*/
|
|
13
|
+
export function findInTree(tree, needle) {
|
|
14
|
+
const lines = tree.split("\n");
|
|
15
|
+
const wanted = needle.toLowerCase();
|
|
16
|
+
const keep = new Set();
|
|
17
|
+
lines.forEach((line, index) => {
|
|
18
|
+
if (!line.toLowerCase().includes(wanted))
|
|
19
|
+
return;
|
|
20
|
+
keep.add(index);
|
|
21
|
+
let level = indentOf(line);
|
|
22
|
+
for (let above = index - 1; above >= 0 && level > 0; above--) {
|
|
23
|
+
const candidate = lines[above];
|
|
24
|
+
const depth = indentOf(candidate);
|
|
25
|
+
if (depth >= level || !candidate.trim())
|
|
26
|
+
continue;
|
|
27
|
+
level = depth;
|
|
28
|
+
if (!BARE.test(candidate.trim()))
|
|
29
|
+
keep.add(above);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return [...keep]
|
|
33
|
+
.sort((a, b) => a - b)
|
|
34
|
+
.map((index) => lines[index])
|
|
35
|
+
.join("\n");
|
|
36
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "camoucore-mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "An MCP server that drives a Camoufox or Camouwolf browser: sessions, accessibility snapshots with refs, input by ref, and handing the window to a person",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/server.js",
|
|
7
|
+
"types": "dist/server.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"camoucore-mcp": "dist/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rimraf dist && tsc",
|
|
13
|
+
"test": "pnpm build && vitest run",
|
|
14
|
+
"check": "biome check",
|
|
15
|
+
"typecheck": "tsc --noEmit",
|
|
16
|
+
"prepare": "pnpm build"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"author": "JWriter20",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/CamouBrowser/camoucore.git",
|
|
26
|
+
"directory": "mcp"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.15"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@modelcontextprotocol/sdk": "1.30.0",
|
|
33
|
+
"zod": "3.25.76"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"captchakraken": "*",
|
|
37
|
+
"playwright-core": "<1.63"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"captchakraken": {
|
|
41
|
+
"optional": true
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"devEngines": {
|
|
45
|
+
"packageManager": {
|
|
46
|
+
"name": "pnpm",
|
|
47
|
+
"version": "10.33.4",
|
|
48
|
+
"onFail": "warn"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@biomejs/biome": "2.4.10",
|
|
53
|
+
"@types/node": "^24.19.2",
|
|
54
|
+
"camouwolf": "0.1.0",
|
|
55
|
+
"playwright-core": "1.62.0",
|
|
56
|
+
"rimraf": "^6.1.4",
|
|
57
|
+
"typescript": "^5.9.3",
|
|
58
|
+
"vite": "8.3.8",
|
|
59
|
+
"vitest": "^4.1.12"
|
|
60
|
+
}
|
|
61
|
+
}
|