A minimal Next.js app that turns the Browserless MCP server into a chat-driven browser-automation agent. Ask it to screenshot a page, scrape content, crawl a site, or run a custom function in a headless browser — the LLM picks the right MCP tool and returns the result.
- Next.js 15 (App Router) + React 19 + TypeScript
@modelcontextprotocol/sdkStreamable-HTTP client- Vercel AI SDK (
experimental_createMCPClient+streamText) orchestrating the tool loop - A simple chat UI that streams responses and shows which MCP tool ran
- A Browserless API key — grab one at account.browserless.io.
- An OpenAI API key — platform.openai.com/api-keys. (Swap in any provider supported by the Vercel AI SDK by editing
app/api/chat/route.ts.)
npm install
cp .env.example .env.local
# edit .env.local and fill in BROWSERLESS_API_KEY and OPENAI_API_KEY
npm run devOpen http://localhost:3000 and try:
search good python tutorialsgo to all recipes and find a good for eggplant ratatouilleextract all links from browserless.io
app/api/chat/route.ts is the server-side bridge:
- Opens a
StreamableHTTPClientTransporttohttps://mcp.browserless.io/mcpwithAuthorization: Bearer ${BROWSERLESS_API_KEY}. - Calls
experimental_createMCPClient({ transport })andmcpClient.tools()to discover every tool the Browserless MCP server exposes (agent, scrape, screenshot, crawl, search, function, …). - Passes those tools into
streamText({ model, messages, tools, maxSteps: 8 })— the LLM decides when to call them. - Streams the result back to the React client via
useChat().
| Variable | Required | Description |
|---|---|---|
BROWSERLESS_API_KEY |
yes | Sent as Authorization: Bearer … to the MCP server |
OPENAI_API_KEY |
yes | Drives the agent loop. Replace with another provider in route.ts if you prefer |
- Swap models / providers: replace
openai('gpt-4o-mini')with anyaiSDK model (@ai-sdk/anthropic,@ai-sdk/google, etc.). - Narrow the tool surface: filter
toolsbefore passing tostreamText— e.g.{ browserless_screenshot: tools.browserless_screenshot }. - Persist sessions: Browserless MCP supports session headers; add them in the
StreamableHTTPClientTransportrequestInit.headers.
MIT