MCP Crash Course
by Ayyappa054
This project demonstrates the integration of LangChain with Model Control Protocol (MCP) adapters, showcasing a system that handles mathematical calculations and weather queries through separate MCP servers. It provides examples of both single and multi-server implementations.
View on GitHub
Last updated: N/A
MCP Crash Course
A demonstration project showcasing the integration of LangChain with Model Control Protocol (MCP) adapters. This project implements a system that can handle both mathematical calculations and weather queries through separate MCP servers.
Features
- Multiple MCP server integration (math and weather servers)
- LangChain integration with OpenAI
- Async operation support
- Environment variable configuration
Prerequisites
- Python 3.x
- OpenAI API key
Installation
- Clone the repository
- Create and activate 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
.env
file and add your OpenAI API key:OPENAI_API_KEY=your_api_key_here
Project Structure
main.py
- Single server implementation using stdio clientlangchain_client.py
- Multi-server implementation using MCP clientserver/
- Directory containing MCP server implementationsmath_server.py
- Server for mathematical operationsweather_server.py
- Server for weather queries
Usage
-
Run the single server example:
python main.py
-
Run the multi-server example:
python langchain_client.py
How It Works
The project demonstrates two approaches to using MCP:
-
Single Server (main.py):
- Uses stdio client for communication
- Connects to a math server for calculations
- Implements a React agent using LangChain
-
Multi-Server (langchain_client.py):
- Uses MultiServerMCPClient for managing multiple servers
- Connects to both math and weather servers
- Allows the agent to choose appropriate tools based on the query
Dependencies
- langchain-core
- langchain-openai
- langchain-mcp-adapters
- langgraph
- python-dotenv
- mcp
License
MIT