Skip to main content
Glama
grok-diff-guardian

grok-diff-guardian

by Pedrohs1771
README.md
# Grok Diff Guardian

**AST-aware git diff analyzer, deterministic security gate, and MCP server built for xAI Grok Build and Grok API workflows.**

`grok-diff-guardian` pairs fast, offline Python AST symbol mapping and static security checks with an optional **xAI Grok API** review pass (`https://api.x.ai/v1`) and a zero-dependency **Model Context Protocol (MCP)** stdio server so coding agents (such as **Grok Build**) can inspect impacted symbols and risk signals before opening or merging a PR.

---

## Architecture

```text
git diff ──▶ diff_parser ──▶ ast_analyzer (AST symbols + static rules)
                                   │
          ┌────────────────────────┴────────────────────────┐
          ▼                                                 ▼
     CLI / CI gate                                  MCP stdio server
  (--json / --strict / --ai)              (analyze_diff, inspect_ast_file,
          │                                       scan_repo_risks)
          ▼
   xAI Grok API (/v1/chat/completions)
```

---

## Features

- **Zero runtime dependencies:** Built entirely on the Python 3.10+ standard library.
- **AST-scoped impact mapping:** Maps changed diff lines directly to enclosing Python classes, functions, and async methods (`SymbolSpan`) with branch complexity hints.
- **Deterministic static security gate:** Detects dangerous sinks (`eval`, `exec`, `os.system`, `pickle.loads`, `subprocess(..., shell=True)`), TLS bypass (`verify=False`), unsafe `yaml.load()`, SQL f-string interpolation, bare `except:`, and hardcoded credential patterns.
- **Native xAI Grok API integration (`--ai`):** Sends a compact AST-enriched context payload and unified diff to `https://api.x.ai/v1/chat/completions` (`grok-code-fast-1` by default, configurable via `--model` / `GROK_MODEL` and `XAI_API_KEY` or `GROK_API_KEY`).
- **Stdio MCP server (`mcp`):** Implements JSON-RPC 2.0 `initialize`, `tools/list`, and `tools/call` exposing `analyze_diff`, `inspect_ast_file`, and `scan_repo_risks`.

---

## Quickstart

### Install locally

```bash
git clone https://github.com/Pedrohs1771/grok-diff-guardian.git
cd grok-diff-guardian
pip install -e ".[dev]"
```

### Review current repository diff (offline deterministic mode)

```bash
grok-diff-guardian review --repo .
grok-diff-guardian review --repo . --staged --json --strict
```

### Enable xAI Grok AI deep review

```bash
export XAI_API_KEY="your-xai-api-key"
grok-diff-guardian review --repo . --ai --model grok-code-fast-1
```

### Scan an entire directory

```bash
grok-diff-guardian scan src --strict
```

---

## MCP Server Configuration (Grok Build / Coding Agents)

Add `grok-diff-guardian` as a local stdio MCP server:

```json
{
  "mcpServers": {
    "grok-diff-guardian": {
      "command": "grok-diff-guardian",
      "args": ["mcp", "--repo", "."]
    }
  }
}
```

Exposed MCP tools:
- `analyze_diff` — Parses a unified diff (or runs `git diff`), maps impacted Python AST symbols, and evaluates static security rules.
- `inspect_ast_file` — Extracts class/function spans, parameter lists, and branch complexity for a Python file.
- `scan_repo_risks` — Recursively scans source files in a path for deterministic security/reliability findings.

---

## Running Tests

```bash
pytest -v
```

---

## License

[MIT](LICENSE) — Built by [@Pedrohs1771](https://github.com/Pedrohs1771).