crawlforge-mcp-server 5.4.0 → 5.4.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crawlforge-mcp-server",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "mcpName": "io.github.mysleekdesigns/crawlforge-mcp-server",
5
5
  "description": "CrawlForge MCP Server - Professional Model Context Protocol server with 29 web scraping, crawling, deep-research, and autonomous-extraction tools. Returns clean Markdown and structured JSON for Claude, Cursor, and any MCP client. Defaults to local Ollama for LLM extraction (no API key needed); OpenAI/Anthropic available as opt-in. Includes a unified multi-format scrape tool, an autonomous agent, pre-built site templates, and Camoufox stealth browsing.",
6
6
  "main": "server.js",
@@ -114,7 +114,7 @@
114
114
  "cheerio": "^1.1.2",
115
115
  "commander": "^14.0.3",
116
116
  "compromise": "^14.14.4",
117
- "crawlforge-extractors": "^1.3.0",
117
+ "crawlforge-extractors": "^1.4.0",
118
118
  "diff": "^9.0.0",
119
119
  "dotenv": "^17.2.1",
120
120
  "franc": "^6.2.0",
@@ -9,8 +9,9 @@
9
9
  */
10
10
 
11
11
  import { fetchAndParse } from './_fetchAndParse.js';
12
- import { extractEmbeddedState } from '../../utils/embeddedState.js';
13
- import { selectJsonPath } from '../../utils/jsonPath.js';
12
+ // Both live in crawlforge-extractors so the REST API's extract_embedded_state
13
+ // runs this exact reader — one RSC flight-stream parser, not two.
14
+ import { extractEmbeddedState, selectJsonPath } from 'crawlforge-extractors';
14
15
 
15
16
  // Above this, an unscoped result is big enough to be a problem for the caller
16
17
  // (context window, transport) rather than just large. Warn — never truncate:
@@ -1,304 +0,0 @@
1
- /**
2
- * embeddedState.js — find the JSON state a page already ships in its own HTML.
3
- *
4
- * Modern SPAs serialize the data their UI renders into the document: Next.js
5
- * pages carry __NEXT_DATA__ or an RSC flight stream, Nuxt carries __NUXT__,
6
- * Apollo/Redux apps carry __APOLLO_STATE__ / __INITIAL_STATE__ /
7
- * __PRELOADED_STATE__. One fetch returns the exact values the site itself uses
8
- * — no LLM in the extraction path, so no fabricated prices.
9
- *
10
- * Pure: HTML in, named payloads out. No fetching, no cheerio — every source
11
- * lives inside a <script> tag, so one regex pass over script tags is enough
12
- * and avoids a second full parse of a multi-megabyte document.
13
- */
14
-
15
- /**
16
- * Global variable assignments we look for, in the order they are reported.
17
- * Each is matched with an optional window./self./globalThis. prefix.
18
- * `name` is the path-safe key the caller addresses with a JSON path — the
19
- * raw variable name has no dots, but keeping the two separate means a caller
20
- * never has to guess how "self.__next_f" would be spelled in a path.
21
- */
22
- const STATE_VARIABLES = [
23
- { name: 'nuxt', variable: '__NUXT__' },
24
- { name: 'apollo_state', variable: '__APOLLO_STATE__' },
25
- { name: 'initial_state', variable: '__INITIAL_STATE__' },
26
- { name: 'preloaded_state', variable: '__PRELOADED_STATE__' }
27
- ];
28
-
29
- // Script bodies cannot contain a literal "</script", so a non-greedy match is
30
- // exact here — the same assumption every HTML parser makes.
31
- const SCRIPT_RE = /<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi;
32
-
33
- const HTML_COMMENT_RE = /<!--[\s\S]*?-->/g;
34
-
35
- // self.__next_f.push([1,"<chunk>"]) — Next.js App Router RSC flight chunks.
36
- const NEXT_F_PUSH_RE = /self\.__next_f\.push\(\s*\[\s*1\s*,\s*/g;
37
-
38
- /**
39
- * Read an HTML attribute out of a raw tag's attribute string.
40
- * @param {string} attrs
41
- * @param {string} name
42
- * @returns {string|null}
43
- */
44
- function attr(attrs, name) {
45
- const match = attrs.match(
46
- new RegExp(`(?:^|\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, 'i')
47
- );
48
- if (!match) return null;
49
- return match[1] ?? match[2] ?? match[3] ?? null;
50
- }
51
-
52
- /**
53
- * Scan a double-quoted string literal starting at `start` and return its
54
- * parsed value plus the index just past its closing quote.
55
- * @param {string} text
56
- * @param {number} start index of the opening quote
57
- * @returns {{ value: string, end: number }|null}
58
- */
59
- function readStringLiteral(text, start) {
60
- if (text[start] !== '"') return null;
61
- let i = start + 1;
62
- while (i < text.length) {
63
- const ch = text[i];
64
- if (ch === '\\') { i += 2; continue; }
65
- if (ch === '"') break;
66
- i++;
67
- }
68
- if (i >= text.length) return null;
69
- try {
70
- return { value: JSON.parse(text.slice(start, i + 1)), end: i + 1 };
71
- } catch {
72
- return null;
73
- }
74
- }
75
-
76
- /**
77
- * Scan a balanced {...} / [...] literal starting at `start`, respecting string
78
- * literals and escapes so braces inside strings don't end it early.
79
- * @param {string} text
80
- * @param {number} start
81
- * @returns {string|null} the raw literal, or null if it never closes
82
- */
83
- function readBracketedLiteral(text, start) {
84
- const open = text[start];
85
- const close = open === '{' ? '}' : open === '[' ? ']' : null;
86
- if (!close) return null;
87
- let depth = 0;
88
- let i = start;
89
- while (i < text.length) {
90
- const ch = text[i];
91
- if (ch === '"' || ch === "'") {
92
- const quote = ch;
93
- i++;
94
- while (i < text.length && text[i] !== quote) {
95
- if (text[i] === '\\') i++;
96
- i++;
97
- }
98
- } else if (ch === open) {
99
- depth++;
100
- } else if (ch === close) {
101
- depth--;
102
- if (depth === 0) return text.slice(start, i + 1);
103
- }
104
- i++;
105
- }
106
- return null;
107
- }
108
-
109
- /**
110
- * Split a concatenated RSC flight stream into its rows.
111
- *
112
- * The stream is a sequence of `<hexId>:<payload>\n` rows. Three payload shapes
113
- * matter:
114
- * - `T<hexByteLength>,` — a length-prefixed text blob. The length is in
115
- * UTF-8 BYTES and INCLUDES the row's terminating newline, so the cursor
116
- * advances exactly that many bytes and no further. (Advancing one extra
117
- * character for a newline silently eats the first hex digit of the next
118
- * row id, turning row "14" into row "4" and overwriting an unrelated row —
119
- * verified against the live Healthgrades capture, where it produced seven
120
- * colliding ids.)
121
- * - `I[...]` / `HL[...]` — module and hint references. Not JSON; kept as the
122
- * raw string so the caller can still see which components a page loads.
123
- * - anything else — JSON, parsed.
124
- *
125
- * @param {string} stream
126
- * @returns {Record<string, unknown>} row id -> value
127
- */
128
- function parseFlightRows(stream) {
129
- const rows = {};
130
- let cursor = 0;
131
-
132
- while (cursor < stream.length) {
133
- const newline = stream.indexOf('\n', cursor);
134
- const lineEnd = newline === -1 ? stream.length : newline;
135
- const header = stream.slice(cursor, lineEnd).match(/^([0-9a-f]+):/i);
136
- if (!header) {
137
- // Not a row start: a chunk boundary landed mid-row, or the stream was
138
- // truncated. Resync on the next line rather than giving up.
139
- cursor = lineEnd + 1;
140
- continue;
141
- }
142
-
143
- const id = header[1];
144
- const payloadStart = cursor + header[0].length;
145
- const payload = stream.slice(payloadStart, lineEnd);
146
-
147
- const textRow = payload.match(/^T([0-9a-f]+),/i);
148
- if (textRow) {
149
- const blobStart = payloadStart + textRow[0].length;
150
- const text = Buffer.from(stream.slice(blobStart), 'utf8')
151
- .subarray(0, parseInt(textRow[1], 16))
152
- .toString('utf8');
153
- rows[id] = text;
154
- cursor = blobStart + text.length;
155
- continue;
156
- }
157
-
158
- try {
159
- rows[id] = JSON.parse(payload);
160
- } catch {
161
- rows[id] = payload;
162
- }
163
- cursor = lineEnd + 1;
164
- }
165
-
166
- return rows;
167
- }
168
-
169
- /**
170
- * Collect every `self.__next_f.push([1,"…"])` chunk in document order and
171
- * concatenate them into the flight stream they encode.
172
- * @param {string} html
173
- * @returns {{ chunks: number, stream: string }}
174
- */
175
- function readFlightStream(html) {
176
- const parts = [];
177
- NEXT_F_PUSH_RE.lastIndex = 0;
178
- let match;
179
- while ((match = NEXT_F_PUSH_RE.exec(html)) !== null) {
180
- const literal = readStringLiteral(html, NEXT_F_PUSH_RE.lastIndex);
181
- if (!literal) continue;
182
- parts.push(literal.value);
183
- NEXT_F_PUSH_RE.lastIndex = literal.end;
184
- }
185
- return { chunks: parts.length, stream: parts.join('') };
186
- }
187
-
188
- const serializedBytes = (value) => Buffer.byteLength(JSON.stringify(value) ?? '');
189
-
190
- /**
191
- * Extract every embedded state payload a page carries.
192
- *
193
- * @param {string} rawHtml raw HTML — NOT a script-stripped document
194
- * @returns {{ data: Record<string, unknown>, found: Array<{name: string, variable: string, bytes: number, note?: string}>, warnings: string[] }}
195
- */
196
- export function extractEmbeddedState(rawHtml) {
197
- const data = {};
198
- const found = [];
199
- const warnings = [];
200
- const jsonScripts = [];
201
-
202
- // Commented-out markup is not state the page renders from, and an opening
203
- // <script> tag inside a comment would otherwise match through to the first
204
- // real </script> — swallowing the genuine tag that follows it.
205
- const html = rawHtml.replace(HTML_COMMENT_RE, '');
206
-
207
- SCRIPT_RE.lastIndex = 0;
208
- let script;
209
- while ((script = SCRIPT_RE.exec(html)) !== null) {
210
- const [, attrs, body] = script;
211
- const type = (attr(attrs, 'type') || '').toLowerCase();
212
- if (type !== 'application/json') continue;
213
-
214
- const id = attr(attrs, 'id');
215
- let parsed;
216
- try {
217
- parsed = JSON.parse(body);
218
- } catch {
219
- warnings.push(
220
- `A <script type="application/json"${id ? ` id="${id}"` : ''}> block is not valid JSON; skipped.`
221
- );
222
- continue;
223
- }
224
-
225
- // __NEXT_DATA__ is a JSON script too. Reported under its own name so a
226
- // caller addressing "next_data" gets it, and not duplicated into
227
- // json_scripts — on a Next.js page that would double a multi-hundred-KB
228
- // payload.
229
- if (id === '__NEXT_DATA__') {
230
- data.next_data = parsed;
231
- found.push({
232
- name: 'next_data',
233
- variable: '__NEXT_DATA__',
234
- bytes: serializedBytes(parsed)
235
- });
236
- } else {
237
- jsonScripts.push({ id: id || null, data: parsed });
238
- }
239
- }
240
-
241
- const flight = readFlightStream(html);
242
- if (flight.chunks > 0) {
243
- const rows = parseFlightRows(flight.stream);
244
- data.next_f = rows;
245
- found.push({
246
- name: 'next_f',
247
- variable: 'self.__next_f',
248
- bytes: serializedBytes(rows),
249
- note: `${flight.chunks} RSC flight chunks concatenated into ${Object.keys(rows).length} rows, keyed by row id`
250
- });
251
- }
252
-
253
- for (const { name, variable } of STATE_VARIABLES) {
254
- const assignment = html.match(
255
- new RegExp(`(?:window|self|globalThis)?\\.?\\b${variable}\\s*=\\s*`)
256
- );
257
- if (!assignment) continue;
258
-
259
- const valueStart = assignment.index + assignment[0].length;
260
- const literal = readBracketedLiteral(html, valueStart);
261
- let parsed;
262
- if (literal !== null) {
263
- try {
264
- parsed = JSON.parse(literal);
265
- } catch {
266
- parsed = undefined;
267
- }
268
- }
269
-
270
- if (parsed === undefined) {
271
- // Nuxt 2 wraps its payload in an IIFE, and Nuxt 3 emits a bare JS object
272
- // literal with unquoted keys. Neither is JSON and neither is worth
273
- // eval()ing — say so instead of reporting a source we did not read.
274
- warnings.push(
275
- `${variable} is present but its value is not a JSON literal (a JS object literal or function-wrapped payload); not parsed.`
276
- );
277
- continue;
278
- }
279
-
280
- // Nuxt 3 emits `window.__NUXT__={}` and then fills it from a second,
281
- // non-JSON statement. Reporting the empty object without saying so reads
282
- // like "this page has no Nuxt state", which is the opposite of true.
283
- if (Object.keys(parsed).length === 0) {
284
- warnings.push(
285
- `${variable} is present but assigned an empty ${Array.isArray(parsed) ? 'array' : 'object'}; the page fills it in from a later statement this tool does not evaluate.`
286
- );
287
- }
288
-
289
- data[name] = parsed;
290
- found.push({ name, variable, bytes: serializedBytes(parsed) });
291
- }
292
-
293
- if (jsonScripts.length > 0) {
294
- data.json_scripts = jsonScripts;
295
- found.push({
296
- name: 'json_scripts',
297
- variable: 'script[type="application/json"]',
298
- bytes: serializedBytes(jsonScripts),
299
- note: `${jsonScripts.length} block(s), each { id, data }`
300
- });
301
- }
302
-
303
- return { data, found, warnings };
304
- }
@@ -1,80 +0,0 @@
1
- /**
2
- * jsonPath.js — the minimal subtree selector for extract_embedded_state.
3
- *
4
- * Deliberately not a JSONPath engine: dotted keys and array indexes only, no
5
- * wildcards, filters, slices or recursive descent. That is enough to turn a
6
- * multi-megabyte state blob into the branch a caller actually wants, and a
7
- * caller who needs more can select a branch and filter it themselves.
8
- *
9
- * Syntax: `next_data.props.pageProps`, `json_scripts.0.data`, `next_f[1a]`,
10
- * `a.b[0].c`. A key containing a literal "." cannot be addressed.
11
- */
12
-
13
- /**
14
- * Split a path into its segments.
15
- * @param {string} path
16
- * @returns {string[]}
17
- */
18
- export function parseJsonPath(path) {
19
- const segments = [];
20
- for (const part of path.split('.')) {
21
- // "a[0][1]" -> "a", "0", "1"
22
- const [head, ...brackets] = part.split('[');
23
- if (head !== '') segments.push(head);
24
- for (const bracket of brackets) {
25
- const key = bracket.endsWith(']') ? bracket.slice(0, -1) : bracket;
26
- segments.push(key.replace(/^['"]|['"]$/g, ''));
27
- }
28
- }
29
- return segments.filter((segment) => segment !== '');
30
- }
31
-
32
- /**
33
- * Describe what a caller could have asked for at a dead end, so a typo comes
34
- * back as a fixable message rather than an empty result.
35
- * @param {unknown} value
36
- * @returns {string}
37
- */
38
- function describeOptions(value) {
39
- if (Array.isArray(value)) return `array of length ${value.length}`;
40
- if (value !== null && typeof value === 'object') {
41
- const keys = Object.keys(value);
42
- const shown = keys.slice(0, 25).join(', ');
43
- return `available keys: ${shown}${keys.length > 25 ? `, … (${keys.length} total)` : ''}`;
44
- }
45
- return `a ${value === null ? 'null' : typeof value} value, which has no keys`;
46
- }
47
-
48
- /**
49
- * Resolve a path against a parsed object.
50
- *
51
- * @param {unknown} root
52
- * @param {string} path
53
- * @returns {unknown}
54
- * @throws {Error} when the path does not resolve — including the keys that
55
- * were available at the point it stopped
56
- */
57
- export function selectJsonPath(root, path) {
58
- const segments = parseJsonPath(path);
59
- if (segments.length === 0) {
60
- throw new Error(`Path "${path}" is empty`);
61
- }
62
-
63
- let current = root;
64
- const walked = [];
65
-
66
- for (const segment of segments) {
67
- const container = current !== null && typeof current === 'object';
68
- const key = Array.isArray(current) ? Number(segment) : segment;
69
- if (!container || !(key in current)) {
70
- const at = walked.length === 0 ? 'the result' : `"${walked.join('.')}"`;
71
- throw new Error(
72
- `Path "${path}" not found: ${at} has no "${segment}" (${describeOptions(current)})`
73
- );
74
- }
75
- current = current[key];
76
- walked.push(segment);
77
- }
78
-
79
- return current;
80
- }