astrograph
An MCP server that detects duplicate code using AST graph isomorphism, blocking writes of structurally identical code to reduce redundancy.
README
ASTrograph
<p align="center"> <img src="astrograph_poster.jpg" alt="ASTrograph" width="400"> </p>
An MCP server that helps AI agents detect duplicate code before writing it. It provides write and edit tools that compare new code against existing functions in your codebase using AST graph isomorphism — powered by algorithms, not LLM tokens. When a structural duplicate is found, the operation is blocked with a pointer to the existing code. Variable names, formatting, and comments are ignored — if two pieces of code share the same abstract structure, ASTrograph flags them as duplicates.
Installation
Add .mcp.json to your project root:
{
"mcpServers": {
"astrograph": {
"command": "docker",
"args": [
"run", "--rm", "-i", "--pull", "missing",
"--add-host", "host.docker.internal:host-gateway",
"-v", ".:/workspace",
"thaylo/astrograph:latest"
]
}
}
}
The image is multi-arch (amd64, arm64). The codebase is indexed at startup. Metadata is stored outside the project directory (in the user data dir) so it never interferes with your codebase.
To update to a new release:
docker pull thaylo/astrograph:latest
The running version is always visible in the MCP serverInfo.version field on connect.
<details> <summary><strong>Claude Desktop</strong></summary>
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", "missing",
"--add-host", "host.docker.internal:host-gateway",
"-v", "/absolute/path/to/project:/workspace",
"thaylo/astrograph:latest"
]
}
}
}
</details>
<details> <summary><strong>Codex</strong></summary>
~/.codex/config.toml:
[mcp_servers.astrograph]
command = "docker"
args = [
"run", "--rm", "-i", "--pull", "missing",
"--add-host", "host.docker.internal:host-gateway",
"-v", "/absolute/path/to/project:/workspace",
"thaylo/astrograph:latest"
]
</details>
<details> <summary><strong>wmark</strong></summary>
~/.config/wmark/.mcp.json (user-level, applies to all projects on macOS):
{
"mcpServers": {
"astrograph": {
"command": "docker",
"args": [
"run", "--rm", "-i", "--pull", "missing",
"--add-host", "host.docker.internal:host-gateway",
"-v", "/Users:/Users:rw",
"thaylo/astrograph:latest"
]
}
}
}
Mounting /Users makes all macOS home paths accessible inside the container unchanged. Call set_workspace with the full host path (e.g. /Users/yourname/project) to index a project.
For Linux, replace /Users:/Users:rw with /home:/home:rw.
</details>
<details> <summary><strong>Without Docker</strong></summary>
pip install .
{
"mcpServers": {
"astrograph": {
"command": "python",
"args": ["-m", "astrograph.server"],
"cwd": "/path/to/astrograph"
}
}
}
</details>
How it works
Your codebase already contains:
# src/math.py
def calculate_sum(a, b):
return a + b
An AI agent tries to write:
# src/utils.py
def add_numbers(x, y):
return x + y
ASTrograph detects the duplicate and blocks the write:
BLOCKED: Cannot write - identical code exists at src/math.py:calculate_sum (lines 1-2).
Reuse the existing implementation instead.
Different variable names, identical structure. Source code is converted into labeled directed graphs and compared using Weisfeiler-Leman hashing with VF2 isomorphism verification — all algorithmic, no LLM tokens spent on the search.
Detection types
ASTrograph detects four types of structural duplication:
| Type | What it catches | How it works |
|---|---|---|
| Exact | Identical AST structure with renamed variables or different formatting | WL hash identity + VF2 graph isomorphism verification |
| Pattern | Same control flow with different operators or constants | Operator-normalized graph hashing |
| Block | Duplicate inner blocks (for/if/while/try) within functions | Block-level AST extraction + hash matching |
| Near-duplicate | ~80% structural similarity — copy-paste-modify patterns | Hierarchy hash prefix matching at 4/5 depth levels |
Near-duplicate detection catches Type-3 clones that exact and pattern detection miss. For example, Flask's TagBytes, TagDateTime, TagTuple, and TagUUID classes share 80%+ identical structure but differ in leaf-level details.
Language support
Python, JavaScript, and TypeScript work out of the box. C, C++, Java, and Go attach to an already-running language server over TCP.
| Language | Versions | Mode | Default endpoint |
|---|---|---|---|
| Python | 3.11 -- 3.14 | bundled | pylsp |
| JavaScript | ES2021+, Node 20/22/24 LTS | bundled | typescript-language-server --stdio |
| TypeScript | TypeScript 5.x, Node 20/22/24 LTS | bundled | typescript-language-server --stdio |
| Go | 1.21 -- 1.25 | attach | tcp://127.0.0.1:2091 |
| C | C11, C17, C23 | attach | tcp://127.0.0.1:2087 |
| C++ | C++17, C++20, C++23 | attach | tcp://127.0.0.1:2088 |
| Java | 11, 17, 21, 25 | attach | tcp://127.0.0.1:2089 |
The Docker image bundles Python and JS/TS LSP runtimes. For attach-based languages, expose the language server on a TCP port using socat and configure via your MCP JSON:
{
"mcpServers": {
"astrograph": {
"command": "docker",
"args": ["run", "--rm", "-i", "--add-host", "host.docker.internal:host-gateway", "-v", ".:/workspace", "thaylo/astrograph:latest"],
"env": {
"ASTROGRAPH_CPP_LSP_COMMAND": "tcp://host.docker.internal:2088",
"ASTROGRAPH_GO_LSP_COMMAND": "tcp://host.docker.internal:2091",
"ASTROGRAPH_JAVA_LSP_COMMAND": "tcp://host.docker.internal:2089",
"ASTROGRAPH_C_LSP_COMMAND": "tcp://host.docker.internal:2087"
}
}
}
}
| Language | Env var | Socat bridge example |
|---|---|---|
| C | ASTROGRAPH_C_LSP_COMMAND |
socat TCP-LISTEN:2087,reuseaddr,fork EXEC:clangd |
| C++ | ASTROGRAPH_CPP_LSP_COMMAND |
socat TCP-LISTEN:2088,reuseaddr,fork EXEC:clangd |
| Java | ASTROGRAPH_JAVA_LSP_COMMAND |
socat TCP-LISTEN:2089,reuseaddr,fork EXEC:jdtls |
| Go | ASTROGRAPH_GO_LSP_COMMAND |
socat TCP-LISTEN:2091,reuseaddr,fork EXEC:"gopls serve" |
| Python | ASTROGRAPH_PY_LSP_COMMAND |
(bundled, override if needed) |
| JS | ASTROGRAPH_JS_LSP_COMMAND |
(bundled, override if needed) |
| TS | ASTROGRAPH_TS_LSP_COMMAND |
(bundled, override if needed) |
Run lsp_setup(mode='inspect') to see which languages are available and what's missing.
Real-world results
Tested on popular open-source projects:
| Project | Language | Files | Code Units | Duplicates Found |
|---|---|---|---|---|
| Redis | C | 208 | 18,272 | 556 groups |
| TypeORM | TypeScript | 492 | 7,107 | 511 groups |
| Express.js | JavaScript | 141 | 3,866 | 468 groups |
| nlohmann/json | C++ | 488 | 9,103 | 959 groups |
| Gin | Go | 99 | 1,557 | 141 groups |
| Flask | Python | 24 | 910 | 48 groups |
| Spring PetClinic | Java | 47 | 270 | 17 groups |
Exact, pattern, and block findings are verified via VF2 graph isomorphism. Near-duplicates are matched via hierarchy hash prefix similarity (~80% structural identity).
Star History
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。