Skip to content

Commit 6db213b

Browse files
authored
Merge pull request #52 from browserbase/ap/bbplaywright
browserbase playwright implementation
2 parents 84e8515 + 2dcd760 commit 6db213b

34 files changed

+3797
-1112
lines changed

browserbase/.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Ignore node_modules, build output, logs, env files, etc.
2+
node_modules
3+
dist
4+
*.log
5+
.env*
6+
7+
# Ignore IDE/editor files
8+
.vscode
9+
.idea
10+
*.swp
11+
*.swo
12+
13+
# Ignore OS files
14+
.DS_Store
15+
Thumbs.db

browserbase/Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Build stage
2+
FROM node:20-slim AS builder
3+
4+
WORKDIR /app
5+
6+
# Copy package files first for better layer caching
7+
COPY package.json package-lock.json ./
8+
9+
# Install all dependencies including dev dependencies for building
10+
# --ignore-scripts prevents the prepare script from running prematurely
11+
RUN npm ci --ignore-scripts
12+
13+
# Copy all source files
14+
COPY . .
15+
16+
# Build the TypeScript source code manually instead of using the npm script
17+
RUN npx tsc && npx shx chmod +x dist/*.js 2>/dev/null || echo "No executable JS files found"
18+
19+
# Production stage
20+
FROM node:18-alpine
21+
22+
# Set the working directory
23+
WORKDIR /app
24+
25+
# Copy package files
26+
COPY package.json package-lock.json ./
27+
28+
# Install only production dependencies
29+
# --ignore-scripts prevents the prepare script from running
30+
RUN npm ci --omit=dev --ignore-scripts
31+
32+
# Copy built files from builder stage
33+
COPY --from=builder /app/dist /app/dist
34+
COPY --from=builder /app/cli.js /app/cli.js
35+
COPY --from=builder /app/index.js /app/index.js
36+
COPY --from=builder /app/index.d.ts /app/index.d.ts
37+
COPY --from=builder /app/config.d.ts /app/config.d.ts
38+
39+
# Define environment variables (will be provided by Smithery)
40+
ENV NODE_ENV=production
41+
42+
# Expose a default port (useful if deploying as a service)
43+
EXPOSE 8931
44+
45+
# Command to run the application
46+
CMD [ "node", "cli.js", "--port", "8931" ]

browserbase/README.md

Lines changed: 217 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,261 @@
1-
# Browserbase MCP Server
1+
# Playwright Browserbase MCP Server
22

3-
![cover](../assets/browserbase-mcp.png)
3+
A Model Context Protocol server that uses Playwright and Browserbase
4+
to provide browser automation tools.
45

5-
## Get Started
6+
## Setup
67

7-
1. Run `npm install` to install the necessary dependencies, then run `npm run build` to get `dist/index.js`.
8+
1. Install dependencies:
9+
```bash
10+
npm install
11+
```
12+
2. Set environment variables (e.g., in a `.env` file):
13+
* `BROWSERBASE_API_KEY`: Your Browserbase API key.
14+
* `BROWSERBASE_PROJECT_ID`: Your Browserbase project ID.
15+
3. Compile TypeScript:
16+
```bash
17+
npm run build
18+
```
819

9-
2. Set up your Claude Desktop configuration to use the server.
20+
## How to setup in MCP json
21+
22+
```json
23+
{
24+
"mcpServers": {
25+
"browserbase": {
26+
"url": "http://localhost:8931/sse",
27+
"env": {
28+
"BROWSERBASE_API_KEY": "",
29+
"BROWSERBASE_PROJECT_ID": ""
30+
}
31+
}
32+
}
33+
}
34+
```
35+
36+
## Local Dev
37+
38+
To run locally we can use STDIO or self-host over SSE.
39+
40+
### STDIO:
41+
42+
To your MCP Config JSON file add the following:
1043

1144
```json
1245
{
1346
"mcpServers": {
14-
"browserbase": {
15-
"command": "node",
16-
"args": ["path/to/mcp-server-browserbase/browserbase/dist/index.js"],
47+
"playwright": {
48+
"command" : "node",
49+
"args" : ["/path/to/mcp-server-browserbase/browserbase/cli.js"],
1750
"env": {
18-
"BROWSERBASE_API_KEY": "<YOUR_BROWSERBASE_API_KEY>",
19-
"BROWSERBASE_PROJECT_ID": "<YOUR_BROWSERBASE_PROJECT_ID>"
51+
"BROWSERBASE_API_KEY": "",
52+
"BROWSERBASE_PROJECT_ID": ""
2053
}
2154
}
2255
}
2356
}
2457
```
2558

26-
3. Restart your Claude Desktop app and you should see the tools available clicking the 🔨 icon.
59+
### SSE:
2760

28-
4. Start using the tools! Below is an image of Claude closing a browser session.
61+
```bash
62+
node cli.js --port 8931
63+
```
2964

30-
<p align="center">
31-
<img src="../assets/browserbase-demo.png" alt="demo" width="600"/>
32-
</p>
65+
From here you should be able to put the url as "http://localhost:8931/sse" in the config.json
3366

67+
## Flags Explained:
3468

35-
## Tools
69+
The Browserbase MCP server accepts the following command-line flags:
3670

37-
### Browserbase API
71+
| Flag | Description |
72+
|------|-------------|
73+
| `--browserbaseApiKey <key>` | Your Browserbase API key for authentication |
74+
| `--browserbaseProjectId <id>` | Your Browserbase project ID |
75+
| `--proxies` | Enable Browserbase proxies for the session |
76+
| `--contextId <contextId>` | Specify a Browserbase Context ID to use |
77+
| `--persist [boolean]` | Whether to persist the Browserbase context (default: true) |
78+
| `--port <port>` | Port to listen on for HTTP/SSE transport |
79+
| `--host <host>` | Host to bind server to (default: localhost, use 0.0.0.0 for all interfaces) |
80+
| `--cookies [json]` | JSON array of cookies to inject into the browser |
81+
| `--browserWidth <width>` | Browser viewport width (default: 1024) |
82+
| `--browserHeight <height>` | Browser viewport height (default: 768) |
3883

39-
- **browserbase_create_session**
84+
These flags can be passed directly to the CLI or configured in your MCP configuration file.
4085

41-
- Create a new cloud browser session using Browserbase
42-
- No required inputs
86+
Currently, these flags can only be used with the local server (npx @browserbasehq/mcp-server-browserbase).
4387

44-
- **browserbase_navigate**
88+
____
4589

46-
- Navigate to any URL in the browser
47-
- Input: `url` (string)
90+
## Flags & Example Configs
4891

49-
- **browserbase_screenshot**
92+
### Proxies
5093

51-
- Capture screenshots of the entire page or specific elements
52-
- Inputs:
53-
- `name` (string, required): Name for the screenshot
54-
- `selector` (string, optional): CSS selector for element to screenshot
55-
- `width` (number, optional, default: 800): Screenshot width
56-
- `height` (number, optional, default: 600): Screenshot height
94+
Here are our docs on [Proxies](https://docs.browserbase.com/features/proxies).
5795

58-
- **browserbase_click**
96+
To use proxies in STDIO, set the --proxies flag in your MCP Config
5997

60-
- Click elements on the page
61-
- Input: `selector` (string): CSS selector for element to click
98+
```json
99+
{
100+
"mcpServers": {
101+
"playwright": {
102+
"command" : "npx",
103+
"args" : ["@browserbasehq/mcp-server-browserbase", "--proxies"],
104+
"env": {
105+
"BROWSERBASE_API_KEY": "",
106+
"BROWSERBASE_PROJECT_ID": ""
107+
}
108+
}
109+
}
110+
}
62111
63-
- **browserbase_fill**
112+
```
64113

65-
- Fill out input fields
66-
- Inputs:
67-
- `selector` (string): CSS selector for input field
68-
- `value` (string): Value to fill
114+
### Contexts
69115

70-
- **browserbase_evaluate**
116+
Here are our docs on [Contexts](https://docs.browserbase.com/features/contexts)
71117

72-
- Execute JavaScript in the browser console
73-
- Input: `script` (string): JavaScript code to execute
118+
To use proxies in STDIO, set the --proxies flag in your MCP Config
74119

75-
- **browserbase_get_content**
120+
```json
121+
{
122+
"mcpServers": {
123+
"playwright": {
124+
"command" : "npx",
125+
"args" : ["@browserbasehq/mcp-server-browserbase", "--contextId", "<YOUR_CONTEXT_ID>"],
126+
"env": {
127+
"BROWSERBASE_API_KEY": "",
128+
"BROWSERBASE_PROJECT_ID": ""
129+
}
130+
}
131+
}
132+
}
76133
77-
- Extract all content from the current page
78-
- Input: `selector` (string, optional): CSS selector to get content from specific elements
134+
```
79135

80-
- **browserbase_parallel_sessions**
81-
- Create multiple browser sessions and navigate to different URLs
82-
- Input: `sessions` (array): Array of objects containing:
83-
- `url` (string): URL to navigate to
84-
- `id` (string): Session identifier
136+
### Cookie Injection
85137

86-
### Resources
138+
Why would you need to inject cookies? Our context API currently works on persistent cookies, but not session cookies. So sometimes our persistent auth might not work (we're working hard to add this functionality).
87139
88-
The server provides access to two types of resources:
140+
You can flag cookies into the MCP by adding the cookies.json to your MCP Config.
89141
90-
1. **Console Logs** (`console://logs`)
142+
To use proxies in STDIO, set the --proxies flag in your MCP Config. Your cookies JSON must be in the type of [Playwright Cookies](https://playwright.dev/docs/api/class-browsercontext#browser-context-cookies)
91143
92-
- Browser console output in text format
93-
- Includes all console messages from the browser
144+
```json
145+
{
146+
"mcpServers": {
147+
"playwright": {
148+
"command" : "npx",
149+
"args" : [
150+
"@browserbasehq/mcp-server-browserbase", "cookies",
151+
"{
152+
COOKIES JSON IN TYPE OF PLAYWRIGHT COOKIES
153+
}"
154+
],
155+
"env": {
156+
"BROWSERBASE_API_KEY": "",
157+
"BROWSERBASE_PROJECT_ID": ""
158+
}
159+
}
160+
}
161+
}
162+
```
94163
95-
2. **Screenshots** (`screenshot://<name>`)
96-
- PNG images of captured screenshots
97-
- Accessible via the screenshot name specified during capture
164+
### Browser Viewport Sizing
98165
99-
## Key Features
166+
The default viewport sizing for a browser session is 1024 x 768. You can adjust the Browser viewport sizing with browserWidth and browserHeight flags.
100167
101-
- Cloud browser automation
102-
- Web data extraction
103-
- Console log monitoring
104-
- Screenshot capabilities
105-
- JavaScript execution
106-
- Basic web interaction (navigation, clicking, form filling)
168+
Here's how to use it for custom browser sizing. We recommend to stick with 16:9 aspect ratios (ie: 1920 x 1080, 1280, 720, 1024 x 768)
107169

108-
## License
170+
```json
171+
{
172+
"mcpServers": {
173+
"playwright": {
174+
"command" : "npx",
175+
"args" : [
176+
"@browserbasehq/mcp-server-browserbase",
177+
"--browserHeight" : 1080,
178+
"--browserWidth" : 1920,
179+
],
180+
"env": {
181+
"BROWSERBASE_API_KEY": "",
182+
"BROWSERBASE_PROJECT_ID": ""
183+
}
184+
}
185+
}
186+
}
187+
```
109188

110-
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.
189+
## Structure
190+
191+
* `src/`: TypeScript source code
192+
* `index.ts`: Main entry point, env checks, shutdown
193+
* `server.ts`: MCP Server setup and request routing
194+
* `sessionManager.ts`: Handles Browserbase session creation/management
195+
* `tools/`: Tool definitions and implementations
196+
* `resources/`: Resource (screenshot) handling
197+
* `types.ts`: Shared TypeScript types
198+
* `dist/`: Compiled JavaScript output
199+
* `tests/`: Placeholder for tests
200+
* `utils/`: Placeholder for utility scripts
201+
* `Dockerfile`: For building a Docker image
202+
* Configuration files (`.json`, `.ts`, `.mjs`, `.npmignore`)
203+
204+
## Contexts for Persistence
205+
206+
This server supports Browserbase's Contexts feature, which allows persisting cookies, authentication, and cached data across browser sessions:
207+
208+
1. **Creating a Context**:
209+
```
210+
browserbase_context_create: Creates a new context, optionally with a friendly name
211+
```
212+
213+
2. **Using a Context with a Session**:
214+
```
215+
browserbase_session_create: Now accepts a 'context' parameter with:
216+
- id: The context ID to use
217+
- name: Alternative to ID, the friendly name of the context
218+
- persist: Whether to save changes (cookies, cache) back to the context (default: true)
219+
```
220+
221+
3. **Deleting a Context**:
222+
```
223+
browserbase_context_delete: Deletes a context when you no longer need it
224+
```
225+
226+
Contexts make it much easier to:
227+
- Maintain login state across sessions
228+
- Reduce page load times by preserving cache
229+
- Avoid CAPTCHAs and detection by reusing browser fingerprints
230+
231+
## Cookie Management
232+
233+
This server also provides direct cookie management capabilities:
234+
235+
1. **Adding Cookies**:
236+
```
237+
browserbase_cookies_add: Add cookies to the current browser session with full control over properties
238+
```
239+
240+
2. **Getting Cookies**:
241+
```
242+
browserbase_cookies_get: View all cookies in the current session (optionally filtered by URLs)
243+
```
244+
245+
3. **Deleting Cookies**:
246+
```
247+
browserbase_cookies_delete: Delete specific cookies or clear all cookies from the session
248+
```
249+
250+
These tools are useful for:
251+
- Setting authentication cookies without navigating to login pages
252+
- Backing up and restoring cookie state
253+
- Debugging cookie-related issues
254+
- Manipulating cookie attributes (expiration, security flags, etc.)
255+
256+
## TODO
257+
258+
* Implement true `ref`-based interaction logic for click, type, drag, hover, select_option.
259+
* Implement element-specific screenshots using `ref`.
260+
* Add more standard Playwright MCP tools (tabs, navigation, etc.).
261+
* Add tests.

browserbase/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
import './dist/program.js';

0 commit comments

Comments
 (0)