stagehand
Version: 1.0.0
Script: scripts/stagehand-mcp-server.mjs
Environment Variables
Architecture
Stagehand wraps a browser automation server (port 3002) and exposes it as MCP tools. Each browser session:- Uses Chromium with stealth mode (
--disable-blink-features=AutomationControlled) - Runs at 1920x1080 resolution
- Can run headless or with visible UI
- Maintains independent cookie stores and state
- Consumes ~300MB RAM per active session
Workflow Pattern
- Start session → get
session_id - Navigate to target URL
- Act / Extract / Observe as needed
- Screenshot for debugging (optional)
- Close session when done
- Start session → get
session_id - Set cookies from browser extension capture
- Navigate to authenticated page (bypasses login)
- Act / Extract data
- Close session
start-session
Start a new Stagehand browser session. Returns a session ID used by all other tools. Parameters:headless(boolean, optional): Run headless (default:true)
session_id and metadata.
Example 1: Headless session (default)
- Platform scraping (OnlyFans, Fansly, etc.)
- Social media posting automation
- Content extraction from authenticated pages
- HITL (human-in-the-loop) login workflows
navigate
Navigate the browser to a URL in an existing session. Parameters:session_id(string, required): Session ID fromstart-sessionurl(string, required): URL to navigate to
- Always call
navigatebefore interacting with page elements - Wait for page load to complete before calling
actorextract - Use with
set-cookiesfor authenticated navigation
act
Perform a browser action using natural language (click, type, scroll, etc.). This is Stagehand’s core AI-powered feature — it understands natural language instructions and translates them to browser actions. Parameters:session_id(string, required): Session ID fromstart-sessionaction(string, required): Natural language action description
- Click elements (buttons, links, etc.)
- Type text into inputs/textareas
- Scroll (up, down, to element)
- Select dropdown options
- Upload files
- Press keyboard keys
- Hover over elements
- Drag and drop
- Use clear, specific descriptions (“Click the blue ‘Submit’ button” vs “click button”)
- Reference visual landmarks when multiple similar elements exist
- Break complex workflows into multiple
actcalls - Check results with
screenshotif action fails
extract
Extract structured data from the current page using a schema description. This is Stagehand’s AI-powered data scraping — it understands what data you want and extracts it intelligently. Parameters:session_id(string, required): Session ID fromstart-sessioninstruction(string, required): What to extract (natural language)schema(object, optional): Optional JSON schema describing the output structure
- Platform statistics scraping
- Content feed extraction
- Fan engagement metrics
- Earnings data collection
- Post metadata gathering
observe
Observe interactive elements on the current page. Parameters:session_id(string, required): Session ID fromstart-sessioninstruction(string, optional): What to look for (e.g. “Find all clickable buttons”)
- Debug why
actisn’t finding an element - Discover available interactions on a page
- Validate page structure before automation
screenshot
Take a screenshot of the current browser page. Parameters:session_id(string, required): Session ID fromstart-session
- Debug failed automation steps
- Verify page state before/after actions
- Capture visual confirmation of task completion
- Generate proof-of-posting for scheduling
get-cookies
Get all cookies from the current browser session. Parameters:session_id(string, required): Session ID fromstart-session
- Save cookies after manual login (HITL flow)
- Store session tokens for future automation
- Transfer authentication between sessions
set-cookies
Inject cookies into the browser session (for bypassing login). This is the core of Genie Helper’s cookie-based authentication flow. Parameters:session_id(string, required): Session ID fromstart-sessioncookies(array, required): Array of cookie objects[{name, value, domain, path, ...}]
- Browser extension capture (primary) — user exports cookies from authenticated session
- HITL login flow — user logs in manually, Stagehand captures cookies via
get-cookies - Stored credentials — cookies retrieved from Directus
platform_sessionscollection
- Set cookies BEFORE navigating to the target site
- Ensure
domainincludes the leading dot (.onlyfans.comnotonlyfans.com) - Include all authentication cookies (not just the main token)
- Check cookie expiry before use
close-session
End and clean up a browser session. Always call this when done to free up system resources (300MB RAM per session). Parameters:session_id(string, required): Session ID to close
- Always close sessions in a
finallyblock (if using programmatically) - Don’t leave sessions idle — they consume RAM
- Close immediately after task completion
- Monitor active sessions with resource tracking
Complete Scraping Example
Here’s a full OnlyFans profile scraping workflow: 1. Start sessionsession_id
2. Set cookies (from browser extension)
Error Handling
All Stagehand tools surface HTTP errors to the agent: Session not found:observe to find available elements, or rephrase action description
Navigation failed:
Performance & Limits
Session limits:- Max concurrent sessions: ~33 (on 10GB available RAM)
- RAM per session: ~300MB
- Session timeout: Auto-closes after 30 minutes idle
start-session: ~2-3snavigate: ~1-3s (depending on page load)act: ~2-5s (AI inference + action execution)extract: ~3-8s (AI inference + data scraping)screenshot: ~0.5-1sset-cookies/get-cookies: ~0.1sclose-session: ~0.5s
- Reuse sessions for multiple operations on the same site
- Close sessions immediately when done
- Use headless mode for production (faster, less RAM)
- Batch extractions when possible (one
extractwith complex schema vs multiple calls)
Stealth Mode
All Stagehand sessions use anti-detection measures:- No
navigator.webdriverflag - Standard user agent strings
- Real browser fingerprint
- Human-like interaction timing (built into Stagehand AI)
- Use cookies from real browser sessions (not manual login automation)
- Add delays between actions (Stagehand does this automatically)
- Don’t scrape too aggressively (rate limit your requests)
- Rotate sessions periodically
