File size: 8,406 Bytes
591456c
 
 
 
 
 
 
7ffd176
591456c
 
 
 
5d1056c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
title: Competitive Analysis Single Agent
emoji: πŸ“Š
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 4.19.2
python_version: 3.11
app_file: app.py
pinned: false
---

# πŸ† Competitive Analysis Agent - MCP Implementation

A sophisticated single-agent system for comprehensive competitive analysis using the **Model Context Protocol (MCP)** with FastMCP. This implementation combines web research, intelligent reasoning, and structured analysis to provide actionable competitive insights.

## 🎯 Architecture Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Single-File Application (app.py)                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Gradio Web Interface (User UI)                    β”‚ β”‚
β”‚  β”‚            :7860                                   β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                        β”‚                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Analysis Engine & Tools:                          β”‚ β”‚
β”‚  β”‚  β€’ validate_company()       - Verify company      β”‚ β”‚
β”‚  β”‚  β€’ identify_sector()        - Find industry       β”‚ β”‚
β”‚  β”‚  β€’ identify_competitors()   - Discover rivals     β”‚ β”‚
β”‚  β”‚  β€’ browse_page()            - Extract data        β”‚ β”‚
β”‚  β”‚  β€’ generate_report()        - Create analysis     β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                        β”‚                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  AI Agent (OpenAI GPT-4)                           β”‚ β”‚
β”‚  β”‚  Strategic reasoning & insights generation        β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                                          β”‚
β”‚              External APIs:                             β”‚
β”‚              β€’ DuckDuckGo (Web Search)                 β”‚
β”‚              β€’ OpenAI (LLM Analysis)                   β”‚
β”‚              β€’ External Web Pages (Scraping)           β”‚
β”‚                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

**Single-File Design**: All functionality is consolidated in one `app.py` file for simplicity and ease of deployment. No separate MCP server needed.

## ✨ Features

### πŸ” Company Analysis
- **Validation**: Confirms company legitimacy using web search
- **Sector Identification**: Determines industry using multi-stage analysis
- **Competitor Discovery**: Identifies top 3 competitors with ranking

### πŸ“Š Strategic Analysis
- **Web Research**: Automatically gathers company information
- **Content Extraction**: Extracts relevant strategic data
- **Structured Reports**: Generates professional analysis with:
  - Executive summary
  - Competitor comparison table
  - Strategic recommendations
  - Actionable insights

### πŸ› οΈ MCP Architecture
- **FastMCP Server**: Lightweight, high-performance tool hosting
- **Tool Isolation**: Each analysis function is a callable MCP tool
- **Scalable Design**: Easy to add new tools or extend functionality
- **Robust Error Handling**: Graceful fallbacks and error management

## πŸ“‹ Prerequisites

- Python 3.8+
- OpenAI API key (get at https://platform.openai.com/api-keys)
- Internet connection (for web search and scraping)

## βš™οΈ Installation

1. **Clone the repository**:
   ```bash
   git clone <repository-url>
   cd single-agent-competitive-analysis-agent
   ```

2. **Install dependencies**:
   ```bash
   pip install -r requirements.txt
   ```

3. **Verify installation**:
   ```bash
   python -c "import gradio; import openai; import requests; import beautifulsoup4; print('βœ“ All dependencies installed')"
   ```

## πŸš€ Running the Application

### Quick Start (One Command!)
```bash
python app.py
```

That's it! The application will:
1. Start the Gradio web interface at **http://0.0.0.0:7860**
2. Initialize the analysis engine
3. Open in your browser automatically (or visit http://localhost:7860)

### Expected Output
```
Running on local URL:  http://0.0.0.0:7860
Opening in browser...
```

## πŸ’‘ Usage Guide

### Basic Workflow

1. **Enter Company Name**: Type the name of the company you want to analyze
   - Example: "Tesla", "Spotify", "Microsoft", "Stripe"

2. **Provide OpenAI API Key**: Paste your OpenAI API key
   - Get one at: https://platform.openai.com/api-keys

3. **Click "Analyze Competitors"**: The system will:
   - Validate the company exists
   - Identify its sector
   - Find top 3 competitors
   - Analyze competitor strategies
   - Generate comprehensive report

4. **Review Analysis Report**: The report includes:
   - Company overview and sector
   - Top 3 competitors
   - Detailed competitor comparison
   - Strategic insights
   - Actionable recommendations

## πŸ“ Project Structure

```
single-agent-competitive-analysis-agent/
β”œβ”€β”€ app.py                 # Complete application (Gradio UI + Analysis Engine)
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ .env.example          # Environment variables template
β”œβ”€β”€ README.md             # This file
└── ARCHITECTURE.md       # Detailed architecture documentation
```

### Application Components

The `app.py` file includes:

#### πŸ” Analysis Tools
- **validate_company**: Company existence verification via web search
- **identify_sector**: Industry classification with multi-strategy analysis
- **identify_competitors**: Competitor discovery and ranking
- **browse_page**: Web content extraction and parsing
- **generate_report**: Structured competitive analysis report generation

#### πŸ€– AI Agent
- OpenAI GPT-4 integration for strategic reasoning
- Orchestrates analysis tools in logical sequence
- Generates insights and actionable recommendations
- Error handling with fallback modes

#### πŸ’» User Interface
- Gradio-based web interface
- Professional report formatting in Markdown
- Real-time analysis execution
- Input validation and error handling

## πŸ”’ Security & Privacy

- βœ… **API Keys**: Never stored, used only in current session
- βœ… **Web Data**: Temporary, not cached
- βœ… **No Tracking**: Local processing only

## πŸš€ Quick Start

```bash
# 1. Install dependencies
pip install -r requirements.txt

# 2. Run the application
python app.py

# 3. Open browser to http://localhost:7860
# (usually opens automatically)
```

**That's all!** No need for multiple terminals or separate server startup.
# 4. Enter company name and OpenAI API key
# 5. Click "Analyze Competitors"
```

## πŸ› Troubleshooting

**OpenAI API Key invalid**: Check it starts with `sk-` and is active  
**MCP Server not running**: Run `python mcp_server.py` in separate terminal  
**Web search failing**: Check internet connection and try different company name  
**Rate limit errors**: Wait 5 minutes before next analysis

## πŸ“Š Performance

- Analysis Time: 30-60 seconds
- Report Generation: ~10 seconds
- API Calls: 5-8 requests per analysis
- Max Competitors: 3 (quality optimized)

---

**Version**: 1.0.0 (MCP Architecture)  
**Last Updated**: March 2026