MCPLEARNING
A beginner-friendly demo project that builds and connects multiple MCP servers (Math and Weather) to a LangChain LLM agent, enabling tool-based interactions via standardized MCP protocol.
README
MCP + LangChain Demo
A beginner-friendly project that demonstrates how to build MCP (Model Context Protocol) servers and connect them to an LLM agent using LangChain and LangGraph.
What is MCP?
MCP (Model Context Protocol) is an open protocol that lets you expose custom tools (functions) to LLMs in a standardized way. Think of it as a universal plugin system for AI models.
Key concepts:
| Term | Definition |
|---|---|
| MCP Server | A process that exposes tools (functions) over a transport (stdio or HTTP). The LLM can call these tools. |
| MCP Client | A process that connects to one or more MCP servers, discovers their tools, and forwards them to an LLM. |
| Tool | A Python function decorated with @mcp.tool() that the LLM can invoke. |
| Transport | The communication method between client and server. stdio = same machine via stdin/stdout. streamable-http = over HTTP. |
| FastMCP | A high-level Python class from the mcp library that makes it easy to create MCP servers. |
Project Structure
MCPLEARNING/
├── mathserver.py # MCP Server 1 - Math tools (stdio transport)
├── weather.py # MCP Server 2 - Weather tool (HTTP transport)
├── client.py # LangChain agent that connects to both servers
├── .env # API keys (NOT pushed to GitHub)
├── .gitignore
├── requirements.txt
└── pyproject.toml
How It Works (Step by Step)
Step 1: MCP Server — mathserver.py
This file creates an MCP server named "Math" that exposes two tools:
add(a, b)— Returns the sum of two integers.multiply(a, b)— Returns the product of two integers.
It runs on stdio transport, meaning the client spawns it as a subprocess and communicates through stdin/stdout. No port needed.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Math")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Addition of two numbers"""
return a + b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""Multiplication of two numbers"""
return a * b
if __name__ == "__main__":
mcp.run(transport="stdio")
Step 2: MCP Server — weather.py
This file creates an MCP server named "Weather" that exposes one tool:
get_weather(location)— Returns weather info for a given location.
It runs on streamable-http transport, meaning it starts a web server on http://127.0.0.1:8000/mcp. The client connects to it over HTTP.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Weather")
@mcp.tool()
async def get_weather(location: str) -> str:
"""Get the weather"""
return "It's always raining in California"
if __name__ == "__main__":
mcp.run(transport="streamable-http")
Step 3: Client Agent — client.py
This is the brain of the project. It:
- Connects to both MCP servers using
MultiServerMCPClient. - Discovers all tools from both servers (
add,multiply,get_weather). - Creates a Groq LLM (hosted open-source model) and binds the tools to it.
- Builds a LangGraph agent — a state machine where:
- The LLM decides whether to call a tool or respond directly.
- If a tool is called, the result is fed back to the LLM for a final answer.
- Tests two queries:
- "What is 3 + 5?" → Uses the
addtool. - "What is the weather in California?" → Uses the
get_weathertool.
- "What is 3 + 5?" → Uses the
Prerequisites
- Python 3.13+
- uv package manager (recommended) or pip
- A Groq API key — Get one free at console.groq.com
Setup
1. Clone the repository
git clone https://github.com/<YOUR_USERNAME>/MCPLEARNING.git
cd MCPLEARNING
2. Create and activate virtual environment
# Using uv (recommended)
uv venv
uv pip install -r requirements.txt
# Or using pip
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # Mac/Linux
pip install -r requirements.txt
3. Set up your API key
Create a .env file in the project root:
GROQ_API_KEY=your_groq_api_key_here
IMPORTANT: Never commit your
.envfile. It is excluded via.gitignore.
Running the Project
You need two terminals open:
Terminal 1 — Start the Weather MCP Server
python weather.py
You should see:
INFO: Uvicorn running on http://127.0.0.1:8000
Note: Only
weather.pyneeds to be started manually. Themathserver.pyis spawned automatically by the client (stdio transport).
Terminal 2 — Run the Client
python client.py
Expected Output
Available MCP tools:
- add
- multiply
- get_weather
==============================
Testing Math MCP
==============================
Math Response: 3 + 5 = 8.
==============================
Testing Weather MCP
==============================
Weather Response: It's always raining in California.
How to Create Your Own MCP Server
- Install the MCP library:
pip install mcp
- Create a new Python file (e.g.,
myserver.py):
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("MyServer")
@mcp.tool()
def my_tool(param: str) -> str:
"""Description of what this tool does."""
return f"Result: {param}"
if __name__ == "__main__":
mcp.run(transport="stdio") # For stdio transport
# mcp.run(transport="streamable-http") # For HTTP transport
- Connect it in your client by adding it to the
MultiServerMCPClientconfig:
client = MultiServerMCPClient({
"myserver": {
"command": "python",
"args": ["myserver.py"],
"transport": "stdio",
},
})
Transport Comparison
| Transport | How it Works | When to Use |
|---|---|---|
| stdio | Client spawns the server as a subprocess. Communicates via stdin/stdout. | Local tools, simple setup, no network needed. |
| streamable-http | Server runs as a web server. Client connects via HTTP. | Remote tools, multiple clients, cross-machine access. |
Key Libraries Used
| Library | Purpose |
|---|---|
mcp |
Build MCP servers with FastMCP. |
langchain-mcp-adapters |
Bridge between MCP servers and LangChain tools. |
langchain-groq |
LangChain integration for Groq-hosted LLMs. |
langgraph |
Build agent workflows as a graph (agent ↔ tools loop). |
python-dotenv |
Load API keys from .env file. |
Important Things to Take Care Of
-
Weather server must be running before the client — Since it uses HTTP transport, the server process must be started first. The math server (stdio) is auto-spawned by the client.
-
Groq API key is required — Without it, the LLM calls will fail. Get a free key at console.groq.com.
-
Never commit
.env— Always add.envto.gitignorebefore pushing code. -
Port conflicts — The weather server runs on port 8000 by default. If another process uses that port, the server will fail to start.
-
Windows encoding issue — On Windows, the console may not support UTF-8 characters returned by the LLM. The
client.pyhandles this withsys.stdout.reconfigure(encoding="utf-8"). -
Model availability — The Groq model name (
openai/gpt-oss-120b) must be valid and available on the Groq platform. Check Groq's model list for current options.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。