# chiasmus

An MCP server that gives LLM coding assistants a formal reasoning engine for structural code analysis. Instead of iteratively grepping through files, the LLM makes a single tool call and gets a provably correct answer about reachability, dead code, dependency cycles, or impact analysis.

## How it works

The pipeline has four stages:

1. **Tree-sitter parsing** — source files become typed ASTs with method definitions, call relationships, imports, and exports identified.
2. **Prolog fact generation** — structural relationships are converted to declarative Prolog facts (e.g., `calls(moduleA, funcX, moduleB, funcY).`).
3. **Rule-based analysis** — built-in Prolog rules handle cycle-safe transitive reachability, dead code detection, and dependency analysis.
4. **Query execution** — Prolog's backtracking engine answers the query exhaustively and returns the result in a single MCP tool response.

This is a [[neurosymbolic-ai]] pattern: the LLM handles natural language understanding and decides what to ask, the symbolic solver handles exhaustive graph traversal and constraint satisfaction.

## What it can answer

- **Transitive reachability**: "Can user input reach this SQL query?" — follows call chains across files and modules.
- **Dead code detection**: routines never called from any entry point.
- **Cycle detection**: circular call dependencies.
- **Impact analysis**: everything that depends on a modified method.

## Token economics

A five-hop transitive analysis via grep might cost ~2,500 tokens across multiple tool calls, with the LLM reasoning about each hop. Chiasmus answers the same question in ~200 tokens with one tool call — the Prolog solver does the heavy computation locally.

## Setup

```bash
claude mcp add chiasmus -- npx -y chiasmus
```

Exposes nine MCP tools for graph analysis, formal verification, template creation, and autonomous solving.

## Limitations

Depends on tree-sitter grammar availability for the target language. The Prolog fact generation captures call-graph structure but not runtime behavior (reflection, dynamic dispatch, eval). Answers are sound for the static structure but not complete for languages with heavy metaprogramming.

https://github.com/yogthos/chiasmus
