Content
# Advanced CrewAI with MCP Sequential Thinking
**Production-ready** multi-agent system for news analysis with the official MCP Sequential Thinking server.
## 🎯 What does this agent do?
1. **Parallel Search** - simultaneously searches for news from 3 sources (BBC, CNN, Reuters)
2. **MCP Sequential Thinking** - uses the official MCP server for structured analysis
3. **Synthesis of Conclusions** - generates a comprehensive report with recommendations
## 📊 Architecture
### 5-Agent System with MCP:
```
┌─────────────────┐
│ BBC Researcher │─┐
└─────────────────┘ │
├──► ┌──────────────────┐ ┌─────────────────┐
┌─────────────────┐ │ │ Senior Analyst │ │ MCP Sequential │
│ CNN Researcher │─┤ │ (with MCP tools) │◄──────┤ Thinking Server │
└─────────────────┘ │ └──────────────────┘ └─────────────────┘
│ ▼ (npx @model...)
┌─────────────────┐ │ ┌──────────────────┐
│ Reuters Researcher│─┘ │ Report Synthesizer│
└─────────────────┘ └──────────────────┘
```
### MCP Sequential Thinking:
CrewAI automatically connects to the official MCP Sequential Thinking server via `MCPServerAdapter`:
```python
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-sequential-thinking"]
)
with MCPServerAdapter(server_params, connect_timeout=60) as mcp_tools:
analyst_agent = Agent(
role='Senior News Analyst',
tools=mcp_tools, # Automatically receives sequentialthinking tool
...
)
```
## 📋 Project Structure
```
module4/
├── README.md # Documentation
├── requirements.txt # Python dependencies (crewai-tools[mcp]!)
├── .env.example # Configuration template
├── .gitignore # Git exclusions
│
├── config.py # Configuration management
├── parallel_agent_with_mcp.py # 🎯 Main agent (5 agents + MCP)
└── mcp_examples.py # 📚 Examples of various MCP servers
```
## 🚀 Installation
### Step 1: Node.js (for MCP server)
The MCP Sequential Thinking server runs via npx. Check:
```bash
node --version # v18+ recommended
npx --version
```
If Node.js is not installed: [https://nodejs.org/](https://nodejs.org/)
### Step 2: Python Dependencies
```bash
pip install -r requirements.txt
```
**Dependencies:**
- `crewai>=0.80.0` - Multi-agent framework
- `crewai-tools[mcp]>=1.3.0` - MCP adapter for CrewAI
- `langchain>=1.0.0` - LLM orchestration
- `langchain-openai>=1.0.0` - OpenAI integration
- `mcp>=1.6.0` - Model Context Protocol
- `requests>=2.31.0` - HTTP requests (for Brave Search API)
- `python-dotenv>=1.0.0` - Environment variables
### Step 3: Configuration
```bash
cp .env.example .env
```
Edit `.env`:
```bash
# Required
OPENAI_API_KEY=your-openai-api-key-here
BRAVE_API_KEY=your-brave-api-key-here # Obtain at https://brave.com/search/api/
# Optional
DEFAULT_MODEL=gpt-4o-mini
TEMPERATURE=0.7
ENABLE_MCP_THINKING=true
MAX_SEARCH_RESULTS=3
```
## 🎮 Usage
### Basic Run
```bash
python parallel_agent_with_mcp.py
```
**Expected output:**
```
🔌 Preparing to connect to MCP Sequential Thinking server...
================================================================================
🚀 ADVANCED CREWAI: Parallel Search + MCP Sequential Thinking
================================================================================
📋 Creating search agents...
🔍 Analysis topic: 'artificial intelligence breakthrough'
🧠 MCP Sequential Thinking: Connecting...
└─ Searching from BBC, CNN, Reuters (in parallel)
✅ MCP server connected! Available tools: 1
📋 Tools:
• sequentialthinking
⚡ Starting parallel search and analysis...
```
### MCP Sequential Thinking in Action
The agent automatically uses the MCP server for a 5-step analysis:
```
Sequential Thinking MCP Server running on stdio
┌────────────────────────────────────────────────┐
│ 💭 Thought 1/5 │
├────────────────────────────────────────────────┤
│ Identifying key themes mentioned across all │
│ sources: AI regulations, ethical concerns... │
└────────────────────────────────────────────────┘
┌────────────────────────────────────────────────┐
│ 💭 Thought 2/5 │
├────────────────────────────────────────────────┤
│ Finding unique insights from each source... │
└────────────────────────────────────────────────┘
...
```
### Programmatic Usage
```python
from parallel_agent_with_mcp import run_advanced_analysis
# Run analysis
result = run_advanced_analysis(
topic="quantum computing breakthrough"
)
print(result['result'])
print(f"Execution time: {result['duration']:.2f}s")
print(f"MCP enabled: {result['mcp_enabled']}")
```
## 🔧 How MCP Integration Works
### 1. MCPServerAdapter connects to npx server
```python
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-sequential-thinking"],
env=None
)
with MCPServerAdapter(server_params, connect_timeout=60) as mcp_tools:
# mcp_tools contains a list of tools from the MCP server
analyst_agent = create_analyst_agent_with_mcp(mcp_tools)
```
### 2. Agent calls MCP tool
The analyst receives instructions to call `sequentialthinking`:
```python
analysis_description = f'''
Use the "sequentialthinking" tool for structured analysis.
Step 1 - thought: "Identifying key themes..."
thoughtNumber: 1, totalThoughts: 5, nextThoughtNeeded: true
Step 2 - thought: "Finding unique insights..."
thoughtNumber: 2, totalThoughts: 5, nextThoughtNeeded: true
...
'''
```
### 3. MCP server visualizes the process
The official MCP server automatically outputs beautiful boxes for each thought step in the console.
## 💡 Advantages of the MCP Approach
✅ **Official server** - uses @modelcontextprotocol/server-sequential-thinking
✅ **Automatic visualization** - MCP server draws thinking boxes itself
✅ **Built-in support for CrewAI** - via MCPServerAdapter
✅ **Standardized protocol** - Model Context Protocol
✅ **Easily extensible** - can add other MCP servers
## 🧪 Requirements
- Python 3.10+
- Node.js 18+ (for npx)
- OpenAI API Key
- Internet connection (for news search and npx)
## 🌟 Other Popular MCP Servers
This project demonstrates Sequential Thinking, but MCP supports many other servers!
### 📋 Official MCP Servers:
#### 1. **Sequential Thinking** ⭐ (used in this project)
```bash
npx -y @modelcontextprotocol/server-sequential-thinking
```
Structured step-by-step thinking for solving complex problems.
#### 2. **Filesystem**
```bash
npx -y @modelcontextprotocol/server-filesystem /path/to/allowed/dir
```
Safe interaction with the filesystem with access control.
- Reading/writing files
- Creating directories
- Searching for files
#### 3. **Fetch**
```bash
npx -y @modelcontextprotocol/server-fetch
```
Downloading and converting web content for LLM.
- Downloading HTML pages
- Converting to markdown
- Working with PDFs
#### 4. **Git**
```bash
npx -y @modelcontextprotocol/server-git --repository /path/to/repo
```
Working with Git repositories.
- Reading commits
- Searching history
- Analyzing changes
#### 5. **Memory**
```bash
npx -y @modelcontextprotocol/server-memory
```
Knowledge graph memory system.
- Creating entities
- Relationships between concepts
- Storing knowledge
### 🎯 Popular Third-Party MCP Servers:
- **Google Drive** - working with Google Drive files
- **Slack** - integration with Slack
- **GitHub** - advanced work with GitHub
- **PostgreSQL** - working with databases
- **MongoDB** - NoSQL databases
- **Puppeteer** - browser automation
- **Brave Search** - searching via Brave
- **AWS** - integration with AWS services
- **Azure** - Microsoft Azure integration
- **Cloudflare** - Cloudflare Workers
### 💡 Usage Examples
A file `mcp_examples.py` has been created with examples of using various MCP servers:
```bash
python mcp_examples.py
```
Examples include:
1. **Sequential Thinking** - structured thinking
2. **Filesystem** - working with files
3. **Fetch** - downloading web content
4. **Git** - analyzing repositories
5. **Memory** - knowledge graph
### 🔗 How to Add Another MCP Server
```python
from crewai import Agent
from crewai_tools import MCPServerAdapter
from mcp import StdioServerParameters
# Parameters for another MCP server
server_params = StdioServerParameters(
command="npx",
args=["-y", "@modelcontextprotocol/server-NAME"]
)
# Usage
with MCPServerAdapter(server_params, connect_timeout=60) as mcp_tools:
agent = Agent(
role='Your Role',
tools=mcp_tools, # Agent receives tools from MCP
...
)
```
### 📊 Complete List of Servers:
- [Official MCP Servers](https://github.com/modelcontextprotocol/servers)
- [Community MCP Servers](https://github.com/punkpeye/awesome-mcp-servers)
- [Marketplace](https://modelcontextprotocol.io/examples)
## 📚 Useful Links
- [CrewAI MCP Documentation](https://docs.crewai.com/en/mcp/overview)
- [MCP Sequential Thinking Server](https://github.com/modelcontextprotocol/servers/tree/main/src/sequentialthinking)
- [Model Context Protocol](https://modelcontextprotocol.io/)
- [CrewAI Documentation](https://docs.crewai.com)
- [OpenAI API Reference](https://platform.openai.com/docs/api-reference)
## 🐛 Troubleshooting
### "OpenAI API key not found"
**Problem:** OPENAI_API_KEY is not set
**Solution:**
```bash
# In .env file
OPENAI_API_KEY=your-api-key-here
# Or via export
export OPENAI_API_KEY="your-api-key-here"
```
### "command not found: npx"
**Problem:** Node.js is not installed
**Solution:**
```bash
# macOS
brew install node
# or download from https://nodejs.org/
```
### "Module 'crewai_tools' not found"
**Problem:** crewai-tools[mcp] is not installed
**Solution:**
```bash
pip install 'crewai-tools[mcp]'
```
### Timeout when connecting to MCP
**Problem:** MCP server is not starting in time
**Solution:**
- Check that npx works: `npx -y @modelcontextprotocol/server-sequential-thinking --version`
- Increase timeout in the code:
```python
with MCPServerAdapter(server_params, connect_timeout=120) as mcp_tools:
```
## 📝 License
MIT License