MCP LangChain客户端
A. 模型上下文协议(MCP) 客户端,使用LangChain作为编排层,通过MCP服务器将LLM连接到现实世界的工具。提供CLI和Streamlit web GUI版本。
为什么这个项目很重要: MCP是将AI代理连接到外部工具和数据源的新兴开放标准,可以将其视为“LLM的USB”。此客户端演示了如何将MCP服务器连接到LangChain代理中,以便LLM可以动态发现和调用工具,而无需将任何工具逻辑硬编码到应用程序本身。
______________________________________________________________________
它做什么
- 同时连接到一个或多个MCP服务器(stdio或HTTP传输)
- 在运行时自动发现这些服务器上的所有可用工具
- 通过LangChain将工具绑定到LLM(OpenAI或Claude)
- LLM决定调用哪些工具,执行它们,并合成最终响应
- 完整的对话历史记录在各个回合中都得到了维护
______________________________________________________________________
演示
CLI版本
Starting Multi-Server MCP Client...
LLM requested 2 tool call(s)
- Calling tool 'multiply' with args: {'a': 12, 'b': 15}
Result: 180.0
- Calling tool 'add' with args: {'a': 25, 'b': 30}
Result: 55.0
Final Response: The product of 12 and 15 is 180, and the sum of 25 and 30 is 55.流线型web GUI
Streamlit版本运行于 http://localhost:8501 并提供:
- 带有历史记录的聊天风格对话界面
- 可扩展的工具调用检查(准确查看触发了哪些工具以及使用了哪些参数)
- 带有示例查询的侧栏
- 实时处理指标
📸 截图即将发布--run streamlit run client2_with_streamlit.py 现场观看。______________________________________________________________________
技术栈
| 层 | 技术 |
|---|---|
| 协议 | 模型上下文协议(MCP) |
| 编排 | LangChain, langchain-mcp-adapters |
| 法学硕士 | OpenAI GPT-4o/Claude(人类学) |
| Web界面 | Streamlit |
| 运行时 | Python 3.12+,UV包管理器 |
| 传输 | stdio(本地服务器),流式HTTP(远程) |
______________________________________________________________________
建筑
User prompt
│
▼
LangChain Agent
│
├── MultiServerMCPClient
│ ├── MCP Server 1 (stdio) → tools: multiply, add, ...
│ ├── MCP Server 2 (HTTP) → tools: search, fetch, ...
│ └── MCP Server N ...
│
▼
Tool discovery → Tool binding → LLM decides → Tool execution → Final response______________________________________________________________________
支持的LLM
OpenAI(默认)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)克劳德(人类学)
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-sonnet-4-20250514", temperature=0)添加 ANTHROPIC_API_KEY 给你的 .env 文件并安装软件包:
uv add langchain-anthropic交换 llm 参考 client1.py 或 client2_with_streamlit.py --其他一切都是一样的,因为LangChain抽象了提供者。
______________________________________________________________________
快速启动
1.克隆并安装
git clone https://github.com/bhakti259/mcp-langchain-client.git
cd mcp-langchain-client
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
uv add langchain langchain-openai langchain-anthropic langchain-mcp-adapters python-dotenv streamlit2.配置
cp .env.example .envOPENAI_API_KEY=your-openai-key-here
ANTHROPIC_API_KEY=your-anthropic-key-here # optional — for Claude3.指向您的MCP服务器
编辑 SERVERS dict in client1.py 或 client2_with_streamlit.py:
SERVERS = {
"math": {
"transport": "stdio",
"command": "path/to/uv.exe",
"args": ["run", "path/to/your/mcp_server.py"]
},
"remote_tool": {
"transport": "streamable_http",
"url": "https://your-mcp-server.com/mcp"
}
}4.跑步
CLI:
python client1.pyWeb图形用户界面:
streamlit run client2_with_streamlit.py______________________________________________________________________
项目结构
mcp-langchain-client/
├── client1.py # CLI version
├── client2_with_streamlit.py # Streamlit web GUI version
├── .env.example # Environment variable template
├── .gitignore
├── pyproject.toml # Dependencies
└── README.md______________________________________________________________________
展示的关键概念
- MCP工具发现 --工具在运行时动态加载;无需硬编码
- 多服务器编排 --在单个客户端会话中连接到N个MCP服务器
- LLM无关设计 --通过一行更改在OpenAI和Claude之间进行交换
- 异步优先 --满
async/await用于生产就绪模式 - 双接口 --相同的核心逻辑,两个表示层(CLI+Streamlit)
- 工具调用透明度 --Streamlit UI准确地揭示了哪些工具被触发以及为什么被触发
______________________________________________________________________
故障排除
| 问题 | 修复 |
|---|---|
API key invalid | 检查你的 .env 文件具有正确的密钥,没有尾随空格 |
MCP server not found | 验证 command UV可执行文件和MCP服务器参数的路径 |
Import error | 快跑 uv add langchain langchain-openai langchain-mcp-adapters |
Streamlit not found | 快跑 uv add streamlit 然后重试 |
______________________________________________________________________
路线图
- \[\]添加Streamlit界面截图/Loom演示
- \[\]支持将Claude作为GUI中的默认LLM选项
- \[\]添加LangSmith跟踪以实现工具调用的可观察性
- \[\]Docker支持一个命令启动
- \[\]存储库中包含的示例MCP服务器,用于零配置测试
______________________________________________________________________
资源
______________________________________________________________________
作者
巴克蒂·库尔卡尼 --前端工程师(React·Vue·TypeScript)转向代理AI工程。 使用LangGraph·LangChain·RAG·MCP·Claude API·FastAPI进行构建。
📍 比利时安特卫普 🔗 领英 🐙
