Skip to main content
Glama
README.md
# πŸŒ‹ Volatility MCP

> An MCP (Model Context Protocol) server for iterative volatility analytics: ARCH β†’ GARCH β†’ GJR-GARCH β†’ EGARCH, with pre-flight statistical gates, input optimization, VaR / Expected Shortfall, Basel backtesting, and a stateful feedback loop that guides an LLM agent through every stage of the analysis.

[![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![MCP Compatible](https://img.shields.io/badge/MCP-compatible-green.svg)](https://modelcontextprotocol.io/)

> **Volatility Analytics Lab** Β· Finance Β· Risk Β· Analytics

---

## What This Is

A **feedback-driven** volatility analytics engine wrapped as an MCP server. An LLM agent (Claude, GPT, etc.) calls tools one at a time; each tool returns results **plus recommendations for the next step**. The agent iterates until diagnostics pass, backtests pass, and reports are generated.

```
data.load β†’ preflight.run β†’ optimize.* β†’ models.fit β†’ diagnostics.run
    β†’ compare.run β†’ risk.var β†’ backtest.rolling β†’ backtest.coverage
    β†’ report.excel β†’ feedback.explain_decision
```

### Three Pillars

| Pillar | What it does | Folder |
|---|---|---|
| πŸ›‘οΈ **Pre-flight gates** | 12 statistical checks run BEFORE any model fit. If Engle ARCH-LM fails, GARCH is blocked. | `core/preflight/` |
| 🎯 **Input optimization** | `(p,q)`, distribution, window, refit frequency chosen by AIC/QLIKE β€” not by guesswork. | `core/optimize/` |
| πŸ” **Feedback loop** | Session state + workflow DAG + advisor. Every tool returns `next_actions[]`. | `core/feedback/` |

---

## Quick Start

```bash
# Clone
git clone https://github.com/volatility-analytics-lab/volatility-mcp.git
cd volatility-mcp

# Install
python -m pip install -e ".[dev]"

# Run tests
make test

# Start the MCP server (stdio transport)
make run

# Or SSE transport for remote access
make run-sse
```

### Connect from Claude Desktop

Add to `~/Library/Application Support/Claude/claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "volatility": {
      "command": "python",
      "args": ["-m", "mcp_server.server"],
      "cwd": "/path/to/volatility-mcp"
    }
  }
}
```

Then ask Claude: *"Analyze Reliance Industries volatility using the volatility MCP server."*

---

## Tool Surface

| Category | Tools |
|---|---|
| **Session** | `open_session`, `close_session`, `get_session_state`, `list_sessions`, `reset_session` |
| **Data** | `load_market`, `load_csv`, `list_universes`, `list_assets` |
| **Pre-flight** | `run_preflight`, `get_gate_status`, `explain_gate` |
| **Optimize** | `optimize_order`, `optimize_distribution`, `optimize_mean`, `optimize_window`, `optimize_refit`, `optimize_horizon` |
| **Models** | `fit_model`, `forecast`, `list_models` |
| **Diagnostics** | `run_diagnostics`, `get_diagnostics` |
| **Compare** | `compare_models` |
| **Risk** | `compute_var`, `compute_es`, `basel_es`, `portfolio_var` |
| **Scenario** | `apply_market_shock`, `stressed_var`, `list_stress_scenarios` |
| **Backtest** | `rolling_backtest`, `backtest_kupiec`, `backtest_christoffersen`, `backtest_traffic_light`, `backtest_dynamic_quantile`, `backtest_diebold_mariano`, `backtest_coverage_scorecard` |
| **Report** | `build_excel`, `build_pdf`, `build_markdown` |
| **Feedback** | `get_next_action`, `explain_decision`, `get_recommendations`, `list_valid_transitions` |

See [docs/MCP_PROTOCOL.md](docs/MCP_PROTOCOL.md) for the full surface.

---

## Architecture

```
Presentation:  Streamlit Β· Claude Desktop Β· REST clients Β· Jupyter
                              β”‚ HTTP / stdio / SSE
Protocol:       FastMCP server (tools Β· resources Β· prompts)
                              β”‚
Engine:         core/  (preflight Β· diagnostics Β· models Β· optimize
                        Β· risk Β· backtest Β· feedback Β· reporting)
                              β”‚
Storage:        session_store/ (SQLite + parquet)
Data:           yfinance Β· CSV Β· Alpha Vantage Β· Polygon Β· Kite
```

See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for the full layered design.

---

## Folder Structure

```
volatility-mcp/
β”œβ”€β”€ mcp_server/                   # MCP protocol layer
β”‚   β”œβ”€β”€ server.py                 # Entry point: registers tools, resources, prompts
β”‚   β”œβ”€β”€ tools/                    # Tools callable by the LLM agent
β”‚   β”‚   β”œβ”€β”€ session.py            # open_session / close_session / get_state
β”‚   β”‚   β”œβ”€β”€ data.py               # load_market / load_csv / list_universes
β”‚   β”‚   β”œβ”€β”€ preflight.py          # run_preflight / get_gate_status
β”‚   β”‚   β”œβ”€β”€ diagnostics.py        # run_diagnostics (LB, ARCH-LM, JB, Q-Q stats)
β”‚   β”‚   β”œβ”€β”€ optimize.py           # optimize_order / optimize_distribution / optimize_window
β”‚   β”‚   β”œβ”€β”€ models.py             # fit_model / forecast / list_models
β”‚   β”‚   β”œβ”€β”€ compare.py            # compare_models (AIC/BIC/QLIKE/RMSE)
β”‚   β”‚   β”œβ”€β”€ risk.py               # compute_var / compute_es / basel_es
β”‚   β”‚   β”œβ”€β”€ scenario.py           # apply_shock / stressed_var
β”‚   β”‚   β”œβ”€β”€ backtest.py           # rolling_backtest / kupiec / christoffersen
β”‚   β”‚   β”œβ”€β”€ report.py             # build_excel / build_pdf / build_markdown
β”‚   β”‚   └── feedback.py           # get_next_action / explain_decision
β”‚   β”œβ”€β”€ resources/                # Static context (read by LLM, not executed)
β”‚   β”‚   β”œβ”€β”€ model_catalog.md
β”‚   β”‚   β”œβ”€β”€ test_catalog.md
β”‚   β”‚   └── workflows/            # 01_discovery β†’ 05_validation
β”‚   β”œβ”€β”€ prompts/                  # Prompt templates for guided workflows
β”‚   └── schemas/                  # JSON schemas for tool I/O contracts
β”‚
β”œβ”€β”€ core/                         # Framework-agnostic engine (no MCP code here)
β”‚   β”œβ”€β”€ data/                     # Providers: yfinance, CSV, Alpha Vantage, Polygon, Kite
β”‚   β”œβ”€β”€ preflight/                # 12-check gate layer
β”‚   β”‚   └── checks/               # 01_sample_size.py … 12_frequency_adequacy.py
β”‚   β”œβ”€β”€ diagnostics/              # Post-fit: LB, ARCH-LM, JB, sign-bias, Nyblom
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ univariate/           # ARCH, GARCH, GJR-GARCH, EGARCH, FIGARCH, EWMA
β”‚   β”‚   β”œβ”€β”€ multivariate/         # DCC, BEKK, O-GARCH
β”‚   β”‚   β”œβ”€β”€ stochastic/           # Heston, SV-Jumps
β”‚   β”‚   └── ml/                   # LSTM, Transformer, TFT
β”‚   β”œβ”€β”€ optimize/                 # order_selector, distribution_selector, window_selector…
β”‚   β”œβ”€β”€ risk/                     # VaR (parametric, historical, FHS, MC), ES, Basel, portfolio
β”‚   β”œβ”€β”€ backtest/                 # Kupiec, Christoffersen, DQ, Traffic Light, Diebold-Mariano
β”‚   β”œβ”€β”€ feedback/                 # Session, state machine, workflow DAG, advisor, decision log
β”‚   └── reporting/                # Excel, PDF, Markdown, Plotly charts
β”‚
β”œβ”€β”€ session_store/                # SQLite / DuckDB persistent session backend
β”œβ”€β”€ tests/                        # Mirrors core/ structure; synthetic GARCH fixtures
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ 01_basic_flow.py          # preflight β†’ fit β†’ forecast β†’ VaR
β”‚   β”œβ”€β”€ 02_feedback_loop_demo.py  # Full iterate-until-stable flow
β”‚   β”œβ”€β”€ 03_optimization_walkthrough.py
β”‚   └── notebooks/mcp_client_demo.ipynb
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ docker-compose.yml
β”‚   └── entrypoint.sh
└── docs/
    β”œβ”€β”€ ARCHITECTURE.md
    β”œβ”€β”€ FEEDBACK_LOOP.md
    β”œβ”€β”€ PREFLIGHT_CHECKS.md
    β”œβ”€β”€ INPUT_OPTIMIZATION.md
    β”œβ”€β”€ MODEL_CATALOG.md
    β”œβ”€β”€ TEST_CATALOG.md
    β”œβ”€β”€ MCP_PROTOCOL.md
    └── DECISION_RULES.md
```

---

## Documentation

| Doc | What it covers |
|---|---|
| [ARCHITECTURE.md](docs/ARCHITECTURE.md) | Layered architecture, data flow, scaling options |
| [FEEDBACK_LOOP.md](docs/FEEDBACK_LOOP.md) | How the iterative workflow works |
| [PREFLIGHT_CHECKS.md](docs/PREFLIGHT_CHECKS.md) | Every gate explained |
| [INPUT_OPTIMIZATION.md](docs/INPUT_OPTIMIZATION.md) | How each input is tuned |
| [MODEL_CATALOG.md](docs/MODEL_CATALOG.md) | When to use which model |
| [TEST_CATALOG.md](docs/TEST_CATALOG.md) | Every statistical test |
| [MCP_PROTOCOL.md](docs/MCP_PROTOCOL.md) | Tool/resource/prompt surface |
| [DECISION_RULES.md](docs/DECISION_RULES.md) | Decision tree for model selection |

---
## Credits & Intellectual Debt

This project is built on the shoulders of the following thinkers and their work.

**Nassim Nicholas Taleb** β€” *The Black Swan* (2007), *Dynamic Hedging* (1997),
*Antifragile* (2012). The pre-flight normality gate, stressed VaR scenarios,
and the philosophy of iterating toward robustness over point estimates all
trace back here.

**Robert F. Engle** β€” ARCH paper (*Econometrica*, 1982). The Engle ARCH-LM
test is the single most critical pre-flight gate: if it fails, GARCH is blocked.

**Tim Bollerslev** β€” GARCH(*p,q*) paper (*Journal of Econometrics*, 1986).
The industry workhorse at the center of the model catalog.

**Nelson (1991), Glosten-Jagannathan-Runkle (1993)** β€” EGARCH and GJR-GARCH
respectively. The leverage asymmetry gate recommends these when sign-bias fires.

**Philippe Jorion** β€” *Value at Risk* (3rd ed., 2006). The parametric,
historical, and FHS VaR implementations follow his treatment directly.

**Kupiec (1995), Christoffersen (1998), Engle & Manganelli (2004)** β€” the
POF, conditional coverage, and Dynamic Quantile backtests that form the
coverage scorecard.

**Patton (2011)** β€” established QLIKE as the robust loss function for
volatility forecast comparison. Default objective in `optimize_window`.

**Diebold & Mariano (1995)** β€” the DM test used in `backtest_diebold_mariano`
to compare competing model forecasts.

**Basel Committee on Banking Supervision** β€” Basel III framework: source of
the 97.5% ES requirement, 10-day horizon, traffic light zones, and capital
multiplier rules.

**Kevin Sheppard** β€” the [`arch`](https://github.com/bashtage/arch) Python
package that powers all GARCH-family estimation in this project.

---

## License

MIT β€” see [LICENSE](LICENSE).

Educational use only. Market data may be delayed. Not investment advice.