Installing mcp_use

Prerequisites: Please install Python (version 3.11 or higher) before proceeding.

1

Install the library

Install mcp_use using your preferred package manager:

pip install mcp-use
2

Install LLM provider

Choose and install your preferred LangChain provider:

pip install langchain-openai
3

Set up environment

Create a .env file for your API keys:

OPENAI_API_KEY=your_openai_key_here
ANTHROPIC_API_KEY=your_anthropic_key_here
GROQ_API_KEY=your_groq_key_here
GOOGLE_API_KEY=your_google_key_here
4

Verify installation

Test your installation with a simple script:

test_install.py
from mcp_use import MCPAgent, MCPClient
print("mcp_use installed successfully!")

Development Installation

If you want to contribute or use the latest features, install from source:

git clone https://github.com/mcp-use/mcp-use.git
cd mcp-use
pip install -e .

Installing MCP Servers

mcp_use connects to MCP servers that provide the actual tools. Here are some popular ones:

Playwright (Web Scraping)

npx @playwright/mcp@latest

Filesystem Server

pip install mcp-server-filesystem

SQLite Server

pip install mcp-server-sqlite

Check out the Awesome MCP Servers repository for a comprehensive list of available servers.

Environment Setup

Using Virtual Environments

It’s recommended to use virtual environments to avoid dependency conflicts:

python -m venv mcp_env
source mcp_env/bin/activate  # On Windows: mcp_env\Scripts\activate
pip install mcp-use langchain-openai

Environment Variables

Create a .env file in your project root:

.env
# LLM Provider Keys
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GROQ_API_KEY=gsk_...
GOOGLE_API_KEY=AI...

# Optional: Logging level
LOG_LEVEL=INFO

# Optional: MCP server paths
MCP_SERVER_PATH=/path/to/mcp/servers

Load environment variables in your Python scripts:

from dotenv import load_dotenv
import os

load_dotenv()

# Your API keys are now available as environment variables
openai_key = os.getenv("OPENAI_API_KEY")

Verification

Verify your installation works correctly:

verify_setup.py
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient

async def verify_installation():
    load_dotenv()

    # Simple configuration for testing
    config = {
        "mcpServers": {
            "test": {
                "command": "echo",
                "args": ["Hello from MCP!"]
            }
        }
    }

    try:
        client = MCPClient.from_dict(config)
        print("✅ MCPClient created successfully")

        llm = ChatOpenAI(model="gpt-3.5-turbo")
        print("✅ LLM initialized successfully")

        agent = MCPAgent(llm=llm, client=client)
        print("✅ MCPAgent created successfully")

        print("\n🎉 Installation verified! You're ready to use mcp_use.")

    except Exception as e:
        print(f"❌ Verification failed: {e}")
        print("Please check your installation and API keys.")

if __name__ == "__main__":
    asyncio.run(verify_installation())

Next Steps

Troubleshooting

Tool Calling Required: Only models with tool calling capabilities can be used with mcp_use. Make sure your chosen model supports function calling or tool use.