多服务器MCP集成工具

一个使用Groq AI模型创建和集成多个模型上下文协议(MCP)服务器与LangChain代理的演示项目,适用于动态工具管理和AI代理开发。

  • by im-suhailkhan
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 im-suhailkhan/Creating-MCP-servers-with-Langchain

使用说明

MCP服务器创建

使用Groq AI模型创建和集成多个模型上下文协议(MCP)服务器与LangChain代理的全面演示。

概述

该项目展示了如何:

  • 使用FastMCP创建多个MCP服务器(数学和天气服务器)
  • 使用以下命令将多个MCP服务器连接到LangChain代理 MultiServerMCPClient
  • 使用不同的传输协议(stdio和流式HTTP)
  • 构建一个可以动态使用多个服务器工具的AI代理

特性

  • 多个MCP服务器:演示不同传输机制的两个示例服务器
    • 数学服务器:通过stdio传输提供算术运算(加法、乘法)
    • 天气服务器:通过HTTP传输提供天气信息
  • 多服务器客户端:无缝连接并管理多个MCP服务器
  • LangChain集成:使用LangGraph的ReAct代理模式进行工具编排
  • Groq人工智能模型:利用Groq的Llama模型进行快速推理

建筑

┌─────────────┐
│   Client    │
│  (LangChain)│
└──────┬──────┘
       │
       ├─────────────────┐
       │                 │
┌──────▼──────┐   ┌───────▼──────┐
│ Math Server │   │Weather Server│
│  (stdio)    │   │   (HTTP)     │
└─────────────┘   └──────────────┘

先决条件

安装

  1. 克隆存储库:
git clone <repository-url>
cd MCP_Server_Creation
  1. 使用uv安装依赖项:
uv sync

或者使用pip:

pip install -r requirements.txt
  1. 创建一个 .env 项目根目录中的文件:
cp env.example .env
# Then edit .env and add your actual Groq API key

或手动创建 .env:

echo "GROQ_API_KEY=your_groq_api_key_here" > .env

项目结构

MCP_Server_Creation/
├── app.py             # Streamlit web UI (recommended interface)
├── client.py          # Command-line client connecting to multiple MCP servers
├── mathserver.py      # Math MCP server (stdio transport)
├── weather.py         # Weather MCP server (HTTP transport)
├── requirements.txt   # Python dependencies
├── pyproject.toml     # Project configuration
├── env.example        # Environment variables template
└── README.md          # This file

用法

选项1:Web UI(推荐)

与MCP服务器交互的最简单方法是通过Streamlit web界面。

第一步:更新你的 .env 文件

确保你的 .env 文件包含:

GROQ_API_KEY=your_groq_api_key_here
WEATHER_API_KEY=your_weather_api_key_here

步骤2:启动天气服务器

在单独的终端中,启动基于HTTP的天气服务器:

uv run python weather.py

服务器将于启动 http://localhost:8000/mcp.

步骤3:运行Streamlit UI

在另一个终端中,启动web界面:

uv run streamlit run app.py

UI将在浏览器中自动打开。然后,您可以:

  • 与客服聊天
  • 问数学问题:“什么是(3+5)×12?”
  • 问天气问题:“纽约的天气怎么样?”
  • 合并运算:“将25和17相加,然后乘以3”

选项2:命令行客户端

如果您更喜欢命令行界面:

步骤1:启动天气服务器

uv run python weather.py

步骤2:运行客户端

在另一个终端中:

uv run python client.py

客户将:

  1. 连接到数学服务器(通过stdio)和天气服务器(通过HTTP)
  2. 使用两台服务器上的工具创建LangChain代理
  3. 流程示例查询:
    • 数学计算:“(3+5)x 12”
    • 天气查询:“加利福尼亚州的天气怎么样?”

运作原理

数学服务器(mathserver.py)

  • 使用FastMCP使用两个工具创建服务器: add 和 multiply
  • 通过stdio传输(标准输入/输出)运行
  • 需要时由客户端自动生成

天气服务器(weather.py)

  • 使用FastMCP和HTTP传输
  • 提供 get_weather 工具
  • 在客户端连接之前,必须独立运行

客户(client.py)

  • 用途 MultiServerMCPClient 管理多个MCP服务器
  • 使用LangGraph的创建ReAct代理 create_react_agent
  • 从所有连接的服务器动态加载工具
  • 使用Groq的LLM进行快速推理

使用的技术

  • MCP(模型上下文协议):将AI模型连接到外部工具的协议
  • FastMCP:在Python中创建MCP服务器的快速简便方法
  • LangChain:构建LLM应用程序的框架
  • LangGraph:使用LLM构建有状态的多参与者应用程序
  • LangChain MCP适配器:LangChain和MCP之间的集成层
  • Groq:高性能AI推理平台
  • 紫外线:快速Python包安装程序和解析器

输出示例

Processing request of type ListToolsRequest
Math response: The result is 96
Weather response: It's always raining in California

配置

改变人工智能模型

编辑 client.py 使用不同的Groq模型:

model=ChatGroq(model="llama-3.1-8b-instant", temperature=0)

可用型号包括:

  • llama-3.1-8b-instant
  • llama-3.3-70b-versatile
  • mixtral-8x7b-32768

添加更多服务器

要添加其他MCP服务器,请更新 MultiServerMCPClient 配置在 client.py:

client=MultiServerMCPClient(
    {
        "math": {...},
        "weather": {...},
        "your_server": {
            "command": "python",
            "args": ["yourserver.py"],
            "transport": "stdio",
        }
    }
)

故障排除

模块未找到错误

如果您遇到导入错误,请确保您正在使用 uv run:

uv run python client.py

这可确保您在使用虚拟环境时安装了所有依赖项。

端口已在使用中

如果端口8000已在使用中:

# Find the process using port 8000
lsof -ti:8000

# Kill the process
kill <process_id>

服务器连接错误

启动客户端之前,请确保天气服务器正在运行:

# Terminal 1
uv run python weather.py

# Terminal 2
uv run python client.py

贡献

欢迎投稿!请随时提交拉取请求。

许可证

[在此处添加您的许可证]

致谢