langgraph-mcp-agents

repository·master·Indexed 20 days ago

https://github.com/braincrew-lab/langgraph-mcp-agents

A toolkit for deploying LangGraph ReAct agents that interact with external tools and data sources via the Model Context Protocol (MCP). It includes a Streamlit-based UI for real-time tool management, support for both stdio and sse transport methods, and integration with Anthropic and OpenAI models. The package provides a MultiServerMCPClient to orchestrate agents across multiple MCP servers and allows combining MCP tools with standard LangChain tools.

Tokens
7.1K
Snippets
19
Records
31
Agent score
71%

What's inside langgraph-mcp-agents

  1. What is LangGraph MCP Agents?

    master

    LangGraph MCP Agents (via LangChain-MCP-Adapters) is a toolkit that enables AI agents to interact with external tools and data sources using the Model Context Protocol (MCP). It provides a user-friendly interface to deploy ReAct agents that can access various APIs and data sources through MCP tools.

    Key features include:

    • Streamlit Interface: A web UI for interacting with LangGraph ReAct Agents.
    • Dynamic Tool Management: Add, remove, and configure MCP tools (supporting Smithery JSON format) via the UI without restarting the application.
    • Streaming Responses: Real-time visibility of agent responses and tool calls.
    • Conversation History: Tracking and managing agent dialogues.
  2. How MCP (Model Context Protocol) works

    master

    The Model Context Protocol (MCP) architecture consists of three main components:

    1. MCP Host: The program that wants to access data via MCP (e.g., Claude Desktop, IDEs, or LangChain/LangGraph).
    2. MCP Client: A protocol client that maintains a 1:1 connection with the server, acting as an intermediary between the host and the server.
    3. MCP Server: A lightweight program that exposes specific functionalities through the standardized Model Context Protocol, serving as the primary data source.
  3. How MCP Architecture works

    master

    The Model Context Protocol (MCP) architecture used in this project consists of three components:

    1. MCP Host: The program seeking access to data (e.g., LangGraph, Claude Desktop, or IDEs).
    2. MCP Client: An intermediary that maintains a 1:1 connection between the Host and the Server.
    3. MCP Server: A lightweight program that exposes specific functionalities or data sources via the standardized protocol.

    This project acts as an MCP Host using LangGraph to orchestrate agents that interact with various MCP Servers via an MCP Client.

  4. Install directly from source code

    master

    To install and run the project manually using Python:

    1. Clone the repository:
      git clone https://github.com/teddynote-lab/langgraph-mcp-agents.git
      cd langgraph-mcp-agents
    2. Create a virtual environment and install dependencies using uv:
      uv venv
      uv pip install -r requirements.txt
      source .venv/bin/activate  # Windows: .venv\Scripts\activate
    3. Configure your .env file:
      cp .env.example .env
    4. Start the Streamlit application (using the Korean version file):
      streamlit run app_KOR.py
  5. Add and Configure MCP Tools via Streamlit UI

    master

    The application provides a Streamlit interface to dynamically manage MCP tools without restarting the application.

    Steps to add a tool:

    1. Find an MCP server configuration (e.g., from Smithery).
    2. Copy the JSON configuration provided by the tool source.
    3. In the Streamlit sidebar, locate the Tool JSON section.
    4. Paste the JSON string into the Tool JSON field.
    5. Click the Add Tool button to add it to the "Registered Tools List".
    6. Crucial: Click the Apply button to initialize the agent with the newly added tools.

    Once applied, you can interact with the ReAct agent in the chat interface using these tools.

  6. Quick Start with Docker Compose

    master

    You can run the LangGraph MCP Agents application using Docker Compose without setting up a local Python environment. This requires Docker Desktop.

    Setup Steps

    1. Navigate to the dockers directory:

      cd dockers
    2. Create a .env file from the example:

      cp .env.example .env
    3. Configure your API keys in the .env file. Required/optional keys include:

      • ANTHROPIC_API_KEY: Enables Claude models (e.g., claude-3-7-sonnet-latest).
      • OPENAI_API_KEY: Enables OpenAI models (e.g., gpt-4o).
      • LANGSMITH_API_KEY: Enables LangSmith tracing.
      • USE_LOGIN: Set to true to enable login functionality.
      • USER_ID / USER_PASSWORD: Credentials if USE_LOGIN is true.
    4. Run the container based on your architecture:

    AMD64/x86_64 (Intel/AMD):

    docker compose -f docker-compose.yaml up -d

    ARM64 (Apple Silicon M1/M2/M3/M4):

    docker compose -f docker-compose-mac.yaml up -d
    1. Access the application at http://localhost:8585.
    # For AMD64
    docker compose -f docker-compose.yaml up -d
    
    # For ARM64
    docker compose -f docker-compose-mac.yaml up -d
  7. Run with Docker Compose

    master

    You can run the project using Docker without setting up a local Python environment.

    Prerequisites:

    Steps:

    1. Navigate to the dockers directory:
      cd dockers
    2. Create a .env file from the example and add your API keys:
      cp .env.example .env
    3. Select and run the appropriate Docker Compose file based on your architecture:

    For AMD64/x86_64 (Intel/AMD):

    # Run container
    docker compose -f docker-compose-KOR.yaml up -d

    For ARM64 (Apple Silicon M1/M2/M3/M4):

    # Run container
    docker compose -f docker-compose-KOR-mac.yaml up -d
    1. Access the application at http://localhost:8585 in your browser.
  8. Install LangGraph MCP Agents from Source

    master

    To install and run the project directly in a local Python environment, follow these steps:

    1. Clone the repository:
      git clone https://github.com/teddynote-lab/langgraph-mcp-agents.git
      cd langgraph-mcp-agents
    2. Create a virtual environment and install dependencies using `uv`:
       ```bash
    uv venv
    uv pip install -r requirements.txt
    source .venv/bin/activate
    1. Configure your environment variables by copying the example file:
      cp .env.example .env
       Edit `.env` with your `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, or `LANGSMITH_API_KEY`.
    4. Run the Streamlit application:
       ```bash
       streamlit run app.py
    git clone https://github.com/teddynote-lab/langgraph-mcp-agents.git
    cd langgraph-mcp-agents
    uv venv
    uv pip install -r requirements.txt
    source .venv/bin/activate
    streamlit run app.py
  9. Hands-on Tutorial: MCP and LangGraph Integration

    master

    For a deep dive into how MCP and LangGraph work together, refer to the Jupyter notebook tutorial: MCP-HandsOn-KOR.ipynb.

    Topics covered in the tutorial:

    • MCP Client Setup: Configuring and initializing MultiServerMCPClient to connect to MCP servers.
    • Local MCP Server Integration: Connecting to local servers via SSE and Stdio methods.
    • RAG Integration: Using MCP to access retriever tools for document search.
    • Mixed Transport Methods: Combining different protocols (SSE and Stdio) within a single agent.
    • LangChain Tools + MCP: Integrating native LangChain tools alongside MCP tools.
  10. Mix SSE and Stdio transport methods in MultiServerMCPClient

    master

    You can combine different transport methods (e.g., stdio for local RAG servers and sse for remote documentation servers) within a single MultiServerMCPClient instance. This allows a single LangGraph agent to access tools from diverse sources.

    client = MultiServerMCPClient(
        {
            "document-retriever": {
                "command": "./.venv/bin/python",
                "args": ["./mcp_server_rag.py"],
                "transport": "stdio",
            },
            "langchain-dev-docs": {
                "url": "https://teddynote.io/mcp/langchain/sse",
                "transport": "sse",
            },
        }
    )
    await client.__aenter__()
  11. Configure Environment Variables (.env)

    master

    The following environment variables are used to configure model providers, tracing, and authentication. Most are optional.

    VariableDescription
    ANTHROPIC_API_KEYEnables Claude models (claude-3-7-sonnet-latest, etc.)
    OPENAI_API_KEYEnables OpenAI models (gpt-4o, gpt-4o-mini)
    LANGSMITH_API_KEYEnables LangSmith tracing
    LANGSMITH_PROJECTName of the LangSmith project
    LANGSMITH_TRACINGSet to true to enable tracing
    LANGSMITH_ENDPOINTLangSmith API endpoint
    USE_LOGINSet to true to enable login/logout functionality
    USER_IDUsername for login
    USER_PASSWORDPassword for login

    Example .env configuration:

    ANTHROPIC_API_KEY=your_anthropic_api_key
    OPENAI_API_KEY=your_openai_api_key
    LANGSMITH_API_KEY=your_langsmith_api_key
    LANGSMITH_PROJECT=LangGraph-MCP-Agents
    LANGSMITH_TRACING=true
    LANGSMITH_ENDPOINT=https://api.smith.langchain.com
    
    USE_LOGIN=true
    USER_ID=admin
    USER_PASSWORD=admin123
  12. Mix SSE and Stdio MCP transports

    master

    You can combine multiple MCP servers using different transport protocols (e.g., one local stdio server and one remote sse server) within a single MultiServerMCPClient instance. This allows an agent to access both local RAG tools and remote documentation tools simultaneously.

    from langchain_mcp_adapters.client import MultiServerMCPClient
    
    client = MultiServerMCPClient(
        {
            "document-retriever": {
                "command": "./.venv/bin/python",
                "args": ["./mcp_server_rag.py"],
                "transport": "stdio",
            },
            "langchain-dev-docs": {
                "url": "https://teddynote.io/mcp/langchain/sse",
                "transport": "sse",
            },
        }
    )
    
    # Must initialize to keep both connections alive
    await client.__aenter__()
    
    # Tools from both servers are merged into one list
    tools = client.get_tools()