{"type":"mcp_server","name":"mcp-use","description":"**Concise Descriptions:**\n\n* **MCP client library for custom agent integration. Simplifies server interaction.** (79 chars)\n* **Easy MCP interaction with custom agents. Client library.** (55 chars)\n* ","category":"AI","language":"Python","stars":10173,"forks":499,"owner":"mcp-use","github_url":"https://github.com/mcp-use/mcp-use","homepage":"https://mcp-use.com","setup":"## Setup\n\n### Prerequisites\n\n*   **Python 3.11 or higher:** MCP-Use requires Python 3.11 or a later version.\n*   **MCP Implementation:** You'll need an MCP server implementation, such as Playwright MCP or a custom MCP server.\n*   **LangChain Provider:** Choose an LLM provider supported by LangChain (e.g., OpenAI, Anthropic, Groq, Llama).\n*   **E2B Account (Optional):** If you plan to use sandboxed execution, sign up for an E2B account at [e2b.dev](https://e2b.dev) to obtain an API key.\n\n### Installation\n\n1.  **Install MCP-Use:**\n\n    ```bash\n    pip install mcp-use\n    ```\n\n    Alternatively, install from source:\n\n    ```bash\n    git clone https://github.com/pietrozullo/mcp-use.git\n    cd mcp-use\n    pip install -e .\n    ```\n\n2.  **Install LangChain Provider:**\n\n    Install the LangChain package for your chosen LLM provider.  Here are a few examples:\n\n    ```bash\n    # For OpenAI\n    pip install langchain-openai\n\n    # For Anthropic\n    pip install langchain-anthropic\n\n    # For Groq\n    pip install langchain-groq\n    ```\n\n    Refer to the [LangChain chat models documentation](https://python.langchain.com/docs/integrations/chat/) for other providers.\n\n3.  **Install E2B (Optional):**\n\n    If you want to use sandboxed execution, install the `e2b-code-interpreter` package:\n\n    ```bash\n    pip install \"mcp-use[e2b]\"\n    # Or\n    pip install e2b-code-interpreter\n    ```\n\n### Configuration\n\n1.  **Environment Variables:**\n\n    Create a `.env` file in your project directory and add the API keys for your chosen LLM provider(s) and E2B (if using sandboxing).\n\n    ```\n    OPENAI_API_KEY=<your_openai_api_key>\n    ANTHROPIC_API_KEY=<your_anthropic_api_key>\n    GROQ_API_KEY=<your_groq_api_key>\n    E2B_API_KEY=<your_e2b_api_key>  # Required for sandboxed execution\n    ```\n\n    Load the environment variables in your Python script:\n\n    ```python\n    from dotenv import load_dotenv\n\n    load_dotenv()\n    ```\n\n2.  **MCP Server Configuration:**\n\n    Configure your MCP servers either programmatically or using a JSON configuration file.  Here's an example `browser_mcp.json` file:\n\n    ```json\n    {\n      \"mcpServers\": {\n        \"playwright\": {\n          \"command\": \"npx\",\n          \"args\": [\"@playwright/mcp@latest\"],\n          \"env\": {\n            \"DISPLAY\": \":1\"\n          }\n        }\n      }\n    }\n    ```\n\n    Load the configuration in your Python script:\n\n    ```python\n    from mcp_use import MCPClient\n    import os\n\n    client = MCPClient.from_config_file(os.path.join(\"browser_mcp.json\"))\n    ```\n\n    Alternatively, configure the MCP servers directly in your Python code:\n\n    ```python\n    from mcp_use import MCPClient\n\n    config = {\n        \"mcpServers\": {\n            \"playwright\": {\n                \"command\": \"npx\",\n                \"args\": [\"@playwright/mcp@latest\"],\n                \"env\": {\n                    \"DISPLAY\": \":1\"\n                }\n            }\n        }\n    }\n\n    client = MCPClient.from_dict(config)\n    ```\n\n3.  **Sandboxed Execution Configuration (Optional):**\n\n    If using sandboxed execution, configure the `SandboxOptions` when creating the `MCPClient`:\n\n    ```python\n    import os\n    from mcp_use import MCPClient\n    from mcp_use.types.sandbox import SandboxOptions\n\n    sandbox_options: SandboxOptions = {\n        \"api_key\": os.getenv(\"E2B_API_KEY\"),  # API key can also be provided directly\n        \"sandbox_template_id\": \"base\",  # Use base template\n    }\n\n    client = MCPClient(\n        config=server_config,\n        sandbox=True,\n        sandbox_options=sandbox_options,\n    )\n    ```\n\n### Important Considerations\n\n*   **Tool Calling Capability:** Ensure that the LLM you choose supports tool calling (also known as function calling).  This is essential for MCP-Use to function correctly.\n*   **MCP Server Availability:** Make sure your MCP server(s) are running and accessible before running your MCP-Use agent.\n*   **Display Environment Variable:** If using Playwright MCP or similar tools that require a display, ensure the `DISPLAY` environment variable is set correctly (e.g., `DISPLAY=:1`).","tools":"## Available Tools\n\nMCP-Use provides the following tools and features to connect LLMs to MCP servers and build custom agents:\n\n*   **Ease of Use:** Create an MCP-capable agent with just a few lines of code.\n    *   **Usage:** See the Quick Start section for a code example.\n\n*   **LLM Flexibility:** Works with any LangChain-supported LLM that supports tool calling (e.g., OpenAI, Anthropic, Groq, Llama).\n    *   **Usage:** Install the appropriate LangChain provider package (e.g., `pip install langchain-openai`).\n\n*   **Code Builder:** Explore MCP capabilities and generate starter code with the interactive [code builder](https://mcp-use.com/builder).\n\n*   **HTTP Support:** Connect directly to MCP servers running on specific HTTP ports.\n    *   **Usage:** Configure the `MCPClient` with the server's URL.\n        ```python\n        config = {\n            \"mcpServers\": {\n                \"http\": {\n                    \"url\": \"http://localhost:8931/sse\"\n                }\n            }\n        }\n        client = MCPClient.from_dict(config)\n        ```\n\n*   **Dynamic Server Selection (Server Manager):** Agents can dynamically choose the most appropriate MCP server for a given task from the available pool. This is enabled by setting `use_server_manager=True` during `MCPAgent` initialization.\n    *   **Usage:**\n        ```python\n        agent = MCPAgent(\n            llm=llm,\n            client=client,\n            use_server_manager=True\n        )\n        ```\n\n*   **Multi-Server Support:** Use multiple MCP servers simultaneously in a single agent.\n    *   **Usage:** Configure multiple servers in the `MCPClient` and optionally specify the `server_name` when calling `agent.run()`.\n        ```python\n        result = await agent.run(\n            \"Search for Airbnb listings in Barcelona\",\n            server_name=\"airbnb\"\n        )\n        ```\n\n*   **Tool Restrictions:** Restrict potentially dangerous tools like file system or network access.\n    *   **Usage:** Provide a list of disallowed tools when creating an `MCPAgent`.\n        ```python\n        agent = MCPAgent(\n            llm=llm,\n            client=client,\n            disallowed_tools=[\"file_system\", \"network\"]\n        )\n        ```\n\n*   **Custom Agents:** Build your own agents with any framework using the LangChain adapter or create new adapters.\n    *   **Usage:** Use the `LangChainAdapter` to create LangChain tools from the MCP client.\n\n*   **Sandboxed Execution:** Run MCP servers in a sandboxed environment using E2B's cloud infrastructure, eliminating the need for local dependency installation.\n    *   **Usage:** Install the `e2b-code-interpreter` dependency and configure the `MCPClient` with `sandbox=True` and `sandbox_options`.\n\n*   **Streaming Agent Output:** Asynchronously stream agent output using the `astream` method on `MCPAgent` for real-time feedback.\n    *   **Usage:**\n        ```python\n        async for chunk in agent.astream(\"Find the best restaurant in San Francisco\"):\n            print(chunk[\"messages\"], end=\"\", flush=True)\n        ```\n\n*   **Configuration File Support:** Initialize `MCPClient` from configuration files for easy management of MCP server setups.\n    *   **Usage:**\n        ```python\n        client = MCPClient.from_config_file(\"mcp-config.json\")\n        ```\n\n*   **Debugging:** Use debug mode to increase log verbosity and diagnose issues.  Enable via environment variable (`DEBUG=1` or `DEBUG=2`) or programmatically (`mcp_use.set_debug(1)` or `mcp_use.set_debug(2)`). Agent-specific verbosity can be enabled with `verbose=True` in `MCPAgent`.","faq":null,"created_at":"2025-03-28T10:06:31+00:00","updated_at":"2025-07-07T16:19:36+00:00","source_url":"https://model-context-protocol.com/servers/mcp-use","related_articles":[{"title":"mcp-use MCP Server: Simplifies Custom Agent Integration with MCP","url":"https://model-context-protocol.com/blog/mcp-use-mcp-server-simplifies-custom-agent-integration-with-mcp-mcp-server-guide"}]}