MCP Server integration with Langchain and Groq for AI agents with multiple tools
MCP Server with Langchain
This project demonstrates how to integrate Model Context Protocol (MCP) servers with Langchain and Groq for building AI agents that can use multiple tools through different transport protocols.
🚀 Features
- Multiple MCP Servers: Math server (stdio) and Weather server (HTTP)
- Langchain Integration: Uses Langchain's MCP adapters for seamless tool integration
- Groq LLM: Powered by Groq's fast inference with
llama-3.1-8b-instant
model - React Agent: Uses Langraph's prebuilt React agent for tool orchestration
- Dual Transport: Demonstrates both stdio and HTTP transport protocols
📋 Prerequisites
- Python 3.13 or higher
- Groq API key
- Virtual environment support
🛠️ Installation
-
Clone/Download the project
cd "MCP Server With Langchain"
-
Create and activate virtual environment
python -m venv .venv .venv\Scripts\activate.bat # On Windows
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
- Copy
.env.example
to.env
:copy .env.example .env
- Edit
.env
and add your Groq API key:GROQ_API_KEY="your_groq_api_key_here"
- Copy
📂 Project Structure
MCP Server With Langchain/
├── .env # Environment variables
├── .venv/ # Virtual environment
├── client.py # Main client application
├── mathserver.py # Math MCP server (stdio transport)
├── weather.py # Weather MCP server (HTTP transport)
├── requirements.txt # Python dependencies
├── pyproject.toml # Project configuration
└── README.md # This file
🔧 Configuration
Dependencies (requirements.txt)
langchain-groq
langchain-mcp-adapters
mcp
httpx
python-dotenv
Environment Variables
GROQ_API_KEY=your_groq_api_key_here
OPENWEATHER_API_KEY=your_openweather_api_key_here # Optional
🏃♂️ Usage
Step 1: Start the Weather Server
Open a terminal and run:
.venv\Scripts\activate.bat
.venv\Scripts\python.exe weather.py
You should see:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Step 2: Run the Client
Open another terminal and run:
.venv\Scripts\activate.bat
.venv\Scripts\python.exe client.py
Expected Output
Available tools: ['add', 'multiply', 'get_weather']
Math response: The result of the calculation is 96.
Testing another math operation...
Math response 2: The result of multiplying 7 and 8 is 56.
🏗️ Architecture
MCP Servers
Math Server (mathserver.py
)
- Transport: stdio
- Tools:
add(a: int, b: int) -> int
: Adds two numbersmultiply(a: int, b: int) -> int
: Multiplies two numbers
Weather Server (weather.py
)
- Transport: streamable_http
- Port: 8000
- API: Uses OpenWeatherMap API for real weather data
- Fallback: Returns demo data when no API key is provided
- Tools:
get_weather(location: str) -> str
: Returns current weather information
Client Application (client.py
)
- Framework: Langchain + Langgraph
- LLM: Groq's
llama-3.1-8b-instant
- Agent: React agent for tool orchestration
- Transport Protocols: Both stdio and HTTP
🔌 MCP Integration
The client uses MultiServerMCPClient
to connect to multiple MCP servers:
client = MultiServerMCPClient({
"math": {
"command": ".venv\\Scripts\\python.exe",
"args": ["mathserver.py"],
"transport": "stdio"
},
"weather": {
"url": "http://127.0.0.1:8000/mcp",
"transport": "streamable_http"
}
})
📊 Example Interactions
Math Operations
User: Calculate (3 + 5) × 12
Agent: Uses add(3, 5) → 8, then multiply(8, 12) → 96
Weather Queries
User: What's the weather in California?
Agent: Uses get_weather("California") → "It's always raining in California"
🛠️ Development
Adding New Tools
- Create a new MCP server following the FastMCP pattern
- Add the server configuration to the client
- Update the client to handle new tool types
Example: Adding a Calculator Server
# calculator_server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Calculator")
@mcp.tool()
def divide(a: float, b: float) -> float:
"""Divide two numbers"""
return a / b
if __name__ == "__main__":
mcp.run(transport="stdio")
🔍 Troubleshooting
Common Issues
-
ModuleNotFoundError: No module named 'mcp'
- Solution: Make sure you're using the virtual environment Python
- Use:
.venv\Scripts\python.exe
instead ofpython
-
Connection refused to weather server
- Solution: Ensure the weather server is running on port 8000
- Check:
python weather.py
is running in another terminal
-
Groq API errors
- Solution: Verify your GROQ_API_KEY in the
.env
file - Check: Your API key has sufficient credits
- Solution: Verify your GROQ_API_KEY in the
Debug Tips
- Check available tools: The client prints all available tools on startup
- Verify server logs: Both servers show request/response logs
- Test individually: You can test each server independently
📚 Learn More
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
📄 License
This project is for educational purposes. Please check individual component licenses.
🎯 Next Steps
- [ ] Add more MCP servers (database, file system, etc.)
- [ ] Implement error handling and retry logic
- [ ] Add streaming responses
- [ ] Create a web interface
- [ ] Add authentication and security
- [ ] Implement tool result caching
Happy Coding! 🚀
For questions or issues, please check the troubleshooting section or create an issue in the repository.