@mosadd/mcp 3.0.0-alpha.51 → 3.0.0-alpha.53
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/CHANGELOG.md +334 -309
- package/README.md +150 -150
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-BTAIZKYT.js → chunk-AWKITPZQ.js} +407 -106
- package/dist/index.d.ts +11 -1
- package/dist/index.js +1 -1
- package/package.json +9 -9
- package/src/__tests__/blob-upload.test.ts +93 -0
- package/src/__tests__/golden-path.test.ts +139 -139
- package/src/__tests__/integration.test.ts +182 -182
- package/src/__tests__/knowledge-ingest.test.ts +114 -114
- package/src/__tests__/mdm-e2ee.test.ts +212 -212
- package/src/__tests__/mdm-mcp-readable.test.ts +284 -0
- package/src/__tests__/mirc-invite.test.ts +112 -112
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/server.test.ts +71 -71
- package/src/__tests__/serverJsonVersion.test.ts +45 -45
- package/src/__tests__/tool-count-consistency.test.ts +199 -199
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -145
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +798 -502
- package/src/index.ts +19 -19
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +52 -6
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +174 -122
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +231 -231
- package/src/server.ts +186 -162
- package/src/tools/__tests__/ragDeleteFoldsSourceId.test.ts +67 -67
- package/src/tools/actions.ts +166 -166
- package/src/tools/attachments.ts +276 -276
- package/src/tools/capabilities.ts +70 -70
- package/src/tools/embed.ts +110 -110
- package/src/tools/index.ts +181 -181
- package/src/tools/knowledge-graph.ts +186 -186
- package/src/tools/knowledge.ts +344 -344
- package/src/tools/mail-agentbox.ts +155 -155
- package/src/tools/mail-aliases.ts +25 -25
- package/src/tools/mail-provenance.ts +137 -137
- package/src/tools/mail.ts +403 -403
- package/src/tools/mcall.ts +216 -216
- package/src/tools/mdm-as-agent.ts +126 -126
- package/src/tools/mdm-voice.ts +220 -220
- package/src/tools/mdm.ts +119 -17
- package/src/tools/mirc-edge.ts +196 -196
- package/src/tools/mirc-members.ts +287 -287
- package/src/tools/mirc.ts +308 -308
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +131 -131
- package/src/tools/murl.ts +314 -314
- package/src/tools/presence.ts +442 -442
- package/src/tools/ptt-ingest.ts +99 -99
- package/src/tools/threat.ts +123 -123
- package/src/types.ts +121 -121
- package/src/version.ts +8 -8
- package/src/voice/floor.ts +132 -132
- package/LICENSE +0 -201
|
@@ -148,6 +148,8 @@ function formatVoiceIfAny(text) {
|
|
|
148
148
|
// src/crypto/mdm-session.ts
|
|
149
149
|
import {
|
|
150
150
|
decryptBytes,
|
|
151
|
+
deriveHkdfKey,
|
|
152
|
+
deriveSharedSecret,
|
|
151
153
|
encryptBytes,
|
|
152
154
|
generateEd25519KeyPair,
|
|
153
155
|
generateX25519KeyPair,
|
|
@@ -211,6 +213,196 @@ var InMemoryMdmKeyStore = class {
|
|
|
211
213
|
function randomId() {
|
|
212
214
|
return crypto.getRandomValues(new Uint32Array(1))[0];
|
|
213
215
|
}
|
|
216
|
+
var X25519_BASE_POINT = (() => {
|
|
217
|
+
const b = new Uint8Array(32);
|
|
218
|
+
b[0] = 9;
|
|
219
|
+
return b;
|
|
220
|
+
})();
|
|
221
|
+
async function x25519PairFromSeed(seed) {
|
|
222
|
+
const privateKey = seed.length === 32 ? seed : seed.slice(0, 32);
|
|
223
|
+
const publicKey = await deriveSharedSecret(privateKey, X25519_BASE_POINT);
|
|
224
|
+
return { privateKey, publicKey };
|
|
225
|
+
}
|
|
226
|
+
var utf8 = new TextEncoder();
|
|
227
|
+
async function subSeed(seed, label) {
|
|
228
|
+
return deriveHkdfKey(seed, { info: utf8.encode(label), length: 32 });
|
|
229
|
+
}
|
|
230
|
+
async function deriveOwnMaterialFromSeed(seed) {
|
|
231
|
+
const [idSeed, signSeed, spkSeed] = await Promise.all([
|
|
232
|
+
subSeed(seed, "mosadd-mdm-identity-x25519"),
|
|
233
|
+
subSeed(seed, "mosadd-mdm-signing-ed25519"),
|
|
234
|
+
subSeed(seed, "mosadd-mdm-signed-prekey-x25519")
|
|
235
|
+
]);
|
|
236
|
+
return {
|
|
237
|
+
identity: await x25519PairFromSeed(idSeed),
|
|
238
|
+
signingIdentity: await generateEd25519KeyPair(signSeed),
|
|
239
|
+
signedPrekey: { id: 1, pair: await x25519PairFromSeed(spkSeed) },
|
|
240
|
+
oneTimePrekeys: []
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async function deriveMdmSeedFromSecret(secret) {
|
|
244
|
+
return deriveHkdfKey(utf8.encode(secret), {
|
|
245
|
+
salt: utf8.encode("mosadd-mcp-api-key"),
|
|
246
|
+
info: utf8.encode("mosadd-mdm-identity-seed"),
|
|
247
|
+
length: 32
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
var SeededMdmKeyStore = class _SeededMdmKeyStore {
|
|
251
|
+
constructor(seed) {
|
|
252
|
+
this.seed = seed;
|
|
253
|
+
if (!seed || seed.length < 32) throw new Error("SeededMdmKeyStore needs a 32-byte seed");
|
|
254
|
+
}
|
|
255
|
+
seed;
|
|
256
|
+
material = null;
|
|
257
|
+
sessions = /* @__PURE__ */ new Map();
|
|
258
|
+
sentMessages = /* @__PURE__ */ new Map();
|
|
259
|
+
static async fromSecret(secret) {
|
|
260
|
+
return new _SeededMdmKeyStore(await deriveMdmSeedFromSecret(secret));
|
|
261
|
+
}
|
|
262
|
+
getOwnMaterial() {
|
|
263
|
+
if (!this.material) this.material = deriveOwnMaterialFromSeed(this.seed);
|
|
264
|
+
return this.material;
|
|
265
|
+
}
|
|
266
|
+
async takeOneTimePrekey(_id) {
|
|
267
|
+
return void 0;
|
|
268
|
+
}
|
|
269
|
+
async getSession(peerId) {
|
|
270
|
+
return this.sessions.get(peerId);
|
|
271
|
+
}
|
|
272
|
+
async putSession(peerId, record) {
|
|
273
|
+
this.sessions.set(peerId, record);
|
|
274
|
+
}
|
|
275
|
+
async putSentMessage(messageId, plaintext) {
|
|
276
|
+
this.sentMessages.set(messageId, plaintext);
|
|
277
|
+
}
|
|
278
|
+
async getSentMessage(messageId) {
|
|
279
|
+
return this.sentMessages.get(messageId);
|
|
280
|
+
}
|
|
281
|
+
/** Same label as the web's getSelfKeyBytes ("mosadd-mdm-self-read"). */
|
|
282
|
+
async getSelfKeyBytes() {
|
|
283
|
+
return deriveHkdfKey(this.seed, { info: utf8.encode("mosadd-mdm-self-read"), length: 32 });
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
var SecretMdmKeyStore = class {
|
|
287
|
+
constructor(secret) {
|
|
288
|
+
this.secret = secret;
|
|
289
|
+
}
|
|
290
|
+
secret;
|
|
291
|
+
inner = null;
|
|
292
|
+
store() {
|
|
293
|
+
if (!this.inner) this.inner = SeededMdmKeyStore.fromSecret(this.secret);
|
|
294
|
+
return this.inner;
|
|
295
|
+
}
|
|
296
|
+
async getOwnMaterial() {
|
|
297
|
+
return (await this.store()).getOwnMaterial();
|
|
298
|
+
}
|
|
299
|
+
async takeOneTimePrekey(id) {
|
|
300
|
+
return (await this.store()).takeOneTimePrekey(id);
|
|
301
|
+
}
|
|
302
|
+
async getSession(peerId) {
|
|
303
|
+
return (await this.store()).getSession(peerId);
|
|
304
|
+
}
|
|
305
|
+
async putSession(peerId, record) {
|
|
306
|
+
return (await this.store()).putSession(peerId, record);
|
|
307
|
+
}
|
|
308
|
+
async putSentMessage(messageId, plaintext) {
|
|
309
|
+
return (await this.store()).putSentMessage(messageId, plaintext);
|
|
310
|
+
}
|
|
311
|
+
async getSentMessage(messageId) {
|
|
312
|
+
return (await this.store()).getSentMessage(messageId);
|
|
313
|
+
}
|
|
314
|
+
async getSelfKeyBytes() {
|
|
315
|
+
return (await this.store()).getSelfKeyBytes();
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
var SELF_TAG = "s1.";
|
|
319
|
+
var PEER_RECOVERABLE_TAG = "r1.";
|
|
320
|
+
var PEER_RECOVERABLE_INFO = "mosadd.mdm.recipient-recoverable.v1";
|
|
321
|
+
async function aesGcmSeal(tag, keyBytes, plaintext) {
|
|
322
|
+
const key = await crypto.subtle.importKey("raw", keyBytes, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
323
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
324
|
+
const ct = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, plaintext));
|
|
325
|
+
const packed = new Uint8Array(iv.length + ct.length);
|
|
326
|
+
packed.set(iv, 0);
|
|
327
|
+
packed.set(ct, iv.length);
|
|
328
|
+
return tag + toBase64(packed);
|
|
329
|
+
}
|
|
330
|
+
async function aesGcmOpen(tag, keyBytes, sealed) {
|
|
331
|
+
if (!sealed.startsWith(tag)) return null;
|
|
332
|
+
const packed = fromBase64(sealed.slice(tag.length));
|
|
333
|
+
if (packed.length <= 12) return null;
|
|
334
|
+
const key = await crypto.subtle.importKey("raw", keyBytes, { name: "AES-GCM" }, false, ["decrypt"]);
|
|
335
|
+
const pt = await crypto.subtle.decrypt({ name: "AES-GCM", iv: packed.slice(0, 12) }, key, packed.slice(12));
|
|
336
|
+
return new Uint8Array(pt);
|
|
337
|
+
}
|
|
338
|
+
async function sealForSelf(keystore, plaintext) {
|
|
339
|
+
try {
|
|
340
|
+
const keyBytes = await keystore.getSelfKeyBytes?.() ?? null;
|
|
341
|
+
if (!keyBytes) return null;
|
|
342
|
+
return await aesGcmSeal(SELF_TAG, keyBytes, plaintext);
|
|
343
|
+
} catch {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
async function openSelfSealed(keystore, sealed) {
|
|
348
|
+
try {
|
|
349
|
+
if (!sealed) return null;
|
|
350
|
+
const keyBytes = await keystore.getSelfKeyBytes?.() ?? null;
|
|
351
|
+
if (!keyBytes) return null;
|
|
352
|
+
return await aesGcmOpen(SELF_TAG, keyBytes, sealed);
|
|
353
|
+
} catch {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function pairKey(keystore, otherIdentityPublicKey) {
|
|
358
|
+
const own = await keystore.getOwnMaterial();
|
|
359
|
+
const secret = await deriveSharedSecret(own.identity.privateKey, otherIdentityPublicKey);
|
|
360
|
+
return deriveHkdfKey(secret, { info: utf8.encode(PEER_RECOVERABLE_INFO), length: 32 });
|
|
361
|
+
}
|
|
362
|
+
async function sealForPeerRecoverable(keystore, peer, plaintext) {
|
|
363
|
+
try {
|
|
364
|
+
return await aesGcmSeal(PEER_RECOVERABLE_TAG, await pairKey(keystore, peer.identityPublicKey), plaintext);
|
|
365
|
+
} catch {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async function openPeerRecoverable(keystore, sender, sealed) {
|
|
370
|
+
try {
|
|
371
|
+
if (!sealed) return null;
|
|
372
|
+
return await aesGcmOpen(PEER_RECOVERABLE_TAG, await pairKey(keystore, sender.identityPublicKey), sealed);
|
|
373
|
+
} catch {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async function fetchVerifiedBundle(dm, peerId) {
|
|
378
|
+
const raw = await dm.fetchPrekeyBundle(peerId);
|
|
379
|
+
if (!raw) return null;
|
|
380
|
+
const parsed = parsePublicBundle(raw);
|
|
381
|
+
return verifyPublicBundle(parsed) ? parsed : null;
|
|
382
|
+
}
|
|
383
|
+
function sameBytes(a, b) {
|
|
384
|
+
if (a.length !== b.length) return false;
|
|
385
|
+
for (let i = 0; i < a.length; i += 1) if (a[i] !== b[i]) return false;
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
async function ensureOwnBundlePublished(keystore, dm, selfId, opts) {
|
|
389
|
+
const own = await keystore.getOwnMaterial();
|
|
390
|
+
const raw = await dm.fetchPrekeyBundle(selfId);
|
|
391
|
+
if (raw) {
|
|
392
|
+
let published = null;
|
|
393
|
+
try {
|
|
394
|
+
published = parsePublicBundle(raw);
|
|
395
|
+
} catch {
|
|
396
|
+
published = null;
|
|
397
|
+
}
|
|
398
|
+
if (published && sameBytes(published.identityPublicKey, own.identity.publicKey) && sameBytes(published.signingIdentityPublicKey, own.signingIdentity.publicKey) && sameBytes(published.signedPrekey.publicKey, own.signedPrekey.pair.publicKey)) {
|
|
399
|
+
return "consistent";
|
|
400
|
+
}
|
|
401
|
+
if (!opts.mayReplaceForeign) return "foreign";
|
|
402
|
+
}
|
|
403
|
+
await dm.publishPrekeyBundle(serializePublicBundle(own));
|
|
404
|
+
return "published";
|
|
405
|
+
}
|
|
214
406
|
function bundleSigningMessage(dhIdentityPub, signedPrekeyPub) {
|
|
215
407
|
const msg = new Uint8Array(dhIdentityPub.length + signedPrekeyPub.length);
|
|
216
408
|
msg.set(dhIdentityPub, 0);
|
|
@@ -432,8 +624,99 @@ async function decryptFromPeer(keystore, peerId, envelopeBytes) {
|
|
|
432
624
|
return plaintext;
|
|
433
625
|
}
|
|
434
626
|
|
|
435
|
-
// src/
|
|
627
|
+
// src/providers/supabase-dm.ts
|
|
436
628
|
var PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
629
|
+
var MDM_TEXT_MESSAGE_TYPE = "txt";
|
|
630
|
+
var SupabaseDmProvider = class {
|
|
631
|
+
cachedSelfId = null;
|
|
632
|
+
cachedSelfKind = null;
|
|
633
|
+
/**
|
|
634
|
+
* `identities.kind` of the caller ("human" | "agent" | …). An agent LINE owns its mDM
|
|
635
|
+
* identity outright (its key is its only runtime); a human's identity is anchored in the app.
|
|
636
|
+
* ensureOwnBundlePublished uses this to decide whether a stale bundle may be replaced.
|
|
637
|
+
*/
|
|
638
|
+
async selfKind() {
|
|
639
|
+
await this.selfId();
|
|
640
|
+
return this.cachedSelfKind;
|
|
641
|
+
}
|
|
642
|
+
async selfId() {
|
|
643
|
+
if (this.cachedSelfId) return this.cachedSelfId;
|
|
644
|
+
readSupabaseEnv();
|
|
645
|
+
const sb = getSupabase();
|
|
646
|
+
const { data: u, error: ue } = await sb.auth.getUser();
|
|
647
|
+
if (ue || !u?.user) {
|
|
648
|
+
throw new Error(
|
|
649
|
+
"Unable to resolve current user. Ensure MOSADD_USER_JWT is set to a valid Supabase session token."
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const { data: identity, error } = await sb.from("identities").select("id, kind").eq("user_id", u.user.id).maybeSingle();
|
|
653
|
+
if (error || !identity) {
|
|
654
|
+
throw new Error("Current user has no mosadd identity row. Sign in to mosadd.com first.");
|
|
655
|
+
}
|
|
656
|
+
this.cachedSelfId = identity.id;
|
|
657
|
+
this.cachedSelfKind = typeof identity.kind === "string" ? identity.kind : null;
|
|
658
|
+
return this.cachedSelfId;
|
|
659
|
+
}
|
|
660
|
+
async send(args) {
|
|
661
|
+
const encrypted_payload = Buffer.from(args.payload).toString("base64");
|
|
662
|
+
const data = await invokeFunction("message-send", {
|
|
663
|
+
space_id: "dm",
|
|
664
|
+
thread_id: args.threadId,
|
|
665
|
+
encrypted_payload,
|
|
666
|
+
message_type: args.messageType ?? MDM_TEXT_MESSAGE_TYPE,
|
|
667
|
+
protocol_version: PROTOCOL_VERSION,
|
|
668
|
+
recipient_account_id: args.to,
|
|
669
|
+
reply_to_id: args.replyToId ?? null,
|
|
670
|
+
// Same optional columns the app writes (ircApi.sendThreadMessage); omitted when absent.
|
|
671
|
+
...args.selfPayload ? { self_payload: args.selfPayload } : {},
|
|
672
|
+
...args.recipientSelfPayload ? { recipient_self_payload: args.recipientSelfPayload } : {}
|
|
673
|
+
});
|
|
674
|
+
if (!data?.message?.id) {
|
|
675
|
+
throw new Error("message-send returned no message id");
|
|
676
|
+
}
|
|
677
|
+
return {
|
|
678
|
+
id: data.message.id,
|
|
679
|
+
deliveredAt: data.message.created_at ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
async publishPrekeyBundle(bundle) {
|
|
683
|
+
await invokeFunction("prekey-bundle-publish", {
|
|
684
|
+
bundle: Buffer.from(bundle).toString("base64")
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
async fetchPrekeyBundle(peerId) {
|
|
688
|
+
const data = await invokeFunction("prekey-bundle-fetch", {
|
|
689
|
+
peer_id: peerId
|
|
690
|
+
});
|
|
691
|
+
if (!data?.bundle) return null;
|
|
692
|
+
return new Uint8Array(Buffer.from(data.bundle, "base64"));
|
|
693
|
+
}
|
|
694
|
+
async list(args) {
|
|
695
|
+
const data = await invokeFunction("message-list", {
|
|
696
|
+
space_id: "dm",
|
|
697
|
+
thread_id: args.threadId,
|
|
698
|
+
limit: args.limit ?? 50,
|
|
699
|
+
before: args.cursor
|
|
700
|
+
});
|
|
701
|
+
return {
|
|
702
|
+
messages: (data?.messages ?? []).map((m) => ({
|
|
703
|
+
id: m.id,
|
|
704
|
+
senderId: m.sender_identity_id,
|
|
705
|
+
payload: new Uint8Array(Buffer.from(m.encrypted_payload, "base64")),
|
|
706
|
+
timestamp: m.created_at,
|
|
707
|
+
threadId: m.thread_id,
|
|
708
|
+
messageType: m.message_type ?? void 0,
|
|
709
|
+
selfPayload: m.self_payload ?? null,
|
|
710
|
+
recipientSelfPayload: m.recipient_self_payload ?? null
|
|
711
|
+
})),
|
|
712
|
+
// history mode (`before`) returns `next_before`; incremental (`since`) returns `cursor`+`has_more`.
|
|
713
|
+
nextCursor: data?.next_before ?? (data?.has_more ? data?.cursor ?? null : null)
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
|
|
718
|
+
// src/tools/mdm.ts
|
|
719
|
+
var PROTOCOL_VERSION2 = "mosadd.chat.v1";
|
|
437
720
|
var ContactRef = z.string().min(1).describe(
|
|
438
721
|
"Recipient identity id (UUID). Use mDM_list_contacts first to find the right id. Future versions will accept handle/email/phone directly."
|
|
439
722
|
);
|
|
@@ -496,27 +779,63 @@ function unpackPayload(payload) {
|
|
|
496
779
|
}
|
|
497
780
|
return { text: "<ciphertext>" };
|
|
498
781
|
}
|
|
782
|
+
async function selfKindOf(dm) {
|
|
783
|
+
const f = dm.selfKind;
|
|
784
|
+
return typeof f === "function" ? await f.call(dm) : null;
|
|
785
|
+
}
|
|
499
786
|
async function mDM_send(input, ctx) {
|
|
500
787
|
const dm = ctx.providers.dm;
|
|
501
788
|
const selfId = await dm.selfId();
|
|
502
789
|
const threadId = dmThreadId(selfId, input.to, input.thread_label);
|
|
790
|
+
const keys = ctx.providers.keys;
|
|
791
|
+
let identity = "foreign";
|
|
792
|
+
try {
|
|
793
|
+
identity = await ensureOwnBundlePublished(keys, dm, selfId, {
|
|
794
|
+
mayReplaceForeign: await selfKindOf(dm) === "agent"
|
|
795
|
+
});
|
|
796
|
+
} catch (err) {
|
|
797
|
+
ctx.log("warn", "mDM_send could not reconcile own prekey bundle", { error: String(err) });
|
|
798
|
+
}
|
|
503
799
|
const inner = packPlaintextPayload(input.text);
|
|
504
|
-
const sealed = await encryptForPeer(
|
|
505
|
-
|
|
506
|
-
|
|
800
|
+
const sealed = await encryptForPeer(keys, dm, input.to, inner);
|
|
801
|
+
const selfPayload = await sealForSelf(keys, inner);
|
|
802
|
+
let recipientSelfPayload = null;
|
|
803
|
+
if (identity !== "foreign" && input.to !== selfId) {
|
|
804
|
+
try {
|
|
805
|
+
const peer = await fetchVerifiedBundle(dm, input.to);
|
|
806
|
+
if (peer) recipientSelfPayload = await sealForPeerRecoverable(keys, peer, inner);
|
|
807
|
+
} catch (err) {
|
|
808
|
+
ctx.log("warn", "mDM_send skipped the recipient copy", { error: String(err) });
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
ctx.log("debug", "mDM_send E2EE via DmProvider", {
|
|
812
|
+
thread_id: threadId,
|
|
813
|
+
bytes: sealed.byteLength,
|
|
814
|
+
identity,
|
|
815
|
+
sender_copy: !!selfPayload,
|
|
816
|
+
recipient_copy: !!recipientSelfPayload
|
|
817
|
+
});
|
|
818
|
+
const sendArgs = {
|
|
507
819
|
to: input.to,
|
|
508
820
|
threadId,
|
|
509
821
|
payload: sealed,
|
|
510
|
-
replyToId: input.reply_to_id
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
822
|
+
replyToId: input.reply_to_id,
|
|
823
|
+
messageType: MDM_TEXT_MESSAGE_TYPE,
|
|
824
|
+
selfPayload,
|
|
825
|
+
recipientSelfPayload
|
|
826
|
+
};
|
|
827
|
+
const result = await dm.send(sendArgs);
|
|
828
|
+
if (keys.putSentMessage) {
|
|
829
|
+
await keys.putSentMessage(result.id, inner);
|
|
514
830
|
}
|
|
515
831
|
return {
|
|
516
832
|
message_id: result.id,
|
|
517
833
|
delivered_at: result.deliveredAt,
|
|
518
834
|
thread_id: threadId,
|
|
519
|
-
encrypted: true
|
|
835
|
+
encrypted: true,
|
|
836
|
+
sender_copy: !!selfPayload,
|
|
837
|
+
recipient_copy: !!recipientSelfPayload,
|
|
838
|
+
identity
|
|
520
839
|
};
|
|
521
840
|
}
|
|
522
841
|
async function mDM_send_unencrypted(input, ctx) {
|
|
@@ -528,12 +847,14 @@ async function mDM_send_unencrypted(input, ctx) {
|
|
|
528
847
|
thread_id: threadId,
|
|
529
848
|
bytes: payload.byteLength
|
|
530
849
|
});
|
|
531
|
-
const
|
|
850
|
+
const plainArgs = {
|
|
532
851
|
to: input.to,
|
|
533
852
|
threadId,
|
|
534
853
|
payload,
|
|
535
|
-
replyToId: input.reply_to_id
|
|
536
|
-
|
|
854
|
+
replyToId: input.reply_to_id,
|
|
855
|
+
messageType: MDM_TEXT_MESSAGE_TYPE
|
|
856
|
+
};
|
|
857
|
+
const result = await dm.send(plainArgs);
|
|
537
858
|
return {
|
|
538
859
|
message_id: result.id,
|
|
539
860
|
delivered_at: result.deliveredAt,
|
|
@@ -552,8 +873,19 @@ async function mDM_list(input, ctx) {
|
|
|
552
873
|
const threadId = dmThreadId(selfId, input.contact_id, input.thread_label);
|
|
553
874
|
ctx.log("debug", "mDM_list via DmProvider", { thread_id: threadId });
|
|
554
875
|
const result = await dm.list({ threadId, limit: input.limit ?? 50, cursor: input.cursor });
|
|
876
|
+
const keys = ctx.providers.keys;
|
|
877
|
+
const bundleBySender = /* @__PURE__ */ new Map();
|
|
878
|
+
const senderBundle = (id) => {
|
|
879
|
+
let p = bundleBySender.get(id);
|
|
880
|
+
if (!p) {
|
|
881
|
+
p = fetchVerifiedBundle(dm, id).catch(() => null);
|
|
882
|
+
bundleBySender.set(id, p);
|
|
883
|
+
}
|
|
884
|
+
return p;
|
|
885
|
+
};
|
|
555
886
|
const messages = await Promise.all(
|
|
556
|
-
result.messages.map(async (
|
|
887
|
+
result.messages.map(async (raw) => {
|
|
888
|
+
const m = raw;
|
|
557
889
|
const base = {
|
|
558
890
|
id: m.id,
|
|
559
891
|
sender_identity_id: m.senderId,
|
|
@@ -564,12 +896,26 @@ async function mDM_list(input, ctx) {
|
|
|
564
896
|
return { ...base, text: formatVoiceIfAny(unpackPayload(m.payload).text), encrypted: false };
|
|
565
897
|
}
|
|
566
898
|
if (m.senderId === selfId) {
|
|
567
|
-
const
|
|
568
|
-
if (
|
|
569
|
-
return { ...base, text: formatVoiceIfAny(unpackPayload(
|
|
899
|
+
const own = await openSelfSealed(keys, m.selfPayload) ?? await keys.getSentMessage?.(m.id);
|
|
900
|
+
if (own) {
|
|
901
|
+
return { ...base, text: formatVoiceIfAny(unpackPayload(own).text), encrypted: true };
|
|
902
|
+
}
|
|
903
|
+
if (input.contact_id === selfId) {
|
|
904
|
+
try {
|
|
905
|
+
const inner = await decryptFromPeer(keys, selfId, m.payload);
|
|
906
|
+
return { ...base, text: formatVoiceIfAny(unpackPayload(inner).text), encrypted: true };
|
|
907
|
+
} catch {
|
|
908
|
+
}
|
|
570
909
|
}
|
|
571
910
|
return { ...base, text: "<encrypted \xB7 sent by you>", encrypted: true };
|
|
572
911
|
}
|
|
912
|
+
if (m.recipientSelfPayload) {
|
|
913
|
+
const bundle = await senderBundle(m.senderId);
|
|
914
|
+
const copy = (bundle ? await openPeerRecoverable(keys, bundle, m.recipientSelfPayload) : null) ?? await openSelfSealed(keys, m.recipientSelfPayload);
|
|
915
|
+
if (copy) {
|
|
916
|
+
return { ...base, text: formatVoiceIfAny(unpackPayload(copy).text), encrypted: true };
|
|
917
|
+
}
|
|
918
|
+
}
|
|
573
919
|
try {
|
|
574
920
|
const inner = await decryptFromPeer(ctx.providers.keys, m.senderId, m.payload);
|
|
575
921
|
return { ...base, text: formatVoiceIfAny(unpackPayload(inner).text), encrypted: true };
|
|
@@ -642,7 +988,7 @@ async function mDM_edit(input, ctx) {
|
|
|
642
988
|
);
|
|
643
989
|
}
|
|
644
990
|
const envelope = {
|
|
645
|
-
v:
|
|
991
|
+
v: PROTOCOL_VERSION2,
|
|
646
992
|
type: "text",
|
|
647
993
|
text: input.new_text,
|
|
648
994
|
edited: true,
|
|
@@ -783,7 +1129,7 @@ var mdmAsAgentTools = [
|
|
|
783
1129
|
|
|
784
1130
|
// src/tools/mdm-voice.ts
|
|
785
1131
|
import { z as z3 } from "zod";
|
|
786
|
-
var
|
|
1132
|
+
var PROTOCOL_VERSION3 = "mosadd.chat.v1";
|
|
787
1133
|
var ContactRef2 = z3.string().min(1).describe("Recipient identity id (UUID). Use mDM_list_contacts to find it.");
|
|
788
1134
|
var RoomId = z3.string().min(1).describe("Voice room id (from mDM_call_start's call_invite).");
|
|
789
1135
|
var mDM_call_start_input = z3.object({
|
|
@@ -809,7 +1155,7 @@ function dmThreadId2(selfId, otherId, label) {
|
|
|
809
1155
|
return label ? `dm:${a}:${b}:${label}` : `dm:${a}:${b}`;
|
|
810
1156
|
}
|
|
811
1157
|
function packEnvelope(obj) {
|
|
812
|
-
const envelope = { v:
|
|
1158
|
+
const envelope = { v: PROTOCOL_VERSION3, sent_at: (/* @__PURE__ */ new Date()).toISOString(), ...obj };
|
|
813
1159
|
return new Uint8Array(Buffer.from(JSON.stringify(envelope), "utf8"));
|
|
814
1160
|
}
|
|
815
1161
|
async function signalDm(ctx, to, threadLabel, envelope) {
|
|
@@ -1468,7 +1814,7 @@ async function decryptChannelPayload(channelId, payloadB64) {
|
|
|
1468
1814
|
}
|
|
1469
1815
|
|
|
1470
1816
|
// src/tools/mirc-messages.ts
|
|
1471
|
-
var
|
|
1817
|
+
var PROTOCOL_VERSION4 = "mosadd.chat.v1";
|
|
1472
1818
|
var ChannelId3 = z6.string().min(1).max(120).describe("Channel id (UUID). From mIRC_create / mIRC_list.");
|
|
1473
1819
|
var mIRC_post_message_input = z6.object({
|
|
1474
1820
|
channel_id: ChannelId3,
|
|
@@ -1487,7 +1833,7 @@ var mIRC_list_messages_input = z6.object({
|
|
|
1487
1833
|
});
|
|
1488
1834
|
function buildPlaintextEnvelope(text, replyToId) {
|
|
1489
1835
|
const envelope = {
|
|
1490
|
-
v:
|
|
1836
|
+
v: PROTOCOL_VERSION4,
|
|
1491
1837
|
type: "text",
|
|
1492
1838
|
text,
|
|
1493
1839
|
reply_to: replyToId ?? null,
|
|
@@ -1531,7 +1877,7 @@ async function mIRC_post_message(input, ctx) {
|
|
|
1531
1877
|
thread_id,
|
|
1532
1878
|
encrypted_payload: payload,
|
|
1533
1879
|
message_type: "txt",
|
|
1534
|
-
protocol_version:
|
|
1880
|
+
protocol_version: PROTOCOL_VERSION4,
|
|
1535
1881
|
reply_to_id: input.reply_to_id ?? null,
|
|
1536
1882
|
// Attribution is decided SERVER-SIDE (message-send → _shared/ownedAgent.ts): you may sign only
|
|
1537
1883
|
// as an agent you own. Sent only when asked for, so the wire shape is unchanged for every
|
|
@@ -2189,17 +2535,37 @@ import { z as z12 } from "zod";
|
|
|
2189
2535
|
|
|
2190
2536
|
// src/providers/blob-upload.ts
|
|
2191
2537
|
function defaultBucket(kind) {
|
|
2192
|
-
return kind === "file" ? "
|
|
2538
|
+
return kind === "file" ? "chat-files" : "voice-messages";
|
|
2539
|
+
}
|
|
2540
|
+
function jwtSub(jwt) {
|
|
2541
|
+
const part = jwt.split(".")[1];
|
|
2542
|
+
if (!part) return null;
|
|
2543
|
+
try {
|
|
2544
|
+
const json = JSON.parse(Buffer.from(part.replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8"));
|
|
2545
|
+
return typeof json?.sub === "string" && json.sub ? json.sub : null;
|
|
2546
|
+
} catch {
|
|
2547
|
+
return null;
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
function ownerFolder(userJwt) {
|
|
2551
|
+
const sub = userJwt ? jwtSub(userJwt) : null;
|
|
2552
|
+
if (!sub) {
|
|
2553
|
+
throw new Error(
|
|
2554
|
+
"blob upload refused: no user id in the session JWT \u2014 bucket policy requires foldername[1] = auth.uid(); run `mosadd login` (or set MOSADD_USER_JWT) and retry"
|
|
2555
|
+
);
|
|
2556
|
+
}
|
|
2557
|
+
return sub;
|
|
2193
2558
|
}
|
|
2194
2559
|
function randomId2() {
|
|
2195
2560
|
return globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
2196
2561
|
}
|
|
2197
2562
|
async function uploadBlob(args) {
|
|
2198
|
-
readSupabaseEnv();
|
|
2563
|
+
const env = readSupabaseEnv();
|
|
2199
2564
|
const sb = getSupabase();
|
|
2200
2565
|
const bucket = args.bucket ?? defaultBucket(args.kind);
|
|
2201
2566
|
const ext = args.filename?.split(".").pop() ?? mimeExt(args.mime);
|
|
2202
|
-
const
|
|
2567
|
+
const folder = args.prefix ? args.prefix.replace(/[^a-zA-Z0-9._/-]/g, "-").replace(/^\/+|\/+$/g, "") : "";
|
|
2568
|
+
const path = [ownerFolder(env.userJwt), folder, `${randomId2()}${ext ? "." + ext : ""}`].filter(Boolean).join("/");
|
|
2203
2569
|
const { error } = await sb.storage.from(bucket).upload(path, args.bytes, {
|
|
2204
2570
|
contentType: args.mime,
|
|
2205
2571
|
upsert: false
|
|
@@ -2234,7 +2600,7 @@ function decodeBase64Body(b64) {
|
|
|
2234
2600
|
}
|
|
2235
2601
|
|
|
2236
2602
|
// src/tools/attachments.ts
|
|
2237
|
-
var
|
|
2603
|
+
var PROTOCOL_VERSION5 = "mosadd.chat.v1";
|
|
2238
2604
|
var Base64Body = z12.string().min(1).max(2e7).describe("Attachment bytes as base64 (or a data: URL). MCP is JSON-only, so binary is base64-encoded. Max ~15 MB decoded.");
|
|
2239
2605
|
var Mime = z12.string().min(1).max(120).describe("MIME type, e.g. audio/ogg, application/pdf, image/png.");
|
|
2240
2606
|
var DurationMs = z12.number().int().positive().optional().describe("Audio clip length in ms (voice only), if known.");
|
|
@@ -2250,7 +2616,7 @@ function dmThreadId3(selfId, otherId, label) {
|
|
|
2250
2616
|
async function sendAttachmentMessage(ctx, args) {
|
|
2251
2617
|
readSupabaseEnv();
|
|
2252
2618
|
const captionEnvelope = {
|
|
2253
|
-
v:
|
|
2619
|
+
v: PROTOCOL_VERSION5,
|
|
2254
2620
|
type: args.message_type,
|
|
2255
2621
|
text: args.caption ?? "",
|
|
2256
2622
|
sent_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -2268,7 +2634,7 @@ async function sendAttachmentMessage(ctx, args) {
|
|
|
2268
2634
|
attachments: [args.attachment],
|
|
2269
2635
|
// TODO(Lane A): confirm field placement
|
|
2270
2636
|
encrypted_payload,
|
|
2271
|
-
protocol_version:
|
|
2637
|
+
protocol_version: PROTOCOL_VERSION5,
|
|
2272
2638
|
recipient_account_id: args.recipient_account_id ?? null,
|
|
2273
2639
|
reply_to_id: args.reply_to_id ?? null
|
|
2274
2640
|
});
|
|
@@ -3483,7 +3849,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
3483
3849
|
// package.json
|
|
3484
3850
|
var package_default = {
|
|
3485
3851
|
name: "@mosadd/mcp",
|
|
3486
|
-
version: "3.0.0-alpha.
|
|
3852
|
+
version: "3.0.0-alpha.53",
|
|
3487
3853
|
mcpName: "io.github.hei33enberg/mosadd-mcp",
|
|
3488
3854
|
description: "Model Context Protocol server for mosadd. Exposes 4 modules (mDM, mIRC, mURL, mAYL) and capabilities (mTALK, mRAG) plus comms agent-actions \u2014 85 tools \u2014 to any agent runtime (Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, Bolt, Goose, Manus, custom).",
|
|
3489
3855
|
homepage: "https://github.com/Hei33enberg/mosADD-OS/tree/main/packages/mcp",
|
|
@@ -3558,79 +3924,6 @@ var package_default = {
|
|
|
3558
3924
|
// src/version.ts
|
|
3559
3925
|
var PKG_VERSION = package_default.version;
|
|
3560
3926
|
|
|
3561
|
-
// src/providers/supabase-dm.ts
|
|
3562
|
-
var PROTOCOL_VERSION5 = "mosadd.chat.v1";
|
|
3563
|
-
var SupabaseDmProvider = class {
|
|
3564
|
-
cachedSelfId = null;
|
|
3565
|
-
async selfId() {
|
|
3566
|
-
if (this.cachedSelfId) return this.cachedSelfId;
|
|
3567
|
-
readSupabaseEnv();
|
|
3568
|
-
const sb = getSupabase();
|
|
3569
|
-
const { data: u, error: ue } = await sb.auth.getUser();
|
|
3570
|
-
if (ue || !u?.user) {
|
|
3571
|
-
throw new Error(
|
|
3572
|
-
"Unable to resolve current user. Ensure MOSADD_USER_JWT is set to a valid Supabase session token."
|
|
3573
|
-
);
|
|
3574
|
-
}
|
|
3575
|
-
const { data: identity, error } = await sb.from("identities").select("id").eq("user_id", u.user.id).maybeSingle();
|
|
3576
|
-
if (error || !identity) {
|
|
3577
|
-
throw new Error("Current user has no mosadd identity row. Sign in to mosadd.com first.");
|
|
3578
|
-
}
|
|
3579
|
-
this.cachedSelfId = identity.id;
|
|
3580
|
-
return this.cachedSelfId;
|
|
3581
|
-
}
|
|
3582
|
-
async send(args) {
|
|
3583
|
-
const encrypted_payload = Buffer.from(args.payload).toString("base64");
|
|
3584
|
-
const data = await invokeFunction("message-send", {
|
|
3585
|
-
space_id: "dm",
|
|
3586
|
-
thread_id: args.threadId,
|
|
3587
|
-
encrypted_payload,
|
|
3588
|
-
message_type: "text",
|
|
3589
|
-
protocol_version: PROTOCOL_VERSION5,
|
|
3590
|
-
recipient_account_id: args.to,
|
|
3591
|
-
reply_to_id: args.replyToId ?? null
|
|
3592
|
-
});
|
|
3593
|
-
if (!data?.message?.id) {
|
|
3594
|
-
throw new Error("message-send returned no message id");
|
|
3595
|
-
}
|
|
3596
|
-
return {
|
|
3597
|
-
id: data.message.id,
|
|
3598
|
-
deliveredAt: data.message.created_at ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
3599
|
-
};
|
|
3600
|
-
}
|
|
3601
|
-
async publishPrekeyBundle(bundle) {
|
|
3602
|
-
await invokeFunction("prekey-bundle-publish", {
|
|
3603
|
-
bundle: Buffer.from(bundle).toString("base64")
|
|
3604
|
-
});
|
|
3605
|
-
}
|
|
3606
|
-
async fetchPrekeyBundle(peerId) {
|
|
3607
|
-
const data = await invokeFunction("prekey-bundle-fetch", {
|
|
3608
|
-
peer_id: peerId
|
|
3609
|
-
});
|
|
3610
|
-
if (!data?.bundle) return null;
|
|
3611
|
-
return new Uint8Array(Buffer.from(data.bundle, "base64"));
|
|
3612
|
-
}
|
|
3613
|
-
async list(args) {
|
|
3614
|
-
const data = await invokeFunction("message-list", {
|
|
3615
|
-
space_id: "dm",
|
|
3616
|
-
thread_id: args.threadId,
|
|
3617
|
-
limit: args.limit ?? 50,
|
|
3618
|
-
before: args.cursor
|
|
3619
|
-
});
|
|
3620
|
-
return {
|
|
3621
|
-
messages: (data?.messages ?? []).map((m) => ({
|
|
3622
|
-
id: m.id,
|
|
3623
|
-
senderId: m.sender_identity_id,
|
|
3624
|
-
payload: new Uint8Array(Buffer.from(m.encrypted_payload, "base64")),
|
|
3625
|
-
timestamp: m.created_at,
|
|
3626
|
-
threadId: m.thread_id
|
|
3627
|
-
})),
|
|
3628
|
-
// history mode (`before`) returns `next_before`; incremental (`since`) returns `cursor`+`has_more`.
|
|
3629
|
-
nextCursor: data?.next_before ?? (data?.has_more ? data?.cursor ?? null : null)
|
|
3630
|
-
};
|
|
3631
|
-
}
|
|
3632
|
-
};
|
|
3633
|
-
|
|
3634
3927
|
// src/providers/supabase-voice.ts
|
|
3635
3928
|
var ROOM_PREFIX = "mosadd-room-";
|
|
3636
3929
|
var SupabaseVoiceProvider = class {
|
|
@@ -3688,18 +3981,22 @@ function slug(s) {
|
|
|
3688
3981
|
}
|
|
3689
3982
|
|
|
3690
3983
|
// src/server.ts
|
|
3691
|
-
function defaultProviders(injected) {
|
|
3984
|
+
function defaultProviders(injected, opts = {}) {
|
|
3692
3985
|
return {
|
|
3693
3986
|
dm: injected?.dm ?? new SupabaseDmProvider(),
|
|
3694
|
-
keys: injected?.keys ??
|
|
3987
|
+
keys: injected?.keys ?? defaultKeyStore(opts.mdmSecret),
|
|
3695
3988
|
voice: injected?.voice ?? new SupabaseVoiceProvider()
|
|
3696
3989
|
};
|
|
3697
3990
|
}
|
|
3991
|
+
function defaultKeyStore(secret) {
|
|
3992
|
+
const s = (secret ?? "").trim();
|
|
3993
|
+
return s ? new SecretMdmKeyStore(s) : new InMemoryMdmKeyStore();
|
|
3994
|
+
}
|
|
3698
3995
|
function createMosaddServer(options = {}) {
|
|
3699
3996
|
const mode = options.mode ?? (options.apiKey ? "cloud" : "local");
|
|
3700
3997
|
const hubUrl = options.hubUrl ?? "https://mcp.mosadd.com";
|
|
3701
3998
|
const logLevel = options.logLevel ?? "info";
|
|
3702
|
-
const providers = defaultProviders(options.providers);
|
|
3999
|
+
const providers = defaultProviders(options.providers, { mdmSecret: options.apiKey });
|
|
3703
4000
|
const ctx = {
|
|
3704
4001
|
options: { ...options, mode, hubUrl },
|
|
3705
4002
|
providers,
|
|
@@ -3712,7 +4009,11 @@ function createMosaddServer(options = {}) {
|
|
|
3712
4009
|
}
|
|
3713
4010
|
};
|
|
3714
4011
|
if (options.autoPublishKeys) {
|
|
3715
|
-
void
|
|
4012
|
+
void (async () => {
|
|
4013
|
+
const selfId = await providers.dm.selfId();
|
|
4014
|
+
const kind = await providers.dm.selfKind?.();
|
|
4015
|
+
return ensureOwnBundlePublished(providers.keys, providers.dm, selfId, { mayReplaceForeign: kind === "agent" });
|
|
4016
|
+
})().then((state) => ctx.log("debug", "auto-ensure mDM prekeys", { state })).catch((err) => ctx.log("debug", "auto-publish mDM prekeys skipped", { error: String(err) }));
|
|
3716
4017
|
}
|
|
3717
4018
|
const server = new McpServer(
|
|
3718
4019
|
{
|