@mosadd/mcp 3.0.0-alpha.4 → 3.0.0-alpha.6
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 +24 -14
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-YDFU6S5E.js → chunk-KAPG5FBN.js} +493 -120
- package/dist/index.d.ts +24 -3
- package/dist/index.js +5 -3
- package/package.json +4 -4
- package/src/__tests__/integration.test.ts +1 -1
- package/src/index.ts +2 -0
- package/src/providers/supabase-dm.ts +2 -2
- package/src/providers/supabase.ts +26 -2
- package/src/tools/index.ts +11 -3
- package/src/tools/knowledge.ts +52 -11
- package/src/tools/mail.ts +70 -2
- package/src/tools/mcall.ts +216 -0
- package/src/tools/mdm-voice.ts +206 -0
- package/src/tools/mdm.ts +58 -2
- package/src/tools/mirc-edge.ts +190 -0
- package/src/tools/mirc-members.ts +1 -1
- package/src/tools/mirc-messages.ts +7 -2
- package/src/tools/mirc.ts +1 -1
- package/src/tools/mroom-messages.ts +1 -1
- package/src/tools/mroom.ts +33 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mDM — 1:1 voice (full-duplex call + voice note).
|
|
3
|
+
*
|
|
4
|
+
* mDM is the PRIVATE channel: 1:1 / threaded text (mdm.ts) PLUS real-time 1:1
|
|
5
|
+
* voice here. This is the ordinary phone-style call between two people — NOT
|
|
6
|
+
* walkie-talkie (that's mTALK, half-duplex floor control) and NOT the telephone
|
|
7
|
+
* network (that's mCALL / PSTN). Audio rides the same media transport the
|
|
8
|
+
* VoiceProvider points at (LiveKit by default); these tools coordinate the room
|
|
9
|
+
* and the call invite/hangup signalling over the DM channel.
|
|
10
|
+
*
|
|
11
|
+
* Flow:
|
|
12
|
+
* caller: mDM_call_start({ to }) → creates a room, returns join creds, AND
|
|
13
|
+
* drops a `call_invite` control message in the DM thread.
|
|
14
|
+
* callee: sees the invite (mDM_list), then mDM_call_answer({ room_id }) → creds.
|
|
15
|
+
* either: mDM_call_end({ room_id, to }) → drops a `call_end` control message.
|
|
16
|
+
*
|
|
17
|
+
* No new backend: rooms + tokens come from ctx.providers.voice (same minting
|
|
18
|
+
* path mTALK uses); signalling reuses ctx.providers.dm.send.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
23
|
+
|
|
24
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
25
|
+
|
|
26
|
+
const ContactRef = z
|
|
27
|
+
.string()
|
|
28
|
+
.min(1)
|
|
29
|
+
.describe("Recipient identity id (UUID). Use mDM_list_contacts to find it.");
|
|
30
|
+
|
|
31
|
+
const RoomId = z.string().min(1).describe("Voice room id (from mDM_call_start's call_invite).");
|
|
32
|
+
|
|
33
|
+
const mDM_call_start_input = z.object({
|
|
34
|
+
to: ContactRef,
|
|
35
|
+
video: z.boolean().optional().describe("Request a video call instead of audio-only. Default false."),
|
|
36
|
+
thread_label: z
|
|
37
|
+
.string()
|
|
38
|
+
.max(120)
|
|
39
|
+
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
40
|
+
.optional()
|
|
41
|
+
.describe("Optional thread label to place the call invite in. Defaults to the contact's main thread."),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const mDM_call_answer_input = z.object({ room_id: RoomId });
|
|
45
|
+
|
|
46
|
+
const mDM_call_end_input = z.object({
|
|
47
|
+
room_id: RoomId,
|
|
48
|
+
to: ContactRef.optional().describe("If set, also drop a call_end control message in this contact's DM thread."),
|
|
49
|
+
thread_label: z.string().max(120).regex(/^[a-zA-Z0-9._-]+$/).optional(),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const mDM_voice_note_input = z.object({
|
|
53
|
+
to: ContactRef,
|
|
54
|
+
audio_url: z.string().url().describe("URL of the recorded audio clip (already uploaded by the client)."),
|
|
55
|
+
duration_ms: z.number().int().positive().optional().describe("Clip length in milliseconds, if known."),
|
|
56
|
+
mime: z.string().max(120).optional().describe("Audio MIME type, e.g. audio/ogg, audio/mpeg. Default audio/ogg."),
|
|
57
|
+
thread_label: z.string().max(120).regex(/^[a-zA-Z0-9._-]+$/).optional(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// ---- Helpers ----
|
|
61
|
+
|
|
62
|
+
/** Deterministic DM thread id (sorted pair) — must match mdm.ts dmThreadId. */
|
|
63
|
+
function dmThreadId(selfId: string, otherId: string, label?: string): string {
|
|
64
|
+
const [a, b] = [selfId, otherId].sort();
|
|
65
|
+
return label ? `dm:${a}:${b}:${label}` : `dm:${a}:${b}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Pack a mosadd.chat.v1 control/voice envelope into the opaque payload bytes. */
|
|
69
|
+
function packEnvelope(obj: Record<string, unknown>): Uint8Array {
|
|
70
|
+
const envelope = { v: PROTOCOL_VERSION, sent_at: new Date().toISOString(), ...obj };
|
|
71
|
+
return new Uint8Array(Buffer.from(JSON.stringify(envelope), "utf8"));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function signalDm(
|
|
75
|
+
ctx: MosaddToolContext,
|
|
76
|
+
to: string,
|
|
77
|
+
threadLabel: string | undefined,
|
|
78
|
+
envelope: Record<string, unknown>,
|
|
79
|
+
): Promise<{ message_id?: string; thread_id: string }> {
|
|
80
|
+
const dm = ctx.providers.dm;
|
|
81
|
+
const selfId = await dm.selfId();
|
|
82
|
+
const threadId = dmThreadId(selfId, to, threadLabel);
|
|
83
|
+
const res = await dm.send({ to, threadId, payload: packEnvelope(envelope) });
|
|
84
|
+
return { message_id: (res as { message_id?: string })?.message_id, thread_id: threadId };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---- Handlers ----
|
|
88
|
+
|
|
89
|
+
async function mDM_call_start(
|
|
90
|
+
input: z.infer<typeof mDM_call_start_input>,
|
|
91
|
+
ctx: MosaddToolContext,
|
|
92
|
+
): Promise<{
|
|
93
|
+
room_id: string;
|
|
94
|
+
token: string;
|
|
95
|
+
url: string;
|
|
96
|
+
identity: string;
|
|
97
|
+
mode: "audio" | "video";
|
|
98
|
+
invited: boolean;
|
|
99
|
+
thread_id: string;
|
|
100
|
+
}> {
|
|
101
|
+
const dm = ctx.providers.dm;
|
|
102
|
+
const selfId = await dm.selfId();
|
|
103
|
+
const mode = input.video ? "video" : "audio";
|
|
104
|
+
|
|
105
|
+
// Stable room label tied to the DM pair so re-dials reuse one room.
|
|
106
|
+
const label = `dmcall-${dmThreadId(selfId, input.to, input.thread_label).replace(/[^a-zA-Z0-9._-]/g, "-")}`.slice(0, 120);
|
|
107
|
+
const { roomId } = await ctx.providers.voice.createRoom(label);
|
|
108
|
+
const ticket = await ctx.providers.voice.joinTicket(roomId);
|
|
109
|
+
|
|
110
|
+
let invited = false;
|
|
111
|
+
let thread_id = dmThreadId(selfId, input.to, input.thread_label);
|
|
112
|
+
try {
|
|
113
|
+
const sig = await signalDm(ctx, input.to, input.thread_label, {
|
|
114
|
+
type: "call_invite",
|
|
115
|
+
room_id: roomId,
|
|
116
|
+
mode,
|
|
117
|
+
from: selfId,
|
|
118
|
+
});
|
|
119
|
+
thread_id = sig.thread_id;
|
|
120
|
+
invited = true;
|
|
121
|
+
} catch (e) {
|
|
122
|
+
ctx.log("warn", "mDM_call_start: invite signal failed (room still usable)", {
|
|
123
|
+
error: (e as Error).message,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
ctx.log("debug", "mDM_call_start", { room_id: roomId, mode, invited });
|
|
128
|
+
return { room_id: roomId, token: ticket.token, url: ticket.url, identity: ticket.identity, mode, invited, thread_id };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function mDM_call_answer(
|
|
132
|
+
input: z.infer<typeof mDM_call_answer_input>,
|
|
133
|
+
ctx: MosaddToolContext,
|
|
134
|
+
): Promise<{ room_id: string; token: string; url: string; identity: string }> {
|
|
135
|
+
const ticket = await ctx.providers.voice.joinTicket(input.room_id);
|
|
136
|
+
ctx.log("debug", "mDM_call_answer", { room_id: ticket.roomId, identity: ticket.identity });
|
|
137
|
+
return { room_id: ticket.roomId, token: ticket.token, url: ticket.url, identity: ticket.identity };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function mDM_call_end(
|
|
141
|
+
input: z.infer<typeof mDM_call_end_input>,
|
|
142
|
+
ctx: MosaddToolContext,
|
|
143
|
+
): Promise<{ room_id: string; signalled: boolean }> {
|
|
144
|
+
let signalled = false;
|
|
145
|
+
if (input.to) {
|
|
146
|
+
try {
|
|
147
|
+
await signalDm(ctx, input.to, input.thread_label, { type: "call_end", room_id: input.room_id });
|
|
148
|
+
signalled = true;
|
|
149
|
+
} catch (e) {
|
|
150
|
+
ctx.log("warn", "mDM_call_end: hangup signal failed", { error: (e as Error).message });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
ctx.log("debug", "mDM_call_end", { room_id: input.room_id, signalled });
|
|
154
|
+
return { room_id: input.room_id, signalled };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function mDM_voice_note(
|
|
158
|
+
input: z.infer<typeof mDM_voice_note_input>,
|
|
159
|
+
ctx: MosaddToolContext,
|
|
160
|
+
): Promise<{ message_id?: string; thread_id: string }> {
|
|
161
|
+
const sig = await signalDm(ctx, input.to, input.thread_label, {
|
|
162
|
+
type: "voice_note",
|
|
163
|
+
audio_url: input.audio_url,
|
|
164
|
+
duration_ms: input.duration_ms ?? null,
|
|
165
|
+
mime: input.mime ?? "audio/ogg",
|
|
166
|
+
});
|
|
167
|
+
ctx.log("debug", "mDM_voice_note", { thread_id: sig.thread_id });
|
|
168
|
+
return sig;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ---- Registration ----
|
|
172
|
+
|
|
173
|
+
export const mdmVoiceTools: MosaddTool[] = [
|
|
174
|
+
{
|
|
175
|
+
name: "mDM_call_start",
|
|
176
|
+
requires: "network",
|
|
177
|
+
description:
|
|
178
|
+
"Start a 1:1 real-time voice (or video) call with a contact — the ordinary full-duplex phone-style call (NOT walkie-talkie mTALK, NOT PSTN mCALL). Creates a room, returns join credentials (token + media url), and drops a `call_invite` control message in the DM thread so the contact can answer. Set video:true for video.",
|
|
179
|
+
inputSchema: mDM_call_start_input,
|
|
180
|
+
handler: mDM_call_start as MosaddTool["handler"],
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: "mDM_call_answer",
|
|
184
|
+
requires: "network",
|
|
185
|
+
description:
|
|
186
|
+
"Answer an incoming 1:1 call by its room_id (from the call_invite seen via mDM_list). Returns join credentials (token + media url) for the same room.",
|
|
187
|
+
inputSchema: mDM_call_answer_input,
|
|
188
|
+
handler: mDM_call_answer as MosaddTool["handler"],
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: "mDM_call_end",
|
|
192
|
+
requires: "network",
|
|
193
|
+
description:
|
|
194
|
+
"End a 1:1 call. The audio leg ends when clients disconnect; pass `to` to also drop a `call_end` (hangup) control message in the contact's DM thread.",
|
|
195
|
+
inputSchema: mDM_call_end_input,
|
|
196
|
+
handler: mDM_call_end as MosaddTool["handler"],
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: "mDM_voice_note",
|
|
200
|
+
requires: "network",
|
|
201
|
+
description:
|
|
202
|
+
"Send an asynchronous voice note to a contact: references an already-uploaded audio clip (audio_url) and posts it as a `voice_note` message in the DM thread. Use for fire-and-forget voice instead of a live call.",
|
|
203
|
+
inputSchema: mDM_voice_note_input,
|
|
204
|
+
handler: mDM_voice_note as MosaddTool["handler"],
|
|
205
|
+
},
|
|
206
|
+
];
|
package/src/tools/mdm.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* threads for clarity (the way GitHub Issues handles conversations), unlike
|
|
6
6
|
* WhatsApp/Telegram which collapse everything into a single chat.
|
|
7
7
|
*
|
|
8
|
-
* Phase 1 alpha: wired to the
|
|
8
|
+
* Phase 1 alpha: wired to the mosadd Supabase Edge Functions as a
|
|
9
9
|
* strangler-fig step (`message-send`, `message-list`). The user supplies
|
|
10
10
|
* their own Supabase URL + anon key + a session JWT via env vars (BYOK).
|
|
11
11
|
*
|
|
@@ -112,7 +112,7 @@ async function resolveSelfIdentityId(): Promise<string> {
|
|
|
112
112
|
* Pack message body into the encrypted_payload string the Edge Function expects.
|
|
113
113
|
*
|
|
114
114
|
* ALPHA: not actually encrypted — wraps plaintext in JSON + base64.
|
|
115
|
-
* Receivers in the
|
|
115
|
+
* Receivers in the mosadd consumer app understand this `mosadd.chat.v1` shape.
|
|
116
116
|
* V0.2: replace with @mosadd/crypto Double Ratchet wrap.
|
|
117
117
|
*/
|
|
118
118
|
function packPlaintextPayload(text: string, replyToId?: string): Uint8Array {
|
|
@@ -338,6 +338,46 @@ async function mDM_respond_request(
|
|
|
338
338
|
|
|
339
339
|
const mDM_publish_keys_input = z.object({});
|
|
340
340
|
|
|
341
|
+
const mDM_edit_input = z.object({
|
|
342
|
+
message_id: z.string().min(1).describe("Id of the message to edit (from mDM_list)."),
|
|
343
|
+
new_text: z.string().min(1).max(64_000).describe("Replacement message body (UTF-8). Max 64 KB."),
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
const mDM_delete_input = z.object({
|
|
347
|
+
message_id: z.string().min(1).describe("Id of the message to delete (from mDM_list)."),
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
async function mDM_edit(
|
|
351
|
+
input: z.infer<typeof mDM_edit_input>,
|
|
352
|
+
ctx: MosaddToolContext,
|
|
353
|
+
): Promise<{ message_id: string; edited: true }> {
|
|
354
|
+
readSupabaseEnv();
|
|
355
|
+
// Wire format = base64(JSON envelope), matching the EF (which atob()s it).
|
|
356
|
+
// NOTE: stored server-readable (like mDM_send_unencrypted). On an E2EE thread
|
|
357
|
+
// this does NOT re-seal the message — prefer delete + re-send for ciphertext.
|
|
358
|
+
const envelope = {
|
|
359
|
+
v: PROTOCOL_VERSION,
|
|
360
|
+
type: "text",
|
|
361
|
+
text: input.new_text,
|
|
362
|
+
edited: true,
|
|
363
|
+
sent_at: new Date().toISOString(),
|
|
364
|
+
};
|
|
365
|
+
const encrypted_payload = Buffer.from(JSON.stringify(envelope), "utf8").toString("base64");
|
|
366
|
+
ctx.log("debug", "mDM_edit via message-edit", { message_id: input.message_id });
|
|
367
|
+
await invokeFunction("message-edit", { message_id: input.message_id, encrypted_payload });
|
|
368
|
+
return { message_id: input.message_id, edited: true };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async function mDM_delete(
|
|
372
|
+
input: z.infer<typeof mDM_delete_input>,
|
|
373
|
+
ctx: MosaddToolContext,
|
|
374
|
+
): Promise<{ message_id: string; deleted: true }> {
|
|
375
|
+
readSupabaseEnv();
|
|
376
|
+
ctx.log("debug", "mDM_delete via message-delete", { message_id: input.message_id });
|
|
377
|
+
await invokeFunction("message-delete", { message_id: input.message_id });
|
|
378
|
+
return { message_id: input.message_id, deleted: true };
|
|
379
|
+
}
|
|
380
|
+
|
|
341
381
|
// ---- Registration ----
|
|
342
382
|
|
|
343
383
|
export const mdmTools: MosaddTool[] = [
|
|
@@ -389,4 +429,20 @@ export const mdmTools: MosaddTool[] = [
|
|
|
389
429
|
inputSchema: mDM_respond_request_input,
|
|
390
430
|
handler: mDM_respond_request as MosaddTool["handler"],
|
|
391
431
|
},
|
|
432
|
+
{
|
|
433
|
+
name: "mDM_edit",
|
|
434
|
+
requires: "any",
|
|
435
|
+
description:
|
|
436
|
+
"Edit a direct message you sent — replace its body by message_id (from mDM_list). Stores a server-readable edited body; on an end-to-end-encrypted thread this does not re-encrypt, so for E2EE prefer deleting and re-sending.",
|
|
437
|
+
inputSchema: mDM_edit_input,
|
|
438
|
+
handler: mDM_edit as MosaddTool["handler"],
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
name: "mDM_delete",
|
|
442
|
+
requires: "any",
|
|
443
|
+
description:
|
|
444
|
+
"Delete a direct message you sent by message_id (from mDM_list). Soft-deletes server-side so it stops showing in mDM_list for both sides.",
|
|
445
|
+
inputSchema: mDM_delete_input,
|
|
446
|
+
handler: mDM_delete as MosaddTool["handler"],
|
|
447
|
+
},
|
|
392
448
|
];
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mIRC edge — Cloudflare Worker + Durable Object backend (LINEAR-2675/2680).
|
|
3
|
+
*
|
|
4
|
+
* Parallel to mirc-messages.ts (Supabase path). These tools talk directly to
|
|
5
|
+
* the mosadd-edge Worker → ChannelDO via REST. Use them when you want:
|
|
6
|
+
* - sub-100ms global fan-out (DO broadcasts WS),
|
|
7
|
+
* - to skip the Supabase write hot path (DO async-flushes to messages_meta
|
|
8
|
+
* anyway, so the message still appears via the existing mIRC_list_messages
|
|
9
|
+
* / message-list path),
|
|
10
|
+
* - to scale to thousands of writers per channel without contention.
|
|
11
|
+
*
|
|
12
|
+
* Auth: a `mosadd_sk_live_…` hub API key (Bearer). Free plan = text is free
|
|
13
|
+
* (the paid gates live where the paid product is). Set the edge URL via
|
|
14
|
+
* MOSADD_EDGE_URL (defaults to the prod Worker).
|
|
15
|
+
*
|
|
16
|
+
* For native WebSocket subscription, MCP stdio is request/response — so live
|
|
17
|
+
* fan-out isn't a fit for a stdio MCP server. Two paths exist for browsers /
|
|
18
|
+
* external apps:
|
|
19
|
+
*
|
|
20
|
+
* - DEPRECATED, server-side only: `wss://${EDGE}/c/${channelId}/ws?k=<hub_key>`.
|
|
21
|
+
* Don't use from a browser — `?k=` lands in CF access logs.
|
|
22
|
+
* - RECOMMENDED (LINEAR-2675/E6): server calls `mIRC_mint_channel_token` to
|
|
23
|
+
* exchange the hub key for a 5-min channel-scoped JWT, then the browser
|
|
24
|
+
* opens the WS with `Sec-WebSocket-Protocol: mosadd.v1, bearer.<jwt>`.
|
|
25
|
+
* The hub key never leaves the server. Recipe: `apps/edge/README.md`.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { z } from "zod";
|
|
29
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
30
|
+
|
|
31
|
+
const DEFAULT_EDGE_URL = "https://mosadd-edge.mr-brics-33.workers.dev";
|
|
32
|
+
const DEFAULT_SUPABASE_URL = "https://rooffhgbxafyjcwmwpsy.supabase.co";
|
|
33
|
+
|
|
34
|
+
function edgeUrl(): string {
|
|
35
|
+
return process.env.MOSADD_EDGE_URL?.replace(/\/$/, "") ?? DEFAULT_EDGE_URL;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Where to call `hub-mint-channel-token`. Defaults to the prod Supabase project
|
|
39
|
+
* but can be overridden with MOSADD_SUPABASE_URL for staging/self-host. */
|
|
40
|
+
function supabaseUrl(): string {
|
|
41
|
+
return (
|
|
42
|
+
process.env.MOSADD_SUPABASE_URL?.replace(/\/$/, "") ??
|
|
43
|
+
process.env.SUPABASE_URL?.replace(/\/$/, "") ??
|
|
44
|
+
DEFAULT_SUPABASE_URL
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Resolve the hub key from env. Same convention as the rest of the toolkit:
|
|
49
|
+
* MOSADD_API_KEY is the standard, with MOSADD_HUB_KEY as a friendly alias. */
|
|
50
|
+
function hubKey(): string {
|
|
51
|
+
const key = process.env.MOSADD_API_KEY ?? process.env.MOSADD_HUB_KEY ?? "";
|
|
52
|
+
if (!key) throw new Error("MOSADD_API_KEY required for mIRC edge tools (see mosadd login or hub-keys).");
|
|
53
|
+
return key;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── send_edge ────────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
const mIRC_send_edge_input = z.object({
|
|
59
|
+
channel_id: z.string().min(1).max(128).describe("Channel id (UUID). Same as mIRC_create / mIRC_list."),
|
|
60
|
+
text: z.string().min(1).max(64_000).describe("Message body (UTF-8, max 64 KB)."),
|
|
61
|
+
from: z.string().max(120).optional().describe("Sender hint shown to other clients (defaults to the hub-key owner)."),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
async function mIRC_send_edge(
|
|
65
|
+
input: z.infer<typeof mIRC_send_edge_input>,
|
|
66
|
+
ctx: MosaddToolContext,
|
|
67
|
+
): Promise<{ message_id: string; delivered_at: number; channel_id: string }> {
|
|
68
|
+
ctx.log("debug", "mIRC_send_edge", { channel_id: input.channel_id });
|
|
69
|
+
const r = await fetch(`${edgeUrl()}/c/${input.channel_id}/send`, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: {
|
|
72
|
+
"Content-Type": "application/json",
|
|
73
|
+
Authorization: `Bearer ${hubKey()}`,
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({ text: input.text, from: input.from }),
|
|
76
|
+
});
|
|
77
|
+
if (!r.ok) {
|
|
78
|
+
const errBody = await r.text().catch(() => "");
|
|
79
|
+
throw new Error(`mosadd-edge ${r.status}: ${errBody.slice(0, 200)}`);
|
|
80
|
+
}
|
|
81
|
+
const data = await r.json() as { id?: string; ts?: number };
|
|
82
|
+
if (!data?.id) throw new Error("mosadd-edge: send returned no id");
|
|
83
|
+
return { message_id: data.id, delivered_at: data.ts ?? Date.now(), channel_id: input.channel_id };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ── history_edge ─────────────────────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
const mIRC_history_edge_input = z.object({
|
|
89
|
+
channel_id: z.string().min(1).max(128).describe("Channel id (UUID)."),
|
|
90
|
+
limit: z.number().int().min(1).max(100).default(50).optional()
|
|
91
|
+
.describe("How many recent messages to return (max 100, the DO ring size)."),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
async function mIRC_history_edge(
|
|
95
|
+
input: z.infer<typeof mIRC_history_edge_input>,
|
|
96
|
+
ctx: MosaddToolContext,
|
|
97
|
+
): Promise<{ messages: Array<{ id: string; from: string | null; text: string; timestamp: number }>; channel_id: string }> {
|
|
98
|
+
const limit = input.limit ?? 50;
|
|
99
|
+
ctx.log("debug", "mIRC_history_edge", { channel_id: input.channel_id, limit });
|
|
100
|
+
const r = await fetch(`${edgeUrl()}/c/${input.channel_id}/history?limit=${limit}`, {
|
|
101
|
+
headers: { Authorization: `Bearer ${hubKey()}` },
|
|
102
|
+
});
|
|
103
|
+
if (!r.ok) {
|
|
104
|
+
const errBody = await r.text().catch(() => "");
|
|
105
|
+
throw new Error(`mosadd-edge ${r.status}: ${errBody.slice(0, 200)}`);
|
|
106
|
+
}
|
|
107
|
+
const data = await r.json() as { messages?: Array<{ id: string; ts: number; from?: string | null; text: string }> };
|
|
108
|
+
return {
|
|
109
|
+
channel_id: input.channel_id,
|
|
110
|
+
messages: (data.messages ?? []).map((m) => ({
|
|
111
|
+
id: m.id,
|
|
112
|
+
from: m.from ?? null,
|
|
113
|
+
text: m.text,
|
|
114
|
+
timestamp: m.ts,
|
|
115
|
+
})),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ── mint_channel_token (LINEAR-2675/E6) ─────────────────────────────────────
|
|
120
|
+
//
|
|
121
|
+
// Server-side helper: exchange the long-lived hub key for a short-lived,
|
|
122
|
+
// channel-scoped JWT that's safe to ship to a browser. The Worker validates
|
|
123
|
+
// these via `Sec-WebSocket-Protocol`, so the hub key never leaves the server.
|
|
124
|
+
// Typical use: a Next.js / Express `/api/chat-token` route calls this tool to
|
|
125
|
+
// mint a token, returns it to the browser, browser opens the WS.
|
|
126
|
+
|
|
127
|
+
const mIRC_mint_channel_token_input = z.object({
|
|
128
|
+
channel_id: z.string().min(1).max(128)
|
|
129
|
+
.describe("Channel id — the JWT will be scoped to this channel only. Must match the WS upgrade path."),
|
|
130
|
+
user_id: z.string().min(1).max(120).optional()
|
|
131
|
+
.describe("Optional end-user id. Stamped onto messages as 'from'. Useful when one hub key fronts many end-users (relay topology)."),
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
interface MintTokenResult {
|
|
135
|
+
token: string;
|
|
136
|
+
expires_in: number;
|
|
137
|
+
channel_id: string;
|
|
138
|
+
scope: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function mIRC_mint_channel_token(
|
|
142
|
+
input: z.infer<typeof mIRC_mint_channel_token_input>,
|
|
143
|
+
ctx: MosaddToolContext,
|
|
144
|
+
): Promise<MintTokenResult> {
|
|
145
|
+
ctx.log("debug", "mIRC_mint_channel_token", { channel_id: input.channel_id });
|
|
146
|
+
const r = await fetch(`${supabaseUrl()}/functions/v1/hub-mint-channel-token`, {
|
|
147
|
+
method: "POST",
|
|
148
|
+
headers: {
|
|
149
|
+
"Content-Type": "application/json",
|
|
150
|
+
Authorization: `Bearer ${hubKey()}`,
|
|
151
|
+
},
|
|
152
|
+
body: JSON.stringify({ channel_id: input.channel_id, user_id: input.user_id }),
|
|
153
|
+
});
|
|
154
|
+
if (!r.ok) {
|
|
155
|
+
const errBody = await r.text().catch(() => "");
|
|
156
|
+
throw new Error(`hub-mint-channel-token ${r.status}: ${errBody.slice(0, 200)}`);
|
|
157
|
+
}
|
|
158
|
+
const data = await r.json() as Partial<MintTokenResult>;
|
|
159
|
+
if (!data?.token || !data?.expires_in || !data?.channel_id || !data?.scope) {
|
|
160
|
+
throw new Error("hub-mint-channel-token: malformed response");
|
|
161
|
+
}
|
|
162
|
+
return data as MintTokenResult;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export const mircEdgeTools: MosaddTool[] = [
|
|
166
|
+
{
|
|
167
|
+
name: "mIRC_mint_channel_token",
|
|
168
|
+
requires: "network",
|
|
169
|
+
description:
|
|
170
|
+
"Exchange the hub key (MOSADD_API_KEY) for a short-lived (5min), channel-scoped JWT. Returns { token, expires_in, channel_id, scope }. The token is safe to hand to a browser to open a WS via `Sec-WebSocket-Protocol: mosadd.v1, bearer.<token>`. The hub key NEVER leaves the server. Use this from any server-side route (Next.js /api/chat-token, Express, etc.) before showing chat in a browser. Required by LINEAR-2675/E6 to avoid leaking the hub key into CDN access logs.",
|
|
171
|
+
inputSchema: mIRC_mint_channel_token_input,
|
|
172
|
+
handler: mIRC_mint_channel_token as MosaddTool["handler"],
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
name: "mIRC_send_edge",
|
|
176
|
+
requires: "network",
|
|
177
|
+
description:
|
|
178
|
+
"Send a text message into a persistent channel via the mosadd-edge Cloudflare Worker (Durable Object backend). Sub-100ms global fan-out to all WebSocket subscribers of the channel + async flush to Supabase as system-of-record (idempotent). Use this when you want native realtime push without polling. Parallel to mIRC_post_message (Supabase path); pick one per channel. Auth: MOSADD_API_KEY env (hub key).",
|
|
179
|
+
inputSchema: mIRC_send_edge_input,
|
|
180
|
+
handler: mIRC_send_edge as MosaddTool["handler"],
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: "mIRC_history_edge",
|
|
184
|
+
requires: "network",
|
|
185
|
+
description:
|
|
186
|
+
"Read the last N (max 100) messages held in the channel's Durable Object ring buffer at the edge. Hot path is the DO, not Supabase — same data lands in messages_meta via async flush, so cold reads / pagination beyond 100 should use mIRC_list_messages.",
|
|
187
|
+
inputSchema: mIRC_history_edge_input,
|
|
188
|
+
handler: mIRC_history_edge as MosaddTool["handler"],
|
|
189
|
+
},
|
|
190
|
+
];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* mIRC channel members — join, leave, kick, ban, set-role, set-ptt.
|
|
3
3
|
*
|
|
4
|
-
* Wraps the
|
|
4
|
+
* Wraps the mosadd backend's `channel-members-manage` Edge Function (audit ab6c885f),
|
|
5
5
|
* which dispatches on the `action` body field.
|
|
6
6
|
*
|
|
7
7
|
* Split from packages/mcp/src/tools/mirc.ts because the channel-management
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* mIRC text mode — post + list messages inside a persistent channel.
|
|
3
3
|
*
|
|
4
|
-
* Channel messages route through the same
|
|
4
|
+
* Channel messages route through the same the mosadd backend's `message-send` / `message-list`
|
|
5
5
|
* Edge Functions as DMs and rooms, but with `thread_id: 'chat:<channelId>'` so the
|
|
6
6
|
* RLS / access checks apply channel-membership rules. Each channel is linked to a
|
|
7
7
|
* backing space (`metadata.linked_space_id`); message-send needs that space id, so
|
|
@@ -37,6 +37,11 @@ const mIRC_list_messages_input = z.object({
|
|
|
37
37
|
space_id: z.string().optional().describe("Optional backing space id. Resolved automatically if omitted."),
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
+
// ALPHA: channel messages are NOT end-to-end encrypted here — the body is wrapped
|
|
41
|
+
// in a JSON envelope and base64-encoded (server-readable). The mosadd.com app
|
|
42
|
+
// encrypts channel messages with a per-channel group key, so app clients cannot
|
|
43
|
+
// decrypt these dev-posted plaintext payloads (and vice-versa) until the toolkit
|
|
44
|
+
// adopts the same group-key scheme. See docs/security/e2ee-posture.md.
|
|
40
45
|
function packPlaintextPayload(text: string, replyToId?: string): string {
|
|
41
46
|
const envelope = {
|
|
42
47
|
v: PROTOCOL_VERSION,
|
|
@@ -136,7 +141,7 @@ export const mircMessagesTools: MosaddTool[] = [
|
|
|
136
141
|
name: "mIRC_post_message",
|
|
137
142
|
requires: "network",
|
|
138
143
|
description:
|
|
139
|
-
"Post a text message into a persistent channel (mIRC). Pass channel_id from mIRC_create / mIRC_list; the backing space is resolved automatically. You must be a member of private channels (use mIRC_join first). This is how an agent actually talks in a channel — the other mIRC_* tools only manage it.",
|
|
144
|
+
"Post a text message into a persistent channel (mIRC). Pass channel_id from mIRC_create / mIRC_list; the backing space is resolved automatically. You must be a member of private channels (use mIRC_join first). This is how an agent actually talks in a channel — the other mIRC_* tools only manage it. NOTE (alpha): the payload is NOT end-to-end encrypted — it is plaintext base64 and is readable by the server; the mosadd.com app uses per-channel group-key encryption, so app clients cannot read these messages yet (and vice-versa). Group-key parity is planned.",
|
|
140
145
|
inputSchema: mIRC_post_message_input,
|
|
141
146
|
handler: mIRC_post_message as MosaddTool["handler"],
|
|
142
147
|
},
|
package/src/tools/mirc.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* IRC-style persistent channels (Discord/Slack semantics). Channels live forever
|
|
5
5
|
* until deleted, members come and go. Distinct from mROOM (ephemeral, no-account).
|
|
6
6
|
*
|
|
7
|
-
* Phase 1 alpha: wired to
|
|
7
|
+
* Phase 1 alpha: wired to the mosadd backend's `channel-manage` Edge Function (strangler-fig).
|
|
8
8
|
* The single edge fn dispatches on `action` — we expose each action as its own
|
|
9
9
|
* MCP tool for clarity.
|
|
10
10
|
*/
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* mROOM text mode — send + list messages inside a private room.
|
|
3
3
|
*
|
|
4
4
|
* mROOM_send_message and mROOM_list_messages route through the same
|
|
5
|
-
*
|
|
5
|
+
* the mosadd backend's `message-send` / `message-list` Edge Functions that mDM uses,
|
|
6
6
|
* but with `space_id: 'rooms'` and `thread_id: 'privroom:<room_id>'` so the
|
|
7
7
|
* RLS policies apply room-membership checks rather than DM contact checks.
|
|
8
8
|
*
|
package/src/tools/mroom.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* mIRC (persistent channels for known users). Use cases: customer support drop-in
|
|
6
6
|
* call, live-stream after-party, anonymous tipline, incident bridge.
|
|
7
7
|
*
|
|
8
|
-
* Phase 1 alpha: wired to
|
|
8
|
+
* Phase 1 alpha: wired to the mosadd backend's `privroom-manage` + `privroom-guest-token`
|
|
9
9
|
* Edge Functions.
|
|
10
10
|
*/
|
|
11
11
|
|
|
@@ -17,6 +17,11 @@ import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
|
17
17
|
|
|
18
18
|
const RoomId = z.string().min(1).describe("Private room id (UUID).");
|
|
19
19
|
|
|
20
|
+
const mROOM_voice_join_input = z.object({
|
|
21
|
+
room_id: RoomId,
|
|
22
|
+
video: z.boolean().optional().describe("Request video instead of audio-only. Default false."),
|
|
23
|
+
});
|
|
24
|
+
|
|
20
25
|
const mROOM_create_input = z.object({
|
|
21
26
|
ttl_seconds: z
|
|
22
27
|
.number()
|
|
@@ -139,6 +144,25 @@ async function mROOM_create_guest_link(
|
|
|
139
144
|
};
|
|
140
145
|
}
|
|
141
146
|
|
|
147
|
+
async function mROOM_voice_join(
|
|
148
|
+
input: z.infer<typeof mROOM_voice_join_input>,
|
|
149
|
+
ctx: MosaddToolContext,
|
|
150
|
+
): Promise<{ room_id: string; token: string; url: string; identity: string; mode: "audio" | "video" }> {
|
|
151
|
+
// Full-duplex GROUP voice in an mROOM. Audio rides the VoiceProvider's media
|
|
152
|
+
// transport (LiveKit by default) — same minting path as mTALK/mDM voice, but
|
|
153
|
+
// no floor control (everyone can talk). Join the membership first via
|
|
154
|
+
// mROOM_join (or a guest link) so you're authorized in the room.
|
|
155
|
+
const ticket = await ctx.providers.voice.joinTicket(input.room_id);
|
|
156
|
+
ctx.log("debug", "mROOM_voice_join", { room_id: ticket.roomId, identity: ticket.identity });
|
|
157
|
+
return {
|
|
158
|
+
room_id: ticket.roomId,
|
|
159
|
+
token: ticket.token,
|
|
160
|
+
url: ticket.url,
|
|
161
|
+
identity: ticket.identity,
|
|
162
|
+
mode: input.video ? "video" : "audio",
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
142
166
|
// ---- Registration ----
|
|
143
167
|
|
|
144
168
|
export const mroomTools: MosaddTool[] = [
|
|
@@ -186,4 +210,12 @@ export const mroomTools: MosaddTool[] = [
|
|
|
186
210
|
inputSchema: mROOM_list_input,
|
|
187
211
|
handler: mROOM_list as MosaddTool["handler"],
|
|
188
212
|
},
|
|
213
|
+
{
|
|
214
|
+
name: "mROOM_voice_join",
|
|
215
|
+
requires: "network",
|
|
216
|
+
description:
|
|
217
|
+
"Join an mROOM's full-duplex GROUP voice (everyone can talk — no walkie-talkie floor; that's mTALK). Returns media credentials (token + url) for the room's audio/video transport. Be a room member first (mROOM_join or a guest link). Set video:true for video.",
|
|
218
|
+
inputSchema: mROOM_voice_join_input,
|
|
219
|
+
handler: mROOM_voice_join as MosaddTool["handler"],
|
|
220
|
+
},
|
|
189
221
|
];
|