Skip to main content
Glama
README.md
# TMH

**Deterministic Multi-Language Code Graph, Living Knowledge Base, Anti-Hallucination Evidence Engine & Zero-Dependency MCP Server.**

[中文说明 (README.zh-CN.md)](./README.zh-CN.md)

TMH is a **pure Python standard-library** (zero third-party dependencies) engineering harness designed for understanding, evolving, and auditing real-world / legacy codebases—both directly from the CLI and as a built-in **stdio MCP server** for AI coding agents such as **Cursor**, **Claude Code**, and **Codex**.

## Why TMH?

AI coding agents often struggle on large or legacy repositories: they grep blindly, miss cross-file call chains and database side effects, hallucinate file paths/line numbers, and lose team domain rules between sessions.

TMH solves this with four deterministic primitives:

1. **Deterministic Polyglot Code Graph (`.tmh/cache/graph.db`)**
   - Indexes call chains (`fn -> fn`), HTTP routes, database tables / DDL / foreign keys / CRUD edges (`fn -> tbl`), business domains, business flows, and code-level statecharts in local SQLite.
   - Supports **Java, Go, Python, JavaScript/TypeScript (Express, NestJS, Next.js), C#/.NET, PHP/Laravel, Rust, Ruby/Rails, Kotlin, C++, Prisma & SQL**.
2. **Living Project Knowledge Base (`.tmh/knowledge/`)**
   - Bootstraps structured module/domain/flow/entity Markdown pages grounded in source citations.
   - Automatically tracks **code-fingerprint staleness** when underlying source files change, and distinguishes `confirmed` vs. `inferred` vs. `stale` knowledge.
   - Ships with built-in domain KBs (`builtin:crypto-sm.md`, `builtin:db-dialects.md`, `builtin:dotnet.md`, `builtin:frontend.md`, `builtin:middleware.md`, `builtin:os-arch.md`) for enterprise modernization.
3. **Anti-Hallucination Verification & Tamper-Evident Evidence Chain**
   - `verify-evidence` validates `file:line[-end]` citations against real source files (with automatic secret-file blocking).
   - `evidence.py` implements an append-only SHA-256 hash chain and **in-toto Statement v1** export/offline anchor verification.
4. **Zero-Dependency Stdio MCP Server (`tmh-mcp`)**
   - Plugs straight into Cursor, Claude Code, Codex, or any MCP client over stdio JSON-RPC 2.0 with **zero `pip` runtime dependencies**.

---

## Installation

Requires **Python 3.10+** (no third-party packages required).

```bash
# Install directly from GitHub
pip install git+https://github.com/auntunt/tmh.git

# Or clone and install in editable mode
git clone https://github.com/auntunt/tmh.git
cd tmh
pip install -e .
```

Verify installation:

```bash
tmh --version
tmh-mcp --version
```

---

## Quickstart (CLI)

```bash
# 1. Scan a repository to build .tmh/cache/graph.db and bootstrap living KB pages
tmh --pretty scan --repo /path/to/your-repo

# 2. View architecture overview (domains, flows, tables, CRUD matrix, built-in KBs)
tmh --pretty overview --repo /path/to/your-repo

# 3. Analyze change impact for a file, function, table, or route
tmh --pretty impact --repo /path/to/your-repo --target OrderService

# 4. Trace business flow & verifiable source evidence for a question
tmh --pretty chain --repo /path/to/your-repo --query "order approval workflow"

# 5. Search code graph + living KB + built-in KBs (CN/EN keyword expansion)
tmh --pretty search --repo /path/to/your-repo --query "invoice"

# 6. Verify a source citation against the actual codebase
tmh --pretty verify-evidence --repo /path/to/your-repo --ref src/orders.py:10-25

# 7. Extract code-level statecharts
tmh --pretty statecharts --repo /path/to/your-repo

# 8. Check living KB coverage and staleness
tmh --pretty kb-status --repo /path/to/your-repo
```

### Tamper-Evident Evidence Chain (in-toto Statement v1)

```bash
# Append records to a hash-chained JSONL log
tmh evidence-append --log .tmh/evidence/PR-42.jsonl --kind static_scan --by ci --data '{"issues":0}'
tmh evidence-append --log .tmh/evidence/PR-42.jsonl --kind human_signoff --by reviewer --data '{"approved":true}'

# Export an in-toto Statement v1 package
tmh evidence-export --log .tmh/evidence/PR-42.jsonl --name PR-42 --summary '{"approved":true}' --out PR-42.statement.json

# Offline verification (optionally anchored to a >=32-hex hash saved at sign-off)
tmh --pretty evidence-verify --statement PR-42.statement.json --anchor <hash-from-signoff>
```

---

## MCP Server Integration (Cursor / Claude Code / Codex)

TMH includes a built-in stdio MCP server (`tmh-mcp` or `tmh mcp`). Pass `--repo` (or set `TMH_REPO`) to bind a default workspace so tools do not need `repoPath` on every call.

### Cursor (`.cursor/mcp.json`)

```json
{
  "mcpServers": {
    "tmh": {
      "command": "tmh-mcp",
      "args": ["--repo", "/absolute/path/to/your-repo"]
    }
  }
}
```

### Claude Code

```bash
claude mcp add tmh -- tmh-mcp --repo /absolute/path/to/your-repo
```

### Codex (`~/.codex/config.toml`)

```toml
[mcp_servers.tmh]
command = "tmh-mcp"
args = ["--repo", "/absolute/path/to/your-repo"]
```

### Exposed MCP Tools

| Tool | Purpose |
| :--- | :--- |
| `tmh_scan` | Scan/index repository into `.tmh/cache/graph.db` and bootstrap living KB pages |
| `tmh_overview` | Architecture summary: domains, flows, DB tables/DDL/FKs, CRUD matrix, built-in KBs |
| `tmh_search` | Search code graph, `.tmh/knowledge/`, and built-in KBs (CN-EN bilingual expansion) |
| `tmh_read` | Read `builtin:<name>.md`, `.tmh/knowledge/` pages, or repo files by line range (blocks secret files) |
| `tmh_verify_evidence` | Verify `file:line[-end]` citations against real source files |
| `tmh_impact` | Change impact analysis across call chains, domains, flows, and DB tables |
| `tmh_chain` | Business-flow Q&A returning grounded flows and verifiable source citations |
| `tmh_statecharts` | Extract deterministic code-level state machines |
| `tmh_kb_status` | Living KB coverage, confirmation state, staleness, and open questions |
| `tmh_kb_next` | Claim next unwritten or stale knowledge pages with grounded graph facts |
| `tmh_kb_commit` | Write/update a living knowledge page with citation & fingerprint anchoring |
| `tmh_kb_confirm` | Mark a living knowledge page as human-confirmed |

---

## Architecture

```text
src/tmh/
├── cli.py                 # Unified `tmh` CLI
├── mcp_server.py          # Zero-dependency stdio MCP JSON-RPC 2.0 server (`tmh-mcp`)
├── bridge.py              # High-level JSON query/workflow engine
├── graph.py               # SQLite deterministic code graph (.tmh/cache/graph.db)
├── polyglot_structure.py  # Universal polyglot route/table/flow extractor
├── flow.py                # Call chain & business flow tracer
├── golang.py / python_index.py / codeindex.py / mapper.py / scanner.py
├── kb.py / kbqa.py / project_knowledge.py / domains.py / context.py / query.py
├── evidence.py            # Append-only SHA-256 hash chain & in-toto Statement v1
└── kb/                    # Bundled built-in domain KBs (*.md)
```

---

## Development & Testing

```bash
PYTHONPATH=src python3 -m unittest discover -s tests -p "test_*.py" -v
```

## License

[MIT](./LICENSE) © 2026 auntunt

TDQS

A3.5/5.0

Scored across 12 tools

Disambiguation4/5

Most tools target clearly distinct operations (scan, overview, search, read, verify, statecharts, and a four-step KB workflow). The main overlap is between tmh_impact (deterministic change impact for a file/symbol/table/route) and tmh_chain (business-question tracing), since both return affected domains, flows, and citations, and between tmh_search and tmh_read where one finds and the other retrieves. Descriptions do enough to steer selection, but an agent could occasionally pick the wrong one of the pair.

Naming Consistency4/5

All names share a consistent tmh_ prefix and snake_case convention, with the KB tools forming a clean tmh_kb_* subgroup. Within the prefix, styles shift between verbs (scan, search, read, verify_evidence, commit, confirm) and noun-like labels (overview, impact, chain, statecharts, status, next). Still highly predictable and readable, just not a single uniform verb_noun pattern.

Tool Count5/5

Twelve tools sit comfortably in the well-scoped 3-15 range and each maps to a real stage of the workflow: indexing, exploration, evidence verification, impact analysis, and the KB authoring lifecycle. No obviously redundant or filler tools are present.

Completeness4/5

The surface covers the full path from scan to knowledge maintenance (status, next, commit, confirm) plus verification and impact tracing, so agents have no major dead ends. Minor gaps remain, such as no explicit delete/retire or invalidate operation for knowledge pages and no raw graph/dependency query separate from impact analysis. These are workaroundable rather than blocking.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues