Claude Agent SDK
The Claude Agent SDK lets you build autonomous AI agents powered by Claude. Unlike simple chat completions, agents execute tools autonomously, persist across multiple turns, and use MCP servers to extend capabilities. By connecting to Browserless, your agents can browse bot-protected websites with stealth mode, automatic CAPTCHA solving, and residential proxies. All without local browser infrastructure.
There are two ways to connect:
- Browserless MCP server (recommended). Add the hosted MCP server to
mcpServersand Claude gets the Browserless tools with no browser code. - Playwright over CDP. Write your own tool that drives a Browserless session with Playwright. Use this when you need full control over every page action.
- Browserless API token (available in your account dashboard)
- An Anthropic API key
- Node.js 20.6 or later (for the built-in
--env-fileflag)
Quickstart: Browserless MCP server
Set your API keys
Go to your Browserless account dashboard and copy your API token. Then set your environment variables:
- .env file
- Command line
BROWSERLESS_API_KEY=your-browserless-token
ANTHROPIC_API_KEY=your-anthropic-keyexport BROWSERLESS_API_KEY=your-browserless-token
export ANTHROPIC_API_KEY=your-anthropic-keyInstall the Claude Agent SDK
npm install @anthropic-ai/claude-agent-sdkConnect to Browserless
Pass the hosted Browserless MCP server in
mcpServers. Save this asquickstart.mjs:import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Go to https://news.ycombinator.com and list the titles of the top 3 stories.",
options: {
mcpServers: {
browserless: {
type: "http",
url: "https://mcp.browserless.io/mcp",
headers: {
Authorization: `Bearer ${process.env.BROWSERLESS_API_KEY}`,
},
},
},
// Lets Claude call every Browserless tool without a permission prompt.
allowedTools: ["mcp__browserless"],
// Without this, Claude tries its built-in fetch first and loses a turn.
disallowedTools: ["WebFetch", "WebSearch"],
},
})) {
if (message.type === "result") console.log(message.result);
}Run it
node --env-file=.env quickstart.mjsIf you exported the variables instead of using a
.envfile, runnode quickstart.mjs.Claude loads the page through
browserless_smartscraperand replies with the titles:These are the top 3 stories on Hacker News right now:
1. …
2. …
3. …
Browserless tools appear in the SDK as mcp__browserless__<tool>, for example mcp__browserless__browserless_smartscraper or mcp__browserless__browserless_agent. allowedTools: ["mcp__browserless"] allows all of them. List individual tool names instead if you want to limit what Claude can call. See MCP tools for the full list.
The tools handle bot protection themselves. browserless_smartscraper starts with a plain HTTP fetch and only escalates to a proxy, a headless browser, and CAPTCHA solving when a site needs them. You can also turn on stealth routes by default or per request (see MCP tools). To pin a regional endpoint, add the x-browserless-api-url header next to the bearer token:
headers: {
Authorization: `Bearer ${process.env.BROWSERLESS_API_KEY}`,
"x-browserless-api-url": "https://production-lon.browserless.io",
},
See the MCP setup page for all configuration options.
Custom tool: Playwright over CDP
Set your API keys
Go to your Browserless account dashboard and copy your API token. You will also need an Anthropic API key.
Set both as environment variables:
- .env file
- Command line
BROWSERLESS_API_KEY=your-browserless-token
ANTHROPIC_API_KEY=your-anthropic-keyexport BROWSERLESS_API_KEY=your-browserless-token
export ANTHROPIC_API_KEY=your-anthropic-keyInstall dependencies
Install the Claude Agent SDK alongside Playwright and Zod:
npm install @anthropic-ai/claude-agent-sdk playwright-core zodConnect to Browserless
The key change is connecting Playwright to Browserless via CDP instead of launching a local browser:
import { chromium } from "playwright-core";
// Browserless connection with stealth + CAPTCHA solving
const BROWSERLESS_URL = `wss://production-sfo.browserless.io/stealth?token=${process.env.BROWSERLESS_API_KEY}&solveCaptchas=true&timeout=300000`;
// Connect via CDP (instead of chromium.launch())
const browser = await chromium.connectOverCDP(BROWSERLESS_URL);
const context = browser.contexts()[0] || await browser.newContext();
const page = context.pages()[0] || await context.newPage();Add system prompt guidance
Claude doesn't know Browserless handles CAPTCHAs automatically. Add these rules to your system prompt:
systemPrompt: `You have access to a Browserless cloud browser via the execute_playwright MCP tool.
The browser has stealth mode and automatic CAPTCHA solving enabled at the infrastructure level.
IMPORTANT RULES:
1. ALWAYS use execute_playwright for web interactions. Do NOT use WebFetch or other tools.
2. The 'page' and 'browser' objects are ALREADY CONNECTED. Do NOT call connectOverCDP yourself.
3. If you encounter a CAPTCHA or challenge page, wait 15-30 seconds and check again — it will be solved automatically.
4. Return data from your code using: return yourData;
5. ALWAYS use a long timeout on page.goto() to allow time for CAPTCHA solving.
6. Complete ALL your work in a SINGLE execute_playwright call. Do NOT make multiple separate calls.`Test it
Run your script with a command and pass a prompt as the argument:
npx tsx your-agent.ts "Go to https://news.ycombinator.com and get the top 5 story titles"
Connection URL anatomy
The Browserless WebSocket URL controls which features are active for the session:
wss://production-sfo.browserless.io/stealth?token=YOUR_API_TOKEN&solveCaptchas=true&timeout=300000&proxy=residential&blockAds=true
| Segment | Purpose |
|---|---|
production-sfo.browserless.io | Regional endpoint (US West). See Connection URLs for all regions |
/stealth | Stealth route: hides automation signals, spoofs fingerprints |
token= | Your Browserless API token |
solveCaptchas=true | Automatically detects and solves CAPTCHAs (reCAPTCHA, Cloudflare Turnstile, DataDome, and more) |
timeout=300000 | Session timeout in ms (5 minutes). Allows time for CAPTCHA solving |
proxy=residential | Route through residential IPs for better IP reputation |
blockAds=true | Block ads and trackers for cleaner page loads |
If you don't need anti-detection or automatic CAPTCHA solving and just want Browserless as managed cloud browser infrastructure, use the standard route and omit solveCaptchas:
wss://production-sfo.browserless.io?token=YOUR_API_TOKEN
You still get a fully managed, remote browser, just without the stealth fingerprinting and CAPTCHA solving layers.
How stealth mode works
The /stealth route automatically applies anti-detection techniques before your agent interacts with any page:
- Hides
navigator.webdriverand other automation signals - Spoofs browser fingerprints (canvas, WebGL, fonts)
- Randomizes user-agent strings
- Mimics human-like behavior patterns
No code changes needed. The protections are applied at the browser level, transparently to Playwright and the Claude Agent SDK.
How automatic CAPTCHA solving works
With solveCaptchas=true in your connection URL, Browserless monitors every page for CAPTCHA challenges and solves them in the background:
- Detects CAPTCHAs by monitoring network requests for known patterns (reCAPTCHA, Cloudflare Turnstile, DataDome, and more)
- Solves them programmatically using Browserless's solving engine
- Continues once solved. Your agent sees the unblocked page
Solving typically takes 15 to 60 seconds depending on complexity. The system prompt should instruct your agent to wait and retry if it encounters a challenge page.
Each successful CAPTCHA solve costs 10 units; attempts that don't clear aren't charged. Using /stealth and residential proxies reduces how often CAPTCHAs appear in the first place.
Example use cases
The integration works well for tasks that would normally trigger bot detection:
Scrape bot-protected review sites:
"Go to https://www.g2.com/products/browserless-io/reviews and get the reviews"
Extract product data from e-commerce:
"Navigate to https://www.amazon.com/dp/B09V3KXJPB and get the product title and price"
Research news sites:
"Go to https://news.ycombinator.com and get the top 5 story titles"
Multi-step workflows:
"Search Google for 'best headless browser 2026', click the first result, and summarize the page"
Resources
- Claude Agent SDK documentation
- Browserless MCP setup
- MCP tools
- Stealth routes
- CAPTCHA solving
- Proxy configuration
- Connection URLs
FAQ & Troubleshooting
Claude tries to solve CAPTCHAs manually
Add clear instructions to your system prompt that CAPTCHAs solve automatically and to wait 15 to 30 seconds. See the system prompt guidance step in Custom tool: Playwright over CDP above.
Claude uses WebFetch instead of Browserless
Add disallowedTools: ["WebFetch", "WebSearch"] to your query() options. Without it, Claude often tries its built-in fetch first. On a bot-protected site that returns the challenge page instead of the content, and if WebFetch isn't allowed the turn ends in a permission denial.
Browserless tool calls end in a permission denial
Add allowedTools: ["mcp__browserless"] to your query() options. The SDK doesn't auto-approve MCP tools, so without it a headless run can't call them. Check message.permission_denials on the result message to see which tool was blocked.
The Browserless MCP server doesn't connect
Check that type is "http", the URL is https://mcp.browserless.io/mcp (not the legacy /sse path), and the Authorization: Bearer … header is inside the browserless entry in mcpServers. The system message with subtype init lists each MCP server with its status, so log it to see whether the server shows as connected.
Connection timeout on page.goto()
Use a long timeout: page.goto(url, { timeout: 120000, waitUntil: 'domcontentloaded' }). The default 30-second timeout isn't enough when CAPTCHA solving is happening.
"Target page, context or browser has been closed"
This means the Browserless session expired. Increase the session timeout in the URL (&timeout=300000 for 5 minutes), instruct Claude to complete all work in a single tool call, or rely on the code's automatic reconnect on the next tool call.
Site still blocks the browser
Layer your defenses: use the /stealth route for anti-detection, add proxy=residential for IP reputation, and add solveCaptchas=true for CAPTCHA challenges.
"WebSocket connection failed"
Check that your BROWSERLESS_API_KEY is set correctly, you have available units in your Browserless account, and the regional endpoint is accessible from your network.