Skip to main content
Glama
README.md
# maza-mcp

A [Model Context Protocol](https://modelcontextprotocol.io) server that lets AI
agents use and manage **MAZA** — the cryptocurrency Sami revived after its
abandonment (see `docs/DESIGN.md` for why that history shaped this design).

An agent connected to this server can ask questions a human would need a
wallet, a block explorer and a calculator to answer — and can move money
under local spend limits, stated in dollars, that the server enforces before any money
moves.

- **Zero dependencies.** The server is pure Python stdlib (≥ 3.10). Nothing to
  audit but the code itself; it runs anywhere Python runs.
- **Three backends, one set of tools.** A real `mazad` node (RPC), a public
  indexer (read-only HTTP), and an offline demo chain. Auto mode uses whichever
  of the first two you configure: with both, reads come from the indexer and
  wallet and spending from the node.
- **Money is integer satoshis inside, decimal strings on the wire.** A
  number that arrives as a float (a daemon amount, a Python caller's value)
  is read through its shortest decimal form first; no money arithmetic is
  done in floating-point. (A figure the daemon reports is exact up to
  67,108,864 MAZA in one amount; above that a JSON number cannot carry
  satoshi precision, and it is the figure shown, never the amount sent.)
- **Sends are fail-closed.** `MAZA_MCP_ALLOW_SEND=0` by default. Two-phase
  preview→confirm (`preview_token`): request binding plus an extra explicit
  tool step, off unless `MAZA_MCP_CONFIRM_SEND=1` — it is not a human approval
  step (see "Preview and confirm" below). Per-transaction and per-UTC-day limits, stated
  in dollars (or in coins) and adjustable while the server runs. Optional
  spend-from allowlist. Every call audited to a `0600` JSONL log with
  redaction while `MAZA_MCP_AUDIT` is on (the default; `0` turns it off).
- **Proofs are re-checked, with the anchor named.** A merkle proof from the
  indexer is *re-verified locally* against the root it names; whether that root
  belongs to the chain you trust is decided by the header you compare it with,
  not by the proof. `verify_merkle_proof` accepts raw data and checks the math.

## Quick start

```sh
pip install git+https://github.com/SamiAhmed7777/maza-mcp.git   # or, in a clone: pip install .
maza-mcp selftest        # offline demo chain: proves the whole tool surface
MAZA_MCP_MODE=demo maza-mcp list   # the tools, their arguments and limits (the simulated chain's set)
maza-mcp check           # your node/indexer reachable? which mode? (nothing set: says what to configure)
maza-mcp limit --state-dir ~/.maza-mcp   # the spend limits in force; change them: --send-usd A --daily-usd B
```

### Connect an agent host (stdio)

An MCP host that can launch a stdio server (Claude Desktop, a custom agent,
another Hermes — the configuration key and launch shape are the host's, not
this server's):

```json
{
  "mcpServers": {
    "maza": {
      "command": "maza-mcp",
      "args": ["serve"],
      "env": {
        "MAZA_MCP_MODE": "rpc",
        "MAZA_MCP_RPC_URL": "http://127.0.0.1:12832",
        "MAZA_MCP_RPC_USER": "…",
        "MAZA_MCP_RPC_PASSWORD": "…",
        "MAZA_MCP_ALLOW_SEND": "1",
        "MAZA_MCP_MAX_SEND_USD": "1",
        "MAZA_MCP_MAX_DAILY_USD": "5"
      }
    }
  }
}
```

Read-only deployments drop the last three variables: the server then answers
chain questions, proofs and history, and *cannot* spend no matter what the
agent (or an attacker whispering to it) asks for.

## Running on mainnet, for real

The server is built for mainnet: `MAZA_MCP_NETWORK` defaults to `main`, and
the address checks, price quotes and examples all assume the real chain.
A deployment that moves real MAZA should look like this:

1. **A dedicated wallet, lightly funded.** Point the agent at its own
   `mazad` wallet (its own datadir or wallet), holding only what it may
   lose. A limit guards against a wrong amount; it is not a wall against an
   agent that has been fooled, because the agent can change a limit when it
   is asked to (see "Spend limits, in dollars" below). The balance is what
   holds. Do not point it at the treasury.
2. **Bind every payment to its preview.** `MAZA_MCP_CONFIRM_SEND=1` makes
   `send` refuse until it carries the token `preview_send` issued for that
   exact payee, amount, key and note. It is request binding and one more
   explicit tool step — not a human approval: an agent that is willing to make
   both calls gets its payment through, and the tool refuses only a request
   that changed between the two. The human boundary is the switch itself
   (`ALLOW_SEND`), the payee list, and what the wallet holds. A retried call —
   the same call again, token included — replays the recorded result instead of
   paying twice, even once the day's cap is full.
3. **Pin the payee when the job allows.** `MAZA_MCP_ALLOW_TO=<address>,…`:
   the limits bound how much may leave, and the agent can change those; this
   bounds where `send` may pay, and no tool changes it.
   Anything else is refused with `payee_not_allowed` before a preview token
   exists (with `MAZA_MCP_CONFIRM_SEND=1`; with it off, before the send is
   attempted). The list binds `send` only: `broadcast_tx` relays a
   transaction that was signed elsewhere, under its own switch (off by
   default), and does not consult it. Leave `MAZA_MCP_ALLOW_BROADCAST` off
   where the pin is the point; `get_status` warns when both are on.
4. **Start read-only.** Leave `MAZA_MCP_ALLOW_SEND` unset for the first days.
   The agent can read the chain, verify proofs and quote prices, and cannot
   spend at all. Turn sending on once the transcripts look right.
5. **Rehearse first.** The same guards run on the demo chain
   (`MAZA_MCP_MODE=demo`, see `examples/training.md`): train the agent where
   a mistake costs nothing. `MAZA_MCP_NETWORK=test` does the same against a
   real testnet node.
6. **Keep the state directory with the wallet.** It holds the caps ledger,
   the used-token set and the audit log (`0700`/`0600`). Losing it resets
   the day's counters and forgets which idempotency keys already paid, so
   back it up alongside `wallet.dat`. While the ledger is unreadable,
   `maza-mcp check` exits 1 and `send` is refused; a directory that
   rejects writes shows at the first send, which is refused with nothing
   sent (`state_unwritable`).

```json
{
  "mcpServers": {
    "maza": {
      "command": "maza-mcp",
      "args": ["serve"],
      "env": {
        "MAZA_MCP_MODE": "rpc",
        "MAZA_MCP_RPC_URL": "http://127.0.0.1:12832",
        "MAZA_MCP_RPC_USER": "…",
        "MAZA_MCP_RPC_PASSWORD": "…",
        "MAZA_MCP_ALLOW_SEND": "1",
        "MAZA_MCP_CONFIRM_SEND": "1",
        "MAZA_MCP_MAX_SEND_USD": "0.50",
        "MAZA_MCP_MAX_DAILY_USD": "2"
      }
    }
  }
}
```

### Spend limits, in dollars

Two limits bound what may leave: the most one payment may carry, and the
most one UTC day may carry. State them in dollars — `MAZA_MCP_MAX_SEND_USD`
and `MAZA_MCP_MAX_DAILY_USD`, on mainnet with the price feed on — or in
coins (`MAZA_MCP_MAX_SEND_MAZA` / `MAZA_MCP_MAX_DAILY_MAZA`, on any
network). One unit per server.

A dollar limit is converted each time a payment is checked, at the MAZA
price the server holds at that moment, rounded down to a satoshi: the coins
a limit allows are worth at most the dollars. The rounding may not take a
positive limit to nothing: a dollar limit that buys fewer than one satoshi
at the price held is refused `amount_below_one_satoshi`, never applied as a
cap of zero — read as "no cap" a zero would open the wallet, read as "no
spending" it would forbid the payments the operator meant to allow. That
refusal happens where a pair becomes caps, at the price in force, so a
stored pair can be refused at the next payment; `maza-mcp limit` therefore
says the pair is re-read, not that it is applied. With no price held, the
payment is refused `limits_unpriced` instead of being converted at a guess, and
the preview says the same. The same refusal answers while the price held is
marked stale — older than 15 minutes: an old price is shown, with its age,
and nothing is converted at it (neither the limits nor `convert`) until a
fresh one is held. Limits stated in MAZA coins are not converted, so an old
price changes nothing on such a server. A day limit below one payment is
refused: at start-up as a configuration error, through the agent as
`daily_below_payment`.

The limits are adjustable while the server runs — no ceiling — and each
change is one line in the audit log while it is on (the default;
`MAZA_MCP_AUDIT=0` turns it off):

- **Through the agent:** `set_spend_limits`, listed while a limit is in
  force. Its description tells the agent to change a limit when the person
  who runs the server asks and not on its own initiative.
- **From a shell:** `maza-mcp limit --state-dir DIR --send-usd 10 --daily-usd 30`.
  It needs neither the node nor the price feed; `maza-mcp limit --state-dir DIR`
  shows the pair in force. A server already running re-reads a change at its
  next payment (a dollar pair is converted there, at the price held then, and
  refused there if it buys no satoshi).
- **At start-up:** the environment, as above.

A change is written to `<state dir>/limits.json` (`0600`), is read again at
the next payment — converted there if it is stated in dollars, and refused
there if it buys no satoshi — and survives a restart. A stored file the
server does not trust (wider than owner-only, not a regular file, oversized,
unreadable or in the other unit) refuses payments — the code says which:
`limits_unsafe`, `limits_corrupt` or `limits_oversized` — until the pair is
written again, and `maza-mcp check` fails while that is so. `get_status`
reports the limits in force in both units, the price they were converted at,
what was spent today and what is left.

Because the agent can change the limits, a limit is a guard against a wrong
amount, not a wall against an agent that has been fooled. The wall is what
no tool changes: sending off by default, and the payee list
`MAZA_MCP_ALLOW_TO`.

## The tools

| Category | Tools |
|---|---|
| info | `get_status` |
| convert | `parse_amount`, `inspect_address`, `parse_uri`, `make_uri`, `convert`, `get_price` |
| read | `get_block`, `get_transaction`, `get_address` |
| search | `search` |
| proof | `get_merkle_proof` |
| verify | `verify_merkle_proof` |
| wallet | `get_wallet_info`, `get_new_address`, `list_transactions`, `list_utxos` |
| spend | `preview_send`, `send` |
| broadcast | `broadcast_tx` |
| admin | `node_diagnostics`, `wallet_backup`, `rpc_read` (allowlisted, opt-in) |
| limits | `set_spend_limits` (while a cap is in force) |
| demo | `demo_mine` (demo mode only) |

Tool results are a stable envelope: `{"ok", "tool", "simulated",
"sats_per_maza": 100000000, "result" | "error": {"code", "message",
"details"}, "text"}`. Error codes are names, not numbers (`insufficient_funds`,
`cap_exceeded`, `confirmation_required`, `not_found`, …) — an agent can branch
on them. Every tool result also carries `text`: a human-readable sentence, so
small models (or humans reading the transcript) don't have to parse JSON.

With `MAZA_MCP_CONFIRM_SEND=1`, `send` does not broadcast on the first ask.
The flow is:

1. `preview_send` with the `idempotency_key` the payment will carry →
   `{"allowed": true, "preview_token": "…", "blocking": []}`
   (or `allowed: false` and why: caps, payee pin, wrong network, …). The key is
   required here as it is in `send`: without it neither tool can say whether the
   payment would go through, so a preview without one is refused.
2. `send` with the same `idempotency_key` **and** the `preview_token` →
   the real transaction. Repeating `send` with the same key — with or without
   the token — is a replay that returns the same txid, not a second spend.
   The `preview_token` belongs to one payee, amount, key and note: a `send`
   that changes any of them is refused as `preview_mismatch` before the token
   is consumed, so it still works for the payment that was previewed.

What the step is, and what it is not: it binds a payment to the request that
was previewed, and it makes `send` a second, explicit call. It is **not** an
approval by a person: the same agent can call `preview_send` and then `send`
with the token it just received, so an agent that is willing and able to make
both calls pays what it asked to pay. A refusal here means the request changed
between the two calls (`preview_mismatch`), the key was already used, or a
guard said no — not that a human agreed. What a rogue or fooled agent runs
into is the switch (`MAZA_MCP_ALLOW_SEND=0`), the payee pin
(`MAZA_MCP_ALLOW_TO`) and the balance; see "Safety model" below.

`MAZA_MCP_CONFIRM_SEND` is `0` by default: the token step is then off, and the
first `send` that passes the address, network, payee and cap checks pays. The
`idempotency_key` is required either way. The
caps, the payee pin and the idempotency ledger apply either way, and so does
the audit log while it is on (`MAZA_MCP_AUDIT=1`, the default); `get_status`
warns `Spending is enabled WITHOUT a preview step`.

## Configuration (environment)

| Variable | Default | Meaning |
|---|---|---|
| `MAZA_MCP_MODE` | `auto` | `auto` / `rpc` / `indexer` / `demo`; `auto` serves whichever of node and indexer is configured |
| `MAZA_MCP_NETWORK` | `main` | `main` / `test` / `regtest` |
| `MAZA_MCP_INDEXER_URL` | *(empty)* | read-only chain data; no default endpoint is hardcoded — point it at your indexer. Required in `indexer` mode; read in `auto` mode when it is set |
| `MAZA_MCP_RPC_URL` | — | mazad JSON-RPC; required in `rpc` mode (e.g. `http://127.0.0.1:12832`). Or set `MAZA_MCP_RPC_PORT=<n>` for `http://127.0.0.1:<n>`. Read in `rpc` mode, and in `auto` mode when it is set |
| `MAZA_MCP_RPC_USER` / `_PASSWORD` / `_COOKIE` | — | node credentials: user and password, or the path to mazad's `.cookie` file |
| `MAZA_MCP_ALLOW_SEND` | `0` | spending needs an explicit yes |
| `MAZA_MCP_CONFIRM_SEND` | `0` | additionally require the preview token |
| `MAZA_MCP_MAX_SEND_MAZA` | — | per-transaction cap in coins (any network) |
| `MAZA_MCP_MAX_DAILY_MAZA` | — | per-UTC-day cap in coins. One unit per server |
| `MAZA_MCP_MAX_SEND_USD` | — | per-transaction cap in dollars (mainnet; needs the price feed) |
| `MAZA_MCP_MAX_DAILY_USD` | — | per-UTC-day cap in dollars, converted at the held MAZA price when a payment is checked, rounded down; a limit that buys no satoshi at that price is refused `amount_below_one_satoshi` |
| `MAZA_MCP_SPEND_FROM` | — | spend only the coins of these addresses (comma-separated) |
| `MAZA_MCP_CHANGE_ADDRESS` | — | fixed change address (needs `SPEND_FROM`); checked for validity and for network like the other two addresses |
| `MAZA_MCP_ALLOW_TO` | — | comma-separated payee allowlist for `send`: the limits bound how much may leave (and the agent can change those); this bounds where it may go, and no tool changes it. `broadcast_tx` does not consult it |
| `MAZA_MCP_ALLOW_BROADCAST` | `0` | raw-tx broadcast: relays a transaction signed elsewhere. Its outputs are capped like a payment, so both caps must be set; the payee list does not apply; there is no preview step |
| `MAZA_MCP_ALLOW_ADMIN` | `0` | diagnostics, backup, allowlisted RPC (rpc mode only) |
| `MAZA_MCP_STATE_DIR` | `~/.maza-mcp` | caps ledger, the stored limits and the audit log (`0700`) |
| `MAZA_MCP_AUDIT` | `1` | one JSON line per call, redacted; `0` turns it off |
| `MAZA_MCP_PRICE` | `1` | FreiExchange + CMC (mainnet only, fail-closed); a dollar limit is converted at it |
| `MAZA_MCP_EXPLORER_URL` | *(empty)* | your own explorer for links |
| `MAZA_MCP_DEBUG` | `0` | raise errors instead of encoding them |

Credentials come from the environment, in one of the two forms above. They
are not echoed: status, logs and errors carry redacted or hashed forms only.

## Safety model (the short version)

1. **Off by default.** No spend, no broadcast, no admin unless each is
   explicitly enabled. The price feed is the exception: it is on by
   default and lazy (nothing is fetched at start-up, and the chain and
   wallet tools never fetch a price); a price is fetched when one is
   asked for (`get_price`, `convert`) or a dollar limit is converted.
   `MAZA_MCP_PRICE=0` removes it: the server then makes no outbound
   price call, and a dollar limit is refused at start-up.
2. **Local limits, adjustable by design.** Limits are enforced in this
   process before any RPC leaves it, and the node has no say in them. The
   agent *can* change them: `set_spend_limits` exists for when you ask it
   to. So a limit guards against a wrong amount, not against a fooled
   agent. Against that: sending off (item 1), the payee list (item 3 of
   the mainnet checklist) and a wallet that holds little. The payee list
   constrains `send` only, so the same boundary is the broadcast switch left
   off: `MAZA_MCP_ALLOW_BROADCAST=1` relays a transaction signed elsewhere,
   whose outputs are capped like a payment but whose destination the list does
   not check. Set the pin and leave the relay off together, or the pin covers
   half the ways money can leave.
3. **Rehearsal mode.** Demo mode with `ALLOW_SEND=1` is a full dress rehearsal:
   same guards, same envelope, same audit log (on unless `MAZA_MCP_AUDIT=0`),
   fake chain. Train agents here.
4. **Proof you can check.** Indexer proofs are re-verified with local SHA256
   merkle math before they are called "verified".
5. **No address reuse pressure.** `get_new_address` returns a fresh address;
   `make_uri`/`parse_uri` are BIP-21-shaped so invoices survive being copy-pasted.
6. **What the server does NOT do.** No private-key export, no `dumpprivkey`,
   no arbitrary RPC passthrough (a read-only allowlist, refused before the node
   is asked), no wallet decryption, no silent network switching.
7. **Arguments are checked as whole strings.** An idempotency key, a file name
   or a path segment has to match its pattern in full: a value that merely
   starts like a valid one (with a newline on the end, say) is refused before
   any handler, wallet or node sees it.

See `docs/DESIGN.md` for the full design record and `docs/BENEFITS.md` for
what this unlocks.

## Tests

```sh
python -m unittest discover -s tests -p "test_*.py" -v     # offline; skips = the opt-in live tests (+ the SDK test below unless "mcp" is installed)
pip install mcp && python -m unittest tests.test_e2e_sdk_client -v   # optional: through the official SDK client

# live, opt-in:
MAZAMCP_TEST_MAZAD=/path/to/mazad python -m unittest tests.test_live_daemon -v
MAZAMCP_TEST_INDEXER=https://your-indexer.example/indexer \
    python -m unittest tests.test_live_indexer -v
```

Re-verified 2026-12-01 (UTC), after the tests were kept off the network:
offline suite OK (1004 tests, 4 skipped) in 50 modules, run three
ways (discovery from the repository root, all modules by dotted name in one
run from another directory, and each module alone from there). The 1002 is
the tree's own count, re-measured from the test modules on the final bytes;
the three commands under "Reproducing this" are what re-establish the run
itself, and they are re-run on the published bytes before a release.
Live, verified 2026-11-30: daemon OK
(3 tests) against a MazaCore 0.16.3 testnet node the test spawns itself
(spend path proven via the node's own `insufficient_funds` refusal on an
empty wallet, plus the local `cap_exceeded` guard); live indexer OK (1
test) against a real indexer with a locally re-verified merkle proof.
The same suite passes on CPython 3.10.22, the oldest version the package
declares (`requires-python = ">=3.10"`), and a wheel built from this tree
installs into a clean 3.12 environment where `maza-mcp selftest` passes.
`tests/fixtures/indexer/` holds anonymized live-shape captures; a shape test
fails if the indexer's contract drifts.

### Reproducing this

```sh
# 1. discovery, from the repository root
python -m unittest discover -s tests -p "test_*.py" -v

# 2. every module by dotted name, in one run, from another directory
cd /tmp && PYTHONPATH=/path/to/maza-mcp python -m unittest $(cd /path/to/maza-mcp && \
    ls tests/test_*.py | sed 's#/#.#g; s#\.py$##')

# 3. each module alone, from the same place (a module that only passes beside another fails here)
cd /tmp && for m in $(cd /path/to/maza-mcp && ls tests/test_*.py | sed 's#/#.#g; s#\.py$##'); do
  PYTHONPATH=/path/to/maza-mcp python -m unittest "$m" || echo "not OK: $m"; done

# 4. the tool surface, offline, on the demo chain
maza-mcp selftest

# 5. a wheel from this tree, installed and run from outside it
python -m venv /tmp/maza-venv && /tmp/maza-venv/bin/pip install . && \
  cd /tmp && /tmp/maza-venv/bin/python -m unittest discover -s /path/to/maza-mcp/tests -p "test_*.py"
```

The figures in this section were taken with those five commands on the tree
as committed.

### The one real payment, and the defects it found

One real mainnet payment has been made: 0.1 MAZA to a fresh address of the
same wallet, fee only. No second payment is authorised. Its transcript found
defects the offline tests had missed; each was fixed at the root after a test
went red, and `docs/VERIFICATION.md` records the list, the tests that lock
each one, and what is still unverified (the restricted spend path and the
dollar limits have moved no real money). The same document records the
mutation battery's history and the crash that was found outside a test.