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
255 lines (240 loc) · 8.64 KB
/
browser_tools.js
File metadata and controls
255 lines (240 loc) · 8.64 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
'use strict'; /*jslint node:true es9:true*/
import {UserError, imageContent} from 'fastmcp';
import {z} from 'zod';
import {Browser_session} from './browser_session.js';
let browser_auth = process.env.BROWSER_AUTH;
let open_session;
const require_browser = ()=>{
return open_session = open_session || new Browser_session({
cdp_endpoint: calculate_cdp_endpoint(),
});
};
const calculate_cdp_endpoint = ()=>{
if (browser_auth.startsWith('ws://') || browser_auth.startsWith('wss://'))
return browser_auth;
return `wss://${browser_auth}@brd.superproxy.io:9222`;
};
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 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 imageContent({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';
}
};
export const tools = browser_auth ? [
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_activation_instructions];