@mosadd/mcp 3.0.0-alpha.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/LICENSE +201 -0
- package/README.md +116 -0
- package/dist/bin/mcp.d.ts +1 -0
- package/dist/bin/mcp.js +36 -0
- package/dist/chunk-25L536JZ.js +1182 -0
- package/dist/index.d.ts +174 -0
- package/dist/index.js +10 -0
- package/package.json +66 -0
- package/src/__tests__/golden-path.test.ts +132 -0
- package/src/__tests__/integration.test.ts +175 -0
- package/src/__tests__/mdm-e2ee.test.ts +149 -0
- package/src/__tests__/server.test.ts +67 -0
- package/src/bin/mcp.ts +47 -0
- package/src/crypto/mdm-session.ts +378 -0
- package/src/index.ts +17 -0
- package/src/observability/sentry.ts +79 -0
- package/src/providers/supabase-dm.ts +117 -0
- package/src/providers/supabase.ts +80 -0
- package/src/server.ts +103 -0
- package/src/tools/capabilities.ts +68 -0
- package/src/tools/index.ts +37 -0
- package/src/tools/mail.ts +89 -0
- package/src/tools/mdm.ts +379 -0
- package/src/tools/mirc-members.ts +177 -0
- package/src/tools/mirc.ts +170 -0
- package/src/tools/mroom-messages.ts +142 -0
- package/src/tools/mroom.ts +189 -0
- package/src/types.ts +79 -0
package/src/tools/mdm.ts
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mDM — Direct Messages.
|
|
3
|
+
*
|
|
4
|
+
* USP: multi-thread per contact. Within one contact you can spin up multiple
|
|
5
|
+
* threads for clarity (the way GitHub Issues handles conversations), unlike
|
|
6
|
+
* WhatsApp/Telegram which collapse everything into a single chat.
|
|
7
|
+
*
|
|
8
|
+
* Phase 1 alpha: wired to the m0ssad-3 Supabase Edge Functions as a
|
|
9
|
+
* strangler-fig step (`message-send`, `message-list`). The user supplies
|
|
10
|
+
* their own Supabase URL + anon key + a session JWT via env vars (BYOK).
|
|
11
|
+
*
|
|
12
|
+
* Phase 2 will route through the hosted gateway (mcp.mosadd.com) with the
|
|
13
|
+
* 167-event radar middleware in front, and replace the plaintext
|
|
14
|
+
* encrypted_payload with real X3DH + Double Ratchet via @mosadd/crypto.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
19
|
+
import { invokeFunction, getSupabase, readSupabaseEnv } from "../providers/supabase.js";
|
|
20
|
+
import {
|
|
21
|
+
encryptForPeer,
|
|
22
|
+
decryptFromPeer,
|
|
23
|
+
publishOwnPrekeys,
|
|
24
|
+
isE2eeEnvelope,
|
|
25
|
+
} from "../crypto/mdm-session.js";
|
|
26
|
+
|
|
27
|
+
// ---- Constants ----
|
|
28
|
+
|
|
29
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
30
|
+
|
|
31
|
+
// ---- Schemas ----
|
|
32
|
+
|
|
33
|
+
const ContactRef = z
|
|
34
|
+
.string()
|
|
35
|
+
.min(1)
|
|
36
|
+
.describe(
|
|
37
|
+
"Recipient identity id (UUID). Use mDM_list_contacts first to find the right id. Future versions will accept handle/email/phone directly.",
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const ThreadSuffix = z
|
|
41
|
+
.string()
|
|
42
|
+
.min(1)
|
|
43
|
+
.max(120)
|
|
44
|
+
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
45
|
+
.describe(
|
|
46
|
+
"Optional thread label within the conversation with this contact. Defaults to the contact's main thread. mosadd USP: multiple threads per contact.",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const mDM_send_input = z.object({
|
|
50
|
+
to: ContactRef,
|
|
51
|
+
text: z.string().min(1).max(64_000).describe("Message body (UTF-8). Max 64 KB."),
|
|
52
|
+
thread_label: ThreadSuffix.optional().describe(
|
|
53
|
+
"Optional thread label. If omitted, uses the default thread for this contact.",
|
|
54
|
+
),
|
|
55
|
+
reply_to_id: z.string().optional().describe("Optional id of the message being replied to."),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const mDM_list_input = z.object({
|
|
59
|
+
contact_id: ContactRef.describe("Identity id of the contact to read messages with."),
|
|
60
|
+
thread_label: ThreadSuffix.optional(),
|
|
61
|
+
limit: z.number().int().min(1).max(200).default(50).optional(),
|
|
62
|
+
cursor: z.string().optional().describe("Opaque pagination cursor from a previous response."),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const mDM_list_contacts_input = z.object({
|
|
66
|
+
limit: z.number().int().min(1).max(500).default(100).optional(),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const mDM_respond_request_input = z.object({
|
|
70
|
+
request_id: z.string().min(1),
|
|
71
|
+
action: z.enum(["accept", "reject"]),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// ---- Helpers ----
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Deterministic DM thread id from a pair of identities. Sorts lexicographically
|
|
78
|
+
* so both peers compute the same id regardless of who sends first.
|
|
79
|
+
*
|
|
80
|
+
* dmThreadId("alice", "bob") === dmThreadId("bob", "alice") === "dm:alice:bob"
|
|
81
|
+
* dmThreadId("alice", "bob", "work") === "dm:alice:bob:work"
|
|
82
|
+
*/
|
|
83
|
+
function dmThreadId(selfId: string, otherId: string, label?: string): string {
|
|
84
|
+
const [a, b] = [selfId, otherId].sort();
|
|
85
|
+
return label ? `dm:${a}:${b}:${label}` : `dm:${a}:${b}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Resolve the current user's identity row from their JWT-authed Supabase client.
|
|
90
|
+
* Returns the identity id (NOT the auth user id — they differ).
|
|
91
|
+
*/
|
|
92
|
+
async function resolveSelfIdentityId(): Promise<string> {
|
|
93
|
+
const sb = getSupabase();
|
|
94
|
+
const { data: u, error: ue } = await sb.auth.getUser();
|
|
95
|
+
if (ue || !u?.user) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
"Unable to resolve current user. Ensure M0SSAD_USER_JWT is set to a valid Supabase session token.",
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
const { data: identity, error } = await sb
|
|
101
|
+
.from("identities")
|
|
102
|
+
.select("id")
|
|
103
|
+
.eq("user_id", u.user.id)
|
|
104
|
+
.maybeSingle();
|
|
105
|
+
if (error || !identity) {
|
|
106
|
+
throw new Error("Current user has no mosadd identity row. Sign in to mosadd.com first.");
|
|
107
|
+
}
|
|
108
|
+
return identity.id as string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Pack message body into the encrypted_payload string the Edge Function expects.
|
|
113
|
+
*
|
|
114
|
+
* ALPHA: not actually encrypted — wraps plaintext in JSON + base64.
|
|
115
|
+
* Receivers in the m0ssad-3 app understand this `mosadd.chat.v1` shape.
|
|
116
|
+
* V0.2: replace with @mosadd/crypto Double Ratchet wrap.
|
|
117
|
+
*/
|
|
118
|
+
function packPlaintextPayload(text: string, replyToId?: string): Uint8Array {
|
|
119
|
+
const envelope = {
|
|
120
|
+
v: PROTOCOL_VERSION,
|
|
121
|
+
type: "text",
|
|
122
|
+
text,
|
|
123
|
+
reply_to: replyToId ?? null,
|
|
124
|
+
sent_at: new Date().toISOString(),
|
|
125
|
+
};
|
|
126
|
+
// OPAQUE bytes handed to the DmProvider — the provider does not interpret
|
|
127
|
+
// them. Today: utf8 JSON. V0.2: @mosadd/crypto Double Ratchet ciphertext.
|
|
128
|
+
return new Uint8Array(Buffer.from(JSON.stringify(envelope), "utf8"));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function unpackPayload(payload: Uint8Array): { text: string; sent_at?: string; reply_to?: string | null } {
|
|
132
|
+
try {
|
|
133
|
+
const json = Buffer.from(payload).toString("utf8");
|
|
134
|
+
const obj = JSON.parse(json);
|
|
135
|
+
if (typeof obj?.text === "string") return obj;
|
|
136
|
+
} catch {
|
|
137
|
+
/* fall through */
|
|
138
|
+
}
|
|
139
|
+
return { text: "<ciphertext>" };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ---- Handlers ----
|
|
143
|
+
|
|
144
|
+
async function mDM_send(
|
|
145
|
+
input: z.infer<typeof mDM_send_input>,
|
|
146
|
+
ctx: MosaddToolContext,
|
|
147
|
+
): Promise<{ message_id: string; delivered_at: string; thread_id: string; encrypted: true }> {
|
|
148
|
+
// E2EE path (default). The plaintext is packed into the inner mosadd.chat.v1
|
|
149
|
+
// envelope, then SEALED via X3DH + Double Ratchet into an opaque envelope the
|
|
150
|
+
// DmProvider moves without understanding. First contact fetches the peer's
|
|
151
|
+
// published prekey bundle through the same provider (decision "1a").
|
|
152
|
+
const dm = ctx.providers.dm;
|
|
153
|
+
const selfId = await dm.selfId();
|
|
154
|
+
const threadId = dmThreadId(selfId, input.to, input.thread_label);
|
|
155
|
+
|
|
156
|
+
const inner = packPlaintextPayload(input.text, input.reply_to_id);
|
|
157
|
+
const sealed = await encryptForPeer(ctx.providers.keys, dm, input.to, inner);
|
|
158
|
+
|
|
159
|
+
ctx.log("debug", "mDM_send E2EE via DmProvider", { thread_id: threadId, bytes: sealed.byteLength });
|
|
160
|
+
|
|
161
|
+
const result = await dm.send({
|
|
162
|
+
to: input.to,
|
|
163
|
+
threadId,
|
|
164
|
+
payload: sealed,
|
|
165
|
+
replyToId: input.reply_to_id,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
message_id: result.id,
|
|
170
|
+
delivered_at: result.deliveredAt,
|
|
171
|
+
thread_id: threadId,
|
|
172
|
+
encrypted: true,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function mDM_send_unencrypted(
|
|
177
|
+
input: z.infer<typeof mDM_send_input>,
|
|
178
|
+
ctx: MosaddToolContext,
|
|
179
|
+
): Promise<{ message_id: string; delivered_at: string; thread_id: string; encrypted: false }> {
|
|
180
|
+
// DEPRECATED migration path: plaintext mosadd.chat.v1 envelope, no E2EE.
|
|
181
|
+
// Kept so peers who have not yet published prekeys can still be reached
|
|
182
|
+
// during the migration window. Will be removed once E2EE is universal.
|
|
183
|
+
const dm = ctx.providers.dm;
|
|
184
|
+
const selfId = await dm.selfId();
|
|
185
|
+
const threadId = dmThreadId(selfId, input.to, input.thread_label);
|
|
186
|
+
const payload = packPlaintextPayload(input.text, input.reply_to_id);
|
|
187
|
+
|
|
188
|
+
ctx.log("warn", "mDM_send_unencrypted (deprecated, plaintext) via DmProvider", {
|
|
189
|
+
thread_id: threadId,
|
|
190
|
+
bytes: payload.byteLength,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const result = await dm.send({
|
|
194
|
+
to: input.to,
|
|
195
|
+
threadId,
|
|
196
|
+
payload,
|
|
197
|
+
replyToId: input.reply_to_id,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
message_id: result.id,
|
|
202
|
+
delivered_at: result.deliveredAt,
|
|
203
|
+
thread_id: threadId,
|
|
204
|
+
encrypted: false,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function mDM_publish_keys(
|
|
209
|
+
_input: Record<string, never>,
|
|
210
|
+
ctx: MosaddToolContext,
|
|
211
|
+
): Promise<{ published: true; one_time_prekeys: number }> {
|
|
212
|
+
// Publish the local prekey bundle so peers can start encrypted sessions with
|
|
213
|
+
// us. Rides the same provider as messages (network row / radio announce).
|
|
214
|
+
const { oneTimePrekeyCount } = await publishOwnPrekeys(ctx.providers.keys, ctx.providers.dm);
|
|
215
|
+
ctx.log("debug", "mDM_publish_keys", { one_time_prekeys: oneTimePrekeyCount });
|
|
216
|
+
return { published: true, one_time_prekeys: oneTimePrekeyCount };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function mDM_list(
|
|
220
|
+
input: z.infer<typeof mDM_list_input>,
|
|
221
|
+
ctx: MosaddToolContext,
|
|
222
|
+
): Promise<{
|
|
223
|
+
messages: Array<{
|
|
224
|
+
id: string;
|
|
225
|
+
sender_identity_id: string;
|
|
226
|
+
text: string;
|
|
227
|
+
timestamp: string;
|
|
228
|
+
thread_id: string;
|
|
229
|
+
encrypted: boolean;
|
|
230
|
+
}>;
|
|
231
|
+
next_cursor: string | null;
|
|
232
|
+
threads: string[];
|
|
233
|
+
}> {
|
|
234
|
+
const dm = ctx.providers.dm;
|
|
235
|
+
const selfId = await dm.selfId();
|
|
236
|
+
const threadId = dmThreadId(selfId, input.contact_id, input.thread_label);
|
|
237
|
+
|
|
238
|
+
ctx.log("debug", "mDM_list via DmProvider", { thread_id: threadId });
|
|
239
|
+
|
|
240
|
+
const result = await dm.list({ threadId, limit: input.limit ?? 50, cursor: input.cursor });
|
|
241
|
+
|
|
242
|
+
const messages = await Promise.all(
|
|
243
|
+
result.messages.map(async (m) => {
|
|
244
|
+
const base = {
|
|
245
|
+
id: m.id,
|
|
246
|
+
sender_identity_id: m.senderId,
|
|
247
|
+
timestamp: m.timestamp,
|
|
248
|
+
thread_id: m.threadId,
|
|
249
|
+
};
|
|
250
|
+
// Legacy plaintext envelope (deprecated path) — unpack directly.
|
|
251
|
+
if (!isE2eeEnvelope(m.payload)) {
|
|
252
|
+
return { ...base, text: unpackPayload(m.payload).text, encrypted: false };
|
|
253
|
+
}
|
|
254
|
+
// Our own outgoing ratchet messages aren't decryptable on read (no local
|
|
255
|
+
// plaintext store yet — tracked follow-up); show a marker, don't fail.
|
|
256
|
+
if (m.senderId === selfId) {
|
|
257
|
+
return { ...base, text: "<encrypted · sent by you>", encrypted: true };
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
const inner = await decryptFromPeer(ctx.providers.keys, m.senderId, m.payload);
|
|
261
|
+
return { ...base, text: unpackPayload(inner).text, encrypted: true };
|
|
262
|
+
} catch (err) {
|
|
263
|
+
ctx.log("warn", "mDM_list failed to decrypt a message", { id: m.id, error: String(err) });
|
|
264
|
+
return { ...base, text: "<undecryptable>", encrypted: true };
|
|
265
|
+
}
|
|
266
|
+
}),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
messages,
|
|
271
|
+
next_cursor: result.nextCursor,
|
|
272
|
+
threads: [threadId],
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function mDM_list_contacts(
|
|
277
|
+
input: z.infer<typeof mDM_list_contacts_input>,
|
|
278
|
+
ctx: MosaddToolContext,
|
|
279
|
+
): Promise<{
|
|
280
|
+
contacts: Array<{ identity_id: string; account_id: string | null; display_name: string | null; state: string }>;
|
|
281
|
+
}> {
|
|
282
|
+
readSupabaseEnv();
|
|
283
|
+
const selfId = await resolveSelfIdentityId();
|
|
284
|
+
const sb = getSupabase();
|
|
285
|
+
|
|
286
|
+
ctx.log("debug", "mDM_list_contacts querying contacts table", { selfId });
|
|
287
|
+
|
|
288
|
+
const { data, error } = await sb
|
|
289
|
+
.from("contacts")
|
|
290
|
+
.select("contact_identity_id, state, identities:contact_identity_id(account_id, display_name)")
|
|
291
|
+
.eq("owner_identity_id", selfId)
|
|
292
|
+
.limit(input.limit ?? 100);
|
|
293
|
+
|
|
294
|
+
if (error) {
|
|
295
|
+
throw new Error(`Failed to list contacts: ${error.message}`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
type Row = {
|
|
299
|
+
contact_identity_id: string;
|
|
300
|
+
state: string;
|
|
301
|
+
identities?: { account_id?: string | null; display_name?: string | null } | null;
|
|
302
|
+
};
|
|
303
|
+
const contacts = (data as unknown as Row[]).map((r) => ({
|
|
304
|
+
identity_id: r.contact_identity_id,
|
|
305
|
+
account_id: r.identities?.account_id ?? null,
|
|
306
|
+
display_name: r.identities?.display_name ?? null,
|
|
307
|
+
state: r.state,
|
|
308
|
+
}));
|
|
309
|
+
|
|
310
|
+
return { contacts };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async function mDM_respond_request(
|
|
314
|
+
input: z.infer<typeof mDM_respond_request_input>,
|
|
315
|
+
ctx: MosaddToolContext,
|
|
316
|
+
): Promise<{ ok: true }> {
|
|
317
|
+
readSupabaseEnv();
|
|
318
|
+
ctx.log("debug", "mDM_respond_request invoking message-request-respond", input);
|
|
319
|
+
await invokeFunction<{ ok: boolean }>("message-request-respond", {
|
|
320
|
+
request_id: input.request_id,
|
|
321
|
+
action: input.action,
|
|
322
|
+
});
|
|
323
|
+
return { ok: true };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const mDM_publish_keys_input = z.object({});
|
|
327
|
+
|
|
328
|
+
// ---- Registration ----
|
|
329
|
+
|
|
330
|
+
export const mdmTools: MosaddTool[] = [
|
|
331
|
+
{
|
|
332
|
+
name: "mDM_list_contacts",
|
|
333
|
+
requires: "any",
|
|
334
|
+
description:
|
|
335
|
+
"List the user's mosadd contacts. Returns identity_id (use this for mDM_send / mDM_list), account handle, display name, and contact state (pending, accepted, blocked).",
|
|
336
|
+
inputSchema: mDM_list_contacts_input,
|
|
337
|
+
handler: mDM_list_contacts as MosaddTool["handler"],
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
name: "mDM_publish_keys",
|
|
341
|
+
requires: "any",
|
|
342
|
+
description:
|
|
343
|
+
"Publish your mDM prekey bundle so other people can start an end-to-end-encrypted conversation with you. Run this once after sign-in (and to replenish one-time prekeys). Rides the same transport as messages, so it works over network or off-grid radio.",
|
|
344
|
+
inputSchema: mDM_publish_keys_input,
|
|
345
|
+
handler: mDM_publish_keys as MosaddTool["handler"],
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
name: "mDM_send",
|
|
349
|
+
requires: "any",
|
|
350
|
+
description:
|
|
351
|
+
"Send an END-TO-END-ENCRYPTED direct message via mosadd mDM. Pass `to` as the recipient's mosadd identity_id (look it up with mDM_list_contacts). Establishes an X3DH + Double Ratchet session on first contact (the recipient must have run mDM_publish_keys). Optional thread_label puts the message in a named thread — mosadd USP: multiple threads per contact, unlike WhatsApp/Telegram. If the recipient has no published keys yet, use mDM_send_unencrypted.",
|
|
352
|
+
inputSchema: mDM_send_input,
|
|
353
|
+
handler: mDM_send as MosaddTool["handler"],
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
name: "mDM_send_unencrypted",
|
|
357
|
+
requires: "any",
|
|
358
|
+
description:
|
|
359
|
+
"DEPRECATED migration-window fallback: send a direct message WITHOUT end-to-end encryption (plaintext envelope). Only use when the recipient has not yet published prekeys (mDM_send fails with that hint). Prefer mDM_send. Will be removed once E2EE is universal.",
|
|
360
|
+
inputSchema: mDM_send_input,
|
|
361
|
+
handler: mDM_send_unencrypted as MosaddTool["handler"],
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
name: "mDM_list",
|
|
365
|
+
requires: "any",
|
|
366
|
+
description:
|
|
367
|
+
"List recent direct messages with a specific contact. Pass contact_id as the identity_id from mDM_list_contacts. Decrypts end-to-end-encrypted messages from the contact automatically; legacy plaintext messages are shown as-is. Optionally filter to a single thread_label.",
|
|
368
|
+
inputSchema: mDM_list_input,
|
|
369
|
+
handler: mDM_list as MosaddTool["handler"],
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
name: "mDM_respond_request",
|
|
373
|
+
requires: "any",
|
|
374
|
+
description:
|
|
375
|
+
"Accept or reject an incoming DM request from a contact who is not yet in the user's whitelist.",
|
|
376
|
+
inputSchema: mDM_respond_request_input,
|
|
377
|
+
handler: mDM_respond_request as MosaddTool["handler"],
|
|
378
|
+
},
|
|
379
|
+
];
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mIRC channel members — join, leave, kick, ban, set-role, set-ptt.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the m0ssad-3 `channel-members-manage` Edge Function (audit ab6c885f),
|
|
5
|
+
* which dispatches on the `action` body field.
|
|
6
|
+
*
|
|
7
|
+
* Split from packages/mcp/src/tools/mirc.ts because the channel-management
|
|
8
|
+
* surface and the member-management surface have different ergonomics — a
|
|
9
|
+
* model that handles roster ops shouldn't have to read past 5 'channel CRUD'
|
|
10
|
+
* tools to find them.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
15
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
16
|
+
|
|
17
|
+
// ---- Schemas ----
|
|
18
|
+
|
|
19
|
+
const ChannelId = z
|
|
20
|
+
.string()
|
|
21
|
+
.min(1)
|
|
22
|
+
.max(120)
|
|
23
|
+
.describe("Channel id (UUID). From mIRC_list / mIRC_create.");
|
|
24
|
+
|
|
25
|
+
const MemberIdentity = z
|
|
26
|
+
.string()
|
|
27
|
+
.min(1)
|
|
28
|
+
.describe("Target user's identity id (UUID). From mDM_list_contacts or radar logs.");
|
|
29
|
+
|
|
30
|
+
const Role = z
|
|
31
|
+
.enum(["owner", "admin", "moderator", "member", "guest"])
|
|
32
|
+
.describe("Member role within the channel.");
|
|
33
|
+
|
|
34
|
+
const mIRC_join_input = z.object({
|
|
35
|
+
channel_id: ChannelId,
|
|
36
|
+
password: z.string().min(1).optional().describe("Required if the channel is access_mode=private."),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const mIRC_request_access_input = z.object({
|
|
40
|
+
channel_id: ChannelId,
|
|
41
|
+
message: z.string().max(500).optional().describe("Optional note for the channel owner explaining why."),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const mIRC_leave_input = z.object({
|
|
45
|
+
channel_id: ChannelId,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const mIRC_approve_request_input = z.object({
|
|
49
|
+
channel_id: ChannelId,
|
|
50
|
+
request_id: z.string().min(1).describe("Pending request id."),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const mIRC_reject_request_input = mIRC_approve_request_input;
|
|
54
|
+
|
|
55
|
+
const mIRC_kick_input = z.object({
|
|
56
|
+
channel_id: ChannelId,
|
|
57
|
+
identity_id: MemberIdentity,
|
|
58
|
+
reason: z.string().max(500).optional(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const mIRC_ban_input = z.object({
|
|
62
|
+
channel_id: ChannelId,
|
|
63
|
+
identity_id: MemberIdentity,
|
|
64
|
+
reason: z.string().max(500).optional(),
|
|
65
|
+
/** Optional unban deadline (ISO-8601). Omit for permanent ban. */
|
|
66
|
+
until: z.string().optional(),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const mIRC_unban_input = z.object({
|
|
70
|
+
channel_id: ChannelId,
|
|
71
|
+
identity_id: MemberIdentity,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const mIRC_set_role_input = z.object({
|
|
75
|
+
channel_id: ChannelId,
|
|
76
|
+
identity_id: MemberIdentity,
|
|
77
|
+
role: Role,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const mIRC_set_ptt_input = z.object({
|
|
81
|
+
channel_id: ChannelId,
|
|
82
|
+
identity_id: MemberIdentity,
|
|
83
|
+
ptt_enabled: z.boolean().describe("Allow this member to hold the PTT floor in voice mode."),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ---- Handlers ----
|
|
87
|
+
|
|
88
|
+
async function invokeChannelMembers(
|
|
89
|
+
action: string,
|
|
90
|
+
body: Record<string, unknown>,
|
|
91
|
+
ctx: MosaddToolContext,
|
|
92
|
+
): Promise<unknown> {
|
|
93
|
+
readSupabaseEnv();
|
|
94
|
+
ctx.log("debug", `mIRC.${action}`, body);
|
|
95
|
+
return await invokeFunction("channel-members-manage", { action, ...body });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ---- Registration ----
|
|
99
|
+
|
|
100
|
+
export const mircMembersTools: MosaddTool[] = [
|
|
101
|
+
{
|
|
102
|
+
name: "mIRC_join",
|
|
103
|
+
requires: "network",
|
|
104
|
+
description:
|
|
105
|
+
"Join a channel as the current user. For private channels pass `password`. Returns a membership row or 409 if already a member.",
|
|
106
|
+
inputSchema: mIRC_join_input,
|
|
107
|
+
handler: ((input, ctx) => invokeChannelMembers("join", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "mIRC_request_access",
|
|
111
|
+
requires: "network",
|
|
112
|
+
description:
|
|
113
|
+
"Request access to an invite_only channel. Owner / admins get a notification with the optional message.",
|
|
114
|
+
inputSchema: mIRC_request_access_input,
|
|
115
|
+
handler: ((input, ctx) => invokeChannelMembers("request-access", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "mIRC_leave",
|
|
119
|
+
requires: "network",
|
|
120
|
+
description: "Leave a channel as the current user. Owner cannot leave without transferring ownership first.",
|
|
121
|
+
inputSchema: mIRC_leave_input,
|
|
122
|
+
handler: ((input, ctx) => invokeChannelMembers("leave", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "mIRC_approve_request",
|
|
126
|
+
requires: "network",
|
|
127
|
+
description: "Approve a pending access request. Admins / moderators / owner only.",
|
|
128
|
+
inputSchema: mIRC_approve_request_input,
|
|
129
|
+
handler: ((input, ctx) => invokeChannelMembers("approve-request", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: "mIRC_reject_request",
|
|
133
|
+
requires: "network",
|
|
134
|
+
description: "Reject a pending access request. Admins / moderators / owner only.",
|
|
135
|
+
inputSchema: mIRC_reject_request_input,
|
|
136
|
+
handler: ((input, ctx) => invokeChannelMembers("reject-request", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: "mIRC_kick",
|
|
140
|
+
requires: "network",
|
|
141
|
+
description:
|
|
142
|
+
"Remove a member from the channel. They can rejoin if not banned. Requires moderator+. `reason` is logged to channel-event-log.",
|
|
143
|
+
inputSchema: mIRC_kick_input,
|
|
144
|
+
handler: ((input, ctx) => invokeChannelMembers("kick", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
name: "mIRC_ban",
|
|
148
|
+
requires: "network",
|
|
149
|
+
description:
|
|
150
|
+
"Ban a member from the channel. They cannot rejoin until unbanned. `until` (ISO-8601) for time-bound ban; omit for permanent. Requires admin+.",
|
|
151
|
+
inputSchema: mIRC_ban_input,
|
|
152
|
+
handler: ((input, ctx) => invokeChannelMembers("ban", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "mIRC_unban",
|
|
156
|
+
requires: "network",
|
|
157
|
+
description: "Lift a ban. Requires admin+.",
|
|
158
|
+
inputSchema: mIRC_unban_input,
|
|
159
|
+
handler: ((input, ctx) => invokeChannelMembers("unban", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: "mIRC_set_role",
|
|
163
|
+
requires: "network",
|
|
164
|
+
description:
|
|
165
|
+
"Promote / demote a member. Roles: owner > admin > moderator > member > guest. Only owner can promote to admin; only admin+ can promote to moderator.",
|
|
166
|
+
inputSchema: mIRC_set_role_input,
|
|
167
|
+
handler: ((input, ctx) => invokeChannelMembers("set-role", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: "mIRC_set_ptt",
|
|
171
|
+
requires: "network",
|
|
172
|
+
description:
|
|
173
|
+
"Grant or revoke PTT floor permission for a member in voice-capable channels (capabilities.ptt=true). Requires moderator+.",
|
|
174
|
+
inputSchema: mIRC_set_ptt_input,
|
|
175
|
+
handler: ((input, ctx) => invokeChannelMembers("set-ptt", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
176
|
+
},
|
|
177
|
+
];
|