mindmeld-mcp 0.4.2 → 0.7.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.
- package/README.md +25 -2
- package/dist/answerer.js +52 -10
- package/dist/auth/config.js +7 -0
- package/dist/auth/github.js +60 -0
- package/dist/auth/session.js +101 -0
- package/dist/channel.js +117 -0
- package/dist/cli.js +186 -11
- package/dist/config.js +55 -3
- package/dist/crypto.js +24 -8
- package/dist/daemon.js +182 -83
- package/dist/identity.js +58 -0
- package/dist/mcp.js +116 -32
- package/dist/mcpteam.js +317 -0
- package/dist/protocol.js +26 -5
- package/dist/relay.js +82 -11
- package/dist/teamapi.js +234 -0
- package/dist/teamchannel.js +486 -0
- package/dist/teamcrypto.js +130 -0
- package/dist/teamkeys.js +245 -0
- package/dist/version.js +13 -0
- package/package.json +3 -2
package/dist/teamkeys.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
3
|
+
import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
|
|
4
|
+
import { assertSessionRelay } from "./auth/session.js";
|
|
5
|
+
import { httpBase } from "./config.js";
|
|
6
|
+
import { openFromDevice, sealToDevice } from "./teamcrypto.js";
|
|
7
|
+
const HEX16 = /^[0-9a-f]{16}$/;
|
|
8
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
9
|
+
const HEX128 = /^[0-9a-f]{128}$/;
|
|
10
|
+
/** Strict Ed25519 (zip215: false) check of `sig` by `signPub` over utf8(message). */
|
|
11
|
+
function verifySig(signPub, message, sig) {
|
|
12
|
+
try {
|
|
13
|
+
if (!HEX128.test(sig))
|
|
14
|
+
return false;
|
|
15
|
+
return ed25519.verify(hexToBytes(sig), new TextEncoder().encode(message), hexToBytes(signPub), { zip215: false });
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Every device in the roster, keyed by deviceId, with its owner's uid, login and role. */
|
|
22
|
+
export function rosterDevices(roster) {
|
|
23
|
+
const out = new Map();
|
|
24
|
+
for (const m of roster.members)
|
|
25
|
+
for (const d of m.devices)
|
|
26
|
+
out.set(d.deviceId, { uid: m.uid, login: m.login, signPub: d.signPub, boxPub: d.boxPub, role: m.role });
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
/** Why `w` (the wrap for epoch `epoch`) must be dropped, or the unwrapped key if it is sound. */
|
|
30
|
+
function checkWrap(teamId, id, devices, epoch, w) {
|
|
31
|
+
if (!w || typeof w.sealed !== "string" || typeof w.by !== "string" || typeof w.sig !== "string")
|
|
32
|
+
return "malformed wrap";
|
|
33
|
+
const writer = HEX16.test(w.by) ? devices.get(w.by) : undefined;
|
|
34
|
+
if (!writer)
|
|
35
|
+
return "wrap written by a device not in the team";
|
|
36
|
+
if (!verifySig(writer.signPub, `mm-wrap|v1|${teamId}|${epoch}|${id.deviceId}|${w.sealed}`, w.sig))
|
|
37
|
+
return "bad wrap signature";
|
|
38
|
+
const c = w.commit;
|
|
39
|
+
if (!c || typeof c.hash !== "string" || typeof c.by !== "string" || typeof c.sig !== "string" || !HEX64.test(c.hash))
|
|
40
|
+
return "no epoch commit";
|
|
41
|
+
// Any current roster device: the relay only accepts a rotation from an admin, and checking the role
|
|
42
|
+
// now would break every epoch committed by an admin who has since been demoted.
|
|
43
|
+
const committer = HEX16.test(c.by) ? devices.get(c.by) : undefined;
|
|
44
|
+
if (!committer)
|
|
45
|
+
return "commit signed by a device not in the team";
|
|
46
|
+
if (!verifySig(committer.signPub, `mm-epoch|v1|${teamId}|${epoch}|${c.hash}`, c.sig))
|
|
47
|
+
return "bad commit signature";
|
|
48
|
+
const key = openFromDevice(id, w.sealed);
|
|
49
|
+
if (!key || key.length !== 32)
|
|
50
|
+
return "cannot unwrap the key for this device";
|
|
51
|
+
if (createHash("sha256").update(key).digest("hex") !== c.hash)
|
|
52
|
+
return "key does not match the commit hash";
|
|
53
|
+
return key;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Verify the wrapped keys from GET /v1/teams/:id/keys against `roster`. For each epoch: the wrap's
|
|
57
|
+
* signature (utf8 "mm-wrap|v1|teamId|epoch|myDeviceId|sealed") must be by its writer `by`, a current
|
|
58
|
+
* member device; the commit signature (utf8 "mm-epoch|v1|teamId|epoch|hash") must be by a current
|
|
59
|
+
* roster device (any role; the relay enforces admin-only rotation when it is written); the key must unwrap with this device's box key and hash (hex sha256) to commit.hash.
|
|
60
|
+
* Epochs failing any check are dropped and listed in `problems`. Never throws.
|
|
61
|
+
*/
|
|
62
|
+
export function verifyTeamKeys(teamId, id, roster, body) {
|
|
63
|
+
const devices = rosterDevices(roster);
|
|
64
|
+
const keys = new Map();
|
|
65
|
+
const problems = [];
|
|
66
|
+
const entries = body && typeof body.epochs === "object" && body.epochs ? Object.entries(body.epochs) : [];
|
|
67
|
+
for (const [k, w] of entries.sort(([a], [b]) => Number(a) - Number(b))) {
|
|
68
|
+
const epoch = Number(k);
|
|
69
|
+
if (!Number.isSafeInteger(epoch) || epoch < 1)
|
|
70
|
+
continue;
|
|
71
|
+
const r = checkWrap(teamId, id, devices, epoch, w);
|
|
72
|
+
if (typeof r === "string")
|
|
73
|
+
problems.push(`team key for epoch ${epoch} failed verification (${r})`);
|
|
74
|
+
else
|
|
75
|
+
keys.set(epoch, r);
|
|
76
|
+
}
|
|
77
|
+
return { epoch: typeof body?.epoch === "number" ? body.epoch : 0, keys, problems };
|
|
78
|
+
}
|
|
79
|
+
export { httpBase };
|
|
80
|
+
/** GET `path` on the relay as `session` (only the relay that issued the session). */
|
|
81
|
+
async function getJson(relayHttp, path, session) {
|
|
82
|
+
assertSessionRelay(relayHttp, session);
|
|
83
|
+
const res = await fetch(`${httpBase(relayHttp)}${path}`, { headers: { authorization: `Bearer ${session.token}` } });
|
|
84
|
+
let body = null;
|
|
85
|
+
try {
|
|
86
|
+
body = await res.json();
|
|
87
|
+
}
|
|
88
|
+
catch { }
|
|
89
|
+
if (!res.ok) {
|
|
90
|
+
const msg = body && typeof body.error === "string" ? body.error : `HTTP ${res.status}`;
|
|
91
|
+
throw Object.assign(new Error(`relay: ${msg}`), { status: res.status });
|
|
92
|
+
}
|
|
93
|
+
return body;
|
|
94
|
+
}
|
|
95
|
+
/** GET /v1/teams/:id: the team roster (members only). Throws on an HTTP error (`status` on the error). */
|
|
96
|
+
export async function fetchRoster(relayHttp, teamId, session) {
|
|
97
|
+
const r = (await getJson(relayHttp, `/v1/teams/${teamId}`, session));
|
|
98
|
+
if (!r || !Array.isArray(r.members) || typeof r.epoch !== "number")
|
|
99
|
+
throw new Error("relay: unexpected roster response");
|
|
100
|
+
return r;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* GET /v1/teams/:id/keys for this device, verified against `roster` (see `verifyTeamKeys`).
|
|
104
|
+
* Throws on an HTTP error; verification failures are reported in `problems`, not thrown.
|
|
105
|
+
*/
|
|
106
|
+
export async function fetchTeamKeys(relayHttp, teamId, session, id, roster) {
|
|
107
|
+
const body = (await getJson(relayHttp, `/v1/teams/${teamId}/keys`, session));
|
|
108
|
+
return verifyTeamKeys(teamId, id, roster, body);
|
|
109
|
+
}
|
|
110
|
+
/** Status text for a device that is in the team but has not been given the current team key yet. */
|
|
111
|
+
export const WAITING_FOR_KEY = "waiting for a teammate to come online to give this device the team key";
|
|
112
|
+
/** A fresh random 32-byte team key. */
|
|
113
|
+
export const newTeamKey = () => new Uint8Array(randomBytes(32));
|
|
114
|
+
const signUtf8 = (signer, msg) => bytesToHex(ed25519.sign(new TextEncoder().encode(msg), hexToBytes(signer.signPriv)));
|
|
115
|
+
/** Ed25519 signature `signer` puts on a wrap: utf8("mm-wrap|v1|" + teamId + "|" + epoch + "|" + recipient + "|" + sealed). */
|
|
116
|
+
export const wrapSig = (signer, teamId, epoch, recipientId, sealed) => signUtf8(signer, `mm-wrap|v1|${teamId}|${epoch}|${recipientId}|${sealed}`);
|
|
117
|
+
/** Seal `teamKey` to `to` and sign the wrap as `signer`. */
|
|
118
|
+
export function signedWrap(signer, teamId, epoch, to, teamKey) {
|
|
119
|
+
const sealed = sealToDevice(to.boxPub, teamKey);
|
|
120
|
+
return { sealed, sig: wrapSig(signer, teamId, epoch, to.deviceId, sealed) };
|
|
121
|
+
}
|
|
122
|
+
/** An epoch commitment: hash = hex sha256(teamKey), sig by `signer` over utf8("mm-epoch|v1|" + teamId + "|" + epoch + "|" + hash). */
|
|
123
|
+
export function epochCommit(signer, teamId, epoch, teamKey) {
|
|
124
|
+
const hash = createHash("sha256").update(teamKey).digest("hex");
|
|
125
|
+
return { hash, sig: signUtf8(signer, `mm-epoch|v1|${teamId}|${epoch}|${hash}`) };
|
|
126
|
+
}
|
|
127
|
+
/** PUT /v1/teams/:id/keys. Throws on an HTTP error (`status` on the error). */
|
|
128
|
+
async function putKeys(o, body) {
|
|
129
|
+
assertSessionRelay(o.relayHttp, o.session);
|
|
130
|
+
const res = await fetch(`${httpBase(o.relayHttp)}/v1/teams/${o.teamId}/keys`, {
|
|
131
|
+
method: "PUT",
|
|
132
|
+
headers: { authorization: `Bearer ${o.session.token}`, "content-type": "application/json" },
|
|
133
|
+
body: JSON.stringify(body),
|
|
134
|
+
});
|
|
135
|
+
let json = null;
|
|
136
|
+
try {
|
|
137
|
+
json = await res.json();
|
|
138
|
+
}
|
|
139
|
+
catch { }
|
|
140
|
+
if (!res.ok) {
|
|
141
|
+
const msg = json && typeof json.error === "string" ? json.error : `HTTP ${res.status}`;
|
|
142
|
+
throw Object.assign(new Error(`relay: ${msg}`), { status: res.status });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const statusOf = (e) => e?.status;
|
|
146
|
+
const roleOf = (roster, uid) => roster.members.find((m) => m.uid === uid)?.role;
|
|
147
|
+
const allDevices = (roster) => roster.members.flatMap((m) => m.devices);
|
|
148
|
+
/** Seal `key` to each of `to`, signed by this device, as a PUT /keys `wrapped` map. */
|
|
149
|
+
const wrapsFor = (o, epoch, key, to) => Object.fromEntries(to.map((d) => [d.deviceId, signedWrap(o.id, o.teamId, epoch, d, key)]));
|
|
150
|
+
/**
|
|
151
|
+
* One key-distribution pass over an already fetched `roster` and this device's `verified` keys:
|
|
152
|
+
* - epoch 0 (no key yet) and the caller is an admin: create the first key (epoch 1, with commit) for every device;
|
|
153
|
+
* - this device holds the verified current key: seal it to every roster device with `hasKey: false`
|
|
154
|
+
* (a 409 means another member got there first, which is fine);
|
|
155
|
+
* - otherwise this device is waiting for a teammate to deliver its key.
|
|
156
|
+
* `wrote` is true when something was PUT (the caller should refetch). Throws on relay errors other
|
|
157
|
+
* than a delivery 409, including a 400/409 when creating epoch 1 races another device.
|
|
158
|
+
* Never logs key material.
|
|
159
|
+
*/
|
|
160
|
+
export async function distributeKeys(o, roster, verified) {
|
|
161
|
+
const waiting = { waiting: WAITING_FOR_KEY };
|
|
162
|
+
if (roster.epoch === 0 && verified.epoch === 0) {
|
|
163
|
+
if (roleOf(roster, o.session.uid) !== "admin")
|
|
164
|
+
return { result: waiting, wrote: false };
|
|
165
|
+
const key = newTeamKey();
|
|
166
|
+
await putKeys(o, { epoch: 1, wrapped: wrapsFor(o, 1, key, allDevices(roster)), commit: epochCommit(o.id, o.teamId, 1, key) });
|
|
167
|
+
return { result: "ok", wrote: true };
|
|
168
|
+
}
|
|
169
|
+
// Keys fetched across a rotation: nothing to deliver from this snapshot; refetch.
|
|
170
|
+
if (verified.epoch !== roster.epoch)
|
|
171
|
+
return { result: verified.keys.has(verified.epoch) ? "ok" : waiting, wrote: true };
|
|
172
|
+
const key = verified.keys.get(roster.epoch);
|
|
173
|
+
if (!key)
|
|
174
|
+
return { result: waiting, wrote: false };
|
|
175
|
+
const pending = allDevices(roster).filter((d) => d.hasKey === false && d.deviceId !== o.id.deviceId);
|
|
176
|
+
if (pending.length === 0)
|
|
177
|
+
return { result: "ok", wrote: false };
|
|
178
|
+
try {
|
|
179
|
+
await putKeys(o, { epoch: roster.epoch, wrapped: wrapsFor(o, roster.epoch, key, pending) });
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
// Another member already delivered (or the team rotated meanwhile): the device is covered.
|
|
183
|
+
if (statusOf(e) !== 409)
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
return { result: "ok", wrote: true };
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Make sure the team's current key exists and has reached every device this device can help:
|
|
190
|
+
* creates epoch 1 when the team has no key and the caller is an admin, and seals the current key to
|
|
191
|
+
* roster devices that lack it. Returns `{ waiting }` (WAITING_FOR_KEY) when this device has no
|
|
192
|
+
* usable current key. A 400/409 from a race with another device is retried once after a refetch.
|
|
193
|
+
*/
|
|
194
|
+
export async function ensureTeamKeys(opts) {
|
|
195
|
+
for (let attempt = 0;; attempt++) {
|
|
196
|
+
const roster = await fetchRoster(opts.relayHttp, opts.teamId, opts.session);
|
|
197
|
+
const verified = await fetchTeamKeys(opts.relayHttp, opts.teamId, opts.session, opts.id, roster);
|
|
198
|
+
try {
|
|
199
|
+
return (await distributeKeys(opts, roster, verified)).result;
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
const s = statusOf(e);
|
|
203
|
+
if (attempt === 0 && (s === 400 || s === 409))
|
|
204
|
+
continue;
|
|
205
|
+
throw e;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Rotate the team key (admin only), e.g. right after removing a member: a fresh 32-byte key sealed
|
|
211
|
+
* to every current roster device (this one included), PUT with a signed commit at epoch + 1.
|
|
212
|
+
* Returns the new epoch. If another rotation landed first (relay 400/409 and the refetched roster is
|
|
213
|
+
* already at that epoch or later), an error naming the team's epoch now is thrown (`status` 409,
|
|
214
|
+
* `epoch`), without retrying. If a device joined between the roster read and the PUT (relay 400
|
|
215
|
+
* "rotation must cover every current device"), the rotation is redone once over a fresh roster.
|
|
216
|
+
*/
|
|
217
|
+
export async function rotate(opts) {
|
|
218
|
+
for (let attempt = 0;; attempt++) {
|
|
219
|
+
const roster = await fetchRoster(opts.relayHttp, opts.teamId, opts.session);
|
|
220
|
+
if (roleOf(roster, opts.session.uid) !== "admin")
|
|
221
|
+
throw Object.assign(new Error("only admins rotate the team key"), { status: 403 });
|
|
222
|
+
const epoch = roster.epoch + 1;
|
|
223
|
+
const key = newTeamKey();
|
|
224
|
+
try {
|
|
225
|
+
await putKeys(opts, { epoch, wrapped: wrapsFor(opts, epoch, key, allDevices(roster)), commit: epochCommit(opts.id, opts.teamId, epoch, key) });
|
|
226
|
+
return epoch;
|
|
227
|
+
}
|
|
228
|
+
catch (e) {
|
|
229
|
+
// A rotation that landed first shows up as 400 ("commit is only for a rotation", same epoch) or 409.
|
|
230
|
+
const s = statusOf(e);
|
|
231
|
+
if (s !== 400 && s !== 409)
|
|
232
|
+
throw e;
|
|
233
|
+
const now = (await fetchRoster(opts.relayHttp, opts.teamId, opts.session)).epoch;
|
|
234
|
+
if (now >= epoch)
|
|
235
|
+
throw Object.assign(new Error(`team key was rotated concurrently; the team is now at epoch ${now}`), { status: 409, epoch: now });
|
|
236
|
+
// A device joined between the roster read and the PUT: rotate once more over the new roster.
|
|
237
|
+
if (s === 400 && /cover every current device/.test(e.message)) {
|
|
238
|
+
if (attempt === 0)
|
|
239
|
+
continue;
|
|
240
|
+
throw Object.assign(new Error("team key rotation failed: the team's devices kept changing; try again"), { status: 400 });
|
|
241
|
+
}
|
|
242
|
+
throw e;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** This client's version. A test keeps it equal to package.json. */
|
|
2
|
+
export const VERSION = "0.7.0";
|
|
3
|
+
/** Compares dotted numeric versions ("0.4.2" vs "0.5.0"): negative, zero or positive. */
|
|
4
|
+
export function compareVersions(a, b) {
|
|
5
|
+
const pa = a.split(/[.-]/).map((n) => parseInt(n, 10) || 0);
|
|
6
|
+
const pb = b.split(/[.-]/).map((n) => parseInt(n, 10) || 0);
|
|
7
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
8
|
+
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
9
|
+
if (d !== 0)
|
|
10
|
+
return d;
|
|
11
|
+
}
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mindmeld-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "mindmeld: let your Claude Code session ask a teammate's Claude Code session questions. E2E encrypted, read-only answers, approved in Claude.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,8 @@
|
|
|
34
34
|
"@types/ws": "^8.18.2",
|
|
35
35
|
"esbuild": "^0.28.2",
|
|
36
36
|
"typescript": "^7.0.2",
|
|
37
|
-
"vitest": "^4.1.12"
|
|
37
|
+
"vitest": "^4.1.12",
|
|
38
|
+
"wrangler": "^4.153.0"
|
|
38
39
|
},
|
|
39
40
|
"keywords": [
|
|
40
41
|
"mindmeld",
|