使用说明
使用LangChain、Groq和MCP进行多代理工具调用
使用LangChain、Groq的Llama3模型和多服务器通信协议(MCP)演示动态工具发现和执行的多代理系统。该项目展示了如何将各种工具服务器(数学和天气)与不同的通信传输(stdio和HTTP流式传输)集成,以供智能代理使用。
特性
- 多服务器通信协议(MCP):利用MCP实现中央客户端和独立工具服务器之间的无缝通信。
- 动态工具发现:代理客户端从注册的MCP服务器中动态发现可用工具及其功能。
- LangChain集成:利用LangChain的
create_react_agent用于强健的代理行为和工具编排。 - Groq Calama3集成:使用
ChatGroqLlama3-70b-8192模型,用于强大的语言理解和响应生成。 - 多样化的工具运输:演示两种类型的MCP传输:
- 标准输入/输出(stdio):对于
Math服务器,展示简单、直接的流程通信。 - 流式HTTP:对于
Weather服务器,演示与uvicorn-支持FastAPI应用程序。
- 标准输入/输出(stdio):对于
- 天气工具:使用OpenWeatherMap API获取给定位置的实时天气信息。
- 数学工具:执行基本算术运算(加法、乘法)。
- 错误处理:包括API密钥问题和无效城市查询的基本错误处理。
项目结构
multi-agent-mcp-project/
├── docs/
│ └── architecture.md
├── .gitignore # Files to be ignored by Git
├── client.py # The main agent application that orchestrates tools
├── mathserver.py # MCP server for mathematical operations
├── requirements.txt # Python dependencies
├── README.md # Project overview, setup, usage
└── weather.py # MCP server for fetching weather data
安装
按照以下步骤在本地设置和运行项目:
-
克隆存储库:
git clone [https://github.com/your-username/multi-agent-mcp-project.git](https://github.com/your-username/multi-agent-mcp-project.git) cd multi-agent-mcp-project -
创建虚拟环境(推荐):
python -m venv venv -
激活虚拟环境:
- 在macOS/Linux上:
source venv/bin/activate - 在Windows上:
.\venv\Scripts\activate
- 在macOS/Linux上:
-
安装依赖项:
pip install -r requirements.txt -
设置环境变量:
您需要Groq和OpenWeatherMap的API密钥。
- Groq API密钥: 从以下位置获取一个 Groq 控制台.
- OpenWeatherMap API密钥: 从以下位置获取一个 OpenWeatherMap.
创建一个
.env项目目录根目录中的文件,基于.env.example,并用您的密钥填充它:cp .env.example .env现在,打开
.env文件并添加您的实际API密钥:GROQ_API_KEY="your_groq_api_key_here" OPENWEATHER_API_KEY="your_openweathermap_api_key_here"替换
"your_groq_api_key_here"和"your_openweathermap_api_key_here"使用您的实际API密钥。
用法
要运行多代理系统,只需执行 client.py 脚本。这 client.py 将自动启动 mathserver.py 和 weather.py 后台的服务器并连接到它们。
python client.py
You will see output similar to this, demonstrating the agent's thought process and the results of tool calls:
🔧 Loading environment variables...
🚀 Starting main()...
🔑 GROQ_API_KEY: FOUND ✅
🌦️ OPENWEATHER_API_KEY: FOUND ✅
⚙️ Initializing MultiServerMCPClient...
🛠️ Getting tools from servers...
✅ Successfully fetched 3 tools:
🔹 add: Add to numbers
🔹 multiple: Multiply two numbers
🔹 get_weather: Get the current weather for a given location.
🧠 Initializing Groq model...
✅ Groq model initialized.
🤖 Creating React-style agent with tools...
✅ Agent created.
💬 Sending math question to agent...
--- ✅ Math Response ---
🧠 Agent Thought: The user is asking to calculate "(3 + 5) x 12". I need to use the 'add' tool first, then the 'multiple' tool.
🟢 Final Answer: 96
💬 Sending weather question to agent...
--- ✅ Weather Response ---
🧠 Agent Thought: The user is asking for the weather in "Hyderabad, India". I should use the `get_weather` tool with "Hyderabad, India" as the location.
🟢 Final Answer: 🌤️ Weather in Hyderabad, IN: overcast clouds. 29.89°C (feels like 34.02°C), 69% humidity.
💬 Sending weather query for invalid city...
--- 🧪 Weather (Invalid City) Response ---
🧠 Agent Thought: The user is asking for the weather in "NotARealCityHere?". I need to use the `get_weather` tool with "NotARealCityHere?" as the location.
🟢 Final Answer: ❌ City 'NotARealCityHere?' not found.
✅ All done!
How it Works
client.py:
Initializes MultiServerMCPClient to manage connections to different MCP servers.
Defines configurations for the Math server (using stdio transport) and the Weather server (using streamable_http transport via http://localhost:3000).
Fetches available tools from all connected servers.
Initializes the ChatGroq model.
Creates a LangChain React-style agent, providing it with the discovered tools.
Invokes the agent with various prompts to demonstrate its ability to use the add, multiple, and get_weather tools.
mathserver.py:
A simple MCP server that exposes add and multiple functions as tools.
It uses transport="stdio" to communicate via standard input/output streams.
weather.py:
An MCP server that exposes a
get_weather function as a tool.
It integrates with the OpenWeatherMap API to fetch weather data.
It explicitly runs a
uvicorn server to serve the FastMCP application via HTTP, allowing client.py to connect using streamable_http transport.
os.environ["FASTMCP_PORT"] = "3000" is set to ensure the weather.py server starts on port 3000.
Contributing
Contributions are welcome! Please follow these steps:
Fork the repository.
Create a new branch for your feature or bug fix.
Make your changes.
Write tests for your changes.
Ensure all tests pass.
Submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.