This project demonstrates how to build and connect to Model Context Protocol (MCP) servers using fastmcp and langchain.
The project consists of three main components:
- Calculator MCP Server (
Calculator_MCP_Server.py): A FastMCP server exposing basic arithmetic operations (add, subtract, multiply, divide). - Weather MCP Server (
Weather_MCP_Server.py): A FastMCP server that fetches current weather information for a given city using the OpenWeather API. - LLM Client (
client.py): A LangChain-powered conversational agent that connects to both MCP servers and can intelligently invoke their tools based on user prompts.
- Python 3.10+
- An OpenRouter API Key for the LLM client.
- An OpenWeather API Key for the Weather server.
-
Clone the repository:
git clone <repository-url> cd MCP_Server_Project
-
Create a virtual environment and install dependencies:
python -m venv .venv source .venv/bin/activate # On Windows use: .venv\Scripts\activate pip install -r requirements.txt
-
Configure Environment Variables: Create a
.envfile in the root directory and add your API keys:OPENWEATHER_API_KEY=your_openweather_api_key OPENROUTER_API_KEY=your_openrouter_api_key MODEL=your_preferred_model_name
To use the client, you first need to start the MCP servers on their respective ports.
-
Start the Calculator Server:
python Calculator_MCP_Server.py
(Runs on http://127.0.0.1:8001)
-
Start the Weather Server:
python Weather_MCP_Server.py
(Runs on http://127.0.0.1:8002)
-
Start the Client (in a new terminal):
python client.py
You can now chat with the agent and ask it to perform calculations or fetch weather data!