thaylo/astrograph

By thaylo

Updated 4 months ago

Stop AI agents from writing duplicate code. MCP server using AST graph isomorphism.

Image
Languages & frameworks
0

6.8K

thaylo/astrograph repository overview

ASTrograph

    █ █ █
  █ █ █ █ █
  █       █
  █ █ █ █ █
    █ █ █
  █ █   █ █
  █       █

Docker Python License MCP Sponsor

Stop writing code that already exists in your codebase.

ASTrograph is an MCP server that detects when you're about to create duplicate code - and blocks it before it happens.

See It In Action

Your codebase already has this:

# src/math.py
def calculate_sum(a, b):
    return a + b

Your AI agent tries to write this:

# src/utils.py
def add_numbers(x, y):
    return x + y

ASTrograph intercepts and blocks:

BLOCKED: Cannot write - identical code exists at src/math.py:calculate_sum (lines 1-2).
Reuse the existing implementation instead.

Different variable names, same structure. ASTrograph compares code graphs, not text.

Quick Start

Add .mcp.json to your project root:

{
  "mcpServers": {
    "astrograph": {
      "command": "docker",
      "args": [
        "run", "--rm", "-i", "--pull", "always",
        "-v", ".:/workspace",
        "thaylo/astrograph"
      ]
    }
  }
}

The codebase is auto-indexed when the Docker container starts. Then:

  1. astrograph_analyze() - Find existing duplicates
  2. Use astrograph_write / astrograph_edit - They'll block duplicates automatically

The Problem

Large codebases accumulate duplicate code because:

  • AI agents have limited context windows → they can't see all existing code → they rewrite what exists → duplicates inflate the codebase → smaller % of codebase fits in context → more duplicates (vicious cycle)
  • Developers can't remember every function they wrote 6 months ago
  • Similar patterns get reimplemented independently
  • Copy-paste spreads bugs across multiple locations

Key Tools

ToolWhat It Does
astrograph_analyzeFind duplicate Python code (verified via graph isomorphism)
astrograph_writeWrite Python file. Blocks if duplicate exists, warns on similarity
astrograph_editEdit Python file. Blocks if new code duplicates existing, warns on similarity

See all 8 tools →

Works With

  • Claude Code - Tested. Project-level .mcp.json
  • Any MCP client - Should work (standard stdio protocol), but untested

Note: Python only for now.


Tool Reference

Click to expand full tool documentation
astrograph_analyze

Find duplicate Python code (verified via graph isomorphism).

ParameterTypeRequiredDefaultDescription
auto_reindexbooleanNotrueAuto re-index if stale (default: true)
astrograph_write

Write Python file. Blocks if duplicate exists, warns on similarity.

ParameterTypeRequiredDefaultDescription
file_pathstringYes-Absolute file path
contentstringYes-Python code to write
astrograph_edit

Edit Python file. Blocks if new code duplicates existing, warns on similarity.

ParameterTypeRequiredDefaultDescription
file_pathstringYes-Absolute file path
old_stringstringYes-Exact text to replace (must be unique)
new_stringstringYes-Replacement code
astrograph_suppress

Suppress a duplicate by WL hash (from analyze output).

ParameterTypeRequiredDefaultDescription
wl_hashstringYes-WL hash from analyze output
astrograph_suppress_batch

Suppress multiple duplicates by WL hash list.

ParameterTypeRequiredDefaultDescription
wl_hashesstring[]Yes-WL hashes from analyze output
astrograph_unsuppress

Unsuppress a hash.

ParameterTypeRequiredDefaultDescription
wl_hashstringYes-The WL hash to unsuppress
astrograph_unsuppress_batch

Unsuppress multiple hashes.

ParameterTypeRequiredDefaultDescription
wl_hashesstring[]Yes-WL hashes to unsuppress
astrograph_list_suppressions

List suppressed hashes.


Configuration Examples

Claude Desktop

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "astrograph": {
      "command": "docker",
      "args": [
        "run", "--rm", "-i", "--pull", "always",
        "-v", "/path/to/your/project:/workspace",
        "-v", "/path/to/your/project/.metadata_astrograph:/workspace/.metadata_astrograph",
        "thaylo/astrograph"
      ]
    }
  }
}
Cursor
{
  "mcpServers": {
    "astrograph": {
      "command": "docker",
      "args": [
        "run", "--rm", "-i", "--pull", "always",
        "-v", "${workspaceFolder}:/workspace",
        "-v", "${workspaceFolder}/.metadata_astrograph:/workspace/.metadata_astrograph",
        "thaylo/astrograph"
      ]
    }
  }
}
Project-level (.mcp.json)
{
  "mcpServers": {
    "astrograph": {
      "command": "docker",
      "args": [
        "run", "--rm", "-i", "--pull", "always",
        "-v", ".:/workspace",
        "-v", "./.metadata_astrograph:/workspace/.metadata_astrograph",
        "thaylo/astrograph"
      ]
    }
  }
}
Local Python (without Docker)
pip install -e .
{
  "mcpServers": {
    "astrograph": {
      "command": "python",
      "args": ["-m", "astrograph.server"],
      "cwd": "/path/to/astrograph"
    }
  }
}

How It Works

Technical details
  1. AST to Graph: Python code is parsed into AST, then converted to a labeled directed graph
  2. Weisfeiler-Leman Hashing: Graphs are hashed using WL algorithm for O(1) lookup
  3. Structural Fingerprinting: Quick filtering based on node counts and degree sequences
  4. Full Isomorphism Verification: NetworkX VF2 algorithm confirms structural equivalence
Python Source → AST Parser → Graph → WL Hash → Index Lookup → Match

The Docker image runs in event-driven mode by default:

  • SQLite persistence with WAL mode (survives restarts)
  • File watching (auto re-index on changes)
  • Analysis cache (instant responses)

Development

pip install -e ".[dev]"
pytest tests/ -q
docker build -t astrograph .

License

MIT

Tag summary

Content type

Image

Digest

sha256:28e58c6dd

Size

145.5 MB

Last updated

4 months ago

docker pull thaylo/astrograph