# MCP Server: Security Performance Report (SPR)

FastAPI + MCP server for multi-security portfolio analysis using dual methodology: custom performance calculations (validated against QuantStats) combined with FFN library analytics. Generates HTML reports with charts and 6 CSV data exports.

## Links
- App: https://rbicc.net/mcp-server-ffn
- Docs: https://tigzig.com/app-documentation/mcp-server-ffn.html
- GitHub (Backend): https://github.com/amararun/shared-fastapi-mcp-ffn
- Live API: https://ffn.hosting.tigzig.com
- MCP Endpoint: https://ffn.hosting.tigzig.com/mcp
- API Docs: https://ffn.hosting.tigzig.com/docs
- Methodology: https://ffn.hosting.tigzig.com/static/docs/SPR_QS_METHODOLOGY.html

## Tags
mcp-servers, portfolio-analysis, ffn, quantstats, fastapi

## Architecture

```
Web UI / API Client / MCP Client → FastAPI Server → Yahoo Finance (data)
                                                  → Custom Calculations (metrics)
                                                  → FFN Library (drawdowns, monthly returns)
                                                  → Matplotlib (charts)
                                                  → Jinja2 (HTML reports)
```

### Dual Calculation Methodology
- Core metrics (Total Return, CAGR, Sharpe, Sortino): Custom implementations based on QuantStats methodology
- Additional analytics (drawdowns, monthly returns, statistical analysis): FFN library
- Validation: 100% match on Total Return/CAGR, 97%+ on Sharpe/Sortino vs QuantStats

### API Endpoints
- `POST /analyze` - Generate portfolio analysis (JSON body: symbols, start_date, end_date, risk_free_rate)
- `POST /api/analyze` - Same but accepts form data (for web UI)
- `POST /mcp` - MCP endpoint for AI/LLM clients
- `GET /` - Web interface

### Response
Returns URLs to 7 generated files:
- HTML report with charts, drawdown analysis, monthly returns, professional branding
- 6 CSV files: price data, daily returns, cumulative returns, summary statistics, correlation matrix, monthly returns

### Environment Variables
```
IS_LOCAL_DEVELOPMENT=1  # Set to 0 for production
BASE_URL_FOR_REPORTS=https://your-domain.com/
PORT=8000
```

Auto-cleanup: Files older than 72 hours removed on server startup.

### Dependencies
FastAPI, fastapi-mcp, FFN, matplotlib, seaborn, yfinance, pandas, numpy, Jinja2

## Setup

```bash
git clone https://github.com/amararun/shared-fastapi-mcp-ffn.git
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
python main.py  # Runs on localhost:8000
```

## Example

```bash
curl -X POST "https://ffn.hosting.tigzig.com/analyze" \
  -H "Content-Type: application/json" \
  -d '{"symbols":"AAPL,MSFT,GOOG","start_date":"2023-01-01","end_date":"2023-12-31","risk_free_rate":5.0}'
```
