agent_with_multiple_mcp_servers
This project provides an agent that interacts with multiple MCP servers. Implemented in Python, it aims to automate workflows efficiently. Users can easily manage data between servers and distribute tasks, enhancing productivity and streamlining operations.
GitHub Stars
0
User Rating
Not Rated
Favorites
0
Views
27
Forks
0
Issues
0
🤖 MCP Agent Framework
A modular system for creating AI agents using the Model Context Protocol (MCP). This framework allows connecting to multiple MCP servers simultaneously, aggregating tools, and executing queries using LangChain agents.
🌟 Features
- 🔌 Multi-server Connection: Connect to multiple MCP servers simultaneously
- 🛠️ Tool Aggregation: Automatically collect and combine tools from all connected servers
- 🤖 LangChain Integration: Create React agents using the LangChain library
- 🔄 Asynchronous Processing: Fully asynchronous code with proper resource management
- 🧠 OpenAI Integration: Seamless integration with OpenAI models for intelligent agent responses
- 📊 Detailed Logging: Comprehensive logging of all operational stages for debugging
- 🧩 Modular Architecture: Clear separation into client and agent components
Demo
https://github.com/user-attachments/assets/6ea18497-79b7-4ed9-8bc0-d211a6f3efd9
https://github.com/user-attachments/assets/31aec298-4a60-430c-b8aa-e5c525ebce06
🚀 Quick Start
1. Installation
# Clone the repository
git clone https://github.com/extrawest/agent_with_multiple_mcp_servers.git
cd agent_with_multiple_mcp_servers
# Install dependencies
pip install -r requirements.txt
2. Configuration
Copy the example environment file and configure your settings:
cp .env.example .env
3. Start MCP Servers
Before running the agent, you need to start the MCP servers. The project includes two different servers:
Stocks Server
Provides financial data tools using yfinance:
# Start the stocks server on port 7860
python -m mcp_components.server.stocks_server
News Server
Provides news retrieval tools using NewsAPI:
# Start the news server on port 7861
python -m mcp_components.server.news_server
4. Run the Agent
Once the servers are running, you can start the agent:
# Run the agent with connections to both MCP servers
python run_agent.py
5. Cursor IDE Integration
You can add the MCP servers to Cursor IDE by adding the following configuration to your Cursor settings:
{
"mcpServers": {
"stocks-mcp-server": {
"url": "http://127.0.0.1:7860/sse"
},
"news-mcp-server": {
"url": "http://127.0.0.1:7861/sse"
}
}
}
Make sure both servers are running before connecting through Cursor IDE.
📦 Project Structure
mcp_components/
├── agent/
│ └── agent.py # AgentRunner implementation for working with MCP servers
├── client/
│ └── client.py # Client for connecting to MCP servers
├── server/
│ ├── stocks_server.py # MCP server providing stock market tools
│ └── news_server.py # MCP server providing news retrieval tools
💻 Usage
Direct MCP Client Usage
from mcp_components.client.client import MCPClientWrapper
from typing import List, Any, Tuple
async def get_mcp_client(server_url: str) -> Tuple[MCPClientWrapper, List[Any]]:
client = MCPClientWrapper(server_url)
await client.connect()
tools = await client.load_tools()
return client, tools
🔧 Components
AgentRunner
Class for creating and managing agents that can use tools from multiple MCP servers:
setup_with_multiple_mcp_servers(): Connect to multiple MCP servers and aggregate toolsrun_query(): Execute a query using the agentcleanup(): Close all connections and free resources
MCPClientWrapper
Class for connecting to MCP servers and loading tools:
connect(): Establish a connection with an MCP serverload_tools(): Load available tools from the serverclose(): Close the connection to the server
MCP Servers
The framework includes two specialized MCP servers:
Stocks Server
- Provides financial data tools using yfinance
- Runs on port 7860 by default
- Tools:
fetch_stock_info: Get general company informationfetch_quarterly_financials: Get quarterly financial datafetch_annual_financials: Get annual financial data
News Server
- Provides news retrieval tools using NewsAPI
- Runs on port 7861 by default
- Tools:
fetch_news: Get news articles related to a query
📋 Requirements
- Python 3.9+
- MCP 1.9.3+
- LangChain
- OpenAI API key (for using OpenAI models)
- NewsAPI key (for the news server)
- yfinance (for the stocks server)
🔍 Implementation Details
- Asynchronous Context Managers: Proper management of asynchronous resources using
AsyncExitStack - Error Handling: Robust exception handling at all levels
- Connection Preservation: Maintaining active connections for tool usage
- Tool Aggregation: Combining tools from different servers into a unified list
Developed by extrawest. Software development company
This hybrid network automation system integrates LangGraph for workflow orchestration, LangChain for natural language processing, and Model Context Protocol (MCP) for intelligent network device management. Users can interact with network devices using conversational commands, simplifying complex multi-device operations. It also features automated network mapping and topology discovery capabilities.