LangChain MCP增强版
增强版 langchain mcp适配器 (3.4k星)——Anthropic模型上下文协议(MCP)的官方LangChain包装器。为MCP客户端配置添加全面的分析、基准测试和优化工具。
原始代码保持不变。 所有增强功能都在 langchain_mcp_enhanced/ 包裹。
增强模块
1.分析(analysis.py)--配置和模式分析
深入检查MCP适配器配置、工具模式、拦截器链和回调。
- ConnectionConfigAnalyzer --验证连接配置,检测传输不匹配,检查超时设置
- 工具模式分析器 --分析工具参数模式,检测服务器之间的命名冲突,计算复杂性得分
- 拦截器链分析仪 --模拟拦截器执行顺序(洋葱模式),检测链中的潜在问题
- 回调分析器 --验证回调配置,检查通知类型的覆盖范围
- MCP适配器诊断 --结合所有分析仪运行完整的诊断套件
from langchain_mcp_enhanced import ConnectionConfigAnalyzer, MCPAdapterDiagnostics
# Validate a connection config
result = ConnectionConfigAnalyzer.analyze_connection("weather", {
"transport": "sse",
"url": "http://localhost:8000/mcp",
"timeout": 5.0
})
print(result["warnings"]) # Potential issues
# Full diagnostics across all servers
diag = MCPAdapterDiagnostics()
report = diag.run_diagnostics(connections, tool_schemas, interceptors, callbacks)2.优化(optimization.py)--性能调整
用于路由优化、连接池分析、缓存策略和配置调优的工具。
- 工具路由器 --构建路由表,检测重复工具,建议前缀策略
- 连接池分析器 --分析连接重用模式,根据预算建议池配置
- CacheStrategyBuilder --基于工具调用模式和TTL策略设计缓存策略
- 拦截器优化器 --优化拦截器链排序,以最小化开销
- 性能调谐器 --生成具有特定传输建议的优化配置
from langchain_mcp_enhanced import ToolRouter, PerformanceTuner
# Build routing table and detect conflicts
table = ToolRouter.build_routing_table({
"math": ["add", "multiply"],
"weather": ["search", "forecast"]
})
# Generate optimized configuration
tuner = PerformanceTuner()
config = tuner.generate_tuned_config(connections, profile="latency")3.基准(benchmark.py)--性能评估
基于估计的基准测试,用于传输延迟、吞吐量、内存使用和可扩展性分析。
- 运输基准 --比较传输类型(stdio/SSE/HTTP/WebSocket)的延迟和吞吐量
- ToolCallCostEstimator --估算工具调用成本,包括序列化和拦截器开销
- 可扩展性估计器 --模拟多服务器扩展,检测瓶颈,推荐服务器数量
- 内存估计器 --估计连接、工具注册表和缓存的内存使用情况
- BenchmarkReporter --生成具有改进建议的人类可读报告
from langchain_mcp_enhanced import BenchmarkReporter
reporter = BenchmarkReporter()
result = reporter.run_full_benchmark({
"transport": "websocket",
"num_servers": 3,
"tools_per_server": 20,
"target_qps": 500.0
})
report = reporter.generate_report(result)
suggestions = reporter.suggest_improvements(result)安装
git clone https://github.com/Rlin1027/langchain-mcp-enhanced.git
cd langchain-mcp-enhanced
pip install -e .测试
python3 -m pytest tests/test_analysis.py tests/test_optimization.py tests/test_benchmark.py -v- 211测试 跨越3个模块(76+54+81)
- 增强模块的零外部依赖性
项目结构
langchain_mcp_enhanced/ # Enhancement package (NEW)
__init__.py # 18 public symbols
analysis.py # Configuration analysis (1,079 lines)
optimization.py # Performance optimization
benchmark.py # Benchmarking toolkit (1,023 lines)
tests/
test_analysis.py # 76 tests
test_optimization.py # 54 tests
test_benchmark.py # 81 tests
langchain_mcp_adapters/ # Original source (UNTOUCHED)许可证
MIT——与最初的langchain mcp适配器相同。
