@gaiventures/pilotfish-mcp-managed 0.1.30 → 0.1.32
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 +42 -13
- package/cli.js +229 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -93,9 +93,24 @@ In `.vscode/mcp.json`:
|
|
|
93
93
|
}
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
+
### Sign in a terminal (no key needed)
|
|
97
|
+
|
|
98
|
+
Clients that start a local server (Codex CLI, any `command`/`args` client), on a laptop or over SSH, can use your own
|
|
99
|
+
sign-in instead of a key. Run this once on that machine:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
npx -y @gaiventures/pilotfish-mcp-managed login
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
It prints a link and a code such as `BCDF-GHJK`. Open the link on any device (your phone works), check that the page
|
|
106
|
+
shows the same code, sign in and choose **Allow**. The relay then keeps the sign-in in `~/.pilotfish/mcp-managed.json`
|
|
107
|
+
(readable only by you) and renews it by itself, so the client configs below need no `env` at all. It acts as you, in
|
|
108
|
+
your Personal project, like signing in from Claude Code. `logout` signs that machine out (the sign-in is revoked at
|
|
109
|
+
pilotfish too). If `PILOTFISH_KEY` is set, the key is used instead.
|
|
110
|
+
|
|
96
111
|
### Codex CLI
|
|
97
112
|
|
|
98
|
-
In `~/.codex/config.toml`, through this package:
|
|
113
|
+
In `~/.codex/config.toml`, through this package (leave out `env` once you've run `login`):
|
|
99
114
|
|
|
100
115
|
```toml
|
|
101
116
|
[mcp_servers.pilotfish]
|
|
@@ -125,9 +140,9 @@ The relay needs Node.js 22 or newer. It only forwards messages to the endpoint w
|
|
|
125
140
|
pilotfish's cloud. On native Windows, if your client cannot start `npx`, use `"command": "cmd"` with
|
|
126
141
|
`"args": ["/c", "npx", "-y", "@gaiventures/pilotfish-mcp-managed"]`.
|
|
127
142
|
|
|
128
|
-
### Check a key
|
|
143
|
+
### Check a key or a sign-in
|
|
129
144
|
|
|
130
|
-
Prints the endpoint and the tools, or why the key was refused:
|
|
145
|
+
Prints the endpoint and the tools, or why the key or sign-in was refused (with no key set, it checks the sign-in):
|
|
131
146
|
|
|
132
147
|
```sh
|
|
133
148
|
PILOTFISH_KEY=pf_YOUR_KEY npx -y @gaiventures/pilotfish-mcp-managed --check
|
|
@@ -151,7 +166,7 @@ $env:PILOTFISH_KEY = "pf_YOUR_KEY"; npx -y @gaiventures/pilotfish-mcp-managed --
|
|
|
151
166
|
| Saved logins, site profiles, saved tasks | Yours (in Personal) | No one's: each end user's logins belong to an identity |
|
|
152
167
|
| Tasks it sees | Your own; an admin can also open any of the organization's tasks by id | Only the tasks started with that key |
|
|
153
168
|
| Paid from | The organization's credits, within your allowance if you have one | The organization's credits, within the key's allowance if it has one |
|
|
154
|
-
| Who sets it up | You, from your client | The organization's admins, under **Org settings → API keys** |
|
|
169
|
+
| Who sets it up | You, from your client (or `login` once, for a client that starts this package) | The organization's admins, under **Org settings → API keys** |
|
|
155
170
|
|
|
156
171
|
**About keys.** Admins create, show again, copy, rotate and revoke keys under **Org settings → API keys**
|
|
157
172
|
(`https://pilotfish.gai.ventures/org?view=keys`). Each key belongs to one shared project, picked when it is made
|
|
@@ -165,7 +180,8 @@ make one in the app's **Org settings → API keys** and pick the project **Perso
|
|
|
165
180
|
Personal project, with your own saved logins, and only you can see or manage it. Admins (the primary admin included)
|
|
166
181
|
can always do this; a member can once an admin turns on **Allow personal API keys** on the member's page (turning it
|
|
167
182
|
off revokes their keys at once), and then finds their keys on their Org settings page. Admins see each person's key
|
|
168
|
-
usage, never the keys. Most people need no key at all: Claude Code, the Claude app, Cursor and VS Code sign in
|
|
183
|
+
usage, never the keys. Most people need no key at all: Claude Code, the Claude app, Cursor and VS Code sign in, and
|
|
184
|
+
a client that starts this package signs in once with `login` (see **Sign in a terminal**).
|
|
169
185
|
|
|
170
186
|
## Projects
|
|
171
187
|
|
|
@@ -173,9 +189,11 @@ Every task runs in a project.
|
|
|
173
189
|
|
|
174
190
|
- **Personal** is your own: your saved logins (several accounts a site, one of them the default), saved and scheduled
|
|
175
191
|
tasks, connected inbox and site settings. It is where you are when you sign in, in the app or from an AI tool.
|
|
176
|
-
- **
|
|
177
|
-
users.
|
|
178
|
-
task with no identity gets a fresh browser of its own, and nothing of it is kept for other sessions.
|
|
192
|
+
- **Default** is the shared project every organization starts with, for product companies running tasks for their
|
|
193
|
+
own end users. It holds no one's personal logins and no saved or scheduled tasks: each end user is an **identity**
|
|
194
|
+
there. A task with no identity gets a fresh browser of its own, and nothing of it is kept for other sessions.
|
|
195
|
+
- **Projects of your own** (made by your organization's admins) are shared projects like Default, one per further
|
|
196
|
+
product or customer, so each keeps its own identities, API keys and spend.
|
|
179
197
|
|
|
180
198
|
Switch in the app's project switcher, or with `switch_project` (no argument lists your projects). Signed in from your
|
|
181
199
|
client (OAuth), the MCP remembers your choice for your next connections. With a personal API key the switch lasts for
|
|
@@ -198,6 +216,10 @@ OS, which sites it has logins for (usernames masked), its credential ids, and wh
|
|
|
198
216
|
- **Run a task as one** with `browser_task {identity: "idn_…"}`. A `follow_up` keeps the session's identity. Each
|
|
199
217
|
identity runs one task at a time.
|
|
200
218
|
- **Delete** wipes its logins, sessions and device for good. Its tasks and their costs stay, for your billing.
|
|
219
|
+
- **One place and one machine.** All of an identity's sites see the same residential IP, from the same city, and the
|
|
220
|
+
same device, as one person's browsing would. A residential provider holds an IP for a day at most, so pilotfish
|
|
221
|
+
renews it between tasks, never during one: a task that starts near the end of the day gets the new IP from its
|
|
222
|
+
first page, in the same city, on the same device. A sign-in made on the old IP stays signed in.
|
|
201
223
|
|
|
202
224
|
### Getting a user's logins in, without an AI seeing them
|
|
203
225
|
|
|
@@ -205,11 +227,15 @@ Each saved login gets a credential id (`cred_…`). A password never passes thro
|
|
|
205
227
|
|
|
206
228
|
1. **A login link.** `identities {action: "login_link", id, sites: ["linkedin.com"]}` (or `POST /identities/:id/link`)
|
|
207
229
|
returns a one-time page, valid for 30 minutes (over REST, `ttlMinutes` sets 5 to 1440). Send it to your end user; they type their own login
|
|
208
|
-
there, and it is saved sealed to their identity.
|
|
230
|
+
there, and it is saved sealed to their identity. Add `webhook_url` (REST: `webhookUrl`, https) to be told as each
|
|
231
|
+
site's login is saved: an `identity.login_saved` event, signed like job webhooks, with the identity, your
|
|
232
|
+
`externalUserId`, the site, the account, the new `credential` id, `linkDone` and the sites `remaining`. Never the
|
|
233
|
+
username or password. Without one, `identities get` shows when it is saved.
|
|
209
234
|
2. **Your own server.** `POST /identities/:id/credentials {site, username, password?, totp?, account?, default?}`
|
|
210
235
|
with your API key, from your backend. The MCP refuses this route.
|
|
211
236
|
3. **Sign-ins during a task.** With `save_logins: true` on the identity, a sign-in typed during one of its tasks is
|
|
212
|
-
kept once it works. Off by default; the signed-in session is kept either way.
|
|
237
|
+
kept once it works. Off by default; the signed-in session is kept either way. The task's own webhook gets the same
|
|
238
|
+
`identity.login_saved` event (`via: "task"`).
|
|
213
239
|
|
|
214
240
|
Use them in a task with `credentials: ["cred_…"]`, or name the account per site with
|
|
215
241
|
`accounts: {"linkedin.com": "work"}`.
|
|
@@ -346,7 +372,7 @@ the monthly limit and spending by category and by model.
|
|
|
346
372
|
| `manage_login` `{action, site, account?}` | Your Personal logins. `add`: a link to the app where you type the login yourself (never in the chat). `set_default`: the account tasks use on a site with several when they don't say. `delete`: forget one. `save_offered` / `discard_offered`: a sign-in a run offered for saving. |
|
|
347
373
|
| `projects` `{action, id?, name?, days?}` | Admins: your organization's shared projects. `list` (each with its API keys, identities and this month's spend), `create`, `rename`, `delete` (wipes its identities, revokes its keys; its tasks' cost stays), `usage` (one project's spend by kind and per key over `days`), `keys` (its keys and what each spent). New API keys are made in the app (Org settings → API keys): a key's value never passes through a chat. |
|
|
348
374
|
| `switch_project` `{project?}` | Which project your tasks run in: `personal`, `default` or a project's id or name. No argument lists them. Signed in (OAuth), remembered for your next connections; with a personal API key, for that connection only. An organization API key stays in its own. |
|
|
349
|
-
| `identities` `{action, id?, external_user_id?, label?, query?, country?, device_os?, email?, save_logins?, sites?, account?, credential?}` | Your end users in a shared project. `list` (by `external_user_id` or `query`), `create`, `get` (logins by credential id, masked; signed-in sites; cost so far), `update`, `delete` (wipes logins, sessions and device; costs stay), `login_link` (`sites`: a one-time page for the user to type their own login), `set_default` and `delete_login` (by `credential`). |
|
|
375
|
+
| `identities` `{action, id?, external_user_id?, label?, query?, country?, device_os?, email?, save_logins?, sites?, account?, credential?, webhook_url?}` | Your end users in a shared project. `list` (by `external_user_id` or `query`), `create`, `get` (logins by credential id, masked; signed-in sites; cost so far), `update`, `delete` (wipes logins, sessions and device; costs stay), `login_link` (`sites`: a one-time page for the user to type their own login; `webhook_url` to be told when it is saved), `set_default` and `delete_login` (by `credential`). |
|
|
350
376
|
| `get_settings` / `update_site_settings` `{site, residential?, stealth?, solve?}` | Your sites and their settings, and change one site's: residential IP (`auto`, `always` or `never`), stealth browsing, CAPTCHA solving. A change applies to every later task on that site. |
|
|
351
377
|
| `get_usage` `{days?}` | Your organization's credits as your role sees them (an admin: its balance and auto reload; a member: whether it is fine, low, out or at its limit), what is left of your allowance (or the key's), this month's spending against the organization's monthly limit, and the last `days` (default 30) of spending by category (model, residential IP, browser, CAPTCHA) and by model. |
|
|
352
378
|
|
|
@@ -405,13 +431,16 @@ Only for this package (the relay). Clients that connect to the URL need none.
|
|
|
405
431
|
|
|
406
432
|
| Variable | Default | Meaning |
|
|
407
433
|
| --- | --- | --- |
|
|
408
|
-
| `PILOTFISH_KEY` | | An API key (`pf_…`): one of your organization's, or a personal key (admins, and members an admin allowed).
|
|
434
|
+
| `PILOTFISH_KEY` | | An API key (`pf_…`): one of your organization's, or a personal key (admins, and members an admin allowed). Leave it out after `login` to use your own sign-in. |
|
|
409
435
|
| `PILOTFISH_URL` | `https://pilotfish-be.gai.ventures` | The pilotfish API, or its `/mcp` URL. |
|
|
436
|
+
| `PILOTFISH_TOKEN_FILE` | `~/.pilotfish/mcp-managed.json` | Where `login` keeps this machine's sign-in. |
|
|
410
437
|
|
|
411
438
|
## Troubleshooting
|
|
412
439
|
|
|
413
440
|
- **401 Unauthorized:** the key was revoked or rotated, or your organization was deleted. Ask an admin for a current
|
|
414
|
-
key (**Org settings → API keys**), or sign in from your client instead.
|
|
441
|
+
key (**Org settings → API keys**), or sign in from your client instead. Signed in with `login`: run `login` again
|
|
442
|
+
(a sign-in unused for 30 days ends).
|
|
443
|
+
- **Not signed in:** run `npx -y @gaiventures/pilotfish-mcp-managed login` on that machine, or set `PILOTFISH_KEY`.
|
|
415
444
|
- **Codex stops the call after a minute:** keep `tool_timeout_sec = 180` as above; the task itself carries on.
|
|
416
445
|
- **A call timed out or was cancelled:** the task carries on. `get_job {id}` gets its result (`list_jobs` lists the id);
|
|
417
446
|
`cancel_job` stops it.
|
package/cli.js
CHANGED
|
@@ -4,13 +4,18 @@
|
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
6
|
|
|
7
|
+
// packages/mcp-managed/dist/auth.js
|
|
8
|
+
import { chmod, mkdir, open, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
|
|
7
12
|
// packages/mcp-managed/dist/bridge.js
|
|
8
13
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
9
14
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
10
15
|
import { isJSONRPCRequest, isJSONRPCResultResponse } from "@modelcontextprotocol/sdk/types.js";
|
|
11
16
|
|
|
12
17
|
// packages/mcp-managed/dist/version.js
|
|
13
|
-
var VERSION = true ? "0.1.
|
|
18
|
+
var VERSION = true ? "0.1.32" : "0.0.0-dev";
|
|
14
19
|
|
|
15
20
|
// packages/mcp-managed/dist/bridge.js
|
|
16
21
|
var DEFAULT_URL = "https://pilotfish-be.gai.ventures";
|
|
@@ -29,12 +34,18 @@ function checkKey(key) {
|
|
|
29
34
|
return k;
|
|
30
35
|
}
|
|
31
36
|
function remoteTransport(o) {
|
|
37
|
+
if (o.token)
|
|
38
|
+
return new StreamableHTTPClientTransport(endpointOf(o.url), { fetch: o.token.fetch() });
|
|
39
|
+
if (!o.key)
|
|
40
|
+
throw new Error("No API key and no sign-in: set PILOTFISH_KEY, or run: npx @gaiventures/pilotfish-mcp-managed login");
|
|
32
41
|
return new StreamableHTTPClientTransport(endpointOf(o.url), { requestInit: { headers: { authorization: `Bearer ${o.key}` } } });
|
|
33
42
|
}
|
|
34
43
|
function explain(e) {
|
|
35
44
|
const m = e instanceof Error ? e.message : String(e);
|
|
45
|
+
if (/ended this sign-in|Run: npx/.test(m))
|
|
46
|
+
return m;
|
|
36
47
|
if (/\b401\b|unauthori[sz]ed/i.test(m))
|
|
37
|
-
return "pilotfish refused the API key (revoked, rotated, mistyped, or its organization was deleted).
|
|
48
|
+
return "pilotfish refused the API key or sign-in (revoked, rotated, mistyped, or its organization was deleted). With a key: ask an admin of your organization for a current one (Org settings \u2192 API keys). Signed in: run npx @gaiventures/pilotfish-mcp-managed login again.";
|
|
38
49
|
if (/fetch failed|ECONNREFUSED|ENOTFOUND|ETIMEDOUT/i.test(m))
|
|
39
50
|
return `Cannot reach pilotfish (${m}). Check PILOTFISH_URL and your connection.`;
|
|
40
51
|
return `pilotfish: ${m}`;
|
|
@@ -157,19 +168,184 @@ async function check(o) {
|
|
|
157
168
|
}
|
|
158
169
|
}
|
|
159
170
|
|
|
171
|
+
// packages/mcp-managed/dist/auth.js
|
|
172
|
+
var CLI_CLIENT_ID = "pilotfish-cli";
|
|
173
|
+
var DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
|
|
174
|
+
var EARLY_MS = 6e4;
|
|
175
|
+
function tokenFileOf(env = process.env) {
|
|
176
|
+
return env["PILOTFISH_TOKEN_FILE"]?.trim() || join(homedir(), ".pilotfish", "mcp-managed.json");
|
|
177
|
+
}
|
|
178
|
+
function apiBaseOf(url) {
|
|
179
|
+
return endpointOf(url).href.replace(/\/mcp\/?$/, "");
|
|
180
|
+
}
|
|
181
|
+
async function form(f, url, body) {
|
|
182
|
+
const r = await f(url, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" }, body: new URLSearchParams(body).toString() });
|
|
183
|
+
const json = await r.json().catch(() => ({}));
|
|
184
|
+
return { status: r.status, json };
|
|
185
|
+
}
|
|
186
|
+
async function readSaved(file) {
|
|
187
|
+
try {
|
|
188
|
+
const s = JSON.parse(await readFile(file, "utf8"));
|
|
189
|
+
return typeof s.access_token === "string" && typeof s.refresh_token === "string" && typeof s.url === "string" ? s : null;
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async function writeSaved(file, s) {
|
|
195
|
+
await mkdir(dirname(file), { recursive: true, mode: 448 });
|
|
196
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
197
|
+
await writeFile(tmp, JSON.stringify(s, null, 2), { mode: 384 });
|
|
198
|
+
await chmod(tmp, 384).catch(() => void 0);
|
|
199
|
+
await rename(tmp, file);
|
|
200
|
+
}
|
|
201
|
+
var tokensFrom = (url, j) => {
|
|
202
|
+
if (typeof j["access_token"] !== "string" || typeof j["refresh_token"] !== "string")
|
|
203
|
+
throw new Error("pilotfish answered the sign-in without tokens");
|
|
204
|
+
return { url, access_token: j["access_token"], refresh_token: j["refresh_token"], expires_at: Date.now() + Number(j["expires_in"] ?? 3600) * 1e3 };
|
|
205
|
+
};
|
|
206
|
+
async function deviceLogin(o) {
|
|
207
|
+
const f = o.fetch ?? fetch;
|
|
208
|
+
const base = apiBaseOf(o.url);
|
|
209
|
+
const sleep = o.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
210
|
+
const start = await form(f, `${base}/oauth/device_authorization`, { client_id: CLI_CLIENT_ID, scope: "pilotfish" });
|
|
211
|
+
if (start.status !== 200)
|
|
212
|
+
throw new Error(`pilotfish refused to start a sign-in: ${String(start.json["error_description"] ?? start.json["error"] ?? start.status)}`);
|
|
213
|
+
const j = start.json;
|
|
214
|
+
o.say(`To sign in, open ${j.verification_uri_complete ?? j.verification_uri}`);
|
|
215
|
+
o.say(`and check that it shows the code ${j.user_code} (or open ${j.verification_uri} and type it).`);
|
|
216
|
+
o.say(`Waiting for you to allow it (the code works for ${Math.round(j.expires_in / 60)} minutes)...`);
|
|
217
|
+
let interval = Math.max(1, j.interval ?? 5) * 1e3;
|
|
218
|
+
const until = Date.now() + j.expires_in * 1e3;
|
|
219
|
+
while (Date.now() < until) {
|
|
220
|
+
await sleep(interval);
|
|
221
|
+
const r = await form(f, `${base}/oauth/token`, { grant_type: DEVICE_GRANT, device_code: j.device_code, client_id: CLI_CLIENT_ID });
|
|
222
|
+
if (r.status === 200) {
|
|
223
|
+
const saved = tokensFrom(base, r.json);
|
|
224
|
+
await writeSaved(o.file, saved);
|
|
225
|
+
return saved;
|
|
226
|
+
}
|
|
227
|
+
const err = String(r.json["error"] ?? "");
|
|
228
|
+
if (err === "authorization_pending")
|
|
229
|
+
continue;
|
|
230
|
+
if (err === "slow_down") {
|
|
231
|
+
interval += 5e3;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (err === "access_denied")
|
|
235
|
+
throw new Error("The sign-in was denied on pilotfish's page.");
|
|
236
|
+
if (err === "expired_token")
|
|
237
|
+
break;
|
|
238
|
+
throw new Error(`pilotfish refused the sign-in: ${String(r.json["error_description"] ?? (err || r.status))}`);
|
|
239
|
+
}
|
|
240
|
+
throw new Error("The code expired before it was allowed. Run login again.");
|
|
241
|
+
}
|
|
242
|
+
async function logout(file, f = fetch) {
|
|
243
|
+
const s = await readSaved(file);
|
|
244
|
+
if (!s)
|
|
245
|
+
return false;
|
|
246
|
+
await form(f, `${s.url}/oauth/revoke`, { token: s.refresh_token, client_id: CLI_CLIENT_ID }).catch(() => void 0);
|
|
247
|
+
await form(f, `${s.url}/oauth/revoke`, { token: s.access_token, client_id: CLI_CLIENT_ID }).catch(() => void 0);
|
|
248
|
+
await rm(file, { force: true });
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
async function locked(file, fn) {
|
|
252
|
+
const lock = `${file}.lock`;
|
|
253
|
+
await mkdir(dirname(file), { recursive: true, mode: 448 });
|
|
254
|
+
for (let i = 0; ; i++) {
|
|
255
|
+
try {
|
|
256
|
+
const h = await open(lock, "wx", 384);
|
|
257
|
+
await h.close();
|
|
258
|
+
break;
|
|
259
|
+
} catch (e) {
|
|
260
|
+
if (e.code !== "EEXIST")
|
|
261
|
+
throw e;
|
|
262
|
+
const age = await stat(lock).then((s) => Date.now() - s.mtimeMs).catch(() => 0);
|
|
263
|
+
if (age > 3e4)
|
|
264
|
+
await rm(lock, { force: true });
|
|
265
|
+
else if (i > 200)
|
|
266
|
+
throw new Error(`Another pilotfish relay holds ${lock}: remove it if no relay is running.`);
|
|
267
|
+
else
|
|
268
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
return await fn();
|
|
273
|
+
} finally {
|
|
274
|
+
await rm(lock, { force: true });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
var TokenSource = class {
|
|
278
|
+
file;
|
|
279
|
+
f;
|
|
280
|
+
current;
|
|
281
|
+
refreshing = null;
|
|
282
|
+
constructor(saved, file, f = fetch) {
|
|
283
|
+
this.file = file;
|
|
284
|
+
this.f = f;
|
|
285
|
+
this.current = saved;
|
|
286
|
+
}
|
|
287
|
+
get url() {
|
|
288
|
+
return this.current.url;
|
|
289
|
+
}
|
|
290
|
+
async token() {
|
|
291
|
+
if (Date.now() > this.current.expires_at - EARLY_MS)
|
|
292
|
+
await this.refresh(this.current.access_token);
|
|
293
|
+
return this.current.access_token;
|
|
294
|
+
}
|
|
295
|
+
/** A new access token unless someone (this relay, or another under the lock) already replaced `stale`. */
|
|
296
|
+
refresh(stale) {
|
|
297
|
+
this.refreshing ??= locked(this.file, async () => {
|
|
298
|
+
const onDisk = await readSaved(this.file);
|
|
299
|
+
if (onDisk && onDisk.access_token !== stale && onDisk.expires_at > Date.now() + EARLY_MS) {
|
|
300
|
+
this.current = onDisk;
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const from = onDisk ?? this.current;
|
|
304
|
+
const r = await form(this.f, `${from.url}/oauth/token`, { grant_type: "refresh_token", refresh_token: from.refresh_token, client_id: CLI_CLIENT_ID });
|
|
305
|
+
if (r.status !== 200)
|
|
306
|
+
throw new Error(`pilotfish ended this sign-in (${String(r.json["error_description"] ?? r.json["error"] ?? r.status)}). Run: npx @gaiventures/pilotfish-mcp-managed login`);
|
|
307
|
+
this.current = tokensFrom(from.url, r.json);
|
|
308
|
+
await writeSaved(this.file, this.current);
|
|
309
|
+
}).finally(() => {
|
|
310
|
+
this.refreshing = null;
|
|
311
|
+
});
|
|
312
|
+
return this.refreshing;
|
|
313
|
+
}
|
|
314
|
+
/** A fetch for the MCP transport: the bearer token on every request, one retry after a refresh on 401. */
|
|
315
|
+
fetch() {
|
|
316
|
+
return async (input, init) => {
|
|
317
|
+
const send = async (token2) => {
|
|
318
|
+
const headers = new Headers(init?.headers);
|
|
319
|
+
headers.set("authorization", `Bearer ${token2}`);
|
|
320
|
+
return this.f(input, { ...init, headers });
|
|
321
|
+
};
|
|
322
|
+
const token = await this.token();
|
|
323
|
+
const r = await send(token);
|
|
324
|
+
if (r.status !== 401)
|
|
325
|
+
return r;
|
|
326
|
+
await this.refresh(token);
|
|
327
|
+
return send(this.current.access_token);
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
160
332
|
// packages/mcp-managed/dist/cli.js
|
|
161
333
|
var USAGE = `pilotfish-mcp-managed \u2014 your pilotfish account (saved tasks, logins, cloud browser, stealth,
|
|
162
334
|
replays) as an MCP server over stdio.
|
|
163
335
|
|
|
336
|
+
pilotfish-mcp-managed login sign this machine in: open the page it shows (on any device), check the code, Allow
|
|
337
|
+
pilotfish-mcp-managed logout sign this machine out
|
|
164
338
|
pilotfish-mcp-managed relay stdio to pilotfish's hosted MCP endpoint
|
|
165
|
-
pilotfish-mcp-managed --check connect once
|
|
339
|
+
pilotfish-mcp-managed --check connect once, list the tools, exit
|
|
166
340
|
pilotfish-mcp-managed --version print the version
|
|
167
341
|
|
|
168
342
|
Environment:
|
|
169
|
-
PILOTFISH_KEY
|
|
170
|
-
PILOTFISH_URL
|
|
343
|
+
PILOTFISH_KEY an organization API key (pf_...), instead of signing in: pilotfish app \u2192 Org settings \u2192 API keys
|
|
344
|
+
PILOTFISH_URL the pilotfish API (default ${DEFAULT_URL})
|
|
345
|
+
PILOTFISH_TOKEN_FILE where the sign-in is kept (default ~/.pilotfish/mcp-managed.json)`;
|
|
171
346
|
async function main() {
|
|
172
|
-
const { values } = parseArgs({
|
|
347
|
+
const { values, positionals } = parseArgs({
|
|
348
|
+
allowPositionals: true,
|
|
173
349
|
options: { check: { type: "boolean", default: false }, help: { type: "boolean", short: "h", default: false }, version: { type: "boolean", short: "v", default: false } }
|
|
174
350
|
});
|
|
175
351
|
if (values.version)
|
|
@@ -178,21 +354,57 @@ async function main() {
|
|
|
178
354
|
if (values.help)
|
|
179
355
|
return void process.stderr.write(`${USAGE}
|
|
180
356
|
`);
|
|
181
|
-
|
|
182
|
-
try {
|
|
183
|
-
key = checkKey(process.env["PILOTFISH_KEY"]);
|
|
184
|
-
} catch (e) {
|
|
185
|
-
process.stderr.write(`pilotfish-mcp-managed: ${e.message}
|
|
357
|
+
const say = (m) => void process.stderr.write(`${m}
|
|
186
358
|
`);
|
|
187
|
-
process.exit(2);
|
|
188
|
-
}
|
|
189
|
-
const url = process.env["PILOTFISH_URL"]?.trim() || DEFAULT_URL;
|
|
190
359
|
const log = (m) => void process.stderr.write(`pilotfish-mcp-managed: ${m}
|
|
191
360
|
`);
|
|
361
|
+
const url = process.env["PILOTFISH_URL"]?.trim() || DEFAULT_URL;
|
|
362
|
+
const file = process.env["PILOTFISH_TOKEN_FILE"]?.trim() || tokenFileOf();
|
|
363
|
+
const command = positionals[0];
|
|
364
|
+
if (command === "login") {
|
|
365
|
+
try {
|
|
366
|
+
await deviceLogin({ url, file, say });
|
|
367
|
+
say(`Signed in. The sign-in is kept in ${file} (only you can read it).`);
|
|
368
|
+
say(`Add the relay to your MCP client with: npx -y @gaiventures/pilotfish-mcp-managed`);
|
|
369
|
+
process.exit(0);
|
|
370
|
+
} catch (e) {
|
|
371
|
+
log(e.message);
|
|
372
|
+
process.exit(1);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (command === "logout") {
|
|
376
|
+
say(await logout(file) ? `Signed out: the sign-in was revoked and ${file} removed.` : "This machine was not signed in.");
|
|
377
|
+
process.exit(0);
|
|
378
|
+
}
|
|
379
|
+
if (command) {
|
|
380
|
+
log(`unknown command "${command}"
|
|
381
|
+
|
|
382
|
+
${USAGE}`);
|
|
383
|
+
process.exit(2);
|
|
384
|
+
}
|
|
385
|
+
let auth;
|
|
386
|
+
if (process.env["PILOTFISH_KEY"]?.trim()) {
|
|
387
|
+
try {
|
|
388
|
+
auth = { key: checkKey(process.env["PILOTFISH_KEY"]) };
|
|
389
|
+
} catch (e) {
|
|
390
|
+
log(e.message);
|
|
391
|
+
process.exit(2);
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
const saved = await readSaved(file);
|
|
395
|
+
if (!saved) {
|
|
396
|
+
log("Not signed in. Run: npx @gaiventures/pilotfish-mcp-managed login (or set PILOTFISH_KEY to an organization API key).");
|
|
397
|
+
process.exit(2);
|
|
398
|
+
}
|
|
399
|
+
if (saved.url !== endpointOf(url).href.replace(/\/mcp\/?$/, ""))
|
|
400
|
+
log(`the sign-in is for ${saved.url}, not ${url}: using ${saved.url}`);
|
|
401
|
+
auth = { token: new TokenSource(saved, file) };
|
|
402
|
+
}
|
|
403
|
+
const target = auth.token ? auth.token.url : url;
|
|
192
404
|
if (values.check) {
|
|
193
405
|
try {
|
|
194
|
-
const r = await check({ url,
|
|
195
|
-
process.stderr.write(`OK: ${endpointOf(
|
|
406
|
+
const r = await check({ url: target, ...auth });
|
|
407
|
+
process.stderr.write(`OK: ${endpointOf(target).href} (${r.server}), ${r.tools.length} tools: ${r.tools.join(", ")}
|
|
196
408
|
`);
|
|
197
409
|
process.exit(0);
|
|
198
410
|
} catch (e) {
|
|
@@ -200,7 +412,7 @@ async function main() {
|
|
|
200
412
|
process.exit(1);
|
|
201
413
|
}
|
|
202
414
|
}
|
|
203
|
-
const bridge = await relay(new StdioServerTransport(), { url,
|
|
415
|
+
const bridge = await relay(new StdioServerTransport(), { url: target, ...auth, log });
|
|
204
416
|
const shutdown = () => void bridge.close().finally(() => process.exit(0));
|
|
205
417
|
process.stdin.on("end", shutdown);
|
|
206
418
|
process.on("SIGINT", shutdown);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gaiventures/pilotfish-mcp-managed",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.32",
|
|
4
4
|
"description": "Whole browser tasks done in pilotfish's cloud for any MCP agent, signed in with your saved logins. mcprush.com-37c48d491684ec6011385ea5",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|