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.
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
server.py: Implements the MCP server with DuckDuckGo search functionalityprompt.py: Contains the system prompt that instructs the AI how to respondmain.py: Main application that connects the model, tools, and user inputrequirements.txt: Lists all required dependencies
- Python 3.8+
- Google API key (for Gemini model access)
There are two methods to install the dependencies:
# 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# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt- Create a
.envfile in the project root directory - Add your Google API key:
GOOGLE_API_KEY=your_api_key_here
First, start the MCP server:
# Using uv
uv run .\src\server.py
# Or using Python directly (after activating virtual environment)
python .\src\server.pyIn a separate terminal:
# Using uv
uv run .\src\main.py
# Or using Python directly (after activating virtual environment)
python .\src\main.py- The MCP server provides a search tool that connects to DuckDuckGo
- The main application creates a reactive agent using LangChain and Gemini
- User questions are passed to the agent
- The agent follows the system prompt to always use the search tool
- Results are formatted and returned to the user
When you run main.py, you'll be prompted to enter a question. The agent will:
- Process your question
- Use the DuckDuckGo search tool to find relevant information
- Format and return an answer based on the search results