Skip to main content
Glama
wrangler-local-mcp

wrangler-local-mcp

by xudukongju
README.md
# wrangler-local-mcp

Read-only dual-mode MCP server and diagnostic CLI for **Cloudflare Wrangler / Miniflare v3** local development state.

Built for AI coding agents (Claude Code, Codex CLI, Cursor, Windsurf) and developers debugging local Workers, D1, KV, R2, Durable Objects, and Wrangler 4.118+ Observability traces — even when `wrangler dev` is stopped or crashed.

- **Zero native compilation dependencies**: Uses Node.js `>=22.13.0` built-in `node:sqlite` (`DatabaseSync`, unflagged since 22.13.0) and `node:v8` (`v8.deserialize`) — no `better-sqlite3` or `node-gyp`.
- **Read-only by default**: Every SQLite handle enforces `readOnly: true`, `PRAGMA query_only = ON`, and `PRAGMA busy_timeout = 1000`, keeping `.sqlite` and `-wal` files byte-for-byte untouched. D1 SQL queries block writes, multi-statement queries, `ATTACH`, and `LOAD_EXTENSION`.
- **Secret-safe (best-effort)**: `.dev.vars` preflight compares key names only (values are never read or returned). Outputs apply best-effort redaction for Bearer/Basic tokens, JWTs, common API key prefixes, PEM private keys, and sensitive field names.
- **Offline + Live dual-mode**: Reads `.wrangler/state/v3` (including active WAL/SHM files) directly when `wrangler dev` is offline, and detects a live Local Explorer endpoint (`/cdn-cgi/local/explorer/api`) when running.

## Capability Matrix

| Capability | `wrangler-local-mcp` | Official `cloudflare/mcp` | Wrangler Local Explorer |
| --- | --- | --- | --- |
| Local `.wrangler/state/v3` offline inspection | Yes | No (cloud API only) | No (requires running `wrangler dev`) |
| D1 schema, migrations, read-only SQL with guardrails | Yes | Cloud D1 only | Browser / REST UI |
| KV namespaces, exact 64-hex mapping, safe blob decode | Yes | Cloud KV only | Browser / REST UI |
| R2 object listing & head metadata (no binary floods) | Yes | Cloud R2 only | Browser / REST UI |
| Durable Objects SQLite tables + `_cf_KV` (V8) + `_cf_ALARM` | Yes | No | SQLite tables only (no `_cf_KV` decode) |
| Local Observability traces/logs (SQLite JSONB + WAL) | Yes | Cloud logs only | Partial |
| State snapshots (`.sqlite`+`-wal`+`-shm`) & structured diff | Yes | No | No |
| Multi-project discovery & key-only `.dev.vars` preflight | Yes | No | No |
| Zero native addons (`node:sqlite`, Node >=22.13) | Yes | N/A | Bundled in Wrangler |

## Quick Start

### 1. Diagnostic CLI (zero config)

```bash
# Run a local state & preflight health report
npx wrangler-local-mcp doctor ./my-worker

# Machine-readable JSON output
npx wrangler-local-mcp doctor ./my-worker --json

# Inspect specific subsystems
npx wrangler-local-mcp inspect ./my-worker --do
npx wrangler-local-mcp inspect ./my-worker --traces --limit 20

# Snapshot and diff local state
npx wrangler-local-mcp snapshot ./my-worker --label before-migration
npx wrangler-local-mcp diff ./my-worker before-migration
```

### 2. MCP Server Registration

Running `wrangler-local-mcp` with no subcommand (or `serve`) starts a stdio MCP server with zero `stdout` log pollution.

**Claude Code:**
```bash
claude mcp add wrangler-local -- npx -y wrangler-local-mcp
```

**Codex CLI (`~/.codex/config.toml`):**
```toml
[mcp_servers.wrangler_local]
command = "npx"
args = ["-y", "wrangler-local-mcp"]
```

**Cursor / Windsurf (`mcp.json`):**
```json
{
  "mcpServers": {
    "wrangler-local": {
      "command": "npx",
      "args": ["-y", "wrangler-local-mcp"]
    }
  }
}
```

## Registered MCP Tools (11)

All tools declare `readOnlyHint: true` and never modify `.sqlite` or `-wal` data files inside `.wrangler/state/v3`:

| Tool | Purpose |
| --- | --- |
| `cf_local_discover` | Discover Wrangler projects (single or monorepo), inventory local state + WAL sizes, probe live Local Explorer |
| `cf_preflight_check` | Validate config, reconcile `.dev.vars` keys (names only), detect unmapped stores, large WALs, pending migrations, alarms, and trace errors |
| `cf_d1_inspect` | Inspect D1 bindings (mapped, uninitialized, unmapped), tables, columns, row counts, indexes, and `d1_migrations` |
| `cf_d1_query` | Run guarded single-statement read-only SQL (`SELECT`/`WITH`/`EXPLAIN`/read-only `PRAGMA`), default `LIMIT 100`, 4 KB cell cap, redaction |
| `cf_kv_list` | List KV entries, expiration status, metadata, and blob sizes across mapped and unmapped namespaces |
| `cf_kv_get` | Read and safely decode a KV blob (JSON, text, or binary digest summary) with secret redaction |
| `cf_do_inspect` | Inspect Durable Objects: reverse-lookup `__miniflare_do_name`, separate system vs business tables, V8-decode `_cf_KV`, list `_cf_ALARM` |
| `cf_r2_list` | List R2 objects or fetch single-object head metadata (`size`, `etag`, `http_metadata`, `custom_metadata`, `checksums`) |
| `cf_local_traces` | Query Wrangler 4.118+ local observability `spans` and `logs` (including active WAL), decoding SQLite JSONB `attributes` |
| `cf_state_snapshot` | Copy `.sqlite` + `-wal` + `-shm` triplets and structural fingerprints into isolated `.wrangler-local-mcp/snapshots/<id>/` |
| `cf_state_diff` | Diff two snapshots, or a baseline snapshot against live `.wrangler/state/v3` |

## Safety Boundaries

- **SQLite read-only guarantees & WAL `-shm` note**: Connections open with `new DatabaseSync(file, { readOnly: true })`, `PRAGMA query_only = ON`, and `PRAGMA busy_timeout = 1000`, keeping `.sqlite` main database files and `-wal` write-ahead logs byte-for-byte unmodified. Note that when `.wrangler/state/v3` databases are in active WAL mode, SQLite's own reader protocol updates volatile read-mark metadata inside the companion `-shm` shared-memory index file (which holds no user data and is regenerated by Wrangler/SQLite on demand).
- **SQL injection & mutation guardrails**: Comments, string literals, and delimited identifiers (`"..."`, `` `...` ``, `[...]`) are parsed before token inspection; multi-statement queries, `INSERT`/`UPDATE`/`DELETE`/`DROP`/`ALTER`/`CREATE`/`REPLACE`, `ATTACH`/`DETACH`, `VACUUM`, `LOAD_EXTENSION` (unquoted or quoted), and writable `PRAGMA`s are rejected. Whitelisted read-only `PRAGMA`s (including `main.` / `temp.` schema qualifiers) are allowed.
- **No `.dev.vars` value reads**: Preflight only extracts key names before `=` and discards values immediately.
- **Best-effort output redaction**: Built-in redaction masks Bearer/Basic credentials, JWTs, common API key prefixes, PEM private keys, and sensitive keys (`*_secret`, `*_password`, `*_token`, `*_key`, `privateKey`, etc. — with explicit allowlisting for structural keys like `primary_key`, `r2_key`, `kv_key`, `public_key`, `key`). Redaction is best-effort pattern/key matching and does not guarantee catching arbitrary unpatterned plaintext values stored under generic column names.
- **Snapshot isolation & operational notes**: Snapshots write only to `.wrangler-local-mcp/snapshots/<id>/` with strict snapshot ID validation and root-containment checks. If `wrangler dev` is actively writing, pause it before taking a snapshot to avoid torn file copies across `.sqlite`/`-wal` files. Queries run synchronously via `node:sqlite`'s `DatabaseSync` and are bounded by row limits and cell/blob byte caps.

## Links

- Repository: `https://github.com/xudukongju/wrangler-local-mcp`
- Security Policy: [SECURITY.md](./SECURITY.md)

## License

[MIT](./LICENSE) Copyright (c) 2026 xudukongju