Skip to main content
Glama
README.md
# Fantasy MCP - Multi-Agent Parlay Optimization System

A sophisticated AI-powered betting advisor featuring a multi-agent architecture built with CrewAI. Four specialized AI agents collaborate to analyze games, evaluate props, and construct optimal parlay combinations.

## ๐ŸŽฏ Overview

This system uses CrewAI to orchestrate multiple specialized agents that work together to provide intelligent, high-confidence parlay recommendations. The agents analyze player availability, run ML predictions, optimize parlay combinations, and validate recommendations for quality and accuracy.

## ๐Ÿค– Multi-Agent Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    USER REQUEST                              โ”‚
โ”‚  "Build 8-leg 100x parlay for Bengals vs Packers"          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
                       โ–ผ
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ”‚  Crew Orchestrator   โ”‚
            โ”‚  - Request Analysis  โ”‚
            โ”‚  - Agent Routing     โ”‚
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ–ผ              โ–ผ              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Roster Agent  โ”‚ โ”‚ Stats Agent  โ”‚ โ”‚ Parlay Optimizerโ”‚
โ”‚ โ€ข Injuries    โ”‚ โ”‚ โ€ข ML Models  โ”‚ โ”‚ โ€ข Combinations  โ”‚
โ”‚ โ€ข Availabilityโ”‚ โ”‚ โ€ข Props      โ”‚ โ”‚ โ€ข Correlations  โ”‚
โ”‚ โ€ข Weather     โ”‚ โ”‚ โ€ข Matchups   โ”‚ โ”‚ โ€ข Optimization  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ”‚                โ”‚                   โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ–ผ
                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                  โ”‚  QA Agent   โ”‚
                  โ”‚ โ€ข Validate  โ”‚
                  โ”‚ โ€ข Correlate โ”‚
                  โ”‚ โ€ข Approve   โ”‚
                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚  2-3 Parlay Options   โ”‚
              โ”‚  โ€ข 8 legs            โ”‚
              โ”‚  โ€ข ~100x multiplier  โ”‚
              โ”‚  โ€ข Confidence scores โ”‚
              โ”‚  โ€ข Full reasoning    โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## โœจ Key Features

### ๐ŸŽญ Four Specialized Agents

1. **Roster Intelligence Agent**
   - Monitors player injury status and availability
   - Analyzes weather conditions and game factors
   - Checks depth charts and playing time projections
   - Validates all players are healthy and active

2. **Stats & Props Agent**
   - Runs ML predictions for player performance
   - Analyzes historical stats and matchups
   - Calculates prop hit probabilities
   - Identifies 20-30 high-confidence opportunities

3. **Quality Assurance Agent**
   - Validates all recommendations for accuracy
   - Checks for contradictory or correlated props
   - Assesses overall correlation risk
   - Provides final approval or rejection

4. **Parlay Optimizer Agent**
   - Constructs optimal parlay combinations
   - Balances confidence with target multipliers
   - Manages correlation risk across legs
   - Generates multiple parlay options

### ๐Ÿ› ๏ธ 16 Specialized Tools

**Roster Tools:**
- Player injury status checking
- Team roster analysis
- Player availability verification
- Weather condition forecasting

**Stats Tools:**
- Historical stats analysis
- ML-based predictions
- Matchup analysis
- Prop probability calculations

**Betting Tools:**
- Parlay odds calculation
- Leg optimization algorithms
- Expected value calculation
- Correlation risk assessment

**Data Tools:**
- Player search and filtering
- Game schedule retrieval
- Prop market analysis

## ๐Ÿš€ Quick Start

### 1. Installation

```bash
# Clone repository
git clone https://github.com/mattarm/fantasy_mcp.git
cd fantasy_mcp

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

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

### 2. Configuration

```bash
# Copy environment template
cp env.example .env

# Edit .env and add your OpenAI API key
OPENAI_API_KEY=your_key_here
OPENAI_MODEL=gpt-4

# Other optional configurations
AGENT_VERBOSE=true
PARLAY_MIN_CONFIDENCE=0.65
PARLAY_MAX_LEGS=15
```

### 3. Test the System

```bash
# Run validation tests
python test_agent_system.py
```

### 4. Start the Server

```bash
# Start MCP server
python -m fantasy_mcp.main
```

## ๐Ÿ’ก Usage Examples

### Example 1: Single Game Parlay

**Request:**
```
"Put together high confidence 8 leg parlay with a 100x return for this weeks Bengals Packers game"
```

**Process:**
1. Roster Agent identifies the game and checks all players
2. Stats Agent analyzes props and runs ML predictions
3. Parlay Optimizer finds 8-leg combinations hitting ~100x
4. QA Agent validates and provides final recommendations

**Output:**
- 2-3 complete parlay options
- Each with 8 legs, ~100x multiplier
- Confidence scores for each leg
- Correlation risk analysis
- Detailed reasoning

### Example 2: Multi-Game Parlay

**Request:**
```
"Put together a 800x parlay for this Sunday's noon games"
```

**Process:**
1. Identifies all Sunday noon games (4-6 games)
2. Analyzes 60-100+ props across all games
3. Finds 10-12 leg combinations hitting ~800x
4. Diversifies across games to reduce correlation
5. Validates and provides recommendations

### Example 3: Via MCP Tools

```python
# Use build_optimized_parlay tool
result = await mcp_client.call_tool("build_optimized_parlay", {
    "request": "Build 8-leg 100x parlay for Bengals vs Packers game"
})

# Get parlay history
history = await mcp_client.call_tool("get_parlay_history", {
    "limit": 10
})

# Retrieve specific parlay
parlay = await mcp_client.call_tool("get_parlay_by_id", {
    "parlay_id": "abc123..."
})
```

## ๐Ÿ“‚ Project Structure

```
fantasy_mcp/
โ”œโ”€โ”€ src/fantasy_mcp/
โ”‚   โ”œโ”€โ”€ agents/              # AI agents
โ”‚   โ”‚   โ”œโ”€โ”€ roster_intelligence_agent.py
โ”‚   โ”‚   โ”œโ”€โ”€ stats_props_agent.py
โ”‚   โ”‚   โ”œโ”€โ”€ qa_agent.py
โ”‚   โ”‚   โ””โ”€โ”€ parlay_optimizer_agent.py
โ”‚   โ”œโ”€โ”€ crews/               # Crew orchestration
โ”‚   โ”‚   โ”œโ”€โ”€ betting_crew.py
โ”‚   โ”‚   โ””โ”€โ”€ crew_orchestrator.py
โ”‚   โ”œโ”€โ”€ tools/               # Agent tools
โ”‚   โ”‚   โ”œโ”€โ”€ roster_tools.py
โ”‚   โ”‚   โ”œโ”€โ”€ stats_tools.py
โ”‚   โ”‚   โ”œโ”€โ”€ betting_tools.py
โ”‚   โ”‚   โ””โ”€โ”€ data_tools.py
โ”‚   โ”œโ”€โ”€ data_store/          # Data management
โ”‚   โ”‚   โ”œโ”€โ”€ file_manager.py
โ”‚   โ”‚   โ””โ”€โ”€ cache_manager.py
โ”‚   โ”œโ”€โ”€ services/            # Core services
โ”‚   โ”‚   โ”œโ”€โ”€ sleeper_api.py
โ”‚   โ”‚   โ”œโ”€โ”€ ml_predictor.py
โ”‚   โ”‚   โ””โ”€โ”€ betting_advisor.py
โ”‚   โ”œโ”€โ”€ api/                 # MCP server
โ”‚   โ”‚   โ””โ”€โ”€ mcp_server.py
โ”‚   โ””โ”€โ”€ core/                # Core utilities
โ”‚       โ”œโ”€โ”€ config.py
โ”‚       โ””โ”€โ”€ database.py
โ”œโ”€โ”€ data/                    # File-based storage
โ”‚   โ”œโ”€โ”€ players/
โ”‚   โ”œโ”€โ”€ stats/
โ”‚   โ”œโ”€โ”€ predictions/
โ”‚   โ”œโ”€โ”€ bets/parlays/
โ”‚   โ””โ”€โ”€ cache/
โ”œโ”€โ”€ tests/                   # Test suite
โ”œโ”€โ”€ archive/                 # Archived old scripts
โ”œโ”€โ”€ test_agent_system.py     # System tests
โ”œโ”€โ”€ AGENT_SYSTEM_README.md   # Detailed agent docs
โ”œโ”€โ”€ IMPLEMENTATION_SUMMARY.md # Implementation details
โ””โ”€โ”€ requirements.txt         # Dependencies
```

## ๐ŸŽฏ Agent Workflow

### Sequential Execution with Context Sharing

1. **Request Analysis** (Orchestrator)
   - Parse user request
   - Extract: target multiplier, number of legs, games, time slots
   - Route to appropriate workflow

2. **Player Availability** (Roster Agent)
   - Check all relevant players
   - Verify injury status
   - Assess weather conditions
   - Return availability report

3. **Prop Analysis** (Stats Agent)
   - Analyze available props
   - Run ML predictions
   - Calculate hit probabilities
   - Return ranked high-confidence props

4. **Parlay Construction** (Optimizer Agent)
   - Build leg combinations
   - Optimize for target multiplier
   - Manage correlation risk
   - Generate multiple options

5. **Quality Validation** (QA Agent)
   - Validate player status
   - Check for contradictions
   - Assess correlations
   - Approve or reject

6. **Final Output**
   - 2-3 complete parlay recommendations
   - Confidence scores and reasoning
   - Risk assessment
   - Saved to data/bets/parlays/

## โš™๏ธ Configuration

### Environment Variables

```bash
# AI/LLM (Required)
OPENAI_API_KEY=your_key_here
OPENAI_MODEL=gpt-4

# Agent Configuration
AGENT_VERBOSE=true
AGENT_MAX_ITERATIONS=15
AGENT_MAX_EXECUTION_TIME=300

# Parlay Settings
PARLAY_MIN_CONFIDENCE=0.65
PARLAY_MAX_LEGS=15
PARLAY_CORRELATION_THRESHOLD=0.3

# Betting Configuration
DEFAULT_BANKROLL=1000.0
KELLY_FRACTION=0.25
```

### Adjustable Parameters

- **Confidence Threshold**: Minimum confidence for props (default: 0.65)
- **Max Legs**: Maximum parlay legs (default: 15)
- **Correlation Threshold**: Maximum acceptable correlation (default: 0.3)
- **Kelly Fraction**: Bet sizing aggressiveness (default: 0.25)

## ๐Ÿ“Š Data Storage

File-based storage (migration-ready for database):

```
data/
โ”œโ”€โ”€ players/
โ”‚   โ””โ”€โ”€ {player_id}.json         # Player info
โ”œโ”€โ”€ stats/
โ”‚   โ””โ”€โ”€ {player_id}/
โ”‚       โ””โ”€โ”€ {season}_week_{week}.json
โ”œโ”€โ”€ predictions/
โ”‚   โ””โ”€โ”€ {date}/
โ”‚       โ””โ”€โ”€ {player_id}.json     # ML predictions
โ”œโ”€โ”€ bets/
โ”‚   โ”œโ”€โ”€ parlays/
โ”‚   โ”‚   โ””โ”€โ”€ {parlay_id}.json     # Saved parlays
โ”‚   โ””โ”€โ”€ history/
โ””โ”€โ”€ cache/
    โ””โ”€โ”€ {cache_key}.json         # API cache
```

## ๐Ÿงช Testing

```bash
# Run system tests
python test_agent_system.py

# With full agent execution (requires API key)
OPENAI_API_KEY=your_key python test_agent_system.py

# Run pytest suite
pytest tests/

# Run with coverage
pytest --cov=src/fantasy_mcp
```

## ๐Ÿ“š Documentation

- **AGENT_SYSTEM_README.md** - Complete agent system documentation
- **IMPLEMENTATION_SUMMARY.md** - Detailed implementation overview
- **test_agent_system.py** - Usage examples and tests
- **archive/README.md** - Information about archived files

## ๐Ÿ”ง MCP Server Integration

The system provides three main MCP tools:

### 1. build_optimized_parlay
Build an AI-optimized parlay with specified parameters.

```python
{
    "request": "Natural language parlay request"
}
```

### 2. get_parlay_history
View recent parlay recommendations.

```python
{
    "limit": 10  # Number of parlays to retrieve
}
```

### 3. get_parlay_by_id
Retrieve a specific parlay recommendation.

```python
{
    "parlay_id": "unique_parlay_id"
}
```

## ๐Ÿšฆ Performance

- **Request Analysis**: <1 second
- **Full Agent Workflow**: 30-60 seconds
- **API Response Caching**: 30-60 minutes TTL
- **Data Persistence**: Immediate (file-based)

## ๐ŸŽ“ Key Technologies

- **CrewAI**: Multi-agent orchestration framework
- **LangChain**: LLM integration and tools
- **OpenAI GPT-4**: Agent reasoning and decision-making
- **Sleeper API**: NFL data and player stats
- **scikit-learn/XGBoost**: ML models
- **Python 3.11+**: Core language

## ๐Ÿ”ฎ Future Enhancements

- [ ] Real-time sportsbook odds integration
- [ ] Live injury monitoring via X (Twitter)
- [ ] Historical parlay performance tracking
- [ ] Multi-LLM support (Anthropic Claude)
- [ ] Automated bet placement
- [ ] Social sentiment analysis
- [ ] Database migration from file storage

## โš ๏ธ Important Notes

1. **API Key Required**: OpenAI API key needed for agent execution
2. **Educational Purpose**: For research and learning only
3. **Data Sources**: Currently using Sleeper API (free tier)
4. **File Storage**: All data stored in JSON files (DB-ready architecture)
5. **Responsible Gaming**: This is a tool to aid analysis, not a guarantee of success

## ๐Ÿค Contributing

Contributions welcome! Please see our contributing guidelines.

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Submit a pull request

## ๐Ÿ“„ License

MIT License - see LICENSE file for details.

## ๐Ÿ“ž Support

- **Issues**: [GitHub Issues](https://github.com/mattarm/fantasy_mcp/issues)
- **Documentation**: See AGENT_SYSTEM_README.md
- **Email**: Support via GitHub

## ๐Ÿ™ Acknowledgments

- CrewAI for the multi-agent framework
- LangChain for LLM tooling
- Sleeper API for NFL data
- OpenAI for GPT-4

---

**Built with AI ๐Ÿค– for intelligent sports betting analysis**