@dreamteamer/connectors-linkedin 0.1.0 → 0.2.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/dist/skills/scraping-contacts/bin/import-avatars.mjs +135 -0
- package/dist/skills/scraping-contacts/bin/li.mjs +375 -0
- package/package.json +10 -5
- package/skills/scraping-contacts/bin/import-avatars.mjs +0 -90
- package/skills/scraping-contacts/bin/li.mjs +0 -285
- /package/{skills → dist/skills}/scraping-contacts/SKILL.md +0 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Import the photo URLs a browser session harvested → `assets/avatars/<id>` + `contacts.avatar`.
|
|
3
|
+
//
|
|
4
|
+
// node import-avatars.mjs harvested.json [--dry]
|
|
5
|
+
//
|
|
6
|
+
// harvested.json: [{ "id": "<contact id>", "photo": "https://media.licdn.com/…" | null, "note"?: "…" }]
|
|
7
|
+
//
|
|
8
|
+
// Checks the bytes, not the status: an error page can arrive as HTTP 200 and a URL can name the wrong
|
|
9
|
+
// type, so the image type is sniffed from the magic bytes and the extension comes from that.
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import os from "node:os";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
// The workspace root: the nearest folder carrying package-lock.json. `package.json` alone is not
|
|
15
|
+
// enough, because a module folder has one too.
|
|
16
|
+
function workspaceRoot(from = process.cwd()) {
|
|
17
|
+
let dir = path.resolve(from);
|
|
18
|
+
for (;;) {
|
|
19
|
+
// a folder sitting directly inside `git_modules/` is a module clone, never the workspace, even when `npm install` left it a lockfile
|
|
20
|
+
const inGitModules = path.basename(path.dirname(dir)) === "git_modules";
|
|
21
|
+
if (!inGitModules && fs.existsSync(path.join(dir, "package-lock.json")))
|
|
22
|
+
return dir;
|
|
23
|
+
const up = path.dirname(dir);
|
|
24
|
+
if (up === dir)
|
|
25
|
+
throw new Error("not inside a dreamteamer workspace (no package-lock.json above " + from + ")");
|
|
26
|
+
dir = up;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const WORKSPACE = workspaceRoot(path.dirname(new URL(import.meta.url).pathname));
|
|
30
|
+
const MAX = 204800; // the assets collection's own max_bytes
|
|
31
|
+
const dry = process.argv.includes("--dry");
|
|
32
|
+
const file = process.argv[2];
|
|
33
|
+
if (!file) {
|
|
34
|
+
console.error('usage: import-avatars.mjs <harvested.json> [--dry]\n harvested.json: [{"id": "<contact id>", "photo": "https://media.licdn.com/…" | null, "note"?: "…"}]');
|
|
35
|
+
process.exit(2);
|
|
36
|
+
}
|
|
37
|
+
/** Magic-byte sniff → extension, or null when these bytes are not an image we accept. */
|
|
38
|
+
function extOf(buf) {
|
|
39
|
+
if (buf.length < 12)
|
|
40
|
+
return null;
|
|
41
|
+
const hex = buf.subarray(0, 12).toString("hex");
|
|
42
|
+
if (hex.startsWith("89504e47"))
|
|
43
|
+
return "png";
|
|
44
|
+
if (hex.startsWith("ffd8ff"))
|
|
45
|
+
return "jpg";
|
|
46
|
+
if (hex.startsWith("47494638"))
|
|
47
|
+
return "gif";
|
|
48
|
+
if (buf.subarray(0, 4).toString("ascii") === "RIFF" && buf.subarray(8, 12).toString("ascii") === "WEBP")
|
|
49
|
+
return "webp";
|
|
50
|
+
const head = buf.subarray(0, 400).toString("utf8").trimStart().toLowerCase();
|
|
51
|
+
if (head.startsWith("<svg") || head.startsWith("<?xml"))
|
|
52
|
+
return "svg";
|
|
53
|
+
return null; // an HTML error page lands here, which is the whole point
|
|
54
|
+
}
|
|
55
|
+
// The workspace's own installed engine, run directly: a workspace need not define an npm `dt` script.
|
|
56
|
+
function engineBin(root) {
|
|
57
|
+
const pkgPath = path.join(root, "node_modules", "dreamteamer", "package.json");
|
|
58
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
59
|
+
const bin = typeof pkg.bin === "string" ? pkg.bin : (pkg.bin.dreamteamer ?? Object.values(pkg.bin)[0]);
|
|
60
|
+
return path.join(path.dirname(pkgPath), bin);
|
|
61
|
+
}
|
|
62
|
+
const dt = (...args) => execFileSync(process.execPath, [engineBin(WORKSPACE), ...args], { cwd: WORKSPACE, encoding: "utf8" });
|
|
63
|
+
const rows = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
64
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "avatars-"));
|
|
65
|
+
let ok = 0, skipped = 0, failed = 0;
|
|
66
|
+
for (const row of rows) {
|
|
67
|
+
const { id, photo } = row ?? {};
|
|
68
|
+
if (!id) {
|
|
69
|
+
console.log(`✖ entry with no id: ${JSON.stringify(row)}`);
|
|
70
|
+
failed++;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!photo) {
|
|
74
|
+
console.log(`· ${id}: no photo${row.note ? ` (${row.note})` : ""}`);
|
|
75
|
+
skipped++;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!/^https:\/\/media\.licdn\.com\//.test(photo)) {
|
|
79
|
+
console.log(`✖ ${id}: not a media.licdn.com URL — a ghost avatar or the wrong link`);
|
|
80
|
+
failed++;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
let buf;
|
|
84
|
+
try {
|
|
85
|
+
const res = await fetch(photo);
|
|
86
|
+
if (!res.ok) {
|
|
87
|
+
console.log(`✖ ${id}: HTTP ${res.status}`);
|
|
88
|
+
failed++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
buf = Buffer.from(await res.arrayBuffer());
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
console.log(`✖ ${id}: ${e.message}`);
|
|
95
|
+
failed++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const ext = extOf(buf);
|
|
99
|
+
if (!ext) {
|
|
100
|
+
console.log(`✖ ${id}: ${buf.length} bytes that are not an image (an error page?)`);
|
|
101
|
+
failed++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (buf.length > MAX) {
|
|
105
|
+
console.log(`✖ ${id}: ${buf.length} bytes, over max_bytes ${MAX}`);
|
|
106
|
+
failed++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const out = path.join(tmp, `${id.replace(/\//g, "_")}.${ext}`);
|
|
110
|
+
fs.writeFileSync(out, buf);
|
|
111
|
+
if (dry) {
|
|
112
|
+
console.log(`· ${id}: would import ${buf.length} bytes (.${ext})`);
|
|
113
|
+
ok++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
dt("add", "assets", `avatars/${id}`, "--from", out, "--force");
|
|
118
|
+
dt("set", `contacts/${id}`, `avatar=assets/avatars/${id}`);
|
|
119
|
+
console.log(`✔ ${id}: ${buf.length} bytes (.${ext})`);
|
|
120
|
+
ok++;
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
console.log(`✖ ${id}: ${String(e.stderr ?? e.message)
|
|
124
|
+
.trim()
|
|
125
|
+
.split("\n")[0]}`);
|
|
126
|
+
failed++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
console.log(`\n${ok} imported · ${skipped} had no photo · ${failed} failed`);
|
|
130
|
+
if (!dry && ok)
|
|
131
|
+
console.log(`then: npx dt check, and commit with\n npx dt commit ${rows
|
|
132
|
+
.filter(r => r.photo)
|
|
133
|
+
.map(r => `assets/avatars/${r.id} contacts/${r.id}`)
|
|
134
|
+
.slice(0, 3)
|
|
135
|
+
.join(" ")} …`);
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* li.mjs — drive linkedin-mcp-server for ONE batch, in ONE ephemeral container, then exit.
|
|
4
|
+
*
|
|
5
|
+
* Why this exists instead of an `.mcp.json` entry: the LinkedIn session is a SINGLETON. A stdio MCP
|
|
6
|
+
* entry spawns one container per agent session, and two containers on the one ~/.linkedin-mcp
|
|
7
|
+
* profile make LinkedIn invalidate the cookie — which costs a host re-login, not a retry. A stdio
|
|
8
|
+
* server cannot be shared between clients (one stdin/stdout pair), and a long-lived HTTP server would
|
|
9
|
+
* be a daemon holding a live session. So: no registered server, no idle container. The lock below is
|
|
10
|
+
* what makes "exactly one" true.
|
|
11
|
+
*
|
|
12
|
+
* Usage
|
|
13
|
+
* node li.mjs <tool> '<jsonArgs>' one call
|
|
14
|
+
* echo '[{"tool":..,"args":{..}}, ...]' | node li.mjs --stdin a batch, one container
|
|
15
|
+
* node li.mjs --doctor is the session alive? (no LinkedIn request)
|
|
16
|
+
*
|
|
17
|
+
* Always prints a JSON array of {tool, args, ok, result|error} to stdout; logs go to stderr.
|
|
18
|
+
* close_session is called for you at the end of every batch.
|
|
19
|
+
*/
|
|
20
|
+
import { spawn, execFileSync } from "node:child_process";
|
|
21
|
+
import fs from "node:fs";
|
|
22
|
+
import os from "node:os";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
const IMAGE = "stickerdaniel/linkedin-mcp-server:latest";
|
|
25
|
+
const PROFILE = path.join(os.homedir(), ".linkedin-mcp");
|
|
26
|
+
const LOCK = path.join(PROFILE, "linkedin-batch.lock"); // ours; the server owns profile.lock
|
|
27
|
+
const STALE_MS = 30 * 60 * 1000;
|
|
28
|
+
const CALL_TIMEOUT_MS = 300_000;
|
|
29
|
+
// ---------- the lock: a directory, because mkdir is atomic on every filesystem -------------------
|
|
30
|
+
function acquire() {
|
|
31
|
+
try {
|
|
32
|
+
fs.mkdirSync(LOCK);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
if (e.code !== "EEXIST")
|
|
36
|
+
throw e;
|
|
37
|
+
const info = readLock();
|
|
38
|
+
if (info && Date.now() - info.at < STALE_MS && alive(info.pid)) {
|
|
39
|
+
throw new Error(`another LinkedIn batch is running (pid ${info.pid}, started ${new Date(info.at).toISOString()}).\n` +
|
|
40
|
+
"Wait for it. Two containers on one profile invalidate the LinkedIn session — that costs a\n" +
|
|
41
|
+
`host re-login, not a retry. If you are certain it is dead: rm -rf ${LOCK}`);
|
|
42
|
+
}
|
|
43
|
+
console.error(`[li] clearing a stale lock (${info ? `pid ${info.pid}` : "unreadable"})`);
|
|
44
|
+
fs.rmSync(LOCK, { recursive: true, force: true });
|
|
45
|
+
fs.mkdirSync(LOCK);
|
|
46
|
+
}
|
|
47
|
+
fs.writeFileSync(path.join(LOCK, "owner.json"), JSON.stringify({ pid: process.pid, at: Date.now() }));
|
|
48
|
+
}
|
|
49
|
+
const readLock = () => {
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(fs.readFileSync(path.join(LOCK, "owner.json"), "utf8"));
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const alive = (pid) => {
|
|
58
|
+
try {
|
|
59
|
+
process.kill(pid, 0);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const release = () => {
|
|
67
|
+
try {
|
|
68
|
+
fs.rmSync(LOCK, { recursive: true, force: true });
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
/* gone */
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
/** read one key from the nearest ancestor .env — the workspace convention for machine config */
|
|
75
|
+
function readEnvFile(key) {
|
|
76
|
+
let dir = process.cwd();
|
|
77
|
+
for (let i = 0; i < 6; i++) {
|
|
78
|
+
const f = path.join(dir, ".env");
|
|
79
|
+
if (fs.existsSync(f)) {
|
|
80
|
+
const m = fs.readFileSync(f, "utf8").match(new RegExp(`^${key}=(.*)$`, "m"));
|
|
81
|
+
if (m)
|
|
82
|
+
return m[1].trim().replace(/^["']|["']$/g, "");
|
|
83
|
+
}
|
|
84
|
+
const up = path.dirname(dir);
|
|
85
|
+
if (up === dir)
|
|
86
|
+
break;
|
|
87
|
+
dir = up;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
// ---------- doctor: answer "is the session alive" from disk, spending no LinkedIn request --------
|
|
92
|
+
function doctor() {
|
|
93
|
+
const out = { profile_dir: PROFILE, exists: fs.existsSync(PROFILE) };
|
|
94
|
+
if (!out.exists) {
|
|
95
|
+
out.verdict = "no profile — run: uvx mcp-server-linkedin@latest --import-from-browser";
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
out.cookies = fs.existsSync(path.join(PROFILE, "cookies.json"));
|
|
99
|
+
try {
|
|
100
|
+
const st = JSON.parse(fs.readFileSync(path.join(PROFILE, "source-state.json"), "utf8"));
|
|
101
|
+
out.login_at = st.created_at;
|
|
102
|
+
out.age_days = +((Date.now() - Date.parse(st.created_at)) / 864e5).toFixed(1);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
out.login_at = null;
|
|
106
|
+
}
|
|
107
|
+
// the newest trace run's post-import screenshot is the ground truth: a real page is ~36KB,
|
|
108
|
+
// a bounce to the sign-in wall leaves it blank at ~4KB.
|
|
109
|
+
const loginMs = out.login_at ? Date.parse(out.login_at) : 0;
|
|
110
|
+
const runs = path.join(PROFILE, "trace-runs");
|
|
111
|
+
if (fs.existsSync(runs)) {
|
|
112
|
+
// newest FIRST, but skip runs with no screenshot — a container killed before it scraped
|
|
113
|
+
// leaves an empty trace dir, and letting that mask the real evidence is how you conclude
|
|
114
|
+
// "no information" while the answer sits one directory down.
|
|
115
|
+
const ordered = fs
|
|
116
|
+
.readdirSync(runs)
|
|
117
|
+
.map(d => ({ d, m: fs.statSync(path.join(runs, d)).mtimeMs }))
|
|
118
|
+
.sort((a, b) => b.m - a.m);
|
|
119
|
+
out.newest_trace = ordered[0]?.d ?? null;
|
|
120
|
+
const withShot = ordered.find(r => fs.existsSync(path.join(runs, r.d, "screens", "004-feed-after-goto.png")));
|
|
121
|
+
if (withShot) {
|
|
122
|
+
const dir = path.join(runs, withShot.d, "screens");
|
|
123
|
+
const bytes = fs.statSync(path.join(dir, "004-feed-after-goto.png")).size;
|
|
124
|
+
out.evidence_from = withShot.d;
|
|
125
|
+
out.last_feed_screenshot_bytes = bytes;
|
|
126
|
+
// A verdict from a run that PREDATES the current login is about the previous session, so it
|
|
127
|
+
// must not be reported as the current one.
|
|
128
|
+
if (loginMs && withShot.m < loginMs) {
|
|
129
|
+
out.evidence_is_stale = `trace is ${Math.round((loginMs - withShot.m) / 1000)}s older than the login`;
|
|
130
|
+
out.verdict = "login is NEWER than any scraping evidence — session untested. Just run the batch.";
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
out.verdict =
|
|
134
|
+
bytes < 10_000
|
|
135
|
+
? "session looks DEAD (blank feed after cookie import). Fix: uvx mcp-server-linkedin@latest " +
|
|
136
|
+
`--import-from-browser. Confirm by eye: ${path.join(dir, "003-bridge-after-cookie-import.png")}`
|
|
137
|
+
: "last scraping run reached a real feed page — session was healthy then";
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
out.verdict ??= "no trace evidence yet; the first call will tell you";
|
|
142
|
+
const held = fs.existsSync(LOCK) ? readLock() : null;
|
|
143
|
+
out.batch_lock = !held
|
|
144
|
+
? "free"
|
|
145
|
+
: alive(held.pid) && Date.now() - held.at < STALE_MS
|
|
146
|
+
? `HELD by pid ${held.pid}`
|
|
147
|
+
: `stale (pid ${held.pid} is gone) — the next batch clears it automatically`;
|
|
148
|
+
out.containers_running = execFileSync("docker", ["ps", "--filter", `ancestor=${IMAGE}`, "--format", "{{.ID}}"])
|
|
149
|
+
.toString()
|
|
150
|
+
.trim()
|
|
151
|
+
.split("\n")
|
|
152
|
+
.filter(Boolean).length;
|
|
153
|
+
Object.assign(out, strandedClients());
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Stranded HOST-side clients. A `mcp-server-linkedin` process that never exited keeps the flock on
|
|
158
|
+
* `~/.linkedin-mcp/profile.lock`, and while it lives every `--import-from-browser` and `--login`
|
|
159
|
+
* refuses with `BrowserBusyError` — so a dead-session verdict must not send you to re-login.
|
|
160
|
+
*
|
|
161
|
+
* This greps `ps` rather than reading the lock: macOS `lsof` shows an empty lock field even for a
|
|
162
|
+
* held flock, and Docker Desktop's VM always has the file open because `~/.linkedin-mcp` is
|
|
163
|
+
* bind-mounted into the container.
|
|
164
|
+
*
|
|
165
|
+
* flock is released by the KERNEL when its holder dies, so anything found here is alive and `kill`
|
|
166
|
+
* is the whole fix.
|
|
167
|
+
*/
|
|
168
|
+
function strandedClients() {
|
|
169
|
+
let lines = [];
|
|
170
|
+
try {
|
|
171
|
+
lines = execFileSync("ps", ["-axo", "pid=,lstart=,command="], { stdio: ["ignore", "pipe", "ignore"] })
|
|
172
|
+
.toString()
|
|
173
|
+
.split("\n")
|
|
174
|
+
.filter(l => /mcp-server-linkedin|patchright-browsers/.test(l) && !/\bgrep\b/.test(l));
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return { host_clients: "unknown (ps failed)" };
|
|
178
|
+
}
|
|
179
|
+
if (!lines.length)
|
|
180
|
+
return { host_clients: "none" };
|
|
181
|
+
// The chromium tree is noise once its parent is named; report the SERVER processes, and count
|
|
182
|
+
// the browsers so a half-dead tree is still visible.
|
|
183
|
+
const servers = lines
|
|
184
|
+
.filter(l => /mcp-server-linkedin/.test(l))
|
|
185
|
+
.map(l => {
|
|
186
|
+
const m = /^\s*(\d+)\s+(.{24})\s+(.*)$/.exec(l);
|
|
187
|
+
return m ? { pid: Number(m[1]), started: m[2].trim() } : null;
|
|
188
|
+
})
|
|
189
|
+
.filter(Boolean);
|
|
190
|
+
const browsers = lines.filter(l => /patchright-browsers/.test(l)).length;
|
|
191
|
+
if (!servers.length)
|
|
192
|
+
return {
|
|
193
|
+
host_clients: `${browsers} orphaned patchright browser process(es), no server — kill them before logging in`,
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
host_clients: `HELD by ${servers.map(sv => `pid ${sv.pid} (started ${sv.started})`).join(", ")}, ${browsers} browser process(es)`,
|
|
197
|
+
verdict_override: `a LinkedIn MCP process still holds the browser lease, so --login and --import-from-browser BOTH refuse with BrowserBusyError. Fix: kill ${servers.map(sv => sv.pid).join(" ")}`,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
// ---------- one container, N calls ---------------------------------------------------------------
|
|
201
|
+
async function runBatch(calls) {
|
|
202
|
+
// USER_AGENT exists to reduce anti-scraping detection: the session is minted on the host and used
|
|
203
|
+
// from a Linux container. Set LINKEDIN_USER_AGENT in .env to the browser the session came from.
|
|
204
|
+
const ua = process.env.LINKEDIN_USER_AGENT || readEnvFile("LINKEDIN_USER_AGENT");
|
|
205
|
+
if (!ua)
|
|
206
|
+
console.error("[li] no LINKEDIN_USER_AGENT set — see the skill; this may raise detection");
|
|
207
|
+
const args = [
|
|
208
|
+
"run",
|
|
209
|
+
"--rm",
|
|
210
|
+
"-i",
|
|
211
|
+
"--init",
|
|
212
|
+
"--security-opt",
|
|
213
|
+
"no-new-privileges",
|
|
214
|
+
"-v",
|
|
215
|
+
`${PROFILE}:/home/pwuser/.linkedin-mcp`,
|
|
216
|
+
"-e",
|
|
217
|
+
"TIMEOUT=10000",
|
|
218
|
+
"-e",
|
|
219
|
+
"TOOL_TIMEOUT=300",
|
|
220
|
+
...(ua ? ["-e", `USER_AGENT=${ua}`] : []),
|
|
221
|
+
IMAGE,
|
|
222
|
+
];
|
|
223
|
+
const p = spawn("docker", args, { stdio: ["pipe", "pipe", "pipe"] });
|
|
224
|
+
const pending = new Map();
|
|
225
|
+
let buf = "";
|
|
226
|
+
let nextId = 1;
|
|
227
|
+
p.stdout.on("data", d => {
|
|
228
|
+
buf += d;
|
|
229
|
+
let i;
|
|
230
|
+
while ((i = buf.indexOf("\n")) !== -1) {
|
|
231
|
+
const line = buf.slice(0, i).trim();
|
|
232
|
+
buf = buf.slice(i + 1);
|
|
233
|
+
if (!line)
|
|
234
|
+
continue;
|
|
235
|
+
let m;
|
|
236
|
+
try {
|
|
237
|
+
m = JSON.parse(line);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const r = pending.get(m.id);
|
|
243
|
+
if (r) {
|
|
244
|
+
pending.delete(m.id);
|
|
245
|
+
r(m);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
p.stderr.on("data", d => {
|
|
250
|
+
const s = String(d);
|
|
251
|
+
if (/ERROR|WARNING|Traceback|session/i.test(s))
|
|
252
|
+
process.stderr.write("[server] " + s);
|
|
253
|
+
});
|
|
254
|
+
const rpc = (method, params) => new Promise((resolve, reject) => {
|
|
255
|
+
const id = nextId++;
|
|
256
|
+
pending.set(id, resolve);
|
|
257
|
+
const t = setTimeout(() => {
|
|
258
|
+
pending.delete(id);
|
|
259
|
+
reject(new Error(`timeout after ${CALL_TIMEOUT_MS}ms`));
|
|
260
|
+
}, CALL_TIMEOUT_MS);
|
|
261
|
+
const done = (v) => {
|
|
262
|
+
clearTimeout(t);
|
|
263
|
+
resolve(v);
|
|
264
|
+
};
|
|
265
|
+
pending.set(id, done);
|
|
266
|
+
p.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n");
|
|
267
|
+
});
|
|
268
|
+
const init = await rpc("initialize", {
|
|
269
|
+
protocolVersion: "2024-11-05",
|
|
270
|
+
capabilities: {},
|
|
271
|
+
clientInfo: { name: "dt-li", version: "1" },
|
|
272
|
+
});
|
|
273
|
+
console.error(`[li] ${init.result?.serverInfo?.name} ${init.result?.serverInfo?.version} — ${calls.length} call(s)`);
|
|
274
|
+
const out = [];
|
|
275
|
+
for (const [n, c] of calls.entries()) {
|
|
276
|
+
console.error(`[li] ${n + 1}/${calls.length} ${c.tool} ${JSON.stringify(c.args ?? {})}`);
|
|
277
|
+
let row = { tool: c.tool, args: c.args ?? {} };
|
|
278
|
+
try {
|
|
279
|
+
const m = await rpc("tools/call", { name: c.tool, arguments: c.args ?? {} });
|
|
280
|
+
if (m.error)
|
|
281
|
+
row = { ...row, ok: false, error: m.error };
|
|
282
|
+
else {
|
|
283
|
+
const text = (m.result?.content ?? [])
|
|
284
|
+
.filter(x => x.type === "text")
|
|
285
|
+
.map(x => x.text)
|
|
286
|
+
.join("\n");
|
|
287
|
+
let parsed;
|
|
288
|
+
try {
|
|
289
|
+
parsed = JSON.parse(text);
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
parsed = text;
|
|
293
|
+
}
|
|
294
|
+
// a non-error response can still carry per-section failures — surface them
|
|
295
|
+
const partial = parsed?.section_errors ? Object.keys(parsed.section_errors) : [];
|
|
296
|
+
row = {
|
|
297
|
+
...row,
|
|
298
|
+
ok: !m.result?.isError,
|
|
299
|
+
result: parsed,
|
|
300
|
+
...(partial.length ? { failed_sections: partial } : {}),
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (e) {
|
|
305
|
+
row = { ...row, ok: false, error: e.message };
|
|
306
|
+
}
|
|
307
|
+
out.push(row);
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
await rpc("tools/call", { name: "close_session", arguments: {} });
|
|
311
|
+
console.error("[li] session closed");
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
console.error("[li] close_session did not answer (container is exiting anyway)");
|
|
315
|
+
}
|
|
316
|
+
p.kill();
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
// ---------- main --------------------------------------------------------------------------------
|
|
320
|
+
const argv = process.argv.slice(2);
|
|
321
|
+
if (argv[0] === "--doctor") {
|
|
322
|
+
console.log(JSON.stringify(doctor(), null, 2));
|
|
323
|
+
process.exit(0);
|
|
324
|
+
}
|
|
325
|
+
let calls;
|
|
326
|
+
if (argv[0] === "--stdin") {
|
|
327
|
+
calls = JSON.parse(fs.readFileSync(0, "utf8"));
|
|
328
|
+
if (!Array.isArray(calls)) {
|
|
329
|
+
console.error("--stdin wants a JSON array of {tool, args}");
|
|
330
|
+
process.exit(2);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
else if (argv[0]) {
|
|
334
|
+
calls = [{ tool: argv[0], args: argv[1] ? JSON.parse(argv[1]) : {} }];
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
console.error(`usage:
|
|
338
|
+
li.mjs <tool> '<jsonArgs>'
|
|
339
|
+
echo '[{"tool":"get_person_profile","args":{"linkedin_username":"dana-oren"}}]' | li.mjs --stdin
|
|
340
|
+
li.mjs --doctor
|
|
341
|
+
|
|
342
|
+
NEVER call connect_with_person or send_message — they act publicly as you.`);
|
|
343
|
+
process.exit(2);
|
|
344
|
+
}
|
|
345
|
+
const BANNED = new Set(["connect_with_person", "send_message"]);
|
|
346
|
+
const bad = calls.filter(c => BANNED.has(c.tool));
|
|
347
|
+
if (bad.length) {
|
|
348
|
+
console.error(`refusing ${bad.map(b => b.tool).join(", ")} — these act publicly and irreversibly as` +
|
|
349
|
+
" you. If it is genuinely wanted, do it by hand, not through a scraper.");
|
|
350
|
+
process.exit(3);
|
|
351
|
+
}
|
|
352
|
+
try {
|
|
353
|
+
acquire();
|
|
354
|
+
}
|
|
355
|
+
catch (e) {
|
|
356
|
+
// a contended lock is an expected outcome, not a crash — say so without a stack trace
|
|
357
|
+
console.error(`[li] ${e.message}`);
|
|
358
|
+
process.exit(4);
|
|
359
|
+
}
|
|
360
|
+
let code = 0;
|
|
361
|
+
try {
|
|
362
|
+
const results = await runBatch(calls);
|
|
363
|
+
console.log(JSON.stringify(results, null, 2));
|
|
364
|
+
code = results.every(r => r.ok) ? 0 : 1;
|
|
365
|
+
}
|
|
366
|
+
catch (e) {
|
|
367
|
+
console.error(`[li] batch failed: ${e.message}`);
|
|
368
|
+
code = 1;
|
|
369
|
+
}
|
|
370
|
+
finally {
|
|
371
|
+
// MUST release before exiting: process.exit() does NOT run finally blocks, so an exit inside
|
|
372
|
+
// the try would leave the lock held after the batch is gone.
|
|
373
|
+
release();
|
|
374
|
+
}
|
|
375
|
+
process.exit(code);
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dreamteamer/connectors-linkedin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Fill in contacts from LinkedIn and the web: /scrape-contact, the scraping-contacts skill and two LinkedIn views.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "git+https://github.com/dreamteamer/dreamteamer-modules.git",
|
|
@@ -17,15 +18,15 @@
|
|
|
17
18
|
],
|
|
18
19
|
"files": [
|
|
19
20
|
"README.md",
|
|
20
|
-
"skills",
|
|
21
21
|
"commands",
|
|
22
22
|
"command-bindings",
|
|
23
|
-
"ui-views"
|
|
23
|
+
"ui-views",
|
|
24
|
+
"dist"
|
|
24
25
|
],
|
|
25
26
|
"dreamteamer": {
|
|
26
27
|
"title": "LinkedIn connector",
|
|
27
28
|
"description": "Reaches LinkedIn for `contacts`: the `scraping-contacts` skill and `/scrape-contact` fill a person in from their LinkedIn profile, one batched container per run, and from web search, with their `companies` record and an avatar in `assets`. Every `draft` contact is queued; two views list who still needs a LinkedIn link and who needs review. Optional key: `LINKEDIN_USER_AGENT`.",
|
|
28
|
-
"engine": ">=0.
|
|
29
|
+
"engine": ">=0.50.0",
|
|
29
30
|
"dependencies": [
|
|
30
31
|
"@dreamteamer/contacts",
|
|
31
32
|
"@dreamteamer/companies",
|
|
@@ -36,11 +37,15 @@
|
|
|
36
37
|
],
|
|
37
38
|
"env": [
|
|
38
39
|
"LINKEDIN_USER_AGENT"
|
|
39
|
-
]
|
|
40
|
+
],
|
|
41
|
+
"skills": "dist/skills"
|
|
40
42
|
},
|
|
41
43
|
"peerDependencies": {
|
|
42
44
|
"@dreamteamer/contacts": ">=0.2.0",
|
|
43
45
|
"@dreamteamer/companies": ">=0.1.1",
|
|
44
46
|
"@dreamteamer/assets": ">=0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"prepack": "node ../../scripts/build.ts --module=connectors-linkedin"
|
|
45
50
|
}
|
|
46
51
|
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Import the photo URLs a browser session harvested → `assets/avatars/<id>` + `contacts.avatar`.
|
|
3
|
-
//
|
|
4
|
-
// node import-avatars.mjs harvested.json [--dry]
|
|
5
|
-
//
|
|
6
|
-
// harvested.json: [{ "id": "<contact id>", "photo": "https://media.licdn.com/…" | null, "note"?: "…" }]
|
|
7
|
-
//
|
|
8
|
-
// Checks the bytes, not the status: an error page can arrive as HTTP 200 and a URL can name the wrong
|
|
9
|
-
// type, so the image type is sniffed from the magic bytes and the extension comes from that.
|
|
10
|
-
import { execFileSync } from 'node:child_process';
|
|
11
|
-
import fs from 'node:fs';
|
|
12
|
-
import os from 'node:os';
|
|
13
|
-
import path from 'node:path';
|
|
14
|
-
|
|
15
|
-
// The workspace root: the nearest folder carrying package-lock.json. `package.json` alone is not
|
|
16
|
-
// enough, because a module folder has one too.
|
|
17
|
-
function workspaceRoot(from = process.cwd()) {
|
|
18
|
-
let dir = path.resolve(from);
|
|
19
|
-
for (;;) {
|
|
20
|
-
// a folder sitting directly inside `git_modules/` is a module clone, never the workspace, even when `npm install` left it a lockfile
|
|
21
|
-
const inGitModules = path.basename(path.dirname(dir)) === 'git_modules';
|
|
22
|
-
if (!inGitModules && fs.existsSync(path.join(dir, 'package-lock.json'))) return dir;
|
|
23
|
-
const up = path.dirname(dir);
|
|
24
|
-
if (up === dir) throw new Error('not inside a dreamteamer workspace (no package-lock.json above ' + from + ')');
|
|
25
|
-
dir = up;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const WORKSPACE = workspaceRoot(path.dirname(new URL(import.meta.url).pathname));
|
|
29
|
-
const MAX = 204800; // the assets collection's own max_bytes
|
|
30
|
-
const dry = process.argv.includes('--dry');
|
|
31
|
-
const file = process.argv[2];
|
|
32
|
-
if (!file) { console.error('usage: import-avatars.mjs <harvested.json> [--dry]\n harvested.json: [{"id": "<contact id>", "photo": "https://media.licdn.com/…" | null, "note"?: "…"}]'); process.exit(2); }
|
|
33
|
-
|
|
34
|
-
/** Magic-byte sniff → extension, or null when these bytes are not an image we accept. */
|
|
35
|
-
function extOf(buf) {
|
|
36
|
-
if (buf.length < 12) return null;
|
|
37
|
-
const hex = buf.subarray(0, 12).toString('hex');
|
|
38
|
-
if (hex.startsWith('89504e47')) return 'png';
|
|
39
|
-
if (hex.startsWith('ffd8ff')) return 'jpg';
|
|
40
|
-
if (hex.startsWith('47494638')) return 'gif';
|
|
41
|
-
if (buf.subarray(0, 4).toString('ascii') === 'RIFF' && buf.subarray(8, 12).toString('ascii') === 'WEBP') return 'webp';
|
|
42
|
-
const head = buf.subarray(0, 400).toString('utf8').trimStart().toLowerCase();
|
|
43
|
-
if (head.startsWith('<svg') || head.startsWith('<?xml')) return 'svg';
|
|
44
|
-
return null; // an HTML error page lands here, which is the whole point
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// The workspace's own installed engine, run directly: a workspace need not define an npm `dt` script.
|
|
48
|
-
function engineBin(root) {
|
|
49
|
-
const pkgPath = path.join(root, 'node_modules', 'dreamteamer', 'package.json');
|
|
50
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
51
|
-
const bin = typeof pkg.bin === 'string' ? pkg.bin : (pkg.bin.dreamteamer ?? Object.values(pkg.bin)[0]);
|
|
52
|
-
return path.join(path.dirname(pkgPath), bin);
|
|
53
|
-
}
|
|
54
|
-
const dt = (...args) => execFileSync(process.execPath, [engineBin(WORKSPACE), ...args], { cwd: WORKSPACE, encoding: 'utf8' });
|
|
55
|
-
|
|
56
|
-
const rows = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
57
|
-
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'avatars-'));
|
|
58
|
-
let ok = 0, skipped = 0, failed = 0;
|
|
59
|
-
|
|
60
|
-
for (const row of rows) {
|
|
61
|
-
const { id, photo } = row ?? {};
|
|
62
|
-
if (!id) { console.log(`✖ entry with no id: ${JSON.stringify(row)}`); failed++; continue; }
|
|
63
|
-
if (!photo) { console.log(`· ${id}: no photo${row.note ? ` (${row.note})` : ''}`); skipped++; continue; }
|
|
64
|
-
if (!/^https:\/\/media\.licdn\.com\//.test(photo)) {
|
|
65
|
-
console.log(`✖ ${id}: not a media.licdn.com URL — a ghost avatar or the wrong link`); failed++; continue;
|
|
66
|
-
}
|
|
67
|
-
let buf;
|
|
68
|
-
try {
|
|
69
|
-
const res = await fetch(photo);
|
|
70
|
-
if (!res.ok) { console.log(`✖ ${id}: HTTP ${res.status}`); failed++; continue; }
|
|
71
|
-
buf = Buffer.from(await res.arrayBuffer());
|
|
72
|
-
} catch (e) { console.log(`✖ ${id}: ${e.message}`); failed++; continue; }
|
|
73
|
-
|
|
74
|
-
const ext = extOf(buf);
|
|
75
|
-
if (!ext) { console.log(`✖ ${id}: ${buf.length} bytes that are not an image (an error page?)`); failed++; continue; }
|
|
76
|
-
if (buf.length > MAX) { console.log(`✖ ${id}: ${buf.length} bytes, over max_bytes ${MAX}`); failed++; continue; }
|
|
77
|
-
|
|
78
|
-
const out = path.join(tmp, `${id.replace(/\//g, '_')}.${ext}`);
|
|
79
|
-
fs.writeFileSync(out, buf);
|
|
80
|
-
if (dry) { console.log(`· ${id}: would import ${buf.length} bytes (.${ext})`); ok++; continue; }
|
|
81
|
-
try {
|
|
82
|
-
dt('add', 'assets', `avatars/${id}`, '--from', out, '--force');
|
|
83
|
-
dt('set', `contacts/${id}`, `avatar=assets/avatars/${id}`);
|
|
84
|
-
console.log(`✔ ${id}: ${buf.length} bytes (.${ext})`);
|
|
85
|
-
ok++;
|
|
86
|
-
} catch (e) { console.log(`✖ ${id}: ${String(e.stderr ?? e.message).trim().split('\n')[0]}`); failed++; }
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
console.log(`\n${ok} imported · ${skipped} had no photo · ${failed} failed`);
|
|
90
|
-
if (!dry && ok) console.log(`then: npx dt check, and commit with\n npx dt commit ${rows.filter((r) => r.photo).map((r) => `assets/avatars/${r.id} contacts/${r.id}`).slice(0, 3).join(' ')} …`);
|
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* li.mjs — drive linkedin-mcp-server for ONE batch, in ONE ephemeral container, then exit.
|
|
4
|
-
*
|
|
5
|
-
* Why this exists instead of an `.mcp.json` entry: the LinkedIn session is a SINGLETON. A stdio MCP
|
|
6
|
-
* entry spawns one container per agent session, and two containers on the one ~/.linkedin-mcp
|
|
7
|
-
* profile make LinkedIn invalidate the cookie — which costs a host re-login, not a retry. A stdio
|
|
8
|
-
* server cannot be shared between clients (one stdin/stdout pair), and a long-lived HTTP server would
|
|
9
|
-
* be a daemon holding a live session. So: no registered server, no idle container. The lock below is
|
|
10
|
-
* what makes "exactly one" true.
|
|
11
|
-
*
|
|
12
|
-
* Usage
|
|
13
|
-
* node li.mjs <tool> '<jsonArgs>' one call
|
|
14
|
-
* echo '[{"tool":..,"args":{..}}, ...]' | node li.mjs --stdin a batch, one container
|
|
15
|
-
* node li.mjs --doctor is the session alive? (no LinkedIn request)
|
|
16
|
-
*
|
|
17
|
-
* Always prints a JSON array of {tool, args, ok, result|error} to stdout; logs go to stderr.
|
|
18
|
-
* close_session is called for you at the end of every batch.
|
|
19
|
-
*/
|
|
20
|
-
import { spawn, execFileSync } from 'node:child_process';
|
|
21
|
-
import fs from 'node:fs';
|
|
22
|
-
import os from 'node:os';
|
|
23
|
-
import path from 'node:path';
|
|
24
|
-
|
|
25
|
-
const IMAGE = 'stickerdaniel/linkedin-mcp-server:latest';
|
|
26
|
-
const PROFILE = path.join(os.homedir(), '.linkedin-mcp');
|
|
27
|
-
const LOCK = path.join(PROFILE, 'linkedin-batch.lock'); // ours; the server owns profile.lock
|
|
28
|
-
const STALE_MS = 30 * 60 * 1000;
|
|
29
|
-
const CALL_TIMEOUT_MS = 300_000;
|
|
30
|
-
|
|
31
|
-
// ---------- the lock: a directory, because mkdir is atomic on every filesystem -------------------
|
|
32
|
-
function acquire() {
|
|
33
|
-
try {
|
|
34
|
-
fs.mkdirSync(LOCK);
|
|
35
|
-
} catch (e) {
|
|
36
|
-
if (e.code !== 'EEXIST') throw e;
|
|
37
|
-
const info = readLock();
|
|
38
|
-
if (info && Date.now() - info.at < STALE_MS && alive(info.pid)) {
|
|
39
|
-
throw new Error(
|
|
40
|
-
`another LinkedIn batch is running (pid ${info.pid}, started ${new Date(info.at).toISOString()}).\n`
|
|
41
|
-
+ 'Wait for it. Two containers on one profile invalidate the LinkedIn session — that costs a\n'
|
|
42
|
-
+ `host re-login, not a retry. If you are certain it is dead: rm -rf ${LOCK}`,
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
console.error(`[li] clearing a stale lock (${info ? `pid ${info.pid}` : 'unreadable'})`);
|
|
46
|
-
fs.rmSync(LOCK, { recursive: true, force: true });
|
|
47
|
-
fs.mkdirSync(LOCK);
|
|
48
|
-
}
|
|
49
|
-
fs.writeFileSync(path.join(LOCK, 'owner.json'), JSON.stringify({ pid: process.pid, at: Date.now() }));
|
|
50
|
-
}
|
|
51
|
-
const readLock = () => {
|
|
52
|
-
try { return JSON.parse(fs.readFileSync(path.join(LOCK, 'owner.json'), 'utf8')); } catch { return null; }
|
|
53
|
-
};
|
|
54
|
-
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
|
55
|
-
const release = () => { try { fs.rmSync(LOCK, { recursive: true, force: true }); } catch { /* gone */ } };
|
|
56
|
-
|
|
57
|
-
/** read one key from the nearest ancestor .env — the workspace convention for machine config */
|
|
58
|
-
function readEnvFile(key) {
|
|
59
|
-
let dir = process.cwd();
|
|
60
|
-
for (let i = 0; i < 6; i++) {
|
|
61
|
-
const f = path.join(dir, '.env');
|
|
62
|
-
if (fs.existsSync(f)) {
|
|
63
|
-
const m = fs.readFileSync(f, 'utf8').match(new RegExp(`^${key}=(.*)$`, 'm'));
|
|
64
|
-
if (m) return m[1].trim().replace(/^["']|["']$/g, '');
|
|
65
|
-
}
|
|
66
|
-
const up = path.dirname(dir);
|
|
67
|
-
if (up === dir) break;
|
|
68
|
-
dir = up;
|
|
69
|
-
}
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// ---------- doctor: answer "is the session alive" from disk, spending no LinkedIn request --------
|
|
74
|
-
function doctor() {
|
|
75
|
-
const out = { profile_dir: PROFILE, exists: fs.existsSync(PROFILE) };
|
|
76
|
-
if (!out.exists) {
|
|
77
|
-
out.verdict = 'no profile — run: uvx mcp-server-linkedin@latest --import-from-browser';
|
|
78
|
-
return out;
|
|
79
|
-
}
|
|
80
|
-
out.cookies = fs.existsSync(path.join(PROFILE, 'cookies.json'));
|
|
81
|
-
try {
|
|
82
|
-
const st = JSON.parse(fs.readFileSync(path.join(PROFILE, 'source-state.json'), 'utf8'));
|
|
83
|
-
out.login_at = st.created_at;
|
|
84
|
-
out.age_days = +((Date.now() - Date.parse(st.created_at)) / 864e5).toFixed(1);
|
|
85
|
-
} catch { out.login_at = null; }
|
|
86
|
-
|
|
87
|
-
// the newest trace run's post-import screenshot is the ground truth: a real page is ~36KB,
|
|
88
|
-
// a bounce to the sign-in wall leaves it blank at ~4KB.
|
|
89
|
-
const loginMs = out.login_at ? Date.parse(out.login_at) : 0;
|
|
90
|
-
const runs = path.join(PROFILE, 'trace-runs');
|
|
91
|
-
if (fs.existsSync(runs)) {
|
|
92
|
-
// newest FIRST, but skip runs with no screenshot — a container killed before it scraped
|
|
93
|
-
// leaves an empty trace dir, and letting that mask the real evidence is how you conclude
|
|
94
|
-
// "no information" while the answer sits one directory down.
|
|
95
|
-
const ordered = fs.readdirSync(runs)
|
|
96
|
-
.map((d) => ({ d, m: fs.statSync(path.join(runs, d)).mtimeMs }))
|
|
97
|
-
.sort((a, b) => b.m - a.m);
|
|
98
|
-
out.newest_trace = ordered[0]?.d ?? null;
|
|
99
|
-
const withShot = ordered.find((r) => fs.existsSync(path.join(runs, r.d, 'screens', '004-feed-after-goto.png')));
|
|
100
|
-
if (withShot) {
|
|
101
|
-
const dir = path.join(runs, withShot.d, 'screens');
|
|
102
|
-
const bytes = fs.statSync(path.join(dir, '004-feed-after-goto.png')).size;
|
|
103
|
-
out.evidence_from = withShot.d;
|
|
104
|
-
out.last_feed_screenshot_bytes = bytes;
|
|
105
|
-
// A verdict from a run that PREDATES the current login is about the previous session, so it
|
|
106
|
-
// must not be reported as the current one.
|
|
107
|
-
if (loginMs && withShot.m < loginMs) {
|
|
108
|
-
out.evidence_is_stale = `trace is ${Math.round((loginMs - withShot.m) / 1000)}s older than the login`;
|
|
109
|
-
out.verdict = 'login is NEWER than any scraping evidence — session untested. Just run the batch.';
|
|
110
|
-
} else {
|
|
111
|
-
out.verdict = bytes < 10_000
|
|
112
|
-
? 'session looks DEAD (blank feed after cookie import). Fix: uvx mcp-server-linkedin@latest '
|
|
113
|
-
+ `--import-from-browser. Confirm by eye: ${path.join(dir, '003-bridge-after-cookie-import.png')}`
|
|
114
|
-
: 'last scraping run reached a real feed page — session was healthy then';
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
out.verdict ??= 'no trace evidence yet; the first call will tell you';
|
|
119
|
-
const held = fs.existsSync(LOCK) ? readLock() : null;
|
|
120
|
-
out.batch_lock = !held ? 'free'
|
|
121
|
-
: alive(held.pid) && Date.now() - held.at < STALE_MS ? `HELD by pid ${held.pid}`
|
|
122
|
-
: `stale (pid ${held.pid} is gone) — the next batch clears it automatically`;
|
|
123
|
-
out.containers_running = execFileSync('docker',
|
|
124
|
-
['ps', '--filter', `ancestor=${IMAGE}`, '--format', '{{.ID}}']).toString().trim().split('\n').filter(Boolean).length;
|
|
125
|
-
Object.assign(out, strandedClients());
|
|
126
|
-
return out;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Stranded HOST-side clients. A `mcp-server-linkedin` process that never exited keeps the flock on
|
|
131
|
-
* `~/.linkedin-mcp/profile.lock`, and while it lives every `--import-from-browser` and `--login`
|
|
132
|
-
* refuses with `BrowserBusyError` — so a dead-session verdict must not send you to re-login.
|
|
133
|
-
*
|
|
134
|
-
* This greps `ps` rather than reading the lock: macOS `lsof` shows an empty lock field even for a
|
|
135
|
-
* held flock, and Docker Desktop's VM always has the file open because `~/.linkedin-mcp` is
|
|
136
|
-
* bind-mounted into the container.
|
|
137
|
-
*
|
|
138
|
-
* flock is released by the KERNEL when its holder dies, so anything found here is alive and `kill`
|
|
139
|
-
* is the whole fix.
|
|
140
|
-
*/
|
|
141
|
-
function strandedClients() {
|
|
142
|
-
let lines = [];
|
|
143
|
-
try {
|
|
144
|
-
lines = execFileSync('ps', ['-axo', 'pid=,lstart=,command='], { stdio: ['ignore', 'pipe', 'ignore'] })
|
|
145
|
-
.toString().split('\n')
|
|
146
|
-
.filter((l) => /mcp-server-linkedin|patchright-browsers/.test(l) && !/\bgrep\b/.test(l));
|
|
147
|
-
} catch { return { host_clients: 'unknown (ps failed)' }; }
|
|
148
|
-
if (!lines.length) return { host_clients: 'none' };
|
|
149
|
-
// The chromium tree is noise once its parent is named; report the SERVER processes, and count
|
|
150
|
-
// the browsers so a half-dead tree is still visible.
|
|
151
|
-
const servers = lines.filter((l) => /mcp-server-linkedin/.test(l)).map((l) => {
|
|
152
|
-
const m = /^\s*(\d+)\s+(.{24})\s+(.*)$/.exec(l);
|
|
153
|
-
return m ? { pid: Number(m[1]), started: m[2].trim() } : null;
|
|
154
|
-
}).filter(Boolean);
|
|
155
|
-
const browsers = lines.filter((l) => /patchright-browsers/.test(l)).length;
|
|
156
|
-
if (!servers.length) return { host_clients: `${browsers} orphaned patchright browser process(es), no server — kill them before logging in` };
|
|
157
|
-
return {
|
|
158
|
-
host_clients: `HELD by ${servers.map((sv) => `pid ${sv.pid} (started ${sv.started})`).join(', ')}, ${browsers} browser process(es)`,
|
|
159
|
-
verdict_override: `a LinkedIn MCP process still holds the browser lease, so --login and --import-from-browser BOTH refuse with BrowserBusyError. Fix: kill ${servers.map((sv) => sv.pid).join(' ')}`,
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// ---------- one container, N calls ---------------------------------------------------------------
|
|
164
|
-
async function runBatch(calls) {
|
|
165
|
-
// USER_AGENT exists to reduce anti-scraping detection: the session is minted on the host and used
|
|
166
|
-
// from a Linux container. Set LINKEDIN_USER_AGENT in .env to the browser the session came from.
|
|
167
|
-
const ua = process.env.LINKEDIN_USER_AGENT || readEnvFile('LINKEDIN_USER_AGENT');
|
|
168
|
-
if (!ua) console.error('[li] no LINKEDIN_USER_AGENT set — see the skill; this may raise detection');
|
|
169
|
-
const args = [
|
|
170
|
-
'run', '--rm', '-i', '--init', '--security-opt', 'no-new-privileges',
|
|
171
|
-
'-v', `${PROFILE}:/home/pwuser/.linkedin-mcp`,
|
|
172
|
-
'-e', 'TIMEOUT=10000', '-e', 'TOOL_TIMEOUT=300',
|
|
173
|
-
...(ua ? ['-e', `USER_AGENT=${ua}`] : []),
|
|
174
|
-
IMAGE,
|
|
175
|
-
];
|
|
176
|
-
const p = spawn('docker', args, { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
177
|
-
const pending = new Map();
|
|
178
|
-
let buf = '';
|
|
179
|
-
let nextId = 1;
|
|
180
|
-
|
|
181
|
-
p.stdout.on('data', (d) => {
|
|
182
|
-
buf += d;
|
|
183
|
-
let i;
|
|
184
|
-
while ((i = buf.indexOf('\n')) !== -1) {
|
|
185
|
-
const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
|
|
186
|
-
if (!line) continue;
|
|
187
|
-
let m; try { m = JSON.parse(line); } catch { continue; }
|
|
188
|
-
const r = pending.get(m.id);
|
|
189
|
-
if (r) { pending.delete(m.id); r(m); }
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
p.stderr.on('data', (d) => {
|
|
193
|
-
const s = String(d);
|
|
194
|
-
if (/ERROR|WARNING|Traceback|session/i.test(s)) process.stderr.write('[server] ' + s);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
const rpc = (method, params) => new Promise((resolve, reject) => {
|
|
198
|
-
const id = nextId++;
|
|
199
|
-
pending.set(id, resolve);
|
|
200
|
-
const t = setTimeout(() => { pending.delete(id); reject(new Error(`timeout after ${CALL_TIMEOUT_MS}ms`)); }, CALL_TIMEOUT_MS);
|
|
201
|
-
const done = (v) => { clearTimeout(t); resolve(v); };
|
|
202
|
-
pending.set(id, done);
|
|
203
|
-
p.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n');
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
const init = await rpc('initialize', {
|
|
207
|
-
protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'dt-li', version: '1' },
|
|
208
|
-
});
|
|
209
|
-
console.error(`[li] ${init.result?.serverInfo?.name} ${init.result?.serverInfo?.version} — ${calls.length} call(s)`);
|
|
210
|
-
|
|
211
|
-
const out = [];
|
|
212
|
-
for (const [n, c] of calls.entries()) {
|
|
213
|
-
console.error(`[li] ${n + 1}/${calls.length} ${c.tool} ${JSON.stringify(c.args ?? {})}`);
|
|
214
|
-
let row = { tool: c.tool, args: c.args ?? {} };
|
|
215
|
-
try {
|
|
216
|
-
const m = await rpc('tools/call', { name: c.tool, arguments: c.args ?? {} });
|
|
217
|
-
if (m.error) row = { ...row, ok: false, error: m.error };
|
|
218
|
-
else {
|
|
219
|
-
const text = (m.result?.content ?? []).filter((x) => x.type === 'text').map((x) => x.text).join('\n');
|
|
220
|
-
let parsed; try { parsed = JSON.parse(text); } catch { parsed = text; }
|
|
221
|
-
// a non-error response can still carry per-section failures — surface them
|
|
222
|
-
const partial = parsed?.section_errors ? Object.keys(parsed.section_errors) : [];
|
|
223
|
-
row = { ...row, ok: !m.result?.isError, result: parsed, ...(partial.length ? { failed_sections: partial } : {}) };
|
|
224
|
-
}
|
|
225
|
-
} catch (e) {
|
|
226
|
-
row = { ...row, ok: false, error: e.message };
|
|
227
|
-
}
|
|
228
|
-
out.push(row);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
try { await rpc('tools/call', { name: 'close_session', arguments: {} }); console.error('[li] session closed'); }
|
|
232
|
-
catch { console.error('[li] close_session did not answer (container is exiting anyway)'); }
|
|
233
|
-
p.kill();
|
|
234
|
-
return out;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// ---------- main --------------------------------------------------------------------------------
|
|
238
|
-
const argv = process.argv.slice(2);
|
|
239
|
-
if (argv[0] === '--doctor') { console.log(JSON.stringify(doctor(), null, 2)); process.exit(0); }
|
|
240
|
-
|
|
241
|
-
let calls;
|
|
242
|
-
if (argv[0] === '--stdin') {
|
|
243
|
-
calls = JSON.parse(fs.readFileSync(0, 'utf8'));
|
|
244
|
-
if (!Array.isArray(calls)) { console.error('--stdin wants a JSON array of {tool, args}'); process.exit(2); }
|
|
245
|
-
} else if (argv[0]) {
|
|
246
|
-
calls = [{ tool: argv[0], args: argv[1] ? JSON.parse(argv[1]) : {} }];
|
|
247
|
-
} else {
|
|
248
|
-
console.error(`usage:
|
|
249
|
-
li.mjs <tool> '<jsonArgs>'
|
|
250
|
-
echo '[{"tool":"get_person_profile","args":{"linkedin_username":"dana-oren"}}]' | li.mjs --stdin
|
|
251
|
-
li.mjs --doctor
|
|
252
|
-
|
|
253
|
-
NEVER call connect_with_person or send_message — they act publicly as you.`);
|
|
254
|
-
process.exit(2);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
const BANNED = new Set(['connect_with_person', 'send_message']);
|
|
258
|
-
const bad = calls.filter((c) => BANNED.has(c.tool));
|
|
259
|
-
if (bad.length) {
|
|
260
|
-
console.error(`refusing ${bad.map((b) => b.tool).join(', ')} — these act publicly and irreversibly as`
|
|
261
|
-
+ ' you. If it is genuinely wanted, do it by hand, not through a scraper.');
|
|
262
|
-
process.exit(3);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
try {
|
|
266
|
-
acquire();
|
|
267
|
-
} catch (e) {
|
|
268
|
-
// a contended lock is an expected outcome, not a crash — say so without a stack trace
|
|
269
|
-
console.error(`[li] ${e.message}`);
|
|
270
|
-
process.exit(4);
|
|
271
|
-
}
|
|
272
|
-
let code = 0;
|
|
273
|
-
try {
|
|
274
|
-
const results = await runBatch(calls);
|
|
275
|
-
console.log(JSON.stringify(results, null, 2));
|
|
276
|
-
code = results.every((r) => r.ok) ? 0 : 1;
|
|
277
|
-
} catch (e) {
|
|
278
|
-
console.error(`[li] batch failed: ${e.message}`);
|
|
279
|
-
code = 1;
|
|
280
|
-
} finally {
|
|
281
|
-
// MUST release before exiting: process.exit() does NOT run finally blocks, so an exit inside
|
|
282
|
-
// the try would leave the lock held after the batch is gone.
|
|
283
|
-
release();
|
|
284
|
-
}
|
|
285
|
-
process.exit(code);
|
|
File without changes
|