What are MCP Integrations?
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.
Connection Options
There are two options for connecting to MCP servers:- Pass a URL directly - The simplest approach for quick setup
- Create a connection first - Gives you more control over the connection
MCP client support is currently only available in TypeScript.
Passing a URL
The simplest way to add MCP integrations is by providing server URLs directly in the agent configuration:Creating a Connection First
Alternatively, you can establish MCP connections first and then pass the client objects:Multiple Integrations
You can combine multiple MCP integrations in a single agent:Best Practices
Choose the Right Connection Approach
- Passing a URL
- Creating a Connection First
When to use:
- Simple setup requirements
- Standard API configurations
- Getting started quickly
- Minimal code required
- Automatic connection handling
- Easy to configure
Environment Variables
Always use environment variables for API keys and sensitive information:Instructions Best Practices
Provide clear instructions about available tools:- Good Instructions
- Poor Instructions
Error Handling
Implement proper error handling for MCP connections:Troubleshooting
Connection timeouts
Connection timeouts
Problem: MCP server connections timing outSolutions:
- Verify server URLs are correct and accessible
- Check network connectivity
- Ensure API keys are valid and have proper permissions
- Try connecting to servers individually to isolate issues
Tool not being used
Tool not being used
Problem: Agent not using available MCP toolsSolutions:
- Make instructions more specific about when to use tools
- Ensure API keys are properly configured
- Check that the MCP server supports the expected tools
- Verify tool descriptions are clear and actionable
Authentication errors
Authentication errors
Problem: API key or authentication failuresSolutions:
- Verify all required environment variables are set
- Check API key validity and permissions
- Ensure URLs include necessary authentication parameters
- Test MCP connections independently before using in agents