|
1 |
| -# Browserbase MCP Server |
| 1 | +# Playwright Browserbase MCP Server |
2 | 2 |
|
3 |
| - |
| 3 | +A Model Context Protocol server that uses Playwright and Browserbase |
| 4 | +to provide browser automation tools. |
4 | 5 |
|
5 |
| -## Get Started |
| 6 | +## Setup |
6 | 7 |
|
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 | + ``` |
8 | 19 |
|
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: |
10 | 43 |
|
11 | 44 | ```json
|
12 | 45 | {
|
13 | 46 | "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"], |
17 | 50 | "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": "" |
20 | 53 | }
|
21 | 54 | }
|
22 | 55 | }
|
23 | 56 | }
|
24 | 57 | ```
|
25 | 58 |
|
26 |
| -3. Restart your Claude Desktop app and you should see the tools available clicking the 🔨 icon. |
| 59 | +### SSE: |
27 | 60 |
|
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 | +``` |
29 | 64 |
|
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 |
33 | 66 |
|
| 67 | +## Flags Explained: |
34 | 68 |
|
35 |
| -## Tools |
| 69 | +The Browserbase MCP server accepts the following command-line flags: |
36 | 70 |
|
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) | |
38 | 83 |
|
39 |
| -- **browserbase_create_session** |
| 84 | +These flags can be passed directly to the CLI or configured in your MCP configuration file. |
40 | 85 |
|
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). |
43 | 87 |
|
44 |
| -- **browserbase_navigate** |
| 88 | +____ |
45 | 89 |
|
46 |
| - - Navigate to any URL in the browser |
47 |
| - - Input: `url` (string) |
| 90 | +## Flags & Example Configs |
48 | 91 |
|
49 |
| -- **browserbase_screenshot** |
| 92 | +### Proxies |
50 | 93 |
|
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). |
57 | 95 |
|
58 |
| -- **browserbase_click** |
| 96 | +To use proxies in STDIO, set the --proxies flag in your MCP Config |
59 | 97 |
|
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 | + } |
62 | 111 |
|
63 |
| -- **browserbase_fill** |
| 112 | +``` |
64 | 113 |
|
65 |
| - - Fill out input fields |
66 |
| - - Inputs: |
67 |
| - - `selector` (string): CSS selector for input field |
68 |
| - - `value` (string): Value to fill |
| 114 | +### Contexts |
69 | 115 |
|
70 |
| -- **browserbase_evaluate** |
| 116 | +Here are our docs on [Contexts](https://docs.browserbase.com/features/contexts) |
71 | 117 |
|
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 |
74 | 119 |
|
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 | + } |
76 | 133 |
|
77 |
| - - Extract all content from the current page |
78 |
| - - Input: `selector` (string, optional): CSS selector to get content from specific elements |
| 134 | +``` |
79 | 135 |
|
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 |
85 | 137 |
|
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). |
87 | 139 |
|
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. |
89 | 141 |
|
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) |
91 | 143 |
|
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 | +``` |
94 | 163 |
|
95 |
| -2. **Screenshots** (`screenshot://<name>`) |
96 |
| - - PNG images of captured screenshots |
97 |
| - - Accessible via the screenshot name specified during capture |
| 164 | +### Browser Viewport Sizing |
98 | 165 |
|
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. |
100 | 167 |
|
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) |
107 | 169 |
|
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 | +``` |
109 | 188 |
|
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. |
0 commit comments