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.
- π 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
cursor.mp4
terminal_demo.mp4
# 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
Copy the example environment file and configure your settings:
cp .env.example .env
Before running the agent, you need to start the MCP servers. The project includes two different servers:
Provides financial data tools using yfinance:
# Start the stocks server on port 7860
python -m mcp_components.server.stocks_server
Provides news retrieval tools using NewsAPI:
# Start the news server on port 7861
python -m mcp_components.server.news_server
Once the servers are running, you can start the agent:
# Run the agent with connections to both MCP servers
python run_agent.py
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.
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
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
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
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
The framework includes two specialized MCP servers:
- 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
- Provides news retrieval tools using NewsAPI
- Runs on port 7861 by default
- Tools:
fetch_news
: Get news articles related to a query
- 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)
- 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