@mosadd/mcp 3.0.0-alpha.55 → 3.0.0-alpha.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +15 -11
  2. package/README.md +152 -150
  3. package/dist/bin/mcp.js +1 -1
  4. package/dist/{chunk-IWUGKLDX.js → chunk-WEGSITAH.js} +30 -14
  5. package/dist/index.js +1 -1
  6. package/package.json +2 -2
  7. package/src/__tests__/blob-upload.test.ts +93 -93
  8. package/src/__tests__/channel-e2ee.test.ts +62 -62
  9. package/src/__tests__/flota-jawnie.test.ts +2 -2
  10. package/src/__tests__/golden-path.test.ts +139 -139
  11. package/src/__tests__/integration.test.ts +217 -182
  12. package/src/__tests__/label-room-owner.test.ts +22 -0
  13. package/src/__tests__/mdm-e2ee.test.ts +212 -212
  14. package/src/__tests__/mdm-mcp-readable.test.ts +390 -390
  15. package/src/__tests__/mirc-invite.test.ts +112 -112
  16. package/src/__tests__/mtalk.test.ts +122 -122
  17. package/src/__tests__/presence.test.ts +199 -199
  18. package/src/__tests__/server.test.ts +71 -71
  19. package/src/__tests__/tool-count-consistency.test.ts +259 -199
  20. package/src/__tests__/voice-fields.test.ts +24 -0
  21. package/src/bin/login.ts +138 -138
  22. package/src/bin/mcp.ts +145 -145
  23. package/src/config.ts +66 -66
  24. package/src/crypto/channel-e2ee.ts +288 -288
  25. package/src/crypto/mdm-session.ts +842 -842
  26. package/src/index.ts +19 -19
  27. package/src/observability/sentry.ts +79 -79
  28. package/src/providers/blob-upload.ts +162 -162
  29. package/src/providers/memory-voice.ts +79 -79
  30. package/src/providers/supabase-dm.ts +212 -202
  31. package/src/providers/supabase-voice.ts +103 -91
  32. package/src/server.ts +195 -195
  33. package/src/tools/actions.ts +166 -166
  34. package/src/tools/attachments.ts +276 -276
  35. package/src/tools/capabilities.ts +70 -70
  36. package/src/tools/channel-payload.ts +2 -2
  37. package/src/tools/embed.ts +110 -110
  38. package/src/tools/index.ts +181 -181
  39. package/src/tools/knowledge.ts +344 -344
  40. package/src/tools/mail-aliases.ts +25 -25
  41. package/src/tools/mail-provenance.ts +137 -137
  42. package/src/tools/mail.ts +403 -403
  43. package/src/tools/mdm-as-agent.ts +138 -126
  44. package/src/tools/mdm.ts +6 -4
  45. package/src/tools/mirc-edge.ts +196 -196
  46. package/src/tools/mirc-members.ts +287 -287
  47. package/src/tools/mirc-messages.ts +1 -1
  48. package/src/tools/mirc.ts +308 -308
  49. package/src/tools/mroom.ts +221 -221
  50. package/src/tools/mtalk.ts +131 -131
  51. package/src/tools/presence.ts +442 -442
  52. package/src/tools/voice-format.ts +43 -28
  53. package/src/types.ts +121 -121
  54. package/src/voice/floor.ts +132 -132
package/src/tools/mirc.ts CHANGED
@@ -1,308 +1,308 @@
1
- /**
2
- * mIRC — Persistent Channels.
3
- *
4
- * IRC-style persistent channels (Discord/Slack semantics). Channels live forever
5
- * until deleted, members come and go. Ephemeral / no-account / single-action
6
- * channels are PRESETS of mIRC (planned: LINEAR-3523 — TTL `expires_at` +
7
- * guest-link + voice), not a separate module. The killed mROOM is absorbed here.
8
- *
9
- * Phase 1 alpha: wired to the mosadd backend's `channel-manage` Edge Function (strangler-fig).
10
- * The single edge fn dispatches on `action` — we expose each action as its own
11
- * MCP tool for clarity.
12
- */
13
-
14
- import { z } from "zod";
15
- import type { MosaddTool, MosaddToolContext } from "../types.js";
16
- import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
17
- import { inviteToChannel, type MircInviteResult } from "./mirc-members.js";
18
-
19
- // ---- Schemas ----
20
-
21
- const ChannelId = z
22
- .string()
23
- .min(1)
24
- .max(120)
25
- .describe("Channel id (UUID). Use mIRC_list to discover available channels.");
26
-
27
- const AccessMode = z
28
- .enum(["open", "password", "private"])
29
- .describe("open: anyone can join · password: join with a shared password · private: invite-only.");
30
-
31
- const Capabilities = z
32
- .object({
33
- txt: z.boolean().default(true),
34
- voice: z.boolean().default(false),
35
- files: z.boolean().default(false),
36
- ptt: z.boolean().default(false),
37
- live: z.boolean().default(false),
38
- })
39
- .describe("Which modes are enabled: txt (text), voice (group voice), files (file attach), ptt (push-to-talk), live (live stream). Voice/PTT/live are server-relayed via LiveKit — NOT end-to-end encrypted.")
40
- .optional();
41
-
42
- const mIRC_create_input = z.object({
43
- name: z.string().min(1).max(80).describe("Channel display name."),
44
- topic: z.string().max(500).optional().describe("Optional channel topic/description."),
45
- access_mode: AccessMode.default("open"),
46
- password: z.string().min(6).max(120).optional().describe("Password (only for access_mode=password)."),
47
- capabilities: Capabilities,
48
- discoverable: z
49
- .boolean()
50
- .optional()
51
- .describe("List this channel in the PUBLIC directory (mIRC_discover). Honoured ONLY for access_mode=open; ignored for password/private."),
52
- wrapped_group_key: z
53
- .string()
54
- .optional()
55
- .describe(
56
- "Required for access_mode=password|private (these channels are provisioned for group-key text encryption on supported clients): the channel's group key wrapped to the creator's identity key. Open channels ignore it. If omitted on a non-open channel the backend rejects the call with a 'wrapped_group_key required' error.",
57
- ),
58
- invite_agents: z
59
- .array(z.string().min(1))
60
- .max(20)
61
- .optional()
62
- .describe(
63
- "Identity ids (UUID) of YOUR agents/robots to add right after the channel is created (agents: mDM_list_my_agents → agents[].identity_id; robots are not in that list — use the identity id from the app's Robots page). Each is fanned out to mIRC_invite: your own agents auto-join (auto_joined:true); anything else gets an invite code. Per-agent failures are reported in invites[] — the channel itself is still created. Max 20 per call (the invite mint is rate-limited to 20/min). On password/private channels the fan-out cannot carry a per-agent wrapped_group_key — use mIRC_invite per agent there.",
64
- ),
65
- });
66
-
67
- const mIRC_list_input = z.object({
68
- limit: z.number().int().min(1).max(500).default(100).optional(),
69
- offset: z.number().int().min(0).default(0).optional(),
70
- // NOTE: an access_mode filter was removed — channel-manage's list branch doesn't
71
- // filter by it (it was a silent no-op). Filter client-side on the returned
72
- // access_mode field. Server-side filtering is tracked in the consistency ticket.
73
- });
74
-
75
- const mIRC_get_input = z.object({
76
- channel_id: ChannelId,
77
- });
78
-
79
- const mIRC_update_input = z.object({
80
- channel_id: ChannelId,
81
- name: z.string().min(1).max(80).optional(),
82
- topic: z.string().max(500).optional(),
83
- // NOTE: access_mode was removed — channel-manage's update branch ignores it, and
84
- // switching an existing channel to/from an E2EE (password/private) mode needs
85
- // group-key handling that isn't wired yet. Recreate the channel to change mode.
86
- capabilities: Capabilities,
87
- discoverable: z.boolean().optional().describe("Toggle public-directory listing (open channels only)."),
88
- });
89
-
90
- const mIRC_discover_input = z.object({
91
- q: z.string().max(100).optional().describe("Optional channel-name filter."),
92
- limit: z.number().int().min(1).max(500).default(50).optional(),
93
- offset: z.number().int().min(0).default(0).optional(),
94
- });
95
-
96
- const mIRC_report_input = z.object({
97
- channel_id: ChannelId,
98
- reason: z.enum(["spam", "abuse", "harassment", "illegal", "other"]).describe("Report reason."),
99
- detail: z.string().max(1000).optional().describe("Optional free-text context."),
100
- });
101
-
102
- const mIRC_delete_input = z.object({
103
- channel_id: ChannelId,
104
- });
105
-
106
- // ---- Handlers ----
107
-
108
- /** One row per agent in mIRC_create's `invite_agents` — a refusal is DATA, never a thrown create. */
109
- export type InviteFanout =
110
- | { identity_id: string; ok: true; auto_joined: boolean; result: MircInviteResult }
111
- | { identity_id: string; ok: false; error: string };
112
-
113
- async function mIRC_create(
114
- input: z.infer<typeof mIRC_create_input>,
115
- ctx: MosaddToolContext,
116
- ): Promise<{ channel: unknown; invites?: InviteFanout[] }> {
117
- readSupabaseEnv();
118
- ctx.log("debug", "mIRC_create invoking channel-manage", { name: input.name });
119
- // channel-manage reads `description`, not `topic` — map it so the channel
120
- // topic isn't silently dropped on create (the update handler does the same).
121
- // `invite_agents` is TOOLKIT-side composition (the fan-out below) — it must not
122
- // reach channel-manage, which knows nothing about it.
123
- const { topic, invite_agents, ...rest } = input as typeof input & { topic?: string };
124
- const payload = topic !== undefined ? { ...rest, description: topic } : { ...rest };
125
- const created = await invokeFunction<{ channel: unknown }>("channel-manage", {
126
- action: "create",
127
- ...payload,
128
- });
129
- if (!invite_agents?.length) return created;
130
-
131
- const channelId = (created.channel as { id?: unknown } | null)?.id;
132
- if (typeof channelId !== "string" || !channelId) {
133
- // The channel exists; only the fan-out is impossible. Report, don't throw — a throw here
134
- // would tell the model the CREATE failed while the row is already there.
135
- return {
136
- ...created,
137
- invites: invite_agents.map((identity_id) => ({
138
- identity_id,
139
- ok: false as const,
140
- error: "channel created but its id was missing from the reply — call mIRC_invite with the id from mIRC_list",
141
- })),
142
- };
143
- }
144
- // Sequential on purpose: channel-members-manage rate-limits the invite mint per caller
145
- // (20/min, fail-closed); a parallel burst would trip it for the tail of the list.
146
- const invites: InviteFanout[] = [];
147
- for (const identity_id of invite_agents) {
148
- try {
149
- const result = await inviteToChannel({ channel_id: channelId, identity_id }, ctx);
150
- invites.push({ identity_id, ok: true, auto_joined: "auto_joined" in result && result.auto_joined === true, result });
151
- } catch (e) {
152
- // One agent's refusal (banned, already a member, not yours, encrypted channel without a
153
- // wrapped key) must not undo the others or mask the channel that was just created.
154
- invites.push({ identity_id, ok: false, error: e instanceof Error ? e.message : String(e) });
155
- }
156
- }
157
- return { ...created, invites };
158
- }
159
-
160
- async function mIRC_list(
161
- input: z.infer<typeof mIRC_list_input>,
162
- ctx: MosaddToolContext,
163
- ): Promise<{ channels: unknown[] }> {
164
- readSupabaseEnv();
165
- ctx.log("debug", "mIRC_list invoking channel-manage");
166
- return await invokeFunction<{ channels: unknown[] }>("channel-manage", {
167
- action: "list",
168
- ...input,
169
- });
170
- }
171
-
172
- async function mIRC_get(
173
- input: z.infer<typeof mIRC_get_input>,
174
- ctx: MosaddToolContext,
175
- ): Promise<{ channel: unknown }> {
176
- readSupabaseEnv();
177
- ctx.log("debug", "mIRC_get invoking channel-manage", { channel_id: input.channel_id });
178
- return await invokeFunction<{ channel: unknown }>("channel-manage", {
179
- action: "get",
180
- channel_id: input.channel_id,
181
- });
182
- }
183
-
184
- async function mIRC_update(
185
- input: z.infer<typeof mIRC_update_input>,
186
- ctx: MosaddToolContext,
187
- ): Promise<{ channel: unknown }> {
188
- readSupabaseEnv();
189
- ctx.log("debug", "mIRC_update invoking channel-manage", { channel_id: input.channel_id });
190
- // channel-manage reads `description`, not `topic` — map it so topic edits aren't
191
- // silently dropped.
192
- const { topic, ...rest } = input as typeof input & { topic?: string };
193
- const payload = topic !== undefined ? { ...rest, description: topic } : { ...rest };
194
- return await invokeFunction<{ channel: unknown }>("channel-manage", {
195
- action: "update",
196
- ...payload,
197
- });
198
- }
199
-
200
- async function mIRC_delete(
201
- input: z.infer<typeof mIRC_delete_input>,
202
- ctx: MosaddToolContext,
203
- ): Promise<{ deleted: true }> {
204
- readSupabaseEnv();
205
- ctx.log("debug", "mIRC_delete invoking channel-manage", { channel_id: input.channel_id });
206
- await invokeFunction<{ deleted: boolean }>("channel-manage", {
207
- action: "delete",
208
- channel_id: input.channel_id,
209
- });
210
- return { deleted: true };
211
- }
212
-
213
- async function mIRC_discover(
214
- input: z.infer<typeof mIRC_discover_input>,
215
- ctx: MosaddToolContext,
216
- ): Promise<{ channels: unknown[] }> {
217
- readSupabaseEnv();
218
- ctx.log("debug", "mIRC_discover invoking channel-manage");
219
- return await invokeFunction<{ channels: unknown[] }>("channel-manage", {
220
- action: "discover",
221
- ...input,
222
- });
223
- }
224
-
225
- async function mIRC_report(
226
- input: z.infer<typeof mIRC_report_input>,
227
- ctx: MosaddToolContext,
228
- ): Promise<{ ok: boolean }> {
229
- readSupabaseEnv();
230
- ctx.log("debug", "mIRC_report invoking report-content", { channel_id: input.channel_id });
231
- return await invokeFunction<{ ok: boolean }>("report-content", {
232
- thread_id: `channel:${input.channel_id}`,
233
- reason: input.reason,
234
- detail: input.detail,
235
- });
236
- }
237
-
238
- // ---- Registration ----
239
-
240
- export const mircTools: MosaddTool[] = [
241
- {
242
- name: "mIRC_create",
243
- title: "Create channel",
244
- annotations: { readOnlyHint: false },
245
- requires: "network",
246
- description:
247
- "Create a new persistent channel (Discord/Slack-style). Set access_mode to open (anyone joins), password (shared-password gated), or private (invite-only). capabilities controls modes (txt/voice/files/ptt/live; voice/ptt/live are server-relayed via LiveKit, NOT E2EE). Set discoverable:true (open channels only) to list it in the public directory. NOTE: password/private channels are provisioned for group-key TEXT encryption — supply wrapped_group_key (the group key wrapped to your identity key); open channels do not need it. The mosadd.com app encrypts/decrypts channel text with that key on supported clients; the toolkit's own mIRC_post_message posts server-readable text today (group-key parity is the next milestone). Channel voice is never E2EE. A channel WITHOUT its agent is a silent room — pass invite_agents (identity ids of your own agents, from mDM_list_my_agents) to add them in the same call, or call mIRC_invite afterwards.",
248
- inputSchema: mIRC_create_input,
249
- handler: mIRC_create as MosaddTool["handler"],
250
- },
251
- {
252
- name: "mIRC_list",
253
- title: "List your channels",
254
- annotations: { readOnlyHint: true },
255
- requires: "network",
256
- description:
257
- "List channels available to the current user.",
258
- inputSchema: mIRC_list_input,
259
- handler: mIRC_list as MosaddTool["handler"],
260
- },
261
- {
262
- name: "mIRC_get",
263
- title: "Get channel details",
264
- annotations: { readOnlyHint: true },
265
- requires: "network",
266
- description: "Get full details of a single channel by id.",
267
- inputSchema: mIRC_get_input,
268
- handler: mIRC_get as MosaddTool["handler"],
269
- },
270
- {
271
- name: "mIRC_update",
272
- title: "Update channel settings",
273
- annotations: { readOnlyHint: false },
274
- requires: "network",
275
- description: "Update channel metadata (name, topic, capabilities). Owner only. Access mode is fixed at creation — recreate the channel to change it.",
276
- inputSchema: mIRC_update_input,
277
- handler: mIRC_update as MosaddTool["handler"],
278
- },
279
- {
280
- name: "mIRC_delete",
281
- title: "Delete channel",
282
- annotations: { destructiveHint: true },
283
- requires: "network",
284
- description: "Delete a channel and cascade-remove its members. Owner only.",
285
- inputSchema: mIRC_delete_input,
286
- handler: mIRC_delete as MosaddTool["handler"],
287
- },
288
- {
289
- name: "mIRC_discover",
290
- title: "Discover public channels",
291
- annotations: { readOnlyHint: true },
292
- requires: "network",
293
- description:
294
- "Browse the PUBLIC channel DIRECTORY — open channels whose owners opted in (discoverable=true). Returns {id, channel_key, name, description, capabilities, member_count, is_member}. Filter by name with q; paginated. Join a result with mIRC_join (open = one-click). Private/password/partner channels never appear.",
295
- inputSchema: mIRC_discover_input,
296
- handler: mIRC_discover as MosaddTool["handler"],
297
- },
298
- {
299
- name: "mIRC_report",
300
- title: "Report abuse",
301
- annotations: { readOnlyHint: false },
302
- requires: "network",
303
- description:
304
- "Report a channel for abuse (spam/harassment/illegal/etc). Metadata + reason only — never message content. The reported party is the channel owner (server-derived). Use for public-directory moderation.",
305
- inputSchema: mIRC_report_input,
306
- handler: mIRC_report as MosaddTool["handler"],
307
- },
308
- ];
1
+ /**
2
+ * mIRC — Persistent Channels.
3
+ *
4
+ * IRC-style persistent channels (Discord/Slack semantics). Channels live forever
5
+ * until deleted, members come and go. Ephemeral / no-account / single-action
6
+ * channels are PRESETS of mIRC (planned: LINEAR-3523 — TTL `expires_at` +
7
+ * guest-link + voice), not a separate module. The killed mROOM is absorbed here.
8
+ *
9
+ * Phase 1 alpha: wired to the mosadd backend's `channel-manage` Edge Function (strangler-fig).
10
+ * The single edge fn dispatches on `action` — we expose each action as its own
11
+ * MCP tool for clarity.
12
+ */
13
+
14
+ import { z } from "zod";
15
+ import type { MosaddTool, MosaddToolContext } from "../types.js";
16
+ import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
17
+ import { inviteToChannel, type MircInviteResult } from "./mirc-members.js";
18
+
19
+ // ---- Schemas ----
20
+
21
+ const ChannelId = z
22
+ .string()
23
+ .min(1)
24
+ .max(120)
25
+ .describe("Channel id (UUID). Use mIRC_list to discover available channels.");
26
+
27
+ const AccessMode = z
28
+ .enum(["open", "password", "private"])
29
+ .describe("open: anyone can join · password: join with a shared password · private: invite-only.");
30
+
31
+ const Capabilities = z
32
+ .object({
33
+ txt: z.boolean().default(true),
34
+ voice: z.boolean().default(false),
35
+ files: z.boolean().default(false),
36
+ ptt: z.boolean().default(false),
37
+ live: z.boolean().default(false),
38
+ })
39
+ .describe("Which modes are enabled: txt (text), voice (group voice), files (file attach), ptt (push-to-talk), live (live stream). Voice/PTT/live are server-relayed via LiveKit — NOT end-to-end encrypted.")
40
+ .optional();
41
+
42
+ const mIRC_create_input = z.object({
43
+ name: z.string().min(1).max(80).describe("Channel display name."),
44
+ topic: z.string().max(500).optional().describe("Optional channel topic/description."),
45
+ access_mode: AccessMode.default("open"),
46
+ password: z.string().min(6).max(120).optional().describe("Password (only for access_mode=password)."),
47
+ capabilities: Capabilities,
48
+ discoverable: z
49
+ .boolean()
50
+ .optional()
51
+ .describe("List this channel in the PUBLIC directory (mIRC_discover). Honoured ONLY for access_mode=open; ignored for password/private."),
52
+ wrapped_group_key: z
53
+ .string()
54
+ .optional()
55
+ .describe(
56
+ "Required for access_mode=password|private (these channels are provisioned for group-key text encryption on supported clients): the channel's group key wrapped to the creator's identity key. Open channels ignore it. If omitted on a non-open channel the backend rejects the call with a 'wrapped_group_key required' error.",
57
+ ),
58
+ invite_agents: z
59
+ .array(z.string().min(1))
60
+ .max(20)
61
+ .optional()
62
+ .describe(
63
+ "Identity ids (UUID) of YOUR agents/robots to add right after the channel is created (agents: mDM_list_my_agents → agents[].identity_id; robots are not in that list — use the identity id from the app's Robots page). Each is fanned out to mIRC_invite: your own agents auto-join (auto_joined:true); anything else gets an invite code. Per-agent failures are reported in invites[] — the channel itself is still created. Max 20 per call (the invite mint is rate-limited to 20/min). On password/private channels the fan-out cannot carry a per-agent wrapped_group_key — use mIRC_invite per agent there.",
64
+ ),
65
+ });
66
+
67
+ const mIRC_list_input = z.object({
68
+ limit: z.number().int().min(1).max(500).default(100).optional(),
69
+ offset: z.number().int().min(0).default(0).optional(),
70
+ // NOTE: an access_mode filter was removed — channel-manage's list branch doesn't
71
+ // filter by it (it was a silent no-op). Filter client-side on the returned
72
+ // access_mode field. Server-side filtering is tracked in the consistency ticket.
73
+ });
74
+
75
+ const mIRC_get_input = z.object({
76
+ channel_id: ChannelId,
77
+ });
78
+
79
+ const mIRC_update_input = z.object({
80
+ channel_id: ChannelId,
81
+ name: z.string().min(1).max(80).optional(),
82
+ topic: z.string().max(500).optional(),
83
+ // NOTE: access_mode was removed — channel-manage's update branch ignores it, and
84
+ // switching an existing channel to/from an E2EE (password/private) mode needs
85
+ // group-key handling that isn't wired yet. Recreate the channel to change mode.
86
+ capabilities: Capabilities,
87
+ discoverable: z.boolean().optional().describe("Toggle public-directory listing (open channels only)."),
88
+ });
89
+
90
+ const mIRC_discover_input = z.object({
91
+ q: z.string().max(100).optional().describe("Optional channel-name filter."),
92
+ limit: z.number().int().min(1).max(500).default(50).optional(),
93
+ offset: z.number().int().min(0).default(0).optional(),
94
+ });
95
+
96
+ const mIRC_report_input = z.object({
97
+ channel_id: ChannelId,
98
+ reason: z.enum(["spam", "abuse", "harassment", "illegal", "other"]).describe("Report reason."),
99
+ detail: z.string().max(1000).optional().describe("Optional free-text context."),
100
+ });
101
+
102
+ const mIRC_delete_input = z.object({
103
+ channel_id: ChannelId,
104
+ });
105
+
106
+ // ---- Handlers ----
107
+
108
+ /** One row per agent in mIRC_create's `invite_agents` — a refusal is DATA, never a thrown create. */
109
+ export type InviteFanout =
110
+ | { identity_id: string; ok: true; auto_joined: boolean; result: MircInviteResult }
111
+ | { identity_id: string; ok: false; error: string };
112
+
113
+ async function mIRC_create(
114
+ input: z.infer<typeof mIRC_create_input>,
115
+ ctx: MosaddToolContext,
116
+ ): Promise<{ channel: unknown; invites?: InviteFanout[] }> {
117
+ readSupabaseEnv();
118
+ ctx.log("debug", "mIRC_create invoking channel-manage", { name: input.name });
119
+ // channel-manage reads `description`, not `topic` — map it so the channel
120
+ // topic isn't silently dropped on create (the update handler does the same).
121
+ // `invite_agents` is TOOLKIT-side composition (the fan-out below) — it must not
122
+ // reach channel-manage, which knows nothing about it.
123
+ const { topic, invite_agents, ...rest } = input as typeof input & { topic?: string };
124
+ const payload = topic !== undefined ? { ...rest, description: topic } : { ...rest };
125
+ const created = await invokeFunction<{ channel: unknown }>("channel-manage", {
126
+ action: "create",
127
+ ...payload,
128
+ });
129
+ if (!invite_agents?.length) return created;
130
+
131
+ const channelId = (created.channel as { id?: unknown } | null)?.id;
132
+ if (typeof channelId !== "string" || !channelId) {
133
+ // The channel exists; only the fan-out is impossible. Report, don't throw — a throw here
134
+ // would tell the model the CREATE failed while the row is already there.
135
+ return {
136
+ ...created,
137
+ invites: invite_agents.map((identity_id) => ({
138
+ identity_id,
139
+ ok: false as const,
140
+ error: "channel created but its id was missing from the reply — call mIRC_invite with the id from mIRC_list",
141
+ })),
142
+ };
143
+ }
144
+ // Sequential on purpose: channel-members-manage rate-limits the invite mint per caller
145
+ // (20/min, fail-closed); a parallel burst would trip it for the tail of the list.
146
+ const invites: InviteFanout[] = [];
147
+ for (const identity_id of invite_agents) {
148
+ try {
149
+ const result = await inviteToChannel({ channel_id: channelId, identity_id }, ctx);
150
+ invites.push({ identity_id, ok: true, auto_joined: "auto_joined" in result && result.auto_joined === true, result });
151
+ } catch (e) {
152
+ // One agent's refusal (banned, already a member, not yours, encrypted channel without a
153
+ // wrapped key) must not undo the others or mask the channel that was just created.
154
+ invites.push({ identity_id, ok: false, error: e instanceof Error ? e.message : String(e) });
155
+ }
156
+ }
157
+ return { ...created, invites };
158
+ }
159
+
160
+ async function mIRC_list(
161
+ input: z.infer<typeof mIRC_list_input>,
162
+ ctx: MosaddToolContext,
163
+ ): Promise<{ channels: unknown[] }> {
164
+ readSupabaseEnv();
165
+ ctx.log("debug", "mIRC_list invoking channel-manage");
166
+ return await invokeFunction<{ channels: unknown[] }>("channel-manage", {
167
+ action: "list",
168
+ ...input,
169
+ });
170
+ }
171
+
172
+ async function mIRC_get(
173
+ input: z.infer<typeof mIRC_get_input>,
174
+ ctx: MosaddToolContext,
175
+ ): Promise<{ channel: unknown }> {
176
+ readSupabaseEnv();
177
+ ctx.log("debug", "mIRC_get invoking channel-manage", { channel_id: input.channel_id });
178
+ return await invokeFunction<{ channel: unknown }>("channel-manage", {
179
+ action: "get",
180
+ channel_id: input.channel_id,
181
+ });
182
+ }
183
+
184
+ async function mIRC_update(
185
+ input: z.infer<typeof mIRC_update_input>,
186
+ ctx: MosaddToolContext,
187
+ ): Promise<{ channel: unknown }> {
188
+ readSupabaseEnv();
189
+ ctx.log("debug", "mIRC_update invoking channel-manage", { channel_id: input.channel_id });
190
+ // channel-manage reads `description`, not `topic` — map it so topic edits aren't
191
+ // silently dropped.
192
+ const { topic, ...rest } = input as typeof input & { topic?: string };
193
+ const payload = topic !== undefined ? { ...rest, description: topic } : { ...rest };
194
+ return await invokeFunction<{ channel: unknown }>("channel-manage", {
195
+ action: "update",
196
+ ...payload,
197
+ });
198
+ }
199
+
200
+ async function mIRC_delete(
201
+ input: z.infer<typeof mIRC_delete_input>,
202
+ ctx: MosaddToolContext,
203
+ ): Promise<{ deleted: true }> {
204
+ readSupabaseEnv();
205
+ ctx.log("debug", "mIRC_delete invoking channel-manage", { channel_id: input.channel_id });
206
+ await invokeFunction<{ deleted: boolean }>("channel-manage", {
207
+ action: "delete",
208
+ channel_id: input.channel_id,
209
+ });
210
+ return { deleted: true };
211
+ }
212
+
213
+ async function mIRC_discover(
214
+ input: z.infer<typeof mIRC_discover_input>,
215
+ ctx: MosaddToolContext,
216
+ ): Promise<{ channels: unknown[] }> {
217
+ readSupabaseEnv();
218
+ ctx.log("debug", "mIRC_discover invoking channel-manage");
219
+ return await invokeFunction<{ channels: unknown[] }>("channel-manage", {
220
+ action: "discover",
221
+ ...input,
222
+ });
223
+ }
224
+
225
+ async function mIRC_report(
226
+ input: z.infer<typeof mIRC_report_input>,
227
+ ctx: MosaddToolContext,
228
+ ): Promise<{ ok: boolean }> {
229
+ readSupabaseEnv();
230
+ ctx.log("debug", "mIRC_report invoking report-content", { channel_id: input.channel_id });
231
+ return await invokeFunction<{ ok: boolean }>("report-content", {
232
+ thread_id: `channel:${input.channel_id}`,
233
+ reason: input.reason,
234
+ detail: input.detail,
235
+ });
236
+ }
237
+
238
+ // ---- Registration ----
239
+
240
+ export const mircTools: MosaddTool[] = [
241
+ {
242
+ name: "mIRC_create",
243
+ title: "Create channel",
244
+ annotations: { readOnlyHint: false },
245
+ requires: "network",
246
+ description:
247
+ "Create a new persistent channel (Discord/Slack-style). Set access_mode to open (anyone joins), password (shared-password gated), or private (invite-only). capabilities controls modes (txt/voice/files/ptt/live; voice/ptt/live are server-relayed via LiveKit, NOT E2EE). Set discoverable:true (open channels only) to list it in the public directory. NOTE: password/private channels are provisioned for group-key TEXT encryption — supply wrapped_group_key (the group key wrapped to your identity key); open channels do not need it. The mosadd.com app encrypts/decrypts channel text with that key on supported clients; the toolkit's own mIRC_post_message posts server-readable text today (group-key parity is the next milestone). Channel voice is never E2EE. A channel WITHOUT its agent is a silent room — pass invite_agents (identity ids of your own agents, from mDM_list_my_agents) to add them in the same call, or call mIRC_invite afterwards.",
248
+ inputSchema: mIRC_create_input,
249
+ handler: mIRC_create as MosaddTool["handler"],
250
+ },
251
+ {
252
+ name: "mIRC_list",
253
+ title: "List your channels",
254
+ annotations: { readOnlyHint: true },
255
+ requires: "network",
256
+ description:
257
+ "List channels available to the current user.",
258
+ inputSchema: mIRC_list_input,
259
+ handler: mIRC_list as MosaddTool["handler"],
260
+ },
261
+ {
262
+ name: "mIRC_get",
263
+ title: "Get channel details",
264
+ annotations: { readOnlyHint: true },
265
+ requires: "network",
266
+ description: "Get full details of a single channel by id.",
267
+ inputSchema: mIRC_get_input,
268
+ handler: mIRC_get as MosaddTool["handler"],
269
+ },
270
+ {
271
+ name: "mIRC_update",
272
+ title: "Update channel settings",
273
+ annotations: { readOnlyHint: false },
274
+ requires: "network",
275
+ description: "Update channel metadata (name, topic, capabilities). Owner only. Access mode is fixed at creation — recreate the channel to change it.",
276
+ inputSchema: mIRC_update_input,
277
+ handler: mIRC_update as MosaddTool["handler"],
278
+ },
279
+ {
280
+ name: "mIRC_delete",
281
+ title: "Delete channel",
282
+ annotations: { destructiveHint: true },
283
+ requires: "network",
284
+ description: "Delete a channel and cascade-remove its members. Owner only.",
285
+ inputSchema: mIRC_delete_input,
286
+ handler: mIRC_delete as MosaddTool["handler"],
287
+ },
288
+ {
289
+ name: "mIRC_discover",
290
+ title: "Discover public channels",
291
+ annotations: { readOnlyHint: true },
292
+ requires: "network",
293
+ description:
294
+ "Browse the PUBLIC channel DIRECTORY — open channels whose owners opted in (discoverable=true). Returns {id, channel_key, name, description, capabilities, member_count, is_member}. Filter by name with q; paginated. Join a result with mIRC_join (open = one-click). Private/password/partner channels never appear.",
295
+ inputSchema: mIRC_discover_input,
296
+ handler: mIRC_discover as MosaddTool["handler"],
297
+ },
298
+ {
299
+ name: "mIRC_report",
300
+ title: "Report abuse",
301
+ annotations: { readOnlyHint: false },
302
+ requires: "network",
303
+ description:
304
+ "Report a channel for abuse (spam/harassment/illegal/etc). Metadata + reason only — never message content. The reported party is the channel owner (server-derived). Use for public-directory moderation.",
305
+ inputSchema: mIRC_report_input,
306
+ handler: mIRC_report as MosaddTool["handler"],
307
+ },
308
+ ];