MCP ExplorerExplorer

Mcp Langchain Examples

@Aakashjammulaon 10 months ago
1 MIT
FreeCommunity
AI Systems
Integrates MCP with LangChain to build AI agents using DuckDuckGo for web searches.

Overview

What is Mcp Langchain Examples

mcp_langchain_examples is a project that demonstrates how to integrate Model Control Protocol (MCP) with LangChain to create AI agents. It utilizes DuckDuckGo for web searches, showcasing the configuration and orchestration of tools to enhance the capabilities of AI agents.

Use cases

Use cases include building AI chatbots that can perform web searches, creating intelligent assistants for information retrieval, and developing applications that require real-time data access from the web.

How to use

To use mcp_langchain_examples, install the required dependencies using either ‘uv’ or ‘pip’, configure your Google API key in a .env file, and then run the MCP server using the provided commands.

Key features

Key features include integration of MCP with LangChain, the use of DuckDuckGo for web searches, a modular project structure with clear separation of components, and the ability to extend functionality by integrating additional tools and models.

Where to use

mcp_langchain_examples can be used in fields such as AI development, web search applications, and any domain requiring intelligent agents that can interact with external tools and services.

Content

MCP LangChain Integration Example

This project serves as a tutorial on integrating Model Control Protocol (MCP) with LangChain to build AI agents. By using a web search tool (DuckDuckGo) as an example, it demonstrates how to configure and orchestrate tools to enhance the agent’s capabilities. The concepts and implementation can be extended to integrate other tools and models.

Overview

The application uses:

  • MCP (Model Control Protocol): Provides a standardized way for models to interact with external tools
  • LangChain: For agent creation and orchestration
  • Google’s Gemini 2.0 Flash: As the underlying AI model
  • DuckDuckGo Search: As an external tool for web searches

Project Structure

  • server.py: Implements the MCP server with DuckDuckGo search functionality
  • prompt.py: Contains the system prompt that instructs the AI how to respond
  • main.py: Main application that connects the model, tools, and user input
  • requirements.txt: Lists all required dependencies

Prerequisites

  • Python 3.8+
  • Google API key (for Gemini model access)

Installation

There are two methods to install the dependencies:

Method 1: Using uv (recommended)

# Install uv first if you don't have it
# For macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh

# For Windows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

# Create virtual environment and install dependencies
uv venv
uv sync

Method 2: Using pip

# Create a virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Configuration

  1. Create a .env file in the project root directory
  2. Add your Google API key:
    GOOGLE_API_KEY=your_api_key_here
    

Running the Application

Start the MCP Server

First, start the MCP server:

# Using uv
uv run .\src\server.py

# Or using Python directly (after activating virtual environment)
python .\src\server.py

Run the Main Application

In a separate terminal:

# Using uv
uv run .\src\main.py

# Or using Python directly (after activating virtual environment)
python .\src\main.py

How It Works

  1. The MCP server provides a search tool that connects to DuckDuckGo
  2. The main application creates a reactive agent using LangChain and Gemini
  3. User questions are passed to the agent
  4. The agent follows the system prompt to always use the search tool
  5. Results are formatted and returned to the user

Example Usage

When you run main.py, you’ll be prompted to enter a question. The agent will:

  1. Process your question
  2. Use the DuckDuckGo search tool to find relevant information
  3. Format and return an answer based on the search results

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers