MCP (Model Context Protocol) integrations allow you to connect your Stagehand agents to external tools, APIs, and services. This enables agents to perform actions beyond browser automation, such as web search, database operations, and API calls.
MCP integrations make your agents more powerful by combining browser automation with external capabilities. The agent can intelligently decide when to use browser actions versus external tools.
The simplest way to add MCP integrations is by providing server URLs directly in the agent configuration:
Copy
Ask AI
const agent = stagehand.agent({ provider: "openai", model: "computer-use-preview", integrations: [ `https://mcp.exa.ai/mcp?exaApiKey=${process.env.EXA_API_KEY}`, ], instructions: `You have access to web search through Exa. Use it to find current information before browsing.`, options: { apiKey: process.env.OPENAI_API_KEY, },});await agent.execute("Search for the best headphones of 2025 and go through checkout for the top recommendation");
Alternatively, you can establish MCP connections first and then pass the client objects:
Copy
Ask AI
import { connectToMCPServer } from "@browserbasehq/stagehand";// Connect to MCP serverconst supabaseClient = await connectToMCPServer( `https://server.smithery.ai/@supabase-community/supabase-mcp/mcp?api_key=${process.env.SMITHERY_API_KEY}`);// Use the connected clientconst agent = stagehand.agent({ provider: "openai", model: "computer-use-preview", integrations: [supabaseClient], instructions: `You can interact with Supabase databases. Use these tools to store and retrieve data.`, options: { apiKey: process.env.OPENAI_API_KEY, },});await agent.execute("Search for restaurants in New Brunswick, NJ and save the first result to the database");
You can combine multiple MCP integrations in a single agent:
Copy
Ask AI
const databaseClient = await connectToMCPServer(/* database config */);const agent = stagehand.agent({ integrations: [ `https://search-service.example.com/mcp?apiKey=${process.env.SEARCH_API_KEY}`, databaseClient ], instructions: `You have access to external tools for search and data storage. Use these tools strategically to complete tasks efficiently.`});
instructions: `You have access to:1. Web search tools - Use to find current information2. Database tools - Use to store/retrieve data3. Browser automation - Use for web interactionsAlways search for current information before making decisions.Store important data for later reference.`
const agent = stagehand.agent({ integrations: [`https://mcp.exa.ai/mcp?exaApiKey=${process.env.EXA_API_KEY}`], instructions: `First search for current information, then use the browser to complete tasks based on what you find.`});await agent.execute("Find the best laptop deals for 2025 and navigate to purchase the top recommendation");
const supabaseClient = await connectToMCPServer(/* config */);const agent = stagehand.agent({ integrations: [supabaseClient], instructions: `Extract data from websites and store it using available database tools.`});await agent.execute("Extract all restaurant information from this directory and save it to the database");
const agent = stagehand.agent({ integrations: [ `https://mcp.exa.ai/mcp?exaApiKey=${process.env.EXA_API_KEY}`, supabaseClient ], instructions: `Use all available tools strategically: search for current info, browse websites, and store important data.`});await agent.execute("Research competitor pricing, compare with our site, and store the analysis");