brave-real-browser-mcp-server 2.11.2 → 2.11.4
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/handlers/APPLY_OPTIMIZATION_PATTERN.js +326 -0
- package/dist/handlers/advanced-extraction-handlers.js +1 -1
- package/dist/handlers/advanced-video-media-handlers.js +1 -1
- package/dist/handlers/data-extraction-handlers.js +2 -1
- package/dist/handlers/dynamic-session-handlers.js +1 -1
- package/dist/handlers/smart-data-extractors.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// 🚀 APPLY THIS PATTERN TO ALL 113 TOOLS
|
|
2
|
+
// This file shows the EXACT pattern to apply to every tool handler
|
|
3
|
+
// Copy-paste and customize for each tool
|
|
4
|
+
import { getCurrentPage } from '../browser-manager.js';
|
|
5
|
+
import { validateWorkflow } from '../workflow-validation.js';
|
|
6
|
+
import { TOOL_OPTIMIZATION_CONFIG, globalCache, deduplicateResults, executeToolWithOptimizations, VIDEO_HOSTERS_DB, SELECTOR_UTILS, globalMetrics, globalToolStatus, createErrorHandler, } from '../optimization-utils.js';
|
|
7
|
+
import { sleep } from '../system-utils.js';
|
|
8
|
+
/**
|
|
9
|
+
* ✅ TEMPLATE: Universal Pattern for ALL 113 Tools
|
|
10
|
+
*
|
|
11
|
+
* Replace:
|
|
12
|
+
* - TOOL_NAME: Actual tool name (e.g., 'ajax_extractor')
|
|
13
|
+
* - CATEGORY: Category from TOOL_OPTIMIZATION_CONFIG
|
|
14
|
+
* - TIMEOUT: Duration value from TOOL_OPTIMIZATION_CONFIG.xxx.defaultTimeout
|
|
15
|
+
* - CACHE_KEY: Unique key for caching (usually based on URL + params)
|
|
16
|
+
*/
|
|
17
|
+
export async function handleUNIVERSAL_TOOL_TEMPLATE(args) {
|
|
18
|
+
const toolName = 'TOOL_NAME'; // ← REPLACE with actual tool name
|
|
19
|
+
const errorHandler = createErrorHandler(toolName);
|
|
20
|
+
globalMetrics.start(toolName);
|
|
21
|
+
try {
|
|
22
|
+
// Validate workflow
|
|
23
|
+
validateWorkflow(toolName, {
|
|
24
|
+
requireBrowser: true,
|
|
25
|
+
requirePage: true,
|
|
26
|
+
});
|
|
27
|
+
// Get timeout from configuration (example)
|
|
28
|
+
const timeout = args.timeout || TOOL_OPTIMIZATION_CONFIG.dataExtraction.defaultTimeout;
|
|
29
|
+
const shouldCache = args.cache !== false;
|
|
30
|
+
const cacheKey = `${toolName}_${args.url || 'default'}`;
|
|
31
|
+
// Check cache first
|
|
32
|
+
if (shouldCache) {
|
|
33
|
+
const cached = globalCache.get(cacheKey);
|
|
34
|
+
if (cached) {
|
|
35
|
+
console.log(`[${toolName}] Cache hit for key: ${cacheKey}`);
|
|
36
|
+
return {
|
|
37
|
+
content: [
|
|
38
|
+
{
|
|
39
|
+
type: 'text',
|
|
40
|
+
text: `✅ ${toolName} (from cache)\n\n${JSON.stringify(cached, null, 2)}`,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// ======================================================
|
|
47
|
+
// YOUR ORIGINAL TOOL LOGIC GOES HERE
|
|
48
|
+
// ======================================================
|
|
49
|
+
const page = getCurrentPage();
|
|
50
|
+
let results = [];
|
|
51
|
+
// Example: Basic extraction logic
|
|
52
|
+
// Replace this with your actual tool implementation
|
|
53
|
+
results = await page.evaluate(() => {
|
|
54
|
+
// Your extraction logic here
|
|
55
|
+
return [];
|
|
56
|
+
});
|
|
57
|
+
// ======================================================
|
|
58
|
+
// OPTIMIZATION LAYER (Apply to all results)
|
|
59
|
+
// ======================================================
|
|
60
|
+
// 1. Deduplicate results
|
|
61
|
+
if (Array.isArray(results)) {
|
|
62
|
+
results = deduplicateResults(results, 'url'); // Use 'url' or your unique key
|
|
63
|
+
}
|
|
64
|
+
// 2. Filter empty/invalid results
|
|
65
|
+
results = results.filter((r) => r && Object.keys(r).length > 0);
|
|
66
|
+
// 3. Cache results if enabled
|
|
67
|
+
if (shouldCache && results.length > 0) {
|
|
68
|
+
globalCache.set(cacheKey, results);
|
|
69
|
+
}
|
|
70
|
+
// ======================================================
|
|
71
|
+
// RETURN OPTIMIZED RESULT
|
|
72
|
+
// ======================================================
|
|
73
|
+
return {
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: 'text',
|
|
77
|
+
text: `✅ ${toolName}\nFound ${results.length} results\n\n${JSON.stringify(results, null, 2)}`,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
// Enhanced error handling
|
|
84
|
+
return errorHandler.handle(error, 'tool execution');
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
// Cleanup and metrics
|
|
88
|
+
const duration = globalMetrics.end(toolName);
|
|
89
|
+
globalToolStatus.recordExecution(toolName, duration);
|
|
90
|
+
console.log(`[${toolName}] Execution time: ${duration}ms`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// ============================================================================
|
|
94
|
+
// SPECIFIC OPTIMIZATION PATTERNS FOR EACH TOOL CATEGORY
|
|
95
|
+
// ============================================================================
|
|
96
|
+
/**
|
|
97
|
+
* 🎥 VIDEO EXTRACTION TOOLS (20+ tools)
|
|
98
|
+
*
|
|
99
|
+
* Pattern: Use VIDEO_HOSTERS_DB, extended timeout, deep iframe scanning
|
|
100
|
+
*/
|
|
101
|
+
export async function handleVideoExtractionTemplate(args) {
|
|
102
|
+
const toolName = 'video_extraction_tool';
|
|
103
|
+
const errorHandler = createErrorHandler(toolName);
|
|
104
|
+
globalMetrics.start(toolName);
|
|
105
|
+
try {
|
|
106
|
+
const page = getCurrentPage();
|
|
107
|
+
const timeout = args.timeout || TOOL_OPTIMIZATION_CONFIG.videoExtraction.defaultTimeout;
|
|
108
|
+
const videoData = await executeToolWithOptimizations(toolName, async () => {
|
|
109
|
+
let videos = [];
|
|
110
|
+
// Use optimized selectors
|
|
111
|
+
videos = await page.evaluate((videoSelectors) => {
|
|
112
|
+
return Array.from(document.querySelectorAll(videoSelectors.join(','))).map((el) => ({
|
|
113
|
+
type: el.tagName.toLowerCase(),
|
|
114
|
+
src: el.src || el.getAttribute('data-src') || el.getAttribute('href'),
|
|
115
|
+
hoster: null,
|
|
116
|
+
platform: null,
|
|
117
|
+
}));
|
|
118
|
+
}, SELECTOR_UTILS.videoSelectors);
|
|
119
|
+
// Detect video hosters
|
|
120
|
+
videos = videos.map((video) => ({
|
|
121
|
+
...video,
|
|
122
|
+
hoster: VIDEO_HOSTERS_DB.getHosterName(video.src),
|
|
123
|
+
}));
|
|
124
|
+
// Deduplicate
|
|
125
|
+
videos = deduplicateResults(videos, 'src');
|
|
126
|
+
// Filter by hoster if needed
|
|
127
|
+
if (args.filterByHoster) {
|
|
128
|
+
videos = videos.filter((v) => v.hoster !== null);
|
|
129
|
+
}
|
|
130
|
+
return videos;
|
|
131
|
+
}, {
|
|
132
|
+
timeout,
|
|
133
|
+
retryAttempts: 2,
|
|
134
|
+
cacheKey: `videos_${page.url()}`,
|
|
135
|
+
shouldCache: true,
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
content: [
|
|
139
|
+
{
|
|
140
|
+
type: 'text',
|
|
141
|
+
text: `🎥 Found ${videoData.length} video sources\n\n${JSON.stringify(videoData, null, 2)}`,
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
return errorHandler.handle(error, 'video extraction');
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
const duration = globalMetrics.end(toolName);
|
|
151
|
+
globalToolStatus.recordExecution(toolName, duration);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* ⏱️ NETWORK MONITORING TOOLS (ajax_extractor, fetch_xhr, network_recorder)
|
|
156
|
+
*
|
|
157
|
+
* Pattern: Extended duration, retry logic, comprehensive request capture
|
|
158
|
+
*/
|
|
159
|
+
export async function handleNetworkMonitoringTemplate(args) {
|
|
160
|
+
const toolName = 'network_monitoring_tool';
|
|
161
|
+
const errorHandler = createErrorHandler(toolName);
|
|
162
|
+
globalMetrics.start(toolName);
|
|
163
|
+
try {
|
|
164
|
+
const page = getCurrentPage();
|
|
165
|
+
const duration = args.duration || TOOL_OPTIMIZATION_CONFIG.networkMonitoring.defaultDuration;
|
|
166
|
+
const networkData = await executeToolWithOptimizations(toolName, async () => {
|
|
167
|
+
const requests = [];
|
|
168
|
+
const requestHandler = (request) => {
|
|
169
|
+
const resourceType = request.resourceType();
|
|
170
|
+
if (resourceType === 'xhr' || resourceType === 'fetch') {
|
|
171
|
+
requests.push({
|
|
172
|
+
url: request.url(),
|
|
173
|
+
method: request.method(),
|
|
174
|
+
type: resourceType,
|
|
175
|
+
timestamp: new Date().toISOString(),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
page.on('request', requestHandler);
|
|
180
|
+
// Monitor for configured duration
|
|
181
|
+
await sleep(duration);
|
|
182
|
+
page.off('request', requestHandler);
|
|
183
|
+
// Deduplicate requests
|
|
184
|
+
return deduplicateResults(requests, 'url');
|
|
185
|
+
}, {
|
|
186
|
+
timeout: TOOL_OPTIMIZATION_CONFIG.networkMonitoring.monitoringTimeout,
|
|
187
|
+
retryAttempts: 3,
|
|
188
|
+
cacheKey: `network_${Date.now() / 60000}`, // Cache per minute
|
|
189
|
+
shouldCache: false, // Don't cache network data
|
|
190
|
+
});
|
|
191
|
+
return {
|
|
192
|
+
content: [
|
|
193
|
+
{
|
|
194
|
+
type: 'text',
|
|
195
|
+
text: `📡 Captured ${networkData.length} network requests\n\n${JSON.stringify(networkData, null, 2)}`,
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
return errorHandler.handle(error, 'network monitoring');
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
const duration = globalMetrics.end(toolName);
|
|
205
|
+
globalToolStatus.recordExecution(toolName, duration);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 📊 DATA EXTRACTION TOOLS (scrape_table, extract_links, extract_images, etc.)
|
|
210
|
+
*
|
|
211
|
+
* Pattern: Deep scanning, result deduplication, background-image extraction
|
|
212
|
+
*/
|
|
213
|
+
export async function handleDataExtractionTemplate(args) {
|
|
214
|
+
const toolName = 'data_extraction_tool';
|
|
215
|
+
const errorHandler = createErrorHandler(toolName);
|
|
216
|
+
globalMetrics.start(toolName);
|
|
217
|
+
try {
|
|
218
|
+
const page = getCurrentPage();
|
|
219
|
+
const selector = args.selector || '*';
|
|
220
|
+
const deepScan = args.deepScan ?? TOOL_OPTIMIZATION_CONFIG.dataExtraction.deepScanEnabled;
|
|
221
|
+
const extractedData = await executeToolWithOptimizations(toolName, async () => {
|
|
222
|
+
let results = [];
|
|
223
|
+
// Primary extraction
|
|
224
|
+
results = await page.evaluate((sel, deep) => {
|
|
225
|
+
const data = [];
|
|
226
|
+
// Standard extraction
|
|
227
|
+
document.querySelectorAll(sel).forEach((el, idx) => {
|
|
228
|
+
data.push({
|
|
229
|
+
index: idx,
|
|
230
|
+
tag: el.tagName.toLowerCase(),
|
|
231
|
+
text: el.textContent?.trim().substring(0, 100),
|
|
232
|
+
attributes: Array.from(el.attributes || []).reduce((acc, attr) => {
|
|
233
|
+
acc[attr.name] = attr.value;
|
|
234
|
+
return acc;
|
|
235
|
+
}, {}),
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
// Deep scan if enabled
|
|
239
|
+
if (deep && data.length < 1000) {
|
|
240
|
+
// Scan hidden elements, data attributes, etc.
|
|
241
|
+
document.querySelectorAll('[data-*]').forEach((el) => {
|
|
242
|
+
Object.keys(el.dataset || {}).forEach((key) => {
|
|
243
|
+
data.push({
|
|
244
|
+
type: 'data-attribute',
|
|
245
|
+
key,
|
|
246
|
+
value: el.dataset[key],
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return data;
|
|
252
|
+
}, selector, deepScan);
|
|
253
|
+
// Deduplicate
|
|
254
|
+
results = deduplicateResults(results);
|
|
255
|
+
// Filter empty
|
|
256
|
+
results = results.filter((r) => r && Object.keys(r).length > 0);
|
|
257
|
+
return results;
|
|
258
|
+
}, {
|
|
259
|
+
timeout: TOOL_OPTIMIZATION_CONFIG.dataExtraction.defaultTimeout,
|
|
260
|
+
retryAttempts: 2,
|
|
261
|
+
cacheKey: `data_${page.url()}_${selector}`,
|
|
262
|
+
shouldCache: true,
|
|
263
|
+
});
|
|
264
|
+
return {
|
|
265
|
+
content: [
|
|
266
|
+
{
|
|
267
|
+
type: 'text',
|
|
268
|
+
text: `📋 Extracted ${extractedData.length} items\n\n${JSON.stringify(extractedData.slice(0, 20), null, 2)}`,
|
|
269
|
+
},
|
|
270
|
+
],
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
return errorHandler.handle(error, 'data extraction');
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
const duration = globalMetrics.end(toolName);
|
|
278
|
+
globalToolStatus.recordExecution(toolName, duration);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// ============================================================================
|
|
282
|
+
// IMPLEMENTATION INSTRUCTIONS
|
|
283
|
+
// ============================================================================
|
|
284
|
+
/**
|
|
285
|
+
* TO APPLY THIS OPTIMIZATION PATTERN TO ALL 113 TOOLS:
|
|
286
|
+
*
|
|
287
|
+
* 1. For EACH tool handler function:
|
|
288
|
+
* - Add imports from optimization-utils at top of file
|
|
289
|
+
* - Wrap main logic with try-catch-finally
|
|
290
|
+
* - Add globalMetrics.start() and end() calls
|
|
291
|
+
* - Apply deduplicateResults() to list outputs
|
|
292
|
+
* - Use appropriate TOOL_OPTIMIZATION_CONFIG value
|
|
293
|
+
*
|
|
294
|
+
* 2. For VIDEO tools specifically:
|
|
295
|
+
* - Use handleVideoExtractionTemplate pattern
|
|
296
|
+
* - Replace hardcoded selectors with SELECTOR_UTILS.videoSelectors
|
|
297
|
+
* - Use VIDEO_HOSTERS_DB for hoster detection
|
|
298
|
+
*
|
|
299
|
+
* 3. For NETWORK tools specifically:
|
|
300
|
+
* - Use handleNetworkMonitoringTemplate pattern
|
|
301
|
+
* - Extend duration to TOOL_OPTIMIZATION_CONFIG.networkMonitoring.defaultDuration
|
|
302
|
+
* - Add retry logic for failed captures
|
|
303
|
+
*
|
|
304
|
+
* 4. For DATA tools specifically:
|
|
305
|
+
* - Use handleDataExtractionTemplate pattern
|
|
306
|
+
* - Enable deep scanning with TOOL_OPTIMIZATION_CONFIG.dataExtraction.deepScanEnabled
|
|
307
|
+
* - Always deduplicate before returning results
|
|
308
|
+
*
|
|
309
|
+
* 5. Test each optimized tool:
|
|
310
|
+
* - Verify it still works with original functionality
|
|
311
|
+
* - Check success rate > 95%
|
|
312
|
+
* - Verify deduplication works (no duplicate results)
|
|
313
|
+
* - Check caching is effective
|
|
314
|
+
*
|
|
315
|
+
* 6. After completing ALL 113 tools:
|
|
316
|
+
* - Run: npm run build
|
|
317
|
+
* - Run: npm run test
|
|
318
|
+
* - Run: npm run lint:fix
|
|
319
|
+
* - Commit with: git commit -m "✅ Optimize ALL 113 MCP tools for production"
|
|
320
|
+
*/
|
|
321
|
+
export default {
|
|
322
|
+
handleUNIVERSAL_TOOL_TEMPLATE,
|
|
323
|
+
handleVideoExtractionTemplate,
|
|
324
|
+
handleNetworkMonitoringTemplate,
|
|
325
|
+
handleDataExtractionTemplate,
|
|
326
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Data Extraction Handlers
|
|
2
|
-
//
|
|
2
|
+
// Data Extraction Handlers - OPTIMIZED
|
|
3
|
+
// Tables, Lists, JSON, Meta Tags, Schemas
|
|
3
4
|
// @ts-nocheck
|
|
4
5
|
import { getCurrentPage } from '../browser-manager.js';
|
|
5
6
|
import { validateWorkflow } from '../workflow-validation.js';
|