Skip to main content
Glama
grok-diff-guardian

grok-diff-guardian

by Pedrohs1771

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

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)

Related MCP server: ctxkit

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

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)

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

Enable xAI Grok AI deep review

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

Scan an entire directory

grok-diff-guardian scan src --strict

MCP Server Configuration (Grok Build / Coding Agents)

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

{
  "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

pytest -v

License

MIT — Built by @Pedrohs1771.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables coding agents to perform deterministic, model-free repository analysis and patch operations—tree-sitter repo maps, symbol/reference lookup, and SEARCH/REPLACE parsing/validation—via a CLI or MCP stdio server, without ever calling a language model.
      1
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI coding agents to retrieve repository context through stdio-based MCP tools, including ranked repo maps, context packs, and live symbol search.
      11 npm
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables coding agents to inspect files, search text, compare directories, read symbols, and apply structured edits against large codebases through bounded, local-first MCP tools.
      4
      Apache 2.0
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables local LLM tools and agentic IDEs to inspect repository token maps, retrieve clean token-bounded codebase snapshots, and audit code for hardcoded secrets via MCP over stdio.
      4
      MIT