@webcrawl/mcp 1.4.4 → 1.4.5
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/core/engine/chrome-cdp.d.ts +7 -0
- package/dist/core/engine/chrome-cdp.js +26 -5
- package/dist/core/engine/chrome-launch.d.ts +11 -0
- package/dist/core/engine/chrome-launch.js +30 -0
- package/dist/core/engine/chrome.d.ts +6 -1
- package/dist/core/engine/chrome.js +14 -5
- package/package.json +3 -3
|
@@ -7,6 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { spawn } from "node:child_process";
|
|
9
9
|
import { type ChromeHeadlessMode } from "./chrome-launch.js";
|
|
10
|
+
/** Chrome spawned but CDP never became ready. `hung` is true when the process was still alive. */
|
|
11
|
+
export declare class ChromeConnectError extends Error {
|
|
12
|
+
readonly hung: boolean;
|
|
13
|
+
constructor(message: string, hung?: boolean);
|
|
14
|
+
}
|
|
10
15
|
export interface CdpSnapshot {
|
|
11
16
|
html: string;
|
|
12
17
|
title: string;
|
|
@@ -15,6 +20,8 @@ export interface CdpSnapshot {
|
|
|
15
20
|
}
|
|
16
21
|
export interface CdpRenderOptions {
|
|
17
22
|
timeoutMs: number;
|
|
23
|
+
/** CDP launch/connect budget. Defaults to 4s (`WEBCRAWL_CHROME_CONNECT_TIMEOUT_MS`). */
|
|
24
|
+
connectTimeoutMs?: number;
|
|
18
25
|
headless: ChromeHeadlessMode;
|
|
19
26
|
/** Injected for tests. */
|
|
20
27
|
spawnChrome?: typeof spawn;
|
|
@@ -9,7 +9,16 @@ import { spawn } from "node:child_process";
|
|
|
9
9
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
12
|
-
import { chromeCdpArgs, killProcessGroup } from "./chrome-launch.js";
|
|
12
|
+
import { chromeCdpArgs, killProcessGroup, resolveChromeConnectTimeoutMs, } from "./chrome-launch.js";
|
|
13
|
+
/** Chrome spawned but CDP never became ready. `hung` is true when the process was still alive. */
|
|
14
|
+
export class ChromeConnectError extends Error {
|
|
15
|
+
hung;
|
|
16
|
+
constructor(message, hung = false) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "ChromeConnectError";
|
|
19
|
+
this.hung = hung;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
13
22
|
const SNAPSHOT_JS = `(() => {
|
|
14
23
|
const body = document.body;
|
|
15
24
|
const text = body && typeof body.innerText === "string" ? body.innerText.trim() : "";
|
|
@@ -64,8 +73,12 @@ const WAIT_FOR_CONTENT_JS = `(async () => {
|
|
|
64
73
|
try { observer && observer.disconnect(); } catch (_) { /* ignore */ }
|
|
65
74
|
return last;
|
|
66
75
|
})()`;
|
|
76
|
+
function childStillRunning(child) {
|
|
77
|
+
return Boolean(child && child.pid && child.exitCode == null && !child.killed);
|
|
78
|
+
}
|
|
67
79
|
export async function renderUrlViaCdp(chromePath, url, opts) {
|
|
68
80
|
const timeoutMs = Math.max(1_500, opts.timeoutMs);
|
|
81
|
+
const connectTimeoutMs = resolveChromeConnectTimeoutMs(opts.connectTimeoutMs);
|
|
69
82
|
const profile = mkdtempSync(join(tmpdir(), "wc-cdp-"));
|
|
70
83
|
const port = 40000 + Math.floor(Math.random() * 20000);
|
|
71
84
|
const spawnFn = opts.spawnChrome ?? spawn;
|
|
@@ -76,7 +89,9 @@ export async function renderUrlViaCdp(chromePath, url, opts) {
|
|
|
76
89
|
binaryPath: chromePath,
|
|
77
90
|
});
|
|
78
91
|
let child;
|
|
92
|
+
let cdpReady = false;
|
|
79
93
|
const ac = new AbortController();
|
|
94
|
+
// Page budget covers navigation after connect; connect has its own deadline.
|
|
80
95
|
const timer = setTimeout(() => ac.abort(), timeoutMs);
|
|
81
96
|
try {
|
|
82
97
|
child = spawnFn(chromePath, args, {
|
|
@@ -86,12 +101,11 @@ export async function renderUrlViaCdp(chromePath, url, opts) {
|
|
|
86
101
|
if (!child.pid) {
|
|
87
102
|
throw new Error("Chrome failed to start (no pid)");
|
|
88
103
|
}
|
|
89
|
-
let cdpReady = false;
|
|
90
104
|
child.once("exit", () => {
|
|
91
105
|
if (!cdpReady && !ac.signal.aborted)
|
|
92
106
|
ac.abort();
|
|
93
107
|
});
|
|
94
|
-
const wsUrl = await waitForPageWebSocket(port,
|
|
108
|
+
const wsUrl = await waitForPageWebSocket(port, connectTimeoutMs, ac.signal);
|
|
95
109
|
cdpReady = true;
|
|
96
110
|
const snapshot = await withCdpSession(wsUrl, url, timeoutMs, ac.signal);
|
|
97
111
|
if (!snapshot.html || snapshot.html.length < 40) {
|
|
@@ -100,6 +114,13 @@ export async function renderUrlViaCdp(chromePath, url, opts) {
|
|
|
100
114
|
return snapshot;
|
|
101
115
|
}
|
|
102
116
|
catch (err) {
|
|
117
|
+
if (!cdpReady) {
|
|
118
|
+
const hung = childStillRunning(child);
|
|
119
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
120
|
+
throw new ChromeConnectError(hung
|
|
121
|
+
? `Chrome did not respond over CDP within ${connectTimeoutMs}ms`
|
|
122
|
+
: `Chrome CDP failed to start (${detail})`, hung);
|
|
123
|
+
}
|
|
103
124
|
if (ac.signal.aborted) {
|
|
104
125
|
throw new Error(`CDP timed out after ${timeoutMs}ms`);
|
|
105
126
|
}
|
|
@@ -117,8 +138,8 @@ export async function renderUrlViaCdp(chromePath, url, opts) {
|
|
|
117
138
|
}
|
|
118
139
|
}
|
|
119
140
|
}
|
|
120
|
-
async function waitForPageWebSocket(port,
|
|
121
|
-
const deadline = Date.now() +
|
|
141
|
+
async function waitForPageWebSocket(port, connectTimeoutMs, signal) {
|
|
142
|
+
const deadline = Date.now() + connectTimeoutMs;
|
|
122
143
|
let lastErr = "CDP HTTP endpoint never became ready";
|
|
123
144
|
while (Date.now() < deadline) {
|
|
124
145
|
if (signal.aborted)
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import type { ChildProcess } from "node:child_process";
|
|
2
2
|
export declare const DUMP_MAX_BYTES: number;
|
|
3
|
+
/** Page navigation + DOM-wait budget once Chrome is answering over CDP. */
|
|
3
4
|
export declare const DEFAULT_TIMEOUT_MS = 18000;
|
|
5
|
+
/**
|
|
6
|
+
* How long to wait for Chrome to accept CDP after spawn. Kept short and
|
|
7
|
+
* separate from `DEFAULT_TIMEOUT_MS` so a hung binary fails fast while a
|
|
8
|
+
* slow but working JS page (often ~7s) still has time to render.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_CONNECT_TIMEOUT_MS = 4000;
|
|
4
11
|
export declare const VIRTUAL_TIME_MS = 10000;
|
|
12
|
+
/** `opts.timeoutMs`, else `WEBCRAWL_CHROME_TIMEOUT_MS`, else 18s. */
|
|
13
|
+
export declare function resolveChromeTimeoutMs(override?: number): number;
|
|
14
|
+
/** `opts.connectTimeoutMs`, else `WEBCRAWL_CHROME_CONNECT_TIMEOUT_MS`, else 4s. */
|
|
15
|
+
export declare function resolveChromeConnectTimeoutMs(override?: number): number;
|
|
5
16
|
export type ChromeHeadlessMode = "new" | "old" | "shell";
|
|
6
17
|
export declare function killProcessGroup(pid: number | undefined, child?: Pick<ChildProcess, "kill">): void;
|
|
7
18
|
export declare function headlessFlag(mode: ChromeHeadlessMode, binaryPath: string): string[];
|
|
@@ -5,8 +5,38 @@ import { existsSync, readdirSync } from "node:fs";
|
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
6
|
import { join } from "node:path";
|
|
7
7
|
export const DUMP_MAX_BYTES = 2 * 1024 * 1024;
|
|
8
|
+
/** Page navigation + DOM-wait budget once Chrome is answering over CDP. */
|
|
8
9
|
export const DEFAULT_TIMEOUT_MS = 18_000;
|
|
10
|
+
/**
|
|
11
|
+
* How long to wait for Chrome to accept CDP after spawn. Kept short and
|
|
12
|
+
* separate from `DEFAULT_TIMEOUT_MS` so a hung binary fails fast while a
|
|
13
|
+
* slow but working JS page (often ~7s) still has time to render.
|
|
14
|
+
*/
|
|
15
|
+
export const DEFAULT_CONNECT_TIMEOUT_MS = 4_000;
|
|
9
16
|
export const VIRTUAL_TIME_MS = 10_000;
|
|
17
|
+
function envPositiveMs(name, fallback, min) {
|
|
18
|
+
const raw = process.env[name];
|
|
19
|
+
if (raw == null || raw.trim() === "")
|
|
20
|
+
return fallback;
|
|
21
|
+
const n = Number(raw);
|
|
22
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
23
|
+
return fallback;
|
|
24
|
+
return Math.max(min, n);
|
|
25
|
+
}
|
|
26
|
+
/** `opts.timeoutMs`, else `WEBCRAWL_CHROME_TIMEOUT_MS`, else 18s. */
|
|
27
|
+
export function resolveChromeTimeoutMs(override) {
|
|
28
|
+
if (typeof override === "number" && Number.isFinite(override) && override > 0) {
|
|
29
|
+
return Math.max(1_500, override);
|
|
30
|
+
}
|
|
31
|
+
return envPositiveMs("WEBCRAWL_CHROME_TIMEOUT_MS", DEFAULT_TIMEOUT_MS, 1_500);
|
|
32
|
+
}
|
|
33
|
+
/** `opts.connectTimeoutMs`, else `WEBCRAWL_CHROME_CONNECT_TIMEOUT_MS`, else 4s. */
|
|
34
|
+
export function resolveChromeConnectTimeoutMs(override) {
|
|
35
|
+
if (typeof override === "number" && Number.isFinite(override) && override > 0) {
|
|
36
|
+
return Math.max(500, override);
|
|
37
|
+
}
|
|
38
|
+
return envPositiveMs("WEBCRAWL_CHROME_CONNECT_TIMEOUT_MS", DEFAULT_CONNECT_TIMEOUT_MS, 500);
|
|
39
|
+
}
|
|
10
40
|
export function killProcessGroup(pid, child) {
|
|
11
41
|
if (pid && pid > 0) {
|
|
12
42
|
try {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { DEFAULT_TIMEOUT_MS, VIRTUAL_TIME_MS, discoverChromeBinaries, findPlaywrightHeadlessShell, installedChromePath, isLocalChromeAvailable, parseDevToolsActivePort, chromeCdpArgs, chromeDumpDomArgs, killProcessGroup, type ChromeHeadlessMode, } from "./chrome-launch.js";
|
|
1
|
+
export { DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS, VIRTUAL_TIME_MS, discoverChromeBinaries, findPlaywrightHeadlessShell, installedChromePath, isLocalChromeAvailable, parseDevToolsActivePort, chromeCdpArgs, chromeDumpDomArgs, killProcessGroup, resolveChromeConnectTimeoutMs, resolveChromeTimeoutMs, type ChromeHeadlessMode, } from "./chrome-launch.js";
|
|
2
|
+
export { ChromeConnectError } from "./chrome-cdp.js";
|
|
2
3
|
export interface ChromeRenderAttempt {
|
|
3
4
|
ok: boolean;
|
|
4
5
|
method: string;
|
|
@@ -17,6 +18,8 @@ export interface ChromeRenderResult {
|
|
|
17
18
|
*/
|
|
18
19
|
export declare function renderViaLocalChrome(url: string, opts?: {
|
|
19
20
|
timeoutMs?: number;
|
|
21
|
+
connectTimeoutMs?: number;
|
|
22
|
+
binaries?: string[];
|
|
20
23
|
}): Promise<string | null>;
|
|
21
24
|
/**
|
|
22
25
|
* Same as `renderViaLocalChrome` but with the attempt log and an actionable
|
|
@@ -25,4 +28,6 @@ export declare function renderViaLocalChrome(url: string, opts?: {
|
|
|
25
28
|
*/
|
|
26
29
|
export declare function renderViaLocalChromeDetailed(url: string, opts?: {
|
|
27
30
|
timeoutMs?: number;
|
|
31
|
+
connectTimeoutMs?: number;
|
|
32
|
+
binaries?: string[];
|
|
28
33
|
}): Promise<ChromeRenderResult>;
|
|
@@ -15,9 +15,10 @@ import { spawn } from "node:child_process";
|
|
|
15
15
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
16
16
|
import { tmpdir } from "node:os";
|
|
17
17
|
import { join } from "node:path";
|
|
18
|
-
import { renderUrlViaCdp } from "./chrome-cdp.js";
|
|
19
|
-
import {
|
|
20
|
-
export { DEFAULT_TIMEOUT_MS, VIRTUAL_TIME_MS, discoverChromeBinaries, findPlaywrightHeadlessShell, installedChromePath, isLocalChromeAvailable, parseDevToolsActivePort, chromeCdpArgs, chromeDumpDomArgs, killProcessGroup, } from "./chrome-launch.js";
|
|
18
|
+
import { ChromeConnectError, renderUrlViaCdp } from "./chrome-cdp.js";
|
|
19
|
+
import { DUMP_MAX_BYTES, chromeDumpDomArgs, discoverChromeBinaries, killProcessGroup, resolveChromeConnectTimeoutMs, resolveChromeTimeoutMs, } from "./chrome-launch.js";
|
|
20
|
+
export { DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS, VIRTUAL_TIME_MS, discoverChromeBinaries, findPlaywrightHeadlessShell, installedChromePath, isLocalChromeAvailable, parseDevToolsActivePort, chromeCdpArgs, chromeDumpDomArgs, killProcessGroup, resolveChromeConnectTimeoutMs, resolveChromeTimeoutMs, } from "./chrome-launch.js";
|
|
21
|
+
export { ChromeConnectError } from "./chrome-cdp.js";
|
|
21
22
|
function attemptPlan(binary) {
|
|
22
23
|
const isShell = /headless[-_]?shell/i.test(binary);
|
|
23
24
|
if (isShell) {
|
|
@@ -51,7 +52,7 @@ export async function renderViaLocalChrome(url, opts = {}) {
|
|
|
51
52
|
* failures should use this.
|
|
52
53
|
*/
|
|
53
54
|
export async function renderViaLocalChromeDetailed(url, opts = {}) {
|
|
54
|
-
const binaries = discoverChromeBinaries();
|
|
55
|
+
const binaries = opts.binaries?.length ? opts.binaries : discoverChromeBinaries();
|
|
55
56
|
if (binaries.length === 0) {
|
|
56
57
|
return {
|
|
57
58
|
html: null,
|
|
@@ -60,15 +61,20 @@ export async function renderViaLocalChromeDetailed(url, opts = {}) {
|
|
|
60
61
|
attempts: [],
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
|
-
const budgetMs = opts.timeoutMs
|
|
64
|
+
const budgetMs = resolveChromeTimeoutMs(opts.timeoutMs);
|
|
65
|
+
const connectTimeoutMs = resolveChromeConnectTimeoutMs(opts.connectTimeoutMs);
|
|
64
66
|
const started = Date.now();
|
|
65
67
|
const attempts = [];
|
|
66
68
|
let lastError = "local Chrome produced no content";
|
|
67
69
|
for (const binary of binaries) {
|
|
70
|
+
let hungConnect = false;
|
|
68
71
|
for (const kind of attemptPlan(binary)) {
|
|
69
72
|
const remaining = budgetMs - (Date.now() - started);
|
|
70
73
|
if (remaining < 1_200)
|
|
71
74
|
break;
|
|
75
|
+
// A process that launched but never answered CDP will hang dump-dom too.
|
|
76
|
+
if (kind.driver === "dump-dom" && hungConnect)
|
|
77
|
+
continue;
|
|
72
78
|
const method = methodName(binary, kind);
|
|
73
79
|
const attemptBudget = kind.driver === "cdp"
|
|
74
80
|
? Math.min(remaining, kind.headless === "new" ? 12_000 : 8_000)
|
|
@@ -78,6 +84,7 @@ export async function renderViaLocalChromeDetailed(url, opts = {}) {
|
|
|
78
84
|
const html = kind.driver === "cdp"
|
|
79
85
|
? (await renderUrlViaCdp(binary, url, {
|
|
80
86
|
timeoutMs: attemptBudget,
|
|
87
|
+
connectTimeoutMs: Math.min(connectTimeoutMs, remaining),
|
|
81
88
|
headless: kind.headless,
|
|
82
89
|
})).html
|
|
83
90
|
: await dumpDom(binary, url, kind.headless, attemptBudget);
|
|
@@ -93,6 +100,8 @@ export async function renderViaLocalChromeDetailed(url, opts = {}) {
|
|
|
93
100
|
const ms = Date.now() - t0;
|
|
94
101
|
lastError = `${method}: ${err instanceof Error ? err.message : String(err)}`;
|
|
95
102
|
attempts.push({ ok: false, method, ms, error: lastError });
|
|
103
|
+
if (err instanceof ChromeConnectError && err.hung)
|
|
104
|
+
hungConnect = true;
|
|
96
105
|
}
|
|
97
106
|
}
|
|
98
107
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webcrawl/mcp",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
4
4
|
"mcpName": "io.github.brainfish-ai/webcrawl",
|
|
5
5
|
"description": "MCP server for Webcrawl Anything: search, scrape, map, crawl, parse and monitor the web from your agent. Runs locally with no API key, or on webcrawl.app cloud.",
|
|
6
6
|
"type": "module",
|
|
@@ -56,8 +56,8 @@
|
|
|
56
56
|
"@firecrawl/pdf-inspector": "^1.25.2",
|
|
57
57
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
58
58
|
"@vercel/sandbox": "^1.0.0",
|
|
59
|
-
"@webcrawl/client": "^1.4.
|
|
60
|
-
"@webcrawl/curl-impersonate": "^1.4.
|
|
59
|
+
"@webcrawl/client": "^1.4.5",
|
|
60
|
+
"@webcrawl/curl-impersonate": "^1.4.5",
|
|
61
61
|
"ai": "^7.0.150",
|
|
62
62
|
"impit": "^0.14.7",
|
|
63
63
|
"mammoth": "^1.13.0",
|