This project demonstrates how to create and use Model Context Protocol (MCP) servers with LangChain. MCP is a protocol that allows AI models to interact with external tools and data sources through a standardized interface.
Model Context Protocol (MCP) is a protocol that enables AI models to:
- Connect to external tools and data sources
- Execute functions on remote servers
- Access real-time information
- Interact with databases, APIs, and other services
langchain-mcp-server/
├── client.py # Main client that connects to MCP servers
├── mathserver.py # Math MCP server (stdio transport)
├── weather.py # Weather MCP server (HTTP transport)
├── requirements.txt # Python dependencies
└── README.md # This file
A simple MCP server that provides mathematical operations:
- Transport: stdio (standard input/output)
- Tools:
add(a: int, b: int) -> int: Adds two numbersmultiply(a: int, b: int) -> int: Multiplies two numbers
A basic MCP server that provides weather information:
- Transport: streamable-http (HTTP-based)
- Tools:
get_weather(city: str) -> str: Returns weather for a given city
A LangChain client that connects to multiple MCP servers and uses them with an AI agent:
- Connects to both math and weather servers
- Uses Groq's Qwen model for reasoning
- Creates a ReAct agent that can use the MCP tools
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables: Create a
.envfile with your Groq API key:GROQ_API_KEY=your_groq_api_key_here -
Start the weather server (in a separate terminal):
python weather.py
This will start the weather server on
http://localhost:8000/mcp
-
Start the weather server (if not already running):
python weather.py
-
Run the main client:
python client.py
The client will:
- Connect to both the math server (via stdio) and weather server (via HTTP)
- Ask the AI agent to solve a math problem:
(19 + 17) * 45 - Ask the AI agent to get weather information for Tokyo
- Display the responses
Math Response: The result of (19 + 17) * 45 is 1620
Weather Response: The weather in Tokyo is sunny
Each MCP server is created using the FastMCP framework:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("ServerName")
@mcp.tool()
def my_function(param: str) -> str:
"""Function description"""
return "result"
if __name__ == "__main__":
mcp.run(transport="stdio") # or "streamable-http"The client connects to multiple servers using MultiServerMCPClient:
client = MultiServerMCPClient({
"math": {
"command": "python",
"args": ["mathserver.py"],
"transport": "stdio",
},
"weather": {
"url": "http://localhost:8000/mcp",
"transport": "streamable-http",
}
})- stdio: Uses standard input/output for local communication
- streamable-http: Uses HTTP for network communication
- Multi-Server Architecture: Connecting to multiple MCP servers simultaneously
- Different Transport Protocols: stdio for local servers, HTTP for remote servers
- Tool Integration: MCP tools become available to LangChain agents
- AI Agent Reasoning: The agent can choose which tools to use based on the user's request
- Create a new server file (e.g.,
databaseserver.py) - Define tools using the
@mcp.tool()decorator - Add the server configuration to
client.py - Restart the client
To add new tools to existing servers, simply add new functions with the @mcp.tool() decorator:
@mcp.tool()
def divide(a: float, b: float) -> float:
"""Divide two numbers"""
return a / blangchain-groq: Groq LLM integrationlangchain-mcp-adapters: MCP client adapters for LangChainmcp: Model Context Protocol frameworklanggraph: For creating ReAct agents
This project serves as a simple introduction to MCP servers and demonstrates how they can be integrated with LangChain for AI-powered applications.