forked from brightdata/brightdata-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_tools.js
More file actions
320 lines (302 loc) · 11 KB
/
browser_tools.js
File metadata and controls
320 lines (302 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
'use strict'; /*jslint node:true es9:true*/
import {UserError, imageContent as image_content} from 'fastmcp';
import {z} from 'zod';
import axios from 'axios';
import {Browser_session} from './browser_session.js';
let browser_zone = process.env.BROWSER_ZONE || 'mcp_browser';
let open_session;
const require_browser = async()=>{
if (!open_session)
{
open_session = new Browser_session({
cdp_endpoint: await calculate_cdp_endpoint(),
});
}
return open_session;
};
const calculate_cdp_endpoint = async()=>{
try {
const status_response = await axios({
url: 'https://api.brightdata.com/status',
method: 'GET',
headers: {authorization: `Bearer ${process.env.API_TOKEN}`},
});
const customer = status_response.data.customer;
const password_response = await axios({
url: `https://api.brightdata.com/zone/passwords?zone=${browser_zone}`,
method: 'GET',
headers: {authorization: `Bearer ${process.env.API_TOKEN}`},
});
const password = password_response.data.passwords[0];
return `wss://brd-customer-${customer}-zone-${browser_zone}:`
+`${password}@brd.superproxy.io:9222`;
} catch(e){
if (e.response?.status===422)
throw new Error(`Browser zone '${browser_zone}' does not exist`);
throw new Error(`Error retrieving browser credentials: ${e.message}`);
}
};
let scraping_browser_navigate = {
name: 'scraping_browser_navigate',
description: 'Navigate a scraping browser session to a new URL',
parameters: z.object({
url: z.string().describe('The URL to navigate to'),
}),
execute: async({url})=>{
const page = await (await require_browser()).get_page({url});
try {
await page.goto(url, {
timeout: 120000,
waitUntil: 'domcontentloaded',
});
return [
`Successfully navigated to ${url}`,
`Title: ${await page.title()}`,
`URL: ${page.url()}`,
].join('\n');
} catch(e){
throw new UserError(`Error navigating to ${url}: ${e}`);
}
},
};
let scraping_browser_go_back = {
name: 'scraping_browser_go_back',
description: 'Go back to the previous page',
parameters: z.object({}),
execute: async()=>{
const page = await (await require_browser()).get_page();
try {
await page.goBack();
return [
'Successfully navigated back',
`Title: ${await page.title()}`,
`URL: ${page.url()}`,
].join('\n');
} catch(e){
throw new UserError(`Error navigating back: ${e}`);
}
},
};
const scraping_browser_go_forward = {
name: 'scraping_browser_go_forward',
description: 'Go forward to the next page',
parameters: z.object({}),
execute: async()=>{
const page = await (await require_browser()).get_page();
try {
await page.goForward();
return [
'Successfully navigated forward',
`Title: ${await page.title()}`,
`URL: ${page.url()}`,
].join('\n');
} catch(e){
throw new UserError(`Error navigating forward: ${e}`);
}
},
};
let scraping_browser_click = {
name: 'scraping_browser_click',
description: [
'Click on an element.',
'Avoid calling this unless you know the element selector (you can use '
+'other tools to find those)',
].join('\n'),
parameters: z.object({
selector: z.string().describe('CSS selector for the element to click'),
}),
execute: async({selector})=>{
const page = await (await require_browser()).get_page();
try {
await page.click(selector, {timeout: 5000});
return `Successfully clicked element: ${selector}`;
} catch(e){
throw new UserError(`Error clicking element ${selector}: ${e}`);
}
},
};
let scraping_browser_links = {
name: 'scraping_browser_links',
description: [
'Get all links on the current page, text and selectors',
"It's strongly recommended that you call the links tool to check that "
+'your click target is valid',
].join('\n'),
parameters: z.object({}),
execute: async()=>{
const page = await (await require_browser()).get_page();
try {
const links = await page.$$eval('a', elements=>{
return elements.map(el=>{
return {
text: el.innerText,
href: el.href,
selector: el.outerHTML,
};
});
});
return JSON.stringify(links, null, 2);
} catch(e){
throw new UserError(`Error getting links: ${e}`);
}
},
};
let scraping_browser_type = {
name: 'scraping_browser_type',
description: 'Type text into an element',
parameters: z.object({
selector: z.string()
.describe('CSS selector for the element to type into'),
text: z.string().describe('Text to type'),
submit: z.boolean().optional()
.describe('Whether to submit the form after typing (press Enter)'),
}),
execute: async({selector, text, submit})=>{
const page = await (await require_browser()).get_page();
try {
await page.fill(selector, text);
if (submit)
await page.press(selector, 'Enter');
return `Successfully typed "${text}" into element: `
+`${selector}${submit ? ' and submitted the form' : ''}`;
} catch(e){
throw new UserError(`Error typing into element ${selector}: ${e}`);
}
},
};
let scraping_browser_wait_for = {
name: 'scraping_browser_wait_for',
description: 'Wait for an element to be visible on the page',
parameters: z.object({
selector: z.string().describe('CSS selector to wait for'),
timeout: z.number().optional()
.describe('Maximum time to wait in milliseconds (default: 30000)'),
}),
execute: async({selector, timeout})=>{
const page = await (await require_browser()).get_page();
try {
await page.waitForSelector(selector, {timeout: timeout||30000});
return `Successfully waited for element: ${selector}`;
} catch(e){
throw new UserError(`Error waiting for element ${selector}: ${e}`);
}
},
};
let scraping_browser_screenshot = {
name: 'scraping_browser_screenshot',
description: 'Take a screenshot of the current page',
parameters: z.object({
full_page: z.boolean().optional().describe([
'Whether to screenshot the full page (default: false)',
'You should avoid fullscreen if it\'s not important, since the '
+'images can be quite large',
].join('\n')),
}),
execute: async({full_page = false})=>{
const page = await (await require_browser()).get_page();
try {
const buffer = await page.screenshot({fullPage: full_page});
return image_content({buffer});
} catch(e){
throw new UserError(`Error taking screenshot: ${e}`);
}
},
};
let scraping_browser_get_html = {
name: 'scraping_browser_get_html',
description: 'Get the HTML content of the current page. Avoid using the '
+'full_page option unless it is important to see things like script tags '
+'since this can be large',
parameters: z.object({
full_page: z.boolean().optional().describe([
'Whether to get the full page HTML including head and script tags',
'Avoid this if you only need the extra HTML, since it can be '
+'quite large',
].join('\n')),
}),
execute: async({full_page = false})=>{
const page = await (await require_browser()).get_page();
try {
if (!full_page)
return await page.$eval('body', body=>body.innerHTML);
const html = await page.content();
if (!full_page && html)
return html.split('<body>')[1].split('</body>')[0];
return html;
} catch(e){
throw new UserError(`Error getting HTML content: ${e}`);
}
},
};
let scraping_browser_get_text = {
name: 'scraping_browser_get_text',
description: 'Get the text content of the current page',
parameters: z.object({}),
execute: async()=>{
const page = await (await require_browser()).get_page();
try { return await page.$eval('body', body=>body.innerText); }
catch(e){ throw new UserError(`Error getting text content: ${e}`); }
},
};
let scraping_browser_activation_instructions = {
name: 'scraping_browser_activation_instructions',
description: 'Instructions for activating the scraping browser',
parameters: z.object({}),
execute: async()=>{
return 'You need to run this MCP server with the BROWSER_AUTH '
+'environment varialbe before the browser tools will become '
+'available';
},
};
let scraping_browser_scroll = {
name: 'scraping_browser_scroll',
description: 'Scroll to the bottom of the current page',
parameters: z.object({}),
execute: async()=>{
const page = await (await require_browser()).get_page();
try {
await page.evaluate(()=>{
window.scrollTo(0, document.body.scrollHeight);
});
return 'Successfully scrolled to the bottom of the page';
} catch(e){
throw new UserError(`Error scrolling page: ${e}`);
}
},
};
let scraping_browser_scroll_to = {
name: 'scraping_browser_scroll_to',
description: 'Scroll to a specific element on the page',
parameters: z.object({
selector: z.string().describe('CSS selector for the element to scroll to'),
}),
execute: async({selector})=>{
const page = await (await require_browser()).get_page();
try {
await page.evaluate(sel=>{
const element = document.querySelector(sel);
if (element)
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
else
throw new Error(`Element with selector "${sel}" not found`);
}, selector);
return `Successfully scrolled to element: ${selector}`;
} catch(e){
throw new UserError(`Error scrolling to element ${selector}: ${e}`);
}
},
};
export const tools = process.env.API_TOKEN ? [
scraping_browser_navigate,
scraping_browser_go_back,
scraping_browser_go_forward,
scraping_browser_links,
scraping_browser_click,
scraping_browser_type,
scraping_browser_wait_for,
scraping_browser_screenshot,
scraping_browser_get_text,
scraping_browser_get_html,
scraping_browser_scroll,
scraping_browser_scroll_to,
] : [scraping_browser_activation_instructions];