Skip to main content
Glama
README.md
# MLOps Model Context Protocol (MCP) Server

A production-grade Python Model Context Protocol (MCP) server built with the official `mcp.server.fastmcp` SDK, designed for local offline Machine Learning operations, dataset profiling, dynamic classification training, hyperparameter optimization, and evaluation metric visualization.

---

## Key Architectural Principles

1. **Protocol Standards:** Implements the Model Context Protocol (MCP) over standard input/output (`stdio`) transport.
2. **Stdout Framing Isolation:** Standard output (`sys.stdout`) is strictly reserved for JSON-RPC 2.0 frames (`tools/call`, `tools/list`, etc.). Zero `print()` statements are allowed.
3. **Stderr Operational Logging:** All debugging information, training progress, and tracebacks are routed strictly to `sys.stderr` via Python's standard `logging` and direct stream flushes.
4. **100% Offline Execution:** Operates on local files using `pandas`, `numpy`, and `scikit-learn` without external APIs.

---

## Implemented Tools Ecosystem (9 Production MLOps Tools)

| # | Tool Signature | Description |
| :--- | :--- | :--- |
| 1 | `profile_and_clean_dataset(file_path: str) -> str` | Inspects CSV metadata, imputes missing values (median for numeric, mode for categorical), saves `{stem}_cleaned.csv`, and outputs a Markdown audit. |
| 2 | `train_and_evaluate_model(file_path: str, target_column: str, model_type: str = "random_forest") -> str` | One-hot encodes features, performs an 80/20 train/test split, fits Random Forest or Gradient Boosting, and generates precision, recall, F1, and accuracy tables. |
| 3 | `optimize_hyperparameters(file_path: str, target_column: str) -> str` | Executes 3-fold cross-validated hyperparameter search over estimators and tree depth, logs progress to `sys.stderr`, and returns the optimal configuration table. |
| 4 | `generate_saved_metrics_plots(file_path: str, target_column: str, output_dir: str = ".") -> str` | Uses `matplotlib` (in headless `Agg` mode) and `seaborn` to render a 300-DPI confusion matrix heatmap and saves it to disk (`confusion_matrix.png`). |
| 5 | `train_neural_network(file_path: str, target_column: str, epochs: int = 50, hidden_units: int = 64) -> str` | Trains an Artificial Neural Network (ANN) using Keras/TensorFlow (or scikit-learn MLP fallback). Handles scaling, loss curves, milestone checkpoints, and test accuracy. |
| 6 | `train_deep_learning_model(file_path: str, target_column: str, epochs: int = 30) -> str` | Builds a deep 3-layer MLP with Batch Normalization, Dropout (30%/20%), L2 regularization, and Early Stopping. Outputs macro/weighted F1 metrics and convergence epoch. |
| 7 | `generate_streamlit_dashboard(file_path: str, target_column: str, output_file: str = "app.py") -> str` | Synthesizes a ready-to-run interactive Streamlit web dashboard with CSV uploader, interactive hyperparameter tuning, model training, and confusion matrix visualization. |
| 8 | `generate_html_report(file_path: str, target_column: str, output_file: str = "ml_report.html") -> str` | Generates a self-contained, responsive HTML website report with embedded base64 confusion matrix images, metric statistics, and MLOps recommendations. |
| 9 | `list_available_models() -> str` | Introspects and returns the comprehensive catalog of all 9 registered MCP tools, model architectures, hyperparameter spaces, and transport rules. |

---

## Quickstart Setup

### 1. Create and Activate Virtual Environment
```bash
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
```

### 2. Install Dependencies
```bash
pip install -r requirements.txt
```

### 3. Test with MCP Inspector
Inspect and interactively call the tools using the official MCP CLI:
```bash
npx @modelcontextprotocol/inspector python ml_server.py
```

### 4. Configure in VS Code, Cursor, or Claude Desktop

#### A. VS Code (Cline / Roo Code / Continue)
1. Install **Cline** or **Roo Code** from the VS Code Extensions Marketplace (`Ctrl+Shift+X` / `Cmd+Shift+X`).
2. Open Cline settings > **MCP Servers** > **Configure MCP Servers** (`cline_mcp_settings.json`).
3. Add the server entry:
```json
{
  "mcpServers": {
    "mlops-engine": {
      "command": "/ABSOLUTE/PATH/TO/.venv/bin/python",
      "args": [
        "/ABSOLUTE/PATH/TO/ml_server.py"
      ],
      "env": {
        "PYTHONUNBUFFERED": "1"
      },
      "disabled": false
    }
  }
}
```
*(On Windows, use `.venv\Scripts\python.exe` with double backslashes `\\`).*

#### B. Cursor IDE
1. Go to **Settings** (`Cmd+,` or `Ctrl+,`) > **Features** > **MCP**.
2. Click **+ Add New MCP Server**.
3. Set:
   - Name: `mlops-server`
   - Type: `command`
   - Command: `/ABSOLUTE/PATH/TO/.venv/bin/python /ABSOLUTE/PATH/TO/ml_server.py`
4. Click Save. The status dot will turn green.

#### C. Claude Desktop
Add to your `claude_desktop_config.json`:
- **macOS:** `~/Library/Application Support/Claude/claude_desktop_config.json`
- **Windows:** `%APPDATA%\Claude\claude_desktop_config.json`

```json
{
  "mcpServers": {
    "mlops-engine": {
      "command": "python",
      "args": [
        "/absolute/path/to/ml_server.py"
      ],
      "env": {
        "PYTHONUNBUFFERED": "1"
      }
    }
  }
}
```

### 5. Example Prompts to Ask in VS Code / Cursor
- *"Profile and clean 'sample_dataset.csv', impute any missing values, and report statistics."*
- *"Train a Random Forest classifier on 'sample_dataset.csv' predicting 'churn' with 80/20 train/test split."*
- *"Run 3-fold cross-validated hyperparameter optimization for tree depth and estimators."*
- *"Train an Artificial Neural Network on 'sample_dataset.csv' for 30 epochs and output test accuracy."*
- *"Generate a 300-DPI confusion matrix heatmap and export an HTML executive report."*
- *"Synthesize a ready-to-run interactive Streamlit web dashboard 'app.py' for customer churn."*

### 6. Running Generated Artifacts
- **Streamlit Web Dashboard:** Run `streamlit run app.py` to open the interactive UI in your browser.
- **Standalone HTML Report:** Open `ml_report.html` in any web browser to view embedded visualizations and metrics.
- **Confusion Matrix:** View `confusion_matrix.png` directly in VS Code.