Skip to main content
Glama
deterministic-mcp-guardrails

deterministic-mcp-guardrails

by mateo627
README.md
# deterministic-mcp-guardrails

Clean-room, zero-external-dependency Node.js ESM reference implementation of a deterministic, zero-LLM compliance and engineering MCP control plane.

Every policy gate, static scanner, cryptographic approval check, and receipt-chain verifier executes deterministically using only Node.js built-in modules (`node:crypto`, `node:fs`, `node:path`, `Intl.DateTimeFormat`). Ambiguous, malformed, expired, replayed, or unverified inputs always **fail closed**.

## Architecture

```text
                        stdio (JSON-RPC 2.0 NDJSON)
   +------------------+ ----------------------------> +------------------------------+
   | MCP Client /     |                               | src/mcp/server.mjs           |
   | Coding Agent     | <---------------------------- | 7 Deterministic MCP Tools    |
   +------------------+   structuredContent + JSON    +--------------+---------------+
                                                                     |
          +----------------------+---------------------+-------------+--------------+
          |                      |                     |                            |
          v                      v                     v                            v
+-------------------+  +-------------------+  +-------------------+  +---------------------------+
| Quiet-Hours       |  | Static Guardrails |  | PreToolUse Gate   |  | PostToolUse Ledger &      |
| Engine            |  | Engine            |  | (HMAC-SHA256)     |  | Verdict Compiler          |
| src/engine/       |  | src/engine/       |  | src/gate/         |  | src/ledger/               |
| quiet-hours.mjs   |  | static-guardrails |  | hmac-gate.mjs     |  | receipt-ledger.mjs        |
+-------------------+  +-------------------+  +-------------------+  +---------------------------+
| - 08:00-21:00 TZ  |  | - SQL-001..003    |  | - timingSafeEqual |  | - SHA-256 JSONL chain     |
| - Intl IANA DST   |  | - JS-001..003     |  | - 6-tuple binding |  | - tamper/gap detection    |
| - Fail-closed on  |  | - HTML-001        |  | - ReplayCache     |  | - compileVerdict rejects  |
|   ambiguous NANP  |  | - Zero LLM calls  |  | - Strict expiry   |  |   on any failed check     |
+-------------------+  +-------------------+  +-------------------+  +---------------------------+
```

## Components

- `src/engine/quiet-hours.mjs`: Pure deterministic US FCC/TCPA quiet-hours engine (`08:00-21:00` recipient local time) using `Intl.DateTimeFormat` IANA timezones, covering single-TZ states, Hawaii, Alaska, Puerto Rico, Arizona (no DST), DST spring-forward/fall-back boundaries, and fail-closed rejection of unknown or multi-TZ ambiguous NANP area codes.
- `src/engine/static-guardrails.mjs`: Zero-LLM SQL migration and JS/TS/HTML static safety rule engine enforcing 7 rules (`SQL-001`, `SQL-002`, `SQL-003`, `JS-001`, `JS-002`, `JS-003`, `HTML-001`).
- `src/gate/hmac-gate.mjs`: Constant-time (`crypto.timingSafeEqual`) HMAC-SHA256 `PreToolUse` human-approval gate cryptographically binding `(action, scope, artifactSha256, approverId, expiresAtIso, nonce)` with single-use `ReplayCache` enforcement and strict expiration validation.
- `src/ledger/receipt-ledger.mjs`: Append-only hash-chained SHA-256 JSONL receipt ledger (`appendReceipt`, `verifyChain`, `verifyChainFromText`) and synchronous `PostToolUse` compliance compiler (`compileVerdict`).
- `src/mcp/server.mjs`: Thin stdio JSON-RPC 2.0 MCP server exposing 7 tools:
  1. `check_quiet_hours`
  2. `scan_sql_migration`
  3. `scan_js_source`
  4. `mint_approval_token`
  5. `verify_approval_gate`
  6. `compile_verdict`
  7. `verify_receipt_chain`

## Quickstart

Requires Node.js >= 20 (zero runtime npm dependencies; no `npm install` needed).

```bash
# Run unit and live stdio MCP integration tests
npm test

# Run the standalone mutation proof matrix (0 false positives, 100% mutant kill rate)
npm run mutation-test

# Start the stdio JSON-RPC 2.0 MCP server
DMG_HMAC_SECRET="replace-with-32-byte-random-secret" npm start
```

## Verified Test Output

### `npm test`

```text
> deterministic-mcp-guardrails@1.0.0 test
> node --test tests/guardrails.test.mjs

✔ quiet-hours: allows calling during 08:00-20:59 local time and blocks outside (10.634ms)
✔ quiet-hours: handles Hawaii, Alaska, Puerto Rico, Arizona no-DST, and DST transitions (0.6468ms)
✔ quiet-hours: fails closed on ambiguous, unknown, malformed inputs, and naive timestamps (0.2607ms)
✔ static-guardrails: SQL-001, SQL-002, SQL-003 positive detection and clean negatives (1.1796ms)
✔ static-guardrails: JS-001, JS-002, JS-003, HTML-001 positive detection and clean negatives (0.7497ms)
✔ hmac-gate: mint, timing-safe verify, single-use replay rejection, tamper & expiry checks (2.6607ms)
✔ receipt-ledger: hash-chained JSONL append, verify, tamper detection, and fail-closed compileVerdict (6.0257ms)
✔ mcp-server: live stdio JSON-RPC 2.0 initialize, tools/list, and tools/call round-trip (54.2873ms)
ℹ tests 8
ℹ suites 0
ℹ pass 8
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 135.9876
```

### `npm run mutation-test`

```json
{
  "baselineFixtures": 7,
  "falsePositives": 0,
  "totalMutants": 22,
  "killedMutants": 22,
  "escapedMutants": 0,
  "killRatePercent": 100,
  "passed": true
}
```

## Failure Modes

See [FAILURE_MODES.md](./FAILURE_MODES.md) for the complete failure-mode matrix and fail-closed guarantees.

## License

MIT License. Copyright (c) 2026 Mateo Cafferata.