@mosadd/mcp 3.0.0-alpha.5 → 3.0.0-alpha.50

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 (64) hide show
  1. package/CHANGELOG.md +309 -0
  2. package/LICENSE +201 -201
  3. package/README.md +150 -126
  4. package/dist/bin/mcp.js +42 -3
  5. package/dist/chunk-L4PB6A6Z.js +3800 -0
  6. package/dist/index.d.ts +118 -5
  7. package/dist/index.js +7 -3
  8. package/package.json +10 -7
  9. package/src/__tests__/channel-e2ee.test.ts +62 -0
  10. package/src/__tests__/golden-path.test.ts +139 -137
  11. package/src/__tests__/integration.test.ts +182 -178
  12. package/src/__tests__/knowledge-ingest.test.ts +114 -0
  13. package/src/__tests__/mdm-e2ee.test.ts +212 -178
  14. package/src/__tests__/mirc-invite.test.ts +112 -0
  15. package/src/__tests__/mtalk.test.ts +122 -122
  16. package/src/__tests__/presence.test.ts +199 -0
  17. package/src/__tests__/server.test.ts +71 -67
  18. package/src/__tests__/serverJsonVersion.test.ts +45 -0
  19. package/src/__tests__/tool-count-consistency.test.ts +199 -0
  20. package/src/bin/login.ts +138 -138
  21. package/src/bin/mcp.ts +145 -97
  22. package/src/config.ts +66 -66
  23. package/src/crypto/channel-e2ee.ts +288 -0
  24. package/src/crypto/mdm-session.ts +502 -403
  25. package/src/index.ts +19 -17
  26. package/src/observability/sentry.ts +79 -79
  27. package/src/providers/blob-upload.ts +116 -0
  28. package/src/providers/memory-voice.ts +79 -79
  29. package/src/providers/supabase-dm.ts +122 -117
  30. package/src/providers/supabase-voice.ts +91 -91
  31. package/src/providers/supabase.ts +231 -96
  32. package/src/server.ts +162 -105
  33. package/src/tools/__tests__/ragDeleteFoldsSourceId.test.ts +67 -0
  34. package/src/tools/actions.ts +166 -0
  35. package/src/tools/attachments.ts +276 -0
  36. package/src/tools/capabilities.ts +70 -68
  37. package/src/tools/embed.ts +110 -0
  38. package/src/tools/index.ts +181 -51
  39. package/src/tools/knowledge-graph.ts +186 -0
  40. package/src/tools/knowledge.ts +344 -132
  41. package/src/tools/mail-agentbox.ts +155 -0
  42. package/src/tools/mail-aliases.ts +25 -0
  43. package/src/tools/mail-provenance.ts +137 -0
  44. package/src/tools/mail.ts +403 -157
  45. package/src/tools/mcall.ts +216 -216
  46. package/src/tools/mdm-as-agent.ts +126 -0
  47. package/src/tools/mdm-voice.ts +220 -206
  48. package/src/tools/mdm.ts +73 -26
  49. package/src/tools/mirc-edge.ts +196 -190
  50. package/src/tools/mirc-members.ts +287 -177
  51. package/src/tools/mirc-messages.ts +76 -27
  52. package/src/tools/mirc.ts +308 -170
  53. package/src/tools/mroom-messages.ts +142 -142
  54. package/src/tools/mroom.ts +221 -221
  55. package/src/tools/mtalk.ts +131 -121
  56. package/src/tools/murl.ts +314 -0
  57. package/src/tools/presence.ts +442 -0
  58. package/src/tools/ptt-ingest.ts +99 -0
  59. package/src/tools/threat.ts +123 -0
  60. package/src/tools/voice-format.ts +28 -0
  61. package/src/types.ts +121 -85
  62. package/src/version.ts +8 -0
  63. package/src/voice/floor.ts +132 -132
  64. package/dist/chunk-4C36JDTJ.js +0 -1836
@@ -1,216 +1,216 @@
1
- /**
2
- * mCALL — Outbound PSTN calls.
3
- *
4
- * Place a real phone call (E.164) from an agent. Control plane is MCP; audio
5
- * never flows through the tool call — it is bridged over LiveKit SIP (primary)
6
- * or a Telnyx fallback to the carrier, with an optional voice vocoder.
7
- *
8
- * Wired to the m0ssad-3 `call-start-pstn` / `call-end-pstn` Edge Functions
9
- * (deployed). A carrier must be wired server-side (LIVEKIT_SIP_TRUNK_ID, or
10
- * TELNYX_API_KEY + TELNYX_CONNECTION_ID + TELNYX_FROM_NUMBER); until then the
11
- * call session is created and metered but no audio leg is bridged.
12
- *
13
- * NOTE: mCALL is the telephone (PSTN). For 1:1 in-app voice use mDM; for
14
- * walkie-talkie / floor-controlled voice use mTALK.
15
- *
16
- * Burner numbers: mCALL_acquire_number leases an ephemeral DID (~10 min) that
17
- * becomes your outbound caller-ID and can receive inbound calls. Inbound rings
18
- * are delivered out-of-band (Supabase Realtime broadcast on
19
- * `mcall-inbound-<identity_id>`, or an integrator webhook) carrying a room_name;
20
- * mCALL_answer mints the LiveKit token to join and bridge that call. Pair with
21
- * the `vocoder` flag / a published vocoder track to mask the caller's voice.
22
- */
23
-
24
- import { z } from "zod";
25
- import type { MosaddTool, MosaddToolContext } from "../types.js";
26
- import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
27
-
28
- // ---- Schemas ----
29
-
30
- const E164 = z
31
- .string()
32
- .regex(/^\+\d{7,15}$/)
33
- .describe("Destination phone number in E.164 format, e.g. +14155550123.");
34
-
35
- const mCALL_start_pstn_input = z.object({
36
- to: E164,
37
- vocoder: z
38
- .boolean()
39
- .optional()
40
- .describe("Disguise the caller's voice with a vocoder (anonymous calling). Default false."),
41
- });
42
-
43
- const mCALL_end_pstn_input = z.object({
44
- session_id: z.string().min(1).describe("Session id returned by mCALL_start_pstn."),
45
- });
46
-
47
- // ---- Burner (ephemeral number) schemas ----
48
-
49
- const mCALL_acquire_number_input = z.object({}).describe(
50
- "No parameters. Leases the next available number for ~10 minutes (idempotent: returns your live lease if you already hold one).",
51
- );
52
-
53
- const mCALL_my_numbers_input = z.object({}).describe("No parameters.");
54
-
55
- const mCALL_extend_number_input = z.object({
56
- number_id: z
57
- .string()
58
- .optional()
59
- .describe("Pool id of the number to extend. Omit to extend your current lease."),
60
- });
61
-
62
- const mCALL_release_number_input = z.object({
63
- number_id: z
64
- .string()
65
- .optional()
66
- .describe("Pool id of the number to release. Omit to release all numbers you hold."),
67
- });
68
-
69
- const mCALL_answer_input = z.object({
70
- room_name: z
71
- .string()
72
- .min(1)
73
- .describe("room_name from an inbound_call event (Realtime broadcast / integrator webhook)."),
74
- });
75
-
76
- // ---- Handlers ----
77
-
78
- async function mCALL_start_pstn(
79
- input: z.infer<typeof mCALL_start_pstn_input>,
80
- ctx: MosaddToolContext,
81
- ): Promise<{
82
- session_id: string;
83
- room_name: string;
84
- destination_number: string;
85
- vocoder_enabled: boolean;
86
- minutes_remaining: number;
87
- zone: string;
88
- zone_label: string;
89
- }> {
90
- readSupabaseEnv();
91
- ctx.log("debug", "mCALL_start_pstn invoking call-start-pstn", { to: input.to });
92
- return await invokeFunction("call-start-pstn", {
93
- destination_number: input.to,
94
- vocoder_enabled: input.vocoder ?? false,
95
- });
96
- }
97
-
98
- async function mCALL_end_pstn(
99
- input: z.infer<typeof mCALL_end_pstn_input>,
100
- ctx: MosaddToolContext,
101
- ): Promise<{ duration_ms: number; cost_credits: number }> {
102
- readSupabaseEnv();
103
- ctx.log("debug", "mCALL_end_pstn invoking call-end-pstn", { session_id: input.session_id });
104
- return await invokeFunction("call-end-pstn", { session_id: input.session_id });
105
- }
106
-
107
- // ---- Burner (ephemeral number) handlers ----
108
-
109
- interface AcquiredNumber {
110
- did: { id: string; phone_number: string; hold_until: string | null };
111
- reused: boolean;
112
- hold_until: string;
113
- seconds_remaining: number;
114
- }
115
-
116
- async function mCALL_acquire_number(
117
- _input: z.infer<typeof mCALL_acquire_number_input>,
118
- ctx: MosaddToolContext,
119
- ): Promise<AcquiredNumber> {
120
- readSupabaseEnv();
121
- ctx.log("debug", "mCALL_acquire_number invoking call-did-pool acquire");
122
- return await invokeFunction("call-did-pool", { action: "acquire" });
123
- }
124
-
125
- async function mCALL_my_numbers(
126
- _input: z.infer<typeof mCALL_my_numbers_input>,
127
- ctx: MosaddToolContext,
128
- ): Promise<{ numbers: Array<{ id: string; phone_number: string; seconds_remaining: number | null }> }> {
129
- readSupabaseEnv();
130
- ctx.log("debug", "mCALL_my_numbers invoking call-did-pool mine");
131
- return await invokeFunction("call-did-pool", { action: "mine" });
132
- }
133
-
134
- async function mCALL_extend_number(
135
- input: z.infer<typeof mCALL_extend_number_input>,
136
- ctx: MosaddToolContext,
137
- ): Promise<{ did: unknown; hold_until: string; seconds_remaining: number }> {
138
- readSupabaseEnv();
139
- ctx.log("debug", "mCALL_extend_number invoking call-did-pool extend", { number_id: input.number_id });
140
- return await invokeFunction("call-did-pool", { action: "extend", did_id: input.number_id });
141
- }
142
-
143
- async function mCALL_release_number(
144
- input: z.infer<typeof mCALL_release_number_input>,
145
- ctx: MosaddToolContext,
146
- ): Promise<{ released: number; dids: unknown[] }> {
147
- readSupabaseEnv();
148
- ctx.log("debug", "mCALL_release_number invoking call-did-pool release_mine", { number_id: input.number_id });
149
- return await invokeFunction("call-did-pool", { action: "release_mine", did_id: input.number_id });
150
- }
151
-
152
- async function mCALL_answer(
153
- input: z.infer<typeof mCALL_answer_input>,
154
- ctx: MosaddToolContext,
155
- ): Promise<{ token: string; url: string; roomName: string; identity: string }> {
156
- readSupabaseEnv();
157
- ctx.log("debug", "mCALL_answer minting livekit token", { room_name: input.room_name });
158
- return await invokeFunction("livekit-token", { roomName: input.room_name });
159
- }
160
-
161
- // ---- Registration ----
162
-
163
- export const mcallTools: MosaddTool[] = [
164
- {
165
- name: "mCALL_start_pstn",
166
- requires: "network",
167
- description:
168
- "Place an outbound PSTN phone call to an E.164 number. Bridges audio over an encrypted SIP relay (LiveKit SIP / Telnyx); optional voice vocoder for anonymous calling. Returns session_id, room_name, zone + per-minute cost, and minutes_remaining. Requires PSTN minutes in the wallet.",
169
- inputSchema: mCALL_start_pstn_input,
170
- handler: mCALL_start_pstn as MosaddTool["handler"],
171
- },
172
- {
173
- name: "mCALL_end_pstn",
174
- requires: "network",
175
- description: "End an active PSTN call by session_id. Returns call duration and credits spent.",
176
- inputSchema: mCALL_end_pstn_input,
177
- handler: mCALL_end_pstn as MosaddTool["handler"],
178
- },
179
- {
180
- name: "mCALL_acquire_number",
181
- requires: "network",
182
- description:
183
- "Lease an ephemeral 'burner' phone number for ~10 minutes. While held, it is presented as your caller-ID on mCALL_start_pstn and can receive inbound calls; it auto-recycles when the lease lapses (unless on a live call). Idempotent — returns your existing lease if you already hold one. Returns the number, hold_until and seconds_remaining.",
184
- inputSchema: mCALL_acquire_number_input,
185
- handler: mCALL_acquire_number as MosaddTool["handler"],
186
- },
187
- {
188
- name: "mCALL_my_numbers",
189
- requires: "network",
190
- description: "List the ephemeral numbers currently leased to you, each with its seconds_remaining.",
191
- inputSchema: mCALL_my_numbers_input,
192
- handler: mCALL_my_numbers as MosaddTool["handler"],
193
- },
194
- {
195
- name: "mCALL_extend_number",
196
- requires: "network",
197
- description: "Extend the lease on a held ephemeral number by another ~10 minutes (by number_id, or your current lease).",
198
- inputSchema: mCALL_extend_number_input,
199
- handler: mCALL_extend_number as MosaddTool["handler"],
200
- },
201
- {
202
- name: "mCALL_release_number",
203
- requires: "network",
204
- description: "Release an ephemeral number back to the pool before its lease expires (by number_id, or all you hold).",
205
- inputSchema: mCALL_release_number_input,
206
- handler: mCALL_release_number as MosaddTool["handler"],
207
- },
208
- {
209
- name: "mCALL_answer",
210
- requires: "network",
211
- description:
212
- "Answer an inbound call to your burner number. Pass the room_name from the inbound_call event; returns a LiveKit token + url so your client joins the room and bridges audio (publish a vocoder track to mask your voice on answer).",
213
- inputSchema: mCALL_answer_input,
214
- handler: mCALL_answer as MosaddTool["handler"],
215
- },
216
- ];
1
+ /**
2
+ * mCALL — Outbound PSTN calls.
3
+ *
4
+ * Place a real phone call (E.164) from an agent. Control plane is MCP; audio
5
+ * never flows through the tool call — it is bridged over LiveKit SIP (primary)
6
+ * or a Telnyx fallback to the carrier, with an optional voice vocoder.
7
+ *
8
+ * Wired to the m0ssad-3 `call-start-pstn` / `call-end-pstn` Edge Functions
9
+ * (deployed). A carrier must be wired server-side (LIVEKIT_SIP_TRUNK_ID, or
10
+ * TELNYX_API_KEY + TELNYX_CONNECTION_ID + TELNYX_FROM_NUMBER); until then the
11
+ * call session is created and metered but no audio leg is bridged.
12
+ *
13
+ * NOTE: mCALL is the telephone (PSTN). For 1:1 in-app voice use mDM; for
14
+ * walkie-talkie / floor-controlled voice use mTALK.
15
+ *
16
+ * Burner numbers: mCALL_acquire_number leases an ephemeral DID (~10 min) that
17
+ * becomes your outbound caller-ID and can receive inbound calls. Inbound rings
18
+ * are delivered out-of-band (Supabase Realtime broadcast on
19
+ * `mcall-inbound-<identity_id>`, or an integrator webhook) carrying a room_name;
20
+ * mCALL_answer mints the LiveKit token to join and bridge that call. Pair with
21
+ * the `vocoder` flag / a published vocoder track to mask the caller's voice.
22
+ */
23
+
24
+ import { z } from "zod";
25
+ import type { MosaddTool, MosaddToolContext } from "../types.js";
26
+ import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
27
+
28
+ // ---- Schemas ----
29
+
30
+ const E164 = z
31
+ .string()
32
+ .regex(/^\+\d{7,15}$/)
33
+ .describe("Destination phone number in E.164 format, e.g. +14155550123.");
34
+
35
+ const mCALL_start_pstn_input = z.object({
36
+ to: E164,
37
+ vocoder: z
38
+ .boolean()
39
+ .optional()
40
+ .describe("Disguise the caller's voice with a vocoder (anonymous calling). Default false."),
41
+ });
42
+
43
+ const mCALL_end_pstn_input = z.object({
44
+ session_id: z.string().min(1).describe("Session id returned by mCALL_start_pstn."),
45
+ });
46
+
47
+ // ---- Burner (ephemeral number) schemas ----
48
+
49
+ const mCALL_acquire_number_input = z.object({}).describe(
50
+ "No parameters. Leases the next available number for ~10 minutes (idempotent: returns your live lease if you already hold one).",
51
+ );
52
+
53
+ const mCALL_my_numbers_input = z.object({}).describe("No parameters.");
54
+
55
+ const mCALL_extend_number_input = z.object({
56
+ number_id: z
57
+ .string()
58
+ .optional()
59
+ .describe("Pool id of the number to extend. Omit to extend your current lease."),
60
+ });
61
+
62
+ const mCALL_release_number_input = z.object({
63
+ number_id: z
64
+ .string()
65
+ .optional()
66
+ .describe("Pool id of the number to release. Omit to release all numbers you hold."),
67
+ });
68
+
69
+ const mCALL_answer_input = z.object({
70
+ room_name: z
71
+ .string()
72
+ .min(1)
73
+ .describe("room_name from an inbound_call event (Realtime broadcast / integrator webhook)."),
74
+ });
75
+
76
+ // ---- Handlers ----
77
+
78
+ async function mCALL_start_pstn(
79
+ input: z.infer<typeof mCALL_start_pstn_input>,
80
+ ctx: MosaddToolContext,
81
+ ): Promise<{
82
+ session_id: string;
83
+ room_name: string;
84
+ destination_number: string;
85
+ vocoder_enabled: boolean;
86
+ minutes_remaining: number;
87
+ zone: string;
88
+ zone_label: string;
89
+ }> {
90
+ readSupabaseEnv();
91
+ ctx.log("debug", "mCALL_start_pstn invoking call-start-pstn", { to: input.to });
92
+ return await invokeFunction("call-start-pstn", {
93
+ destination_number: input.to,
94
+ vocoder_enabled: input.vocoder ?? false,
95
+ });
96
+ }
97
+
98
+ async function mCALL_end_pstn(
99
+ input: z.infer<typeof mCALL_end_pstn_input>,
100
+ ctx: MosaddToolContext,
101
+ ): Promise<{ duration_ms: number; cost_credits: number }> {
102
+ readSupabaseEnv();
103
+ ctx.log("debug", "mCALL_end_pstn invoking call-end-pstn", { session_id: input.session_id });
104
+ return await invokeFunction("call-end-pstn", { session_id: input.session_id });
105
+ }
106
+
107
+ // ---- Burner (ephemeral number) handlers ----
108
+
109
+ interface AcquiredNumber {
110
+ did: { id: string; phone_number: string; hold_until: string | null };
111
+ reused: boolean;
112
+ hold_until: string;
113
+ seconds_remaining: number;
114
+ }
115
+
116
+ async function mCALL_acquire_number(
117
+ _input: z.infer<typeof mCALL_acquire_number_input>,
118
+ ctx: MosaddToolContext,
119
+ ): Promise<AcquiredNumber> {
120
+ readSupabaseEnv();
121
+ ctx.log("debug", "mCALL_acquire_number invoking call-did-pool acquire");
122
+ return await invokeFunction("call-did-pool", { action: "acquire" });
123
+ }
124
+
125
+ async function mCALL_my_numbers(
126
+ _input: z.infer<typeof mCALL_my_numbers_input>,
127
+ ctx: MosaddToolContext,
128
+ ): Promise<{ numbers: Array<{ id: string; phone_number: string; seconds_remaining: number | null }> }> {
129
+ readSupabaseEnv();
130
+ ctx.log("debug", "mCALL_my_numbers invoking call-did-pool mine");
131
+ return await invokeFunction("call-did-pool", { action: "mine" });
132
+ }
133
+
134
+ async function mCALL_extend_number(
135
+ input: z.infer<typeof mCALL_extend_number_input>,
136
+ ctx: MosaddToolContext,
137
+ ): Promise<{ did: unknown; hold_until: string; seconds_remaining: number }> {
138
+ readSupabaseEnv();
139
+ ctx.log("debug", "mCALL_extend_number invoking call-did-pool extend", { number_id: input.number_id });
140
+ return await invokeFunction("call-did-pool", { action: "extend", did_id: input.number_id });
141
+ }
142
+
143
+ async function mCALL_release_number(
144
+ input: z.infer<typeof mCALL_release_number_input>,
145
+ ctx: MosaddToolContext,
146
+ ): Promise<{ released: number; dids: unknown[] }> {
147
+ readSupabaseEnv();
148
+ ctx.log("debug", "mCALL_release_number invoking call-did-pool release_mine", { number_id: input.number_id });
149
+ return await invokeFunction("call-did-pool", { action: "release_mine", did_id: input.number_id });
150
+ }
151
+
152
+ async function mCALL_answer(
153
+ input: z.infer<typeof mCALL_answer_input>,
154
+ ctx: MosaddToolContext,
155
+ ): Promise<{ token: string; url: string; roomName: string; identity: string }> {
156
+ readSupabaseEnv();
157
+ ctx.log("debug", "mCALL_answer minting livekit token", { room_name: input.room_name });
158
+ return await invokeFunction("livekit-token", { roomName: input.room_name });
159
+ }
160
+
161
+ // ---- Registration ----
162
+
163
+ export const mcallTools: MosaddTool[] = [
164
+ {
165
+ name: "mCALL_start_pstn",
166
+ requires: "network",
167
+ description:
168
+ "Place an outbound PSTN phone call to an E.164 number. Bridges audio over an encrypted SIP relay (LiveKit SIP / Telnyx); optional voice vocoder for anonymous calling. Returns session_id, room_name, zone + per-minute cost, and minutes_remaining. Requires PSTN minutes in the wallet.",
169
+ inputSchema: mCALL_start_pstn_input,
170
+ handler: mCALL_start_pstn as MosaddTool["handler"],
171
+ },
172
+ {
173
+ name: "mCALL_end_pstn",
174
+ requires: "network",
175
+ description: "End an active PSTN call by session_id. Returns call duration and credits spent.",
176
+ inputSchema: mCALL_end_pstn_input,
177
+ handler: mCALL_end_pstn as MosaddTool["handler"],
178
+ },
179
+ {
180
+ name: "mCALL_acquire_number",
181
+ requires: "network",
182
+ description:
183
+ "Lease an ephemeral 'burner' phone number for ~10 minutes. While held, it is presented as your caller-ID on mCALL_start_pstn and can receive inbound calls; it auto-recycles when the lease lapses (unless on a live call). Idempotent — returns your existing lease if you already hold one. Returns the number, hold_until and seconds_remaining.",
184
+ inputSchema: mCALL_acquire_number_input,
185
+ handler: mCALL_acquire_number as MosaddTool["handler"],
186
+ },
187
+ {
188
+ name: "mCALL_my_numbers",
189
+ requires: "network",
190
+ description: "List the ephemeral numbers currently leased to you, each with its seconds_remaining.",
191
+ inputSchema: mCALL_my_numbers_input,
192
+ handler: mCALL_my_numbers as MosaddTool["handler"],
193
+ },
194
+ {
195
+ name: "mCALL_extend_number",
196
+ requires: "network",
197
+ description: "Extend the lease on a held ephemeral number by another ~10 minutes (by number_id, or your current lease).",
198
+ inputSchema: mCALL_extend_number_input,
199
+ handler: mCALL_extend_number as MosaddTool["handler"],
200
+ },
201
+ {
202
+ name: "mCALL_release_number",
203
+ requires: "network",
204
+ description: "Release an ephemeral number back to the pool before its lease expires (by number_id, or all you hold).",
205
+ inputSchema: mCALL_release_number_input,
206
+ handler: mCALL_release_number as MosaddTool["handler"],
207
+ },
208
+ {
209
+ name: "mCALL_answer",
210
+ requires: "network",
211
+ description:
212
+ "Answer an inbound call to your burner number. Pass the room_name from the inbound_call event; returns a LiveKit token + url so your client joins the room and bridges audio (publish a vocoder track to mask your voice on answer).",
213
+ inputSchema: mCALL_answer_input,
214
+ handler: mCALL_answer as MosaddTool["handler"],
215
+ },
216
+ ];
@@ -0,0 +1,126 @@
1
+ /**
2
+ * mDM agent-attributed send — a person's session speaks AS one of THEIR agents.
3
+ *
4
+ * ── THE PROBLEM THIS SOLVES ──────────────────────────────────────────────────
5
+ * An MCP key is bound to a USER, and the hosted gateway exchanges it for THAT
6
+ * user's session. So when someone adds the connector while signed in as
7
+ * themselves, their agent runtime comes up holding the OWNER'S identity. The
8
+ * founder hit exactly this on 2026-08-17: his "Dispatch" agent authenticated as
9
+ * him, went looking for him among his own contacts, did not find him, and
10
+ * reported the agent↔agent guard as the cause. The real cost was that his mDM
11
+ * had no living 1:1 thread with his dispatcher.
12
+ *
13
+ * With this tool the same session can post AS `dispatcher@mosadd.com`, so the
14
+ * DYSPOZYTOR thread fills with messages FROM the dispatcher.
15
+ *
16
+ * ── AUTHORIZATION ────────────────────────────────────────────────────────────
17
+ * Enforced SERVER-SIDE by the `mdm-send-as-agent` Edge Function, never here:
18
+ * the named identity must be `kind='agent'`, owned by the CALLER
19
+ * (`owner_user_id`), and not retired. A client-side check would be decoration —
20
+ * the tool arguments are attacker-controlled by definition.
21
+ *
22
+ * Shape follows the accepted `mAYL_send_as_agent` precedent (owner's credential
23
+ * in, agent-attributed message out). Its AUTH model was deliberately not copied:
24
+ * measured 2026-08-17, `hub-claim-mint` accepts any `agent_id` string and never
25
+ * compares it to an owner, so the agent name in a mAYL provenance stamp is
26
+ * caller-asserted text. That gap is filed as LINEAR-5625; this tool does not
27
+ * reproduce it.
28
+ *
29
+ * Auth transport also differs on purpose: mAYL_send_as_agent reads
30
+ * `process.env.MOSADD_API_KEY`, which does not exist on the hosted multi-tenant
31
+ * gateway (the key is per-request there, not process-wide). This tool goes
32
+ * through `invokeFunction`, i.e. the caller's own session — so it works
33
+ * identically over stdio and over mcp.mosadd.com.
34
+ *
35
+ * Agent DMs travel as plaintext base64 on the established agent lane (the same
36
+ * path agent-dm-responder uses); this tool does not change that property.
37
+ * LINEAR-5620.
38
+ */
39
+
40
+ import { z } from "zod";
41
+ import type { MosaddTool, MosaddToolContext } from "../types.js";
42
+ import { invokeFunction } from "../providers/supabase.js";
43
+
44
+ const mDM_send_as_agent_input = z.object({
45
+ agent: z
46
+ .string()
47
+ .min(1)
48
+ .max(200)
49
+ .describe(
50
+ "Which of YOUR agents to speak as — its mosADD address (dispatcher@mosadd.com), " +
51
+ "its identity id, or its display name. Must be an agent you own.",
52
+ ),
53
+ text: z.string().min(1).max(8000).describe("The message body, as the agent would write it."),
54
+ to: z
55
+ .string()
56
+ .max(200)
57
+ .optional()
58
+ .describe(
59
+ "Recipient identity id or mosADD address. Defaults to YOU (the key's owner) — " +
60
+ "the common case is an agent reporting to its owner. Must be a person, not another agent.",
61
+ ),
62
+ silent: z
63
+ .boolean()
64
+ .optional()
65
+ .describe("Deliver without waking a phone: the message still arrives and shows unread, only the OS push is skipped."),
66
+ });
67
+
68
+ const mDM_list_my_agents_input = z.object({});
69
+
70
+ type SendResult = {
71
+ ok: boolean;
72
+ sent_as: { identity_id: string; address: string | null; display_name: string | null };
73
+ to: { identity_id: string; display_name: string | null };
74
+ thread_id: string;
75
+ };
76
+
77
+ async function mDM_send_as_agent(
78
+ input: z.infer<typeof mDM_send_as_agent_input>,
79
+ ctx: MosaddToolContext,
80
+ ): Promise<SendResult> {
81
+ ctx.log("debug", "mDM_send_as_agent", { agent: input.agent });
82
+ return await invokeFunction<SendResult>("mdm-send-as-agent", {
83
+ agent: input.agent,
84
+ text: input.text,
85
+ ...(input.to ? { to: input.to } : {}),
86
+ ...(input.silent ? { silent: true } : {}),
87
+ });
88
+ }
89
+
90
+ async function mDM_list_my_agents(
91
+ _input: z.infer<typeof mDM_list_my_agents_input>,
92
+ ctx: MosaddToolContext,
93
+ ): Promise<{ agents: Array<{ identity_id: string; address: string | null; display_name: string | null }> }> {
94
+ ctx.log("debug", "mDM_list_my_agents");
95
+ return await invokeFunction("mdm-send-as-agent", { action: "list_agents" });
96
+ }
97
+
98
+ export const mdmAsAgentTools: MosaddTool[] = [
99
+ {
100
+ name: "mDM_send_as_agent",
101
+ title: "Send DM as your agent",
102
+ annotations: { readOnlyHint: false },
103
+ requires: "network",
104
+ description:
105
+ "Send a direct message AS ONE OF YOUR OWN AGENTS instead of as yourself. Use this when you are an " +
106
+ "agent runtime (Dispatch, Hermes, a CTO agent) connected with your owner's key: without it every " +
107
+ "message you send appears to come FROM the owner, so the owner has no thread with you. Names the " +
108
+ "agent by mosADD address, identity id or display name; `to` defaults to the key's owner. The server " +
109
+ "verifies you OWN that agent — speaking as someone else's agent, or as another person, is refused. " +
110
+ "Recipient must be a person: agent-to-agent DM stays blocked.",
111
+ inputSchema: mDM_send_as_agent_input,
112
+ handler: mDM_send_as_agent as MosaddTool["handler"],
113
+ },
114
+ {
115
+ name: "mDM_list_my_agents",
116
+ title: "List your agents",
117
+ annotations: { readOnlyHint: true },
118
+ requires: "network",
119
+ description:
120
+ "List the agents you are allowed to speak as with mDM_send_as_agent — every live agent owned by " +
121
+ "the account behind this key, with its identity id, mosADD address and display name. Call this " +
122
+ "first when you are unsure which agent identity you hold.",
123
+ inputSchema: mDM_list_my_agents_input,
124
+ handler: mDM_list_my_agents as MosaddTool["handler"],
125
+ },
126
+ ];