Skip to content

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

License

Notifications You must be signed in to change notification settings

extrawest/agent_with_multiple_mcp_servers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– MCP Agent Framework

Maintenance Maintainer Ask Me Anything ! License GitHub release

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

cursor.mp4
terminal_demo.mp4

πŸš€ 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 tools
  • run_query(): Execute a query using the agent
  • cleanup(): Close all connections and free resources

MCPClientWrapper

Class for connecting to MCP servers and loading tools:

  • connect(): Establish a connection with an MCP server
  • load_tools(): Load available tools from the server
  • close(): 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 information
    • fetch_quarterly_financials: Get quarterly financial data
    • fetch_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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages