Skip to main content
Glama
HimanshuMohanty-Git24

Multi-Agent Research System MCP Server

README.md
# πŸš€ Multi-Agent Research System MCP Server

> **Advanced Multi-Agent Architecture for VS Code Copilot Integration**
>
> A next-generation Model Context Protocol (MCP) server that demonstrates how to build powerful multi-agent systems with sophisticated tooling, breaking away from traditional specialized-tool-only MCP implementations.

---

## πŸ“‹ Table of Contents

- [Overview](#overview)
- [Architecture](#architecture)
- [Key Features](#key-features)
- [Technology Stack](#technology-stack)
- [Project Structure](#project-structure)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Workflow Pipeline](#workflow-pipeline)
- [VS Code Integration](#vs-code-integration)
- [API Reference](#api-reference)
- [Examples](#examples)
- [Development](#development)

---

## 🎯 Overview

This project represents a **paradigm shift in MCP server design**. While traditional MCP servers focus on exposing simple, specialized tools, this implementation leverages **multi-agent orchestration** to create a sophisticated research system that can be seamlessly integrated into VS Code Copilot.

The system combines three specialized AI agents with powerful internal tools to perform comprehensive research tasksβ€”all exposed through simple, intuitive MCP tools.

### Why This Matters

- **Traditional MCP**: Single-purpose tools exposed directly to clients
- **This Approach**: Multi-agent coordination, tool orchestration, and intelligent workflow management wrapped in a clean interface
- **Result**: More powerful, context-aware, and reliable results delivered through familiar tools

---

## πŸ—οΈ Architecture

### System Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    VS Code Copilot                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                    MCP Protocol (STDIO)
                         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              FastMCP Server (Entry Point)                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Exposed Tools:                                        β”‚ β”‚
β”‚  β”‚  β€’ run_research_graph(query, num_sources)             β”‚ β”‚
β”‚  β”‚  β€’ workflow_info()                                     β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β”‚ Invokes
      β”‚
β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         LangGraph Workflow (State Management)                β”‚
β”‚                                                              β”‚
β”‚  START β†’ Research Agent β†’ Validator Agent β†’ Final Output β†’ END
β”‚          Agent            Agent           Agent
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚         β”‚              β”‚
     β–Ό         β–Ό              β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚       Internal Tools (Not Exposed to Client)            β”‚
  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
  β”‚  β”‚  Web Tools     β”‚  β”‚  LLM Agents (Groq - 70B)        β”‚β”‚
  β”‚  β”‚  β€’ web_search  β”‚  β”‚  β€’ Research Analysis            β”‚β”‚
  β”‚  β”‚  β€’ fetch_page  β”‚  β”‚  β€’ Validation & Scoring         β”‚β”‚
  β”‚  β”‚  β€’ search_news β”‚  β”‚  β€’ Report Generation            β”‚β”‚
  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Agent Pipeline

```
1. RESEARCH AGENT
   β”œβ”€ Performs web searches using DuckDuckGo
   β”œβ”€ Fetches detailed content from URLs
   β”œβ”€ Performs LLM-based analysis
   β”œβ”€ Extracts facts and insights
   └─ Outputs: summary, key_facts, insights

2. VALIDATOR AGENT
   β”œβ”€ Evaluates research quality
   β”œβ”€ Scores reliability (0-100)
   β”œβ”€ Identifies issues and gaps
   β”œβ”€ Provides improvement recommendations
   └─ Outputs: validation_score, reliability, status

3. FINAL OUTPUT AGENT
   β”œβ”€ Synthesizes all agent outputs
   β”œβ”€ Generates professional markdown report
   β”œβ”€ Organizes findings hierarchically
   β”œβ”€ Includes sources and recommendations
   └─ Outputs: final_report (professional documentation)
```

---

## ✨ Key Features

### πŸ€– Multi-Agent Orchestration
- **Three Specialized Agents**: Research, Validation, and Output generation
- **Sequential Workflow**: Each agent refines the previous agent's output
- **State Preservation**: TypedDict-based state management ensures data consistency

### πŸ”§ Advanced Tooling
- **Web Search**: DuckDuckGo integration for source discovery
- **Content Extraction**: Beautiful Soup-based webpage parsing
- **News Search**: Specialized news discovery capability
- **All Internal**: Tools not exposed to clientsβ€”only results are shared

### 🧠 Intelligent Analysis
- **LLM-Powered**: Groq's Llama 3.3 (70B) for accurate analysis
- **Temperature Control**: Optimized settings per agent (Research: 0.3, Validation: 0.2, Output: 0.4)
- **JSON Parsing**: Structured output with fallback mechanisms

### πŸ“Š Quality Assurance
- **Validation Scoring**: 0-100 confidence scoring system
- **Reliability Assessment**: Multi-factor reliability ratings
- **Error Tracking**: Issue identification and recommendations

### πŸ” Secure Integration
- **Environment Variables**: API keys managed via `.env`
- **STDIO Transport**: Safe MCP communication channel
- **No Data Leakage**: Internal tools hidden from client

---

## πŸ› οΈ Technology Stack

| Component | Technology | Purpose |
|-----------|-----------|---------|
| **Agent Framework** | LangGraph | Workflow orchestration & state management |
| **LLM Provider** | Groq (Llama 3.3 70B) | Advanced reasoning & analysis |
| **Language** | Python 3.10+ | Implementation language |
| **MCP Framework** | FastMCP | Server protocol & tool exposure |
| **HTTP Client** | HTTPX | Async web requests |
| **HTML Parser** | BeautifulSoup 4 | Content extraction |
| **Chat Models** | LangChain | LLM integration abstraction |

---

## πŸ“‚ Project Structure

```
AgentsCrossToolMCP/
β”œβ”€β”€ server.py                      # FastMCP server & tool definitions
β”œβ”€β”€ graph_workflow.py              # LangGraph workflow pipeline
β”œβ”€β”€ state.py                       # Shared state TypedDict definition
β”œβ”€β”€ requirements.txt               # Python dependencies
β”œβ”€β”€ pyproject.toml                 # Project metadata
β”‚
β”œβ”€β”€ agents/                        # Multi-agent components
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ research_agent.py          # Source discovery & analysis
β”‚   β”œβ”€β”€ validator_agent.py         # Quality validation & scoring
β”‚   └── final_output_agent.py      # Report generation
β”‚
└── tools/                         # Internal tool library
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ web_tools.py               # Web search, fetch, news search
    └── __pycache__/
```

---

## πŸ“¦ Installation

### Prerequisites
- Python 3.10 or higher
- Groq API key (get one at [https://console.groq.com](https://console.groq.com))
- VS Code with Copilot extension

### Setup Steps

1. **Clone or download the project**
   ```powershell
   cd d:\GENAI\AgentsCrossToolMCP
   ```

2. **Create virtual environment**
   ```powershell
   python -m venv .venv
   .\.venv\Scripts\Activate.ps1
   ```

3. **Install dependencies**
   ```powershell
   pip install -r requirements.txt
   ```

4. **Configure environment variables**
   Create a `.env` file in the project root:
   ```env
   GROQ_API_KEY=your_groq_api_key_here
   ```

5. **Verify installation**
   ```powershell
   python server.py
   ```
   You should see the FastMCP banner and server startup messages.

---

## βš™οΈ Configuration

### Environment Variables

```env
# Required
GROQ_API_KEY=gsk_xxxxxxxxxxxxxxxxxxxxxxxxxxxx

# Optional (uses defaults if not set)
GROQ_MODEL=llama-3.3-70b-versatile    # Model for all agents
RESEARCH_TEMPERATURE=0.3               # Research agent creativity
VALIDATOR_TEMPERATURE=0.2              # Validator strictness
OUTPUT_TEMPERATURE=0.4                 # Output composition creativity
```

### MCP Server Configuration

The MCP server is configured in VS Code through the settings:

```json
{
  "mcpServers": {
    "my-mcp-server": {
      "command": "python",
      "args": ["d:\\GENAI\\AgentsCrossToolMCP\\server.py"],
      "disabled": false,
      "alwaysAllow": ["run_research_graph", "workflow_info"]
    }
  }
}
```

---

## πŸš€ Usage

### Via VS Code Copilot

Once connected, you can use the system directly in Copilot:

**Example Prompt:**
```
@my-mcp-server Use run_research_graph to research "AI safety in Large Language Models" 
with 5 sources and provide a comprehensive analysis.
```

**Copilot will:**
1. Call `run_research_graph(query, 5)`
2. Display the multi-page research report
3. Cite sources and validation metrics

### Programmatic Usage

```python
from server import run_research_graph
import asyncio

async def main():
    result = await run_research_graph(
        query="Is Indian GDP growing? Current growth rate and challenges",
        num_sources=5
    )
    print(result)

asyncio.run(main())
```

### Command Line

```powershell
# Start the server
python server.py

# In another terminal, test via MCP client
# (Configure in VS Code settings)
```

---

## πŸ”„ Workflow Pipeline

### Step-by-Step Execution

#### 1️⃣ **Initialization**
- User provides query and source count
- System initializes ResearchState object
- Workflow begins

#### 2️⃣ **Research Agent Processing**
```
Input: query, num_sources
β”œβ”€ web_search(query) β†’ 5 search results
β”œβ”€ fetch_webpage(url) for top 2 results β†’ raw content
β”œβ”€ LLM Analysis with tools
└─ Output: summary, key_facts, insights
```

#### 3️⃣ **Validation Agent Processing**
```
Input: research_summary, key_facts
β”œβ”€ LLM Assessment of quality
β”œβ”€ Scoring (0-100)
β”œβ”€ Reliability rating
└─ Output: validation_score, status, issues
```

#### 4️⃣ **Final Output Agent Processing**
```
Input: all previous outputs + sources
β”œβ”€ Combine all findings
β”œβ”€ Format as markdown report
β”œβ”€ Add structure & organization
└─ Output: final_report (professional document)
```

#### 5️⃣ **Return to Client**
- MCP server returns final_report
- Copilot displays in editor
- Sources and validation metrics included

---

## πŸ”Œ VS Code Integration

### Setup Instructions

1. **Open VS Code Settings** (`Ctrl+,`)

2. **Go to MCP Servers section**

3. **Add configuration:**
   ```json
   "mcpServers": {
     "my-mcp-server": {
       "command": "python",
       "args": ["d:\\GENAI\\AgentsCrossToolMCP\\server.py"],
       "disabled": false
     }
   }
   ```

4. **Restart VS Code**

5. **Verify in Copilot Chat:**
   - Open Copilot Chat (`Ctrl+L`)
   - Type `@my-mcp-server`
   - Should see available tools

### Usage in Copilot

```
@my-mcp-server Can you research the latest developments in quantum computing
and provide a detailed analysis with key insights?
```

---

## πŸ”— API Reference

### Tool: `run_research_graph`

**Purpose**: Execute comprehensive research workflow

**Parameters:**
```python
run_research_graph(
    query: str,              # Research topic/question
    num_sources: int = 5     # Number of sources to fetch
) -> str
```

**Returns:**
```
Professional markdown report containing:
- Executive Summary
- Key Findings
- Detailed Analysis
- Research Sources
- Validation Metrics
- Recommendations
```

**Example:**
```python
report = await run_research_graph(
    query="Climate change impact on agricultural productivity",
    num_sources=5
)
print(report)
```

---

### Tool: `workflow_info`

**Purpose**: Get information about the multi-agent system

**Parameters:** None

**Returns:**
```
String describing:
- Agent roles and responsibilities
- Available capabilities
- Tool information
```

**Example:**
```python
info = await workflow_info()
print(info)
```

---

## πŸ“š Examples

### Example 1: Economic Research

**Query:**
```
Query: India GDP growth rate 2024 2025 economic challenges obstacles
Sources: 5
```

**Output includes:**
- GDP growth statistics
- Economic challenges identified
- Market obstacles
- Expert recommendations
- Data reliability assessment

---

### Example 2: Technology Trends

**Query:**
```
Query: Latest developments in quantum computing and AI integration
Sources: 8
```

**Output includes:**
- Recent breakthroughs
- Technical insights
- Industry trends
- Research opportunities
- Cross-domain applications

---

## πŸ§‘β€πŸ’» Development

### Project Architecture Principles

1. **Separation of Concerns**
   - Agents focus on specific tasks
   - Tools handle data fetching
   - Server handles protocol translation

2. **State Immutability Pattern**
   - TypedDict ensures type safety
   - Operator.add for message accumulation
   - Clear state transitions

3. **Error Handling**
   - Graceful degradation for tool failures
   - LLM JSON parsing fallbacks
   - Comprehensive error messages

4. **Extensibility**
   - Easy to add new agents
   - Simple to integrate new tools
   - Flexible temperature/model parameters

### Adding New Agents

1. Create new agent class in `agents/`
2. Implement `async __call__(self, state)` method
3. Add to graph in `graph_workflow.py`
4. Update state.py if needed

### Adding New Tools

1. Create tool function in `tools/web_tools.py`
2. Decorate with `@tool`
3. Add to agent's `bind_tools()` call
4. Keep tools internal (not exposed via MCP)

---

## πŸ› Troubleshooting

### Issue: "GROQ_API_KEY not found"
**Solution:** Ensure `.env` file exists with valid API key
```powershell
# Verify .env exists
Test-Path .\.env

# Check content (don't share publicly)
Get-Content .\.env
```

### Issue: MCP server won't start
**Solution:** Check dependencies and Python version
```powershell
python --version  # Should be 3.10+
pip list | grep -i fastmcp
```

### Issue: Web fetch fails (403 Forbidden)
**Solution:** Some websites block scraping. System handles this gracefully
- Validator agent scores lower
- System uses alternative sources
- Report still generated with available data

### Issue: Slow response times
**Solution:** Configure fewer sources or optimize LLM
```python
# Use fewer sources
run_research_graph(query, num_sources=3)

# Or increase timeout in web_tools.py
timeout=60.0  # Increase from 30.0
```

---

## πŸ“Š Performance Metrics

### Typical Execution Times
- **Web Search**: 2-3 seconds
- **Content Fetch**: 1-2 seconds per page
- **Research Agent**: 3-5 seconds
- **Validation Agent**: 2-3 seconds
- **Final Output Agent**: 2-3 seconds
- **Total**: ~10-15 seconds for 5 sources

### Resource Requirements
- **CPU**: Minimal (network-bound)
- **Memory**: ~200-300 MB
- **Network**: Required (STDIO/HTTP requests)
- **Storage**: <50 MB code

---

## πŸ” Security & Privacy

- **No Data Storage**: Results not persisted
- **API Key Protection**: Via environment variables
- **STDIO Transport**: Encrypted by VS Code
- **No Third-Party Analytics**: Pure execution
- **Tool Isolation**: Internal tools never exposed

---

## 🌟 Why This Architecture?

### Traditional MCP Limitations
```
User Request
    ↓
Tool Call
    ↓
Simple Result
```

**Problems:**
- No intelligence between tools
- User must coordinate multiple calls
- No quality validation
- Results not synthesized

### This System's Advantages
```
User Request
    ↓
Workflow Graph
    β”œβ”€ Research Agent (intelligent search)
    β”œβ”€ Validator Agent (quality check)
    └─ Final Output Agent (synthesis)
    ↓
Professional Report
```

**Benefits:**
- Autonomous orchestration
- Intelligent analysis layers
- Built-in quality validation
- Professional output
- Single-call interface

---

### VSCode Copilot Conversation

<img width="1375" height="745" alt="Screenshot 2025-11-12 170848" src="https://github.com/user-attachments/assets/5d36c8f1-ed2d-451d-a229-385a1a8041e0" />
<img width="1196" height="755" alt="Screenshot 2025-11-12 170936" src="https://github.com/user-attachments/assets/870df046-4dfd-4886-935b-8f6c9a699015" />
<img width="1217" height="649" alt="Screenshot 2025-11-12 170948 - Copy" src="https://github.com/user-attachments/assets/c63b37ff-ba0f-47b7-b747-6768a258eb4b" />
<img width="1238" height="638" alt="Screenshot 2025-11-12 171001" src="https://github.com/user-attachments/assets/eca92598-56cc-4c31-b253-ff130c0e4eba" />