@gvnrdao/dh-mcp 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +127 -19
  2. package/dist/boundary.js +84900 -0
  3. package/dist/index.js +85672 -1169
  4. package/package.json +33 -11
  5. package/.env.example +0 -22
  6. package/src/actions/index.ts +0 -13
  7. package/src/actions/loan-create.action.ts +0 -28
  8. package/src/actions/loan-extend.action.ts +0 -17
  9. package/src/actions/loan-mint.action.ts +0 -17
  10. package/src/actions/loan-pay.action.ts +0 -17
  11. package/src/actions/loan-withdraw.action.ts +0 -17
  12. package/src/actions/types.ts +0 -33
  13. package/src/config.ts +0 -43
  14. package/src/context.ts +0 -87
  15. package/src/index.ts +0 -23
  16. package/src/resources/position.ts +0 -39
  17. package/src/resources/protocol.ts +0 -57
  18. package/src/server.ts +0 -58
  19. package/src/signer/eoa-dh.ts +0 -35
  20. package/src/signer/index.ts +0 -12
  21. package/src/signer/safe-module-signer-adapter.ts +0 -61
  22. package/src/signer/safe-module.ts +0 -110
  23. package/src/signer/types.ts +0 -12
  24. package/src/tools/_helpers.ts +0 -47
  25. package/src/tools/btc.ts +0 -38
  26. package/src/tools/loan.ts +0 -197
  27. package/src/tools/position.ts +0 -93
  28. package/src/tools/protocol.ts +0 -35
  29. package/src/util/btc-address.ts +0 -58
  30. package/src/util/eip712-login.ts +0 -68
  31. package/src/util/errors.ts +0 -24
  32. package/src/util/extend-fee-bound.ts +0 -51
  33. package/src/util/format.ts +0 -9
  34. package/src/util/network.ts +0 -34
  35. package/src/util/server-fetch.ts +0 -27
  36. package/src/util/server-mint.ts +0 -108
  37. package/src/util/server-renew.ts +0 -107
  38. package/src/util/server-repay.ts +0 -118
  39. package/src/util/server-session.ts +0 -108
  40. package/src/util/server-withdraw.ts +0 -34
  41. package/src/util/tx-validator.ts +0 -231
  42. package/src/util/validate.ts +0 -41
  43. package/tsconfig.json +0 -20
  44. package/tsup.config.ts +0 -11
package/README.md CHANGED
@@ -1,7 +1,113 @@
1
1
  # @gvnrdao/dh-mcp
2
2
 
3
- MCP (Model Context Protocol) server for the Diamond Hands protocol. Exposes read and write tools
4
- for lending positions (create, mint, repay, extend, withdraw BTC) backed by the Diamond Hands SDK.
3
+ MCP (Model Context Protocol) server for the Diamond Hands protocol. It lets an AI agent —
4
+ through any MCP host (Claude, IDEs, agent runtimes) — operate Bitcoin-collateralized UCD
5
+ lending on a user's behalf, with a human approving every fund-moving step.
6
+
7
+ ## Install
8
+
9
+ Claude Code:
10
+
11
+ ```bash
12
+ claude mcp add diamond-hands -- npx -y @gvnrdao/dh-mcp
13
+ ```
14
+
15
+ Claude Desktop / Cursor / any MCP host (JSON config):
16
+
17
+ ```json
18
+ { "mcpServers": { "diamond-hands": { "command": "npx", "args": ["-y", "@gvnrdao/dh-mcp"] } } }
19
+ ```
20
+
21
+ That is the whole install. **Zero configuration boots the real protocol on SEPOLIA**
22
+ (the test network — real contracts, test funds) with public endpoints. Everything is
23
+ overridable by environment variables:
24
+
25
+ | Variable | Default | Meaning |
26
+ |---|---|---|
27
+ | `DH_ADAPTER` | `real` | `real` protocol, or `mock` (offline practice sandbox; needs `DH_MOCK_DB` + `DH_MOCK_LATENCY`) |
28
+ | `DH_NETWORK` | `sepolia` | `sepolia` or `mainnet` (mainnet is READ-ONLY: writes refuse pending security review) |
29
+ | `ETH_RPC_URL` | public node per network | your own RPC if preferred |
30
+ | `DH_SERVICE_ENDPOINT` | per network | Diamond Hands ops service |
31
+ | `DH_KEYSTORE_PATH` + `DH_KEYSTORE_PASSWORD` | unset | optional: encrypted keystore for automatic signing (both or neither) |
32
+
33
+ Signing is chosen **by conversation** (`signer_setup`): your own wallet (browser popup or
34
+ WalletConnect), a local encrypted keystore, or a Safe smart-account (single- or
35
+ multi-owner — co-signers join through one shareable ceremony link).
36
+
37
+ ## Security posture (what this package will never do)
38
+
39
+ - **No secrets ship, ever.** The package contains code only; every credential enters on
40
+ YOUR machine via env vars, a secure local unlock page, or your own wallet. Passwords
41
+ pasted into chat are refused by design.
42
+ - The approval bridge listens on **loopback only** (127.0.0.1, token-gated).
43
+ - Every fund-moving call requires your explicit confirmation AND a real signature; the
44
+ card you sign is decoded from the exact transaction — never re-formatted.
45
+ - **Mainnet writes are refused** in this build regardless of configuration.
46
+
47
+ ## What it solves
48
+
49
+ Diamond Hands is otherwise operated by hand. This server adds a conversational access
50
+ channel: "open me a 12-month position", "mint 500 UCD", "how is my loan doing?" — while
51
+ keeping every irreversible action behind explicit human confirmation.
52
+
53
+ For an agent to act safely on a live lending protocol, it must never act on guessed
54
+ configuration, stale state, or errors it cannot interpret. The server is therefore built
55
+ around a strict trust boundary (see below) whose contract is: never guess, never
56
+ misrepresent upstream state, always answer with legible, typed results.
57
+
58
+ ## What it supports
59
+
60
+ **Borrowing lifecycle** (write tools — human-gated, signer required):
61
+
62
+ | Tool | Meaning |
63
+ |---|---|
64
+ | `loan_create` | Open a position: BTC custody vault + PKP, returns the vault deposit address |
65
+ | `loan_mint` | Mint UCD against BTC collateral |
66
+ | `loan_pay` | Repay UCD debt (partial or full) |
67
+ | `loan_extend` | Renew the position term (pro-rata fee) |
68
+ | `loan_withdraw` | Withdraw BTC from the vault (unrecoverable — strictest gate) |
69
+ | `loan_recover_stale_spend` | Clear a stale BTC withdrawal reservation (permissionless) |
70
+
71
+ **Observability** (read tools + resources, no signer needed): position status, listings,
72
+ event history, pending withdrawals (`position_*`, `position://{id}`), protocol health and
73
+ terms (`protocol_*`, `protocol://config`, `protocol://terms`), BTC balance and oracle price
74
+ (`btc_*`), and `boundary://info` — which world the server is talking to (adapter, sources,
75
+ signer identity; never secrets).
76
+
77
+ ## Architecture: the protocol boundary
78
+
79
+ All protocol traffic passes through one module — the `ProtocolBoundary` port
80
+ (anticorruption layer). Tools and resources never touch protocol surfaces directly.
81
+
82
+ - Every call returns a result envelope: data + data-source identity + explicit discrepancy
83
+ warnings, or a typed error from a small closed taxonomy. Raw upstream text never reaches
84
+ the agent.
85
+ - Configuration has zero silent defaults: anything missing or invalid is a loud, actionable
86
+ `CONFIG_MISSING` error.
87
+ - If upstream sources disagree or lag, the boundary reports the divergence explicitly
88
+ instead of presenting one side as truth.
89
+ - A single switch (`DH_ADAPTER=mock|real`) selects the adapter behind the port.
90
+
91
+ The **mock adapter** is a full protocol simulator: complete position lifecycle
92
+ (deposit → active → repaid → closed), LTV-checked mints, fee math, event history — held in
93
+ memory, persisted to a local file DB (survives restarts), with deterministic operation
94
+ delays and injectable fault scenarios. It enables end-to-end agent development and testing
95
+ with zero funds at risk, offline. The **real adapter** (SDK + lit-ops-server + RPC) is a
96
+ separate, later delivery.
97
+
98
+ Full design — use cases, sequence diagrams, data contracts, scenario catalog:
99
+ [`docs/DESIGN.md`](docs/DESIGN.md).
100
+
101
+ ## Reliability status
102
+
103
+ - Behavior is pinned by scenario tests that run through the genuine MCP request path
104
+ (in-memory client/server pair), covering happy paths and failure classes alike
105
+ (missing config, uncollateralized operations, source divergence, upstream outages).
106
+ - Current stage: **mock adapter only** — deterministic and fully tested, but simulated.
107
+ Real-protocol wiring and its verification are the next delivery; the mock is then
108
+ reshaped against observed protocol behavior wherever the two disagree.
109
+ - Mainnet writes are disabled in this build pending a dedicated security review of key
110
+ custody and the host approval flow. Reads are unrestricted.
5
111
 
6
112
  ## Security model
7
113
 
@@ -9,31 +115,33 @@ These tools move real funds and create on-chain state. Treat them accordingly.
9
115
 
10
116
  ### Write tools require explicit human confirmation (audit H-5)
11
117
 
12
- The irreversible, fund-moving tools — `loan_create`, `loan_mint`, `loan_withdraw` — are gated by a
13
- `confirmed: true` parameter. They **refuse to run** until it is set, and it is meant to be set by a
14
- **human**, not by the model:
118
+ The irreversible, fund-moving tools — `loan_create`, `loan_mint`, `loan_pay`,
119
+ `loan_extend`, `loan_withdraw` — are gated by a `confirmed: true` parameter. They **refuse
120
+ to run** until it is set, and it is meant to be set by a **human**, not by the model:
15
121
 
16
- - The model should surface the exact operation (amounts, destination address, position) to the
17
- user and obtain explicit approval **before** re-invoking with `confirmed: true`.
18
- - These tools are annotated `destructiveHint: true`; compliant MCP hosts present an approval prompt.
19
- - In a fully headless context with no human in the loop, leave `confirmed` unset — the tool will
20
- refuse rather than move funds silently.
122
+ - The model should surface the exact operation (amounts, destination address, position) to
123
+ the user and obtain explicit approval **before** re-invoking with `confirmed: true`.
124
+ - These tools are annotated `destructiveHint: true`; compliant MCP hosts present an
125
+ approval prompt.
126
+ - In a fully headless context with no human in the loop, leave `confirmed` unset — the tool
127
+ will refuse rather than move funds silently.
21
128
 
22
- `loan_withdraw` sends BTC to an external address and is **unrecoverable**. The destination address
23
- is validated against the configured BTC network (`validateBtcAddress`) — a mismatched-network
24
- address is rejected before signing — but address correctness is still the human's responsibility.
129
+ `loan_withdraw` sends BTC to an external address and is **unrecoverable**. The destination
130
+ address is validated against the configured BTC network — a mismatched-network address is
131
+ rejected before signing — but address correctness is still the human's responsibility.
25
132
 
26
133
  ### Signer required for writes
27
134
 
28
- All write tools run through `getSignedCtx` (a signer must be configured). Writes are additionally
29
- blocked on mainnet via `requireNotMainnetForWrites`.
135
+ All write tools require a configured signer; without one they return a typed
136
+ `SIGNER_UNAVAILABLE` error. In mock mode the signed identity is `DH_MOCK_BORROWER`.
30
137
 
31
138
  ### Supply chain (audit H-6)
32
139
 
33
- `@gvnrdao/dh-sdk` is pinned to an exact version (not `latest`) so the dependency cannot drift
34
- silently between installs. Bump it deliberately, in lockstep with the CLI, and run `npm ci` in CI.
140
+ Dependencies are pinned to exact versions so nothing drifts silently between installs.
141
+ Bump deliberately, in lockstep with sibling packages, and run `npm ci` in CI.
35
142
 
36
143
  ### Not mainnet-ready without review
37
144
 
38
- This server has not been hardened for unattended mainnet operation. Do not point it at mainnet
39
- funds without a dedicated security review of the host's approval flow and key management.
145
+ This server has not been hardened for unattended mainnet operation. Do not point it at
146
+ mainnet funds without a dedicated security review of the host's approval flow and key
147
+ management; mainnet writes are refused until that review lifts the block.