# Directory Structure
```
├── browser_use_mcp
│ ├── __init__.py
│ ├── __main__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-311.pyc
│ │ ├── main.cpython-311.pyc
│ │ └── models.cpython-311.pyc
│ ├── main.py
│ └── models.py
├── example.py
├── public
│ └── light.svg
├── pyproject.toml
└── README.md
```
# Files
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
```markdown
<div align="center">
<br />
<br />
<img src="public/light.svg" alt="Browser Use MCP Server" width="100%">
<br />
<br />
</div>
# Browser Use MCP Server
A FastMCP server that enables browser automation through natural language commands. This server allows Language Models to browse the web, fill out forms, click buttons, and perform other web-based tasks via a simple API.
## Quick Start
### 1. Install the package
Install with a specific provider (e.g., OpenAI)
```bash
pip install -e "git+https://github.com/yourusername/browser-use-mcp.git#egg=browser-use-mcp[openai]"
```
Or install all providers
```bash
pip install -e "git+https://github.com/yourusername/browser-use-mcp.git#egg=browser-use-mcp[all-providers]"
```
Install Playwright browsers
```bash
playwright install chromium
```
### 2. Configure your MCP client
Add the browser-use-mcp server to your MCP client configuration:
```javascript
{
"mcpServers": {
"browser-use-mcp": {
"command": "browser-use-mcp",
"args": ["--model", "gpt-4o"],
"env": {
"OPENAI_API_KEY": "your-openai-api-key", // Or any other provider's API key
"DISPLAY": ":0" // For GUI environments
}
}
}
}
```
Replace `"your-openai-api-key"` with your actual API key or use an environment variable reference like `process.env.OPENAI_API_KEY`.
### 3. Use it with your favorite MCP client
#### Example using mcp-use with Python
```python
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
# Load environment variables
load_dotenv()
# Create MCPClient from config file
client = MCPClient(
config={
"mcpServers": {
"browser-use-mcp": {
"command": "browser-use-mcp",
"args": ["--model", "gpt-4o"],
"env": {
"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY"),
"DISPLAY": ":0",
},
}
}
}
)
# Create LLM
llm = ChatOpenAI(model="gpt-4o")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# Run the query
result = await agent.run(
"""
Navigate to https://github.com, search for "browser-use-mcp", and summarize the project.
""",
max_steps=30,
)
print(f"\nResult: {result}")
if __name__ == "__main__":
asyncio.run(main())
```
#### Using Claude for Desktop
1. Open Claude for Desktop
2. Go to Settings → Experimental features
3. Enable Claude API Beta and OpenAPI schema for API
4. Add the following configuration to your Claude Desktop config file:
- Mac: `~/Library/Application Support/Claude/claude_desktop_config.json`
- Windows: `%AppData%\Claude\claude_desktop_config.json`
```json
{
"mcpServers": {
"browser-use": {
"command": "browser-use-mcp",
"args": ["--model", "claude-3-opus-20240229"]
}
}
}
```
5. Start a new conversation with Claude and ask it to perform web tasks
## Supported LLM Providers
The following LLM providers are supported for browser automation:
| Provider | API Key Environment Variable |
|----------|----------------------------|
| OpenAI | `OPENAI_API_KEY` |
| Anthropic | `ANTHROPIC_API_KEY` |
| Google | `GOOGLE_API_KEY` |
| Cohere | `COHERE_API_KEY` |
| Mistral AI | `MISTRAL_API_KEY` |
| Groq | `GROQ_API_KEY` |
| Together AI | `TOGETHER_API_KEY` |
| AWS Bedrock | `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` |
| Fireworks | `FIREWORKS_API_KEY` |
| Azure OpenAI | `AZURE_OPENAI_API_KEY` and `AZURE_OPENAI_ENDPOINT` |
| Vertex AI | `GOOGLE_APPLICATION_CREDENTIALS` |
| NVIDIA | `NVIDIA_API_KEY` |
| AI21 | `AI21_API_KEY` |
| Databricks | `DATABRICKS_HOST` and `DATABRICKS_TOKEN` |
| IBM watsonx.ai | `WATSONX_API_KEY` |
| xAI | `XAI_API_KEY` |
| Upstage | `UPSTAGE_API_KEY` |
| Hugging Face | `HUGGINGFACE_API_KEY` |
| Ollama | `OLLAMA_BASE_URL` |
| Llama.cpp | `LLAMA_CPP_SERVER_URL` |
For more information check out: https://python.langchain.com/docs/integrations/chat/
You can create a `.env` file in the project directory with your API keys:
```
OPENAI_API_KEY=your_openai_key_here
# Or any other provider key
```
## Troubleshooting
- **API Key Issues**: Ensure your API key is correctly set in your environment variables or `.env` file.
- **Provider Not Found**: Make sure you've installed the required provider package.
- **Browser Automation Errors**: Check that Playwright is correctly installed with `playwright install chromium`.
- **Model Selection**: If you get errors about an invalid model, try using the `--model` flag to specify a valid model for your provider.
- **Debug Mode**: Use `--debug` to enable more detailed logging that can help identify issues.
- **MCP Client Configuration**: Make sure your MCP client is correctly configured with the right command and environment variables.
## License
MIT # browser-use-mcp
```
--------------------------------------------------------------------------------
/browser_use_mcp/__main__.py:
--------------------------------------------------------------------------------
```python
#!/usr/bin/env python3
"""
Main entry point for browser-use-mcp
"""
from .main import main
if __name__ == "__main__":
main()
```
--------------------------------------------------------------------------------
/browser_use_mcp/__init__.py:
--------------------------------------------------------------------------------
```python
"""Browser Use MCP Server package."""
__version__ = "0.1.0"
from browser_use_mcp.models import get_llm
from browser_use_mcp.main import main, run_browser_use_task
```
--------------------------------------------------------------------------------
/example.py:
--------------------------------------------------------------------------------
```python
"""
Basic usage example for mcp_use.
This example demonstrates how to use the mcp_use library with MCPClient
to connect any LLM to MCP tools through a unified interface.
Special thanks to https://github.com/microsoft/playwright-mcp for the server.
"""
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
"""Run the example using a configuration file."""
# Load environment variables
load_dotenv()
# Create MCPClient from config file
client = MCPClient(
config={
"mcpServers": {
"browser-use-mcp": {
"command": "browser-use-mcp",
"args": ["--model", "gpt-4o"],
"env": {
"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY"),
"DISPLAY": ":0",
},
}
}
}
)
# Create LLM
llm = ChatOpenAI(model="gpt-4o")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30)
# Run the query
result = await agent.run(
"""
Navigate to https://github.com/mcp-use/mcp-use, give a star to the project and write
a summary of the project.
""",
max_steps=30,
)
print(f"\nResult: {result}")
if __name__ == "__main__":
# Run the appropriate example
asyncio.run(main())
```
--------------------------------------------------------------------------------
/browser_use_mcp/main.py:
--------------------------------------------------------------------------------
```python
from mcp.server.fastmcp import FastMCP
from browser_use import Agent
import logging
import argparse
# Import the get_llm function from models.py
from browser_use_mcp.models import get_llm
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# Initialize FastMCP server
mcp = FastMCP("browser-use")
# Global variable for the LLM instance
llm = None
@mcp.tool()
async def run_browser_use_task(task: str) -> str:
"""Run a browser automation task described in natural language using browser_use Agent, it can perform many tasks like browsing the web, searching the web, and more.
Args:
task: Natural language description of the task to perform
"""
try:
# Log that we're starting the task
logger.info(f"Starting browser task: {task}")
# Create and run the browser_use agent
agent = Agent(task=task, llm=llm)
result = await agent.run()
return f"Task completed: {result}"
except Exception as e:
logger.error(f"Error in async browser task: {str(e)}")
return f"Error performing task: {str(e)}"
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Browser Use MCP Server")
parser.add_argument(
"--model",
"-m",
type=str,
default=None,
help="Specify the model to use. Overrides the default model for the provider.",
)
return parser.parse_args()
def main():
"""Entry point for the command-line tool"""
args = parse_args()
# Initialize the LLM with the specified model
global llm
try:
llm = get_llm(model_name=args.model)
except ValueError as e:
logger.error(f"Error initializing LLM: {str(e)}")
return 1
logger.info("Starting browser-use-mcp server")
# Initialize and run the server
mcp.run(transport="stdio")
if __name__ == "__main__":
main()
```
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
```toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "browser-use-mcp"
version = "0.1.0"
description = "Browser automation MCP server using browser-use"
authors = [
{name = "Pietro Zullo"}
]
readme = "README.md"
requires-python = ">=3.11"
license = {text = "MIT"}
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.11",
]
dependencies = [
"mcp[cli]",
"playwright",
"browser-use",
"python-dotenv",
# Core language model providers
"langchain-openai",
"langchain-anthropic",
"langchain-google-genai",
"langchain-cohere",
"langchain-mistralai",
"langchain-groq",
"langchain-community",
]
[project.optional-dependencies]
# Core LLM providers
openai = ["langchain-openai"]
anthropic = ["langchain-anthropic"]
google = ["langchain-google-genai"]
cohere = ["langchain-cohere"]
mistral = ["langchain-mistralai"]
groq = ["langchain-groq"]
together = ["langchain-together"]
# Cloud providers
aws = ["langchain-aws"]
azure = ["langchain-openai"]
vertex = ["langchain-google-vertexai"]
fireworks = ["langchain-fireworks"]
nvidia = ["langchain-nvidia-ai-endpoints"]
databricks = ["langchain-databricks"]
ai21 = ["langchain-ai21"]
ibm = ["langchain-ibm"]
xai = ["langchain-xai"]
upstage = ["langchain-upstage"]
# Local models
huggingface = ["langchain-huggingface"]
ollama = ["langchain-ollama"]
llama-cpp = ["langchain-community"]
# Group packages
local-models = ["langchain-huggingface", "langchain-ollama", "langchain-community"]
cloud-providers = [
"langchain-aws",
"langchain-google-vertexai",
"langchain-fireworks",
"langchain-nvidia-ai-endpoints",
"langchain-databricks",
"langchain-ai21",
"langchain-ibm",
"langchain-xai",
"langchain-upstage"
]
all-providers = [
# Core language model providers
"langchain-openai",
"langchain-anthropic",
"langchain-google-genai",
"langchain-cohere",
"langchain-mistralai",
"langchain-groq",
"langchain-together",
# Cloud providers
"langchain-aws",
"langchain-fireworks",
"langchain-google-vertexai",
"langchain-nvidia-ai-endpoints",
"langchain-ai21",
"langchain-databricks",
"langchain-ibm",
"langchain-xai",
"langchain-upstage",
# Local models
"langchain-huggingface",
"langchain-ollama",
"langchain-community",
]
dev = [
"black",
"isort",
"pytest",
"pytest-asyncio",
]
[project.scripts]
browser-use-mcp = "browser_use_mcp.main:main"
[tool.hatch.build.targets.wheel]
packages = ["browser_use_mcp"]
[tool.pytest.ini_options]
asyncio_mode = "strict"
asyncio_default_fixture_loop_scope = "function"
[tool.black]
line-length = 88
target-version = ["py311"]
[tool.isort]
profile = "black"
line_length = 88
```
--------------------------------------------------------------------------------
/public/light.svg:
--------------------------------------------------------------------------------
```
<svg width="1867" height="292" viewBox="0 0 1867 292" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M266.265 106.202C224.72 90.3033 258.998 29.4218 201.857 30.9671C132.032 32.8557 22.6176 142.669 33.7922 214.023C39.3453 249.357 66.9381 234.523 79.8952 239.674C88.499 243.073 98.6794 258.423 109.683 260.861C61.0078 288.846 -1.51452 262.2 0.0279922 202.211C2.25607 113.859 129.873 -14.2905 220.847 1.29899C271.647 10.0209 277.954 64.0347 266.265 106.202Z" fill="black"/>
<path d="M67.4872 157.091L107.73 195.069C133.37 218.11 201.446 258.32 232.296 231.056C268.665 198.915 191.746 87.9001 156.337 70.1817L187.427 56.2061C235.862 91.6086 309.49 206.676 249.504 256.775C210.804 289.087 148.418 261.72 111.741 237.649C83.8041 219.312 43.4241 193.009 67.4872 157.057V157.091Z" fill="black"/>
<path d="M109.717 12.9395C96.6917 13.2829 86.511 30.246 76.9474 33.2334C68.4465 35.8774 56.6886 28.6321 43.9029 38.2125C30.0546 48.5826 34.7861 98.0981 8.22063 110.426C-8.12999 62.3865 2.04951 5.11049 61.1106 0.200137C65.6695 -0.177582 116.367 3.1876 109.717 12.9053V12.9395Z" fill="black"/>
<path d="M206.999 157.091C228.8 193.214 189.277 234.489 156.37 201.559L206.999 157.091Z" fill="black"/>
<path d="M504.359 178.759C504.359 195.08 498.53 206.701 486.872 213.621C475.289 220.54 460.397 224 442.195 224H393.795V67.9692H439.374C456.523 67.9692 470.513 71.053 481.344 77.2205C492.174 83.388 497.59 93.5419 497.59 107.682C497.59 116.708 494.882 124.079 489.467 129.795C484.051 135.511 477.169 139.385 468.821 141.415C478.899 142.995 487.323 146.718 494.092 152.585C500.937 158.451 504.359 167.176 504.359 178.759ZM466.338 110.39C466.338 103.47 464.157 98.4308 459.795 95.2718C455.508 92.1128 449.002 90.5333 440.277 90.5333H424.708V131.713H441.631C450.13 131.713 456.373 130.021 460.359 126.636C464.345 123.251 466.338 117.836 466.338 110.39ZM472.318 177.969C472.318 169.019 469.836 162.701 464.872 159.015C459.908 155.33 452.913 153.487 443.887 153.487H424.708V200.872H442.533C451.409 200.872 458.591 199.255 464.082 196.021C469.573 192.711 472.318 186.694 472.318 177.969ZM619.064 101.251C622.675 101.251 625.946 101.552 628.88 102.154C631.888 102.68 634.859 103.47 637.793 104.523L632.49 151.118H612.634V127.764C605.037 128.366 598.381 131.788 592.664 138.031C587.023 144.274 582.548 152.735 579.239 163.415V203.354H603.044V224H532.644V203.354H549.454V125.056H532.644V104.523H572.131L577.659 131.938C582.097 121.634 587.625 113.962 594.244 108.923C600.938 103.809 609.211 101.251 619.064 101.251ZM716.508 101.138C728.242 101.138 738.283 103.733 746.631 108.923C754.98 114.038 761.298 121.333 765.585 130.81C769.948 140.212 772.129 151.268 772.129 163.979C772.129 176.916 769.948 188.161 765.585 197.713C761.223 207.19 754.83 214.523 746.406 219.713C738.057 224.827 728.054 227.385 716.395 227.385C704.662 227.385 694.621 224.865 686.272 219.826C677.924 214.786 671.568 207.528 667.206 198.051C662.843 188.574 660.662 177.292 660.662 164.205C660.662 151.72 662.843 140.738 667.206 131.262C671.643 121.709 678.036 114.301 686.385 109.036C694.809 103.771 704.85 101.138 716.508 101.138ZM716.508 123.59C708.084 123.59 701.842 126.899 697.78 133.518C693.719 140.062 691.688 150.291 691.688 164.205C691.688 178.27 693.719 188.574 697.78 195.118C701.842 201.662 708.047 204.933 716.395 204.933C724.744 204.933 730.949 201.662 735.011 195.118C739.072 188.499 741.103 178.12 741.103 163.979C741.103 150.215 739.072 140.062 735.011 133.518C730.949 126.899 724.782 123.59 716.508 123.59ZM917.521 104.523L899.244 224H864.27L852.198 141.077L839.337 224H805.039L785.973 104.523H813.952L824.332 205.497L839.111 119.641H866.526L880.065 205.497L890.67 104.523H917.521ZM983.037 205.385C990.182 205.385 995.823 204.181 999.96 201.774C1004.17 199.292 1006.28 195.832 1006.28 191.395C1006.28 188.311 1005.56 185.791 1004.13 183.836C1002.71 181.88 999.96 180.038 995.899 178.308C991.912 176.503 985.858 174.509 977.734 172.328C969.461 170.222 962.541 167.74 956.975 164.882C951.41 161.949 947.085 158.188 944.001 153.6C940.917 148.937 939.375 143.221 939.375 136.451C939.375 129.532 941.331 123.402 945.242 118.062C949.228 112.721 954.945 108.585 962.391 105.651C969.912 102.643 978.787 101.138 989.017 101.138C1007.22 101.138 1022.75 105.802 1035.61 115.128L1023.43 133.292C1012.52 126.373 1001.28 122.913 989.693 122.913C976.305 122.913 969.611 126.749 969.611 134.421C969.611 137.053 970.401 139.234 971.981 140.964C973.635 142.694 976.456 144.349 980.442 145.928C984.504 147.508 990.709 149.463 999.058 151.795C1007.63 154.202 1014.63 156.834 1020.04 159.692C1025.53 162.55 1029.78 166.349 1032.79 171.087C1035.87 175.826 1037.42 181.88 1037.42 189.251C1037.42 197.525 1034.93 204.557 1029.97 210.349C1025.08 216.065 1018.54 220.352 1010.34 223.21C1002.14 225.993 993.078 227.385 983.15 227.385C972.319 227.385 962.654 225.843 954.155 222.759C945.656 219.675 938.322 215.426 932.155 210.01L947.611 192.636C952.5 196.547 957.953 199.668 963.97 202C969.987 204.256 976.343 205.385 983.037 205.385ZM1098.31 173.344C1099.13 184.099 1102.29 192.072 1107.78 197.262C1113.35 202.451 1120.46 205.046 1129.11 205.046C1134.6 205.046 1139.86 204.181 1144.9 202.451C1149.94 200.721 1155.02 198.164 1160.13 194.779L1172.54 211.815C1166.75 216.704 1159.98 220.54 1152.23 223.323C1144.49 226.031 1136.14 227.385 1127.19 227.385C1114.48 227.385 1103.65 224.752 1094.7 219.487C1085.82 214.222 1079.09 206.851 1074.5 197.374C1069.99 187.897 1067.73 176.916 1067.73 164.431C1067.73 152.472 1069.95 141.716 1074.39 132.164C1078.9 122.537 1085.37 114.978 1093.79 109.487C1102.29 103.921 1112.3 101.138 1123.8 101.138C1134.63 101.138 1144.07 103.545 1152.12 108.359C1160.17 113.173 1166.37 120.13 1170.74 129.231C1175.1 138.256 1177.28 149.012 1177.28 161.497C1177.28 165.785 1177.09 169.733 1176.72 173.344H1098.31ZM1123.92 122.123C1116.47 122.123 1110.45 124.793 1105.87 130.133C1101.35 135.398 1098.72 143.446 1097.97 154.277H1148.29C1148.14 143.973 1145.99 136.038 1141.86 130.472C1137.79 124.906 1131.81 122.123 1123.92 122.123ZM1295.82 101.251C1299.43 101.251 1302.7 101.552 1305.64 102.154C1308.65 102.68 1311.62 103.47 1314.55 104.523L1309.25 151.118H1289.39V127.764C1281.79 128.366 1275.14 131.788 1269.42 138.031C1263.78 144.274 1259.31 152.735 1256 163.415V203.354H1279.8V224H1209.4V203.354H1226.21V125.056H1209.4V104.523H1248.89L1254.42 131.938C1258.85 121.634 1264.38 113.962 1271 108.923C1277.7 103.809 1285.97 101.251 1295.82 101.251ZM1584.35 172.328C1584.35 183.084 1582.17 192.636 1577.81 200.985C1573.52 209.258 1567.16 215.726 1558.74 220.39C1550.32 225.053 1540.2 227.385 1528.39 227.385C1516.43 227.385 1506.28 225.091 1497.93 220.503C1489.58 215.915 1483.26 209.484 1478.98 201.21C1474.76 192.937 1472.66 183.309 1472.66 172.328V67.9692H1503.57V163.641C1503.57 172.817 1504.36 180.301 1505.94 186.092C1507.52 191.884 1510.12 196.246 1513.73 199.179C1517.34 202.113 1522.22 203.579 1528.39 203.579C1534.56 203.579 1539.45 202.113 1543.06 199.179C1546.74 196.246 1549.38 191.884 1550.96 186.092C1552.54 180.301 1553.33 172.817 1553.33 163.641V67.9692H1584.35V172.328ZM1659.79 205.385C1666.94 205.385 1672.58 204.181 1676.72 201.774C1680.93 199.292 1683.04 195.832 1683.04 191.395C1683.04 188.311 1682.32 185.791 1680.89 183.836C1679.46 181.88 1676.72 180.038 1672.66 178.308C1668.67 176.503 1662.62 174.509 1654.49 172.328C1646.22 170.222 1639.3 167.74 1633.73 164.882C1628.17 161.949 1623.84 158.188 1620.76 153.6C1617.68 148.937 1616.13 143.221 1616.13 136.451C1616.13 129.532 1618.09 123.402 1622 118.062C1625.99 112.721 1631.7 108.585 1639.15 105.651C1646.67 102.643 1655.55 101.138 1665.77 101.138C1683.98 101.138 1699.51 105.802 1712.37 115.128L1700.18 133.292C1689.28 126.373 1678.03 122.913 1666.45 122.913C1653.06 122.913 1646.37 126.749 1646.37 134.421C1646.37 137.053 1647.16 139.234 1648.74 140.964C1650.39 142.694 1653.21 144.349 1657.2 145.928C1661.26 147.508 1667.47 149.463 1675.82 151.795C1684.39 154.202 1691.38 156.834 1696.8 159.692C1702.29 162.55 1706.54 166.349 1709.55 171.087C1712.63 175.826 1714.17 181.88 1714.17 189.251C1714.17 197.525 1711.69 204.557 1706.73 210.349C1701.84 216.065 1695.3 220.352 1687.1 223.21C1678.9 225.993 1669.84 227.385 1659.91 227.385C1649.08 227.385 1639.41 225.843 1630.91 222.759C1622.41 219.675 1615.08 215.426 1608.91 210.01L1624.37 192.636C1629.26 196.547 1634.71 199.668 1640.73 202C1646.75 204.256 1653.1 205.385 1659.79 205.385ZM1775.06 173.344C1775.89 184.099 1779.05 192.072 1784.54 197.262C1790.11 202.451 1797.21 205.046 1805.86 205.046C1811.35 205.046 1816.62 204.181 1821.66 202.451C1826.7 200.721 1831.78 198.164 1836.89 194.779L1849.3 211.815C1843.51 216.704 1836.74 220.54 1828.99 223.323C1821.25 226.031 1812.9 227.385 1803.95 227.385C1791.24 227.385 1780.4 224.752 1771.45 219.487C1762.58 214.222 1755.85 206.851 1751.26 197.374C1746.75 187.897 1744.49 176.916 1744.49 164.431C1744.49 152.472 1746.71 141.716 1751.15 132.164C1755.66 122.537 1762.13 114.978 1770.55 109.487C1779.05 103.921 1789.05 101.138 1800.56 101.138C1811.39 101.138 1820.83 103.545 1828.88 108.359C1836.93 113.173 1843.13 120.13 1847.5 129.231C1851.86 138.256 1854.04 149.012 1854.04 161.497C1854.04 165.785 1853.85 169.733 1853.47 173.344H1775.06ZM1800.67 122.123C1793.23 122.123 1787.21 124.793 1782.62 130.133C1778.11 135.398 1775.48 143.446 1774.73 154.277H1825.04C1824.89 143.973 1822.75 136.038 1818.61 130.472C1814.55 124.906 1808.57 122.123 1800.67 122.123Z" fill="black"/>
</svg>
```
--------------------------------------------------------------------------------
/browser_use_mcp/models.py:
--------------------------------------------------------------------------------
```python
"""
Module containing LLM provider selection and initialization logic.
"""
from langchain.chat_models.base import BaseChatModel
import os
import logging
from typing import Optional
logger = logging.getLogger(__name__)
def get_llm(model_name: Optional[str] = None) -> BaseChatModel:
"""
Initialize and return a LangChain chat model based on available API keys.
The function checks for various API keys in the environment and initializes
the appropriate model if the key is found. Only one model will be initialized
based on priority. All models in this function support both tool calling and
structured output.
Args:
model_name: Optional model name to override the default model for the provider.
Examples: "gpt-4", "claude-3-haiku-20240307", "gemini-1.0-pro", etc.
Returns:
BaseChatModel: An instance of a LangChain chat model
"""
# Check for OpenAI API key
if os.environ.get("OPENAI_API_KEY"):
from langchain_openai import ChatOpenAI
use_model = model_name if model_name else "gpt-4o"
logger.info(f"Using OpenAI {use_model}")
return ChatOpenAI(model=use_model)
# Check for Anthropic API key
elif os.environ.get("ANTHROPIC_API_KEY"):
from langchain_anthropic import ChatAnthropic
use_model = model_name if model_name else "claude-3-opus-20240229"
logger.info(f"Using Anthropic {use_model}")
return ChatAnthropic(model=use_model)
# Check for Google API key
elif os.environ.get("GOOGLE_API_KEY"):
from langchain_google_genai import ChatGoogleGenerativeAI
use_model = model_name if model_name else "gemini-1.5-pro"
logger.info(f"Using Google {use_model}")
return ChatGoogleGenerativeAI(model=use_model)
# Check for Cohere API key
elif os.environ.get("COHERE_API_KEY"):
from langchain_cohere import ChatCohere
use_model = model_name if model_name else "command-r-plus"
logger.info(f"Using Cohere {use_model}")
return ChatCohere(model=use_model)
# Check for Mistral API key
elif os.environ.get("MISTRAL_API_KEY"):
from langchain_mistralai import ChatMistralAI
use_model = model_name if model_name else "mistral-large-latest"
logger.info(f"Using Mistral {use_model}")
return ChatMistralAI(model=use_model)
# Check for Groq API key
elif os.environ.get("GROQ_API_KEY"):
from langchain_groq import ChatGroq
use_model = model_name if model_name else "llama3-70b-8192"
logger.info(f"Using Groq {use_model}")
return ChatGroq(model=use_model)
# Check for Together API key
elif os.environ.get("TOGETHER_API_KEY"):
from langchain_together import ChatTogether
use_model = model_name if model_name else "meta-llama/Llama-3-70b-chat"
logger.info(f"Using Together AI {use_model}")
return ChatTogether(model=use_model)
# Check for AWS Bedrock
elif os.environ.get("AWS_ACCESS_KEY_ID") and os.environ.get(
"AWS_SECRET_ACCESS_KEY"
):
from langchain_aws import ChatBedrock
use_model = model_name if model_name else "anthropic.claude-3-sonnet-20240229"
logger.info(f"Using AWS Bedrock {use_model}")
return ChatBedrock(model_id=use_model)
# Check for Fireworks API key
elif os.environ.get("FIREWORKS_API_KEY"):
from langchain_fireworks import ChatFireworks
use_model = (
model_name if model_name else "accounts/fireworks/models/llama-v3-70b-chat"
)
logger.info(f"Using Fireworks {use_model}")
return ChatFireworks(model=use_model)
# Check for Azure OpenAI API key
elif os.environ.get("AZURE_OPENAI_API_KEY") and os.environ.get(
"AZURE_OPENAI_ENDPOINT"
):
from langchain_openai import AzureChatOpenAI
model_name = (
model_name
if model_name
else os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4")
)
logger.info(f"Using Azure OpenAI {model_name}")
return AzureChatOpenAI(
azure_deployment=model_name,
openai_api_version=os.environ.get("AZURE_OPENAI_API_VERSION", "2023-05-15"),
)
# Check for Vertex AI
elif os.environ.get("GOOGLE_APPLICATION_CREDENTIALS"):
try:
from langchain_google_vertexai import ChatVertexAI
use_model = model_name if model_name else "gemini-1.5-pro"
logger.info(f"Using Google Vertex AI {use_model}")
return ChatVertexAI(model_name=use_model)
except Exception as e:
logger.warning(f"Failed to initialize Google Vertex AI: {e}")
# Check for NVIDIA AI Endpoints
elif os.environ.get("NVIDIA_API_KEY"):
try:
from langchain_nvidia_ai_endpoints import ChatNVIDIA
use_model = model_name if model_name else "meta/llama3-70b-instruct"
logger.info(f"Using NVIDIA AI {use_model}")
return ChatNVIDIA(model=use_model)
except Exception as e:
logger.warning(f"Failed to initialize NVIDIA AI: {e}")
# Check for AI21 API key
elif os.environ.get("AI21_API_KEY"):
try:
from langchain_ai21 import ChatAI21
use_model = model_name if model_name else "j2-ultra"
logger.info(f"Using AI21 {use_model}")
return ChatAI21(model=use_model)
except Exception as e:
logger.warning(f"Failed to initialize AI21: {e}")
# Check for Databricks
elif os.environ.get("DATABRICKS_HOST") and os.environ.get("DATABRICKS_TOKEN"):
try:
from langchain_databricks import ChatDatabricks
use_model = model_name if model_name else "databricks-llama-3-70b"
logger.info(f"Using Databricks {use_model}")
return ChatDatabricks(endpoint=use_model)
except Exception as e:
logger.warning(f"Failed to initialize Databricks: {e}")
# Check for IBM watsonx.ai
elif os.environ.get("WATSONX_API_KEY"):
try:
from langchain_ibm import ChatWatsonx
use_model = model_name if model_name else "meta-llama/llama-3-70b-instruct"
logger.info(f"Using IBM Watsonx {use_model}")
return ChatWatsonx(model_id=use_model)
except Exception as e:
logger.warning(f"Failed to initialize IBM Watsonx: {e}")
# Check for xAI
elif os.environ.get("XAI_API_KEY"):
try:
from langchain_xai import ChatXAI
use_model = model_name if model_name else "grok-1"
logger.info(f"Using xAI {use_model}")
return ChatXAI(model=use_model)
except Exception as e:
logger.warning(f"Failed to initialize xAI: {e}")
# Check for Upstage
elif os.environ.get("UPSTAGE_API_KEY"):
try:
from langchain_upstage import ChatUpstage
use_model = model_name if model_name else "solar-1-mini-chat"
logger.info(f"Using Upstage {use_model}")
return ChatUpstage(model_name=use_model)
except Exception as e:
logger.warning(f"Failed to initialize Upstage: {e}")
# Check for Hugging Face API key (local models supported)
elif os.environ.get("HUGGINGFACEHUB_API_TOKEN"):
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
use_model = model_name if model_name else "meta-llama/Llama-3-8b-chat-hf"
logger.info(f"Using Hugging Face {use_model}")
llm = HuggingFaceEndpoint(
repo_id=use_model,
task="text-generation",
max_new_tokens=512,
)
return ChatHuggingFace(llm=llm)
# Check for Ollama (local models)
elif (
os.environ.get("OLLAMA_HOST")
or os.path.exists("/usr/local/bin/ollama")
or os.path.exists("/usr/bin/ollama")
):
try:
from langchain_ollama import ChatOllama
use_model = model_name if model_name else "llama3"
logger.info(f"Using Ollama {use_model}")
return ChatOllama(model=use_model)
except Exception as e:
logger.warning(f"Failed to initialize Ollama: {e}")
# Check for llama.cpp (local models)
elif os.environ.get("LLAMA_CPP_MODEL_PATH"):
try:
from langchain_community.chat_models import ChatLlamaCpp
model_path = (
model_name if model_name else os.environ.get("LLAMA_CPP_MODEL_PATH")
)
logger.info(f"Using Llama.cpp with model at {model_path}")
return ChatLlamaCpp(model_path=model_path)
except Exception as e:
logger.warning(f"Failed to initialize Llama.cpp: {e}")
# Fallback to error if no API keys are available
else:
raise ValueError(
"No API keys found. Please set one of the following environment variables:\n"
"- OPENAI_API_KEY\n"
"- ANTHROPIC_API_KEY\n"
"- GOOGLE_API_KEY\n"
"- COHERE_API_KEY\n"
"- MISTRAL_API_KEY\n"
"- GROQ_API_KEY\n"
"- TOGETHER_API_KEY\n"
"- AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY\n"
"- FIREWORKS_API_KEY\n"
"- AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT\n"
"- GOOGLE_APPLICATION_CREDENTIALS (for Vertex AI)\n"
"- NVIDIA_API_KEY\n"
"- AI21_API_KEY\n"
"- DATABRICKS_HOST and DATABRICKS_TOKEN\n"
"- WATSONX_API_KEY\n"
"- XAI_API_KEY\n"
"- UPSTAGE_API_KEY\n"
"- HUGGINGFACEHUB_API_TOKEN\n"
"- OLLAMA_HOST (for local models)\n"
"- LLAMA_CPP_MODEL_PATH (for local models)"
)
```