agent_with_multiple_mcp_servers

このプロジェクトは、複数のMCPサーバーと連携するエージェントを提供します。Pythonで実装されており、効率的なワークフローの自動化を目指しています。ユーザーは、サーバー間のデータ管理やタスクの分散処理を簡単に行うことができます。

GitHubスター

0

ユーザー評価

未評価

お気に入り

0

閲覧数

17

フォーク

0

イシュー

0

README
🤖 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

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 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