@mosadd/mcp 3.0.0-alpha.3 → 3.0.0-alpha.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +224 -0
- package/README.md +150 -116
- package/dist/bin/mcp.js +42 -3
- package/dist/chunk-P4ENU5MW.js +2556 -0
- package/dist/index.d.ts +99 -7
- package/dist/index.js +7 -3
- package/package.json +10 -7
- package/src/__tests__/golden-path.test.ts +137 -132
- package/src/__tests__/integration.test.ts +179 -175
- package/src/__tests__/mdm-e2ee.test.ts +212 -149
- package/src/__tests__/mtalk.test.ts +122 -0
- package/src/__tests__/server.test.ts +71 -67
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -97
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +502 -378
- package/src/index.ts +19 -17
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +116 -0
- package/src/providers/memory-voice.ts +79 -0
- package/src/providers/supabase-dm.ts +122 -117
- package/src/providers/supabase-voice.ts +91 -0
- package/src/providers/supabase.ts +148 -96
- package/src/server.ts +123 -103
- package/src/tools/actions.ts +162 -0
- package/src/tools/attachments.ts +268 -0
- package/src/tools/capabilities.ts +68 -68
- package/src/tools/embed.ts +108 -0
- package/src/tools/index.ts +104 -37
- package/src/tools/knowledge.ts +191 -0
- package/src/tools/mail-aliases.ts +25 -0
- package/src/tools/mail-provenance.ts +135 -0
- package/src/tools/mail.ts +381 -89
- package/src/tools/mcall.ts +216 -0
- package/src/tools/mdm-voice.ts +212 -0
- package/src/tools/mdm.ts +479 -379
- package/src/tools/mirc-edge.ts +190 -0
- package/src/tools/mirc-members.ts +198 -177
- package/src/tools/mirc-messages.ts +155 -0
- package/src/tools/mirc.ts +190 -170
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -189
- package/src/tools/mtalk.ts +121 -0
- package/src/tools/murl.ts +298 -0
- package/src/tools/ptt-ingest.ts +97 -0
- package/src/tools/threat.ts +119 -0
- package/src/types.ts +94 -79
- package/src/voice/floor.ts +132 -0
- package/dist/chunk-HD5RLE5K.js +0 -1193
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mURL — IRC-for-URLs: real-time chat attached to any web DOMAIN, AGENT-NATIVE.
|
|
3
|
+
*
|
|
4
|
+
* Same mosadd-edge Cloudflare Worker / Durable Object backend as mIRC edge, but the
|
|
5
|
+
* channel id is the URL SLUG: host("."→"-") + path("/"→"__") (RFC-0004). A bare domain
|
|
6
|
+
* → the domain room; a full URL with a path → a per-PAGE room (the per-article ask).
|
|
7
|
+
* Auto-created on first activity (status "open"); the domain owner can verify/claim the
|
|
8
|
+
* whole domain (a domain block covers all its page rooms).
|
|
9
|
+
*
|
|
10
|
+
* THE WEDGE: every prior "chat on a web page" (Google Sidewiki, Genius, Hypothes.is,
|
|
11
|
+
* HERE.fm) died on the empty-room cold-start because they were HUMAN-ONLY — you arrive,
|
|
12
|
+
* nobody's there, you leave. mURL is agent-native: an AI agent researching a domain can
|
|
13
|
+
* READ the channel (human + agent context — "is this a scam?", "promo code here") and
|
|
14
|
+
* WRITE findings back, so the room is never empty. First per-domain chat where agents are
|
|
15
|
+
* first-class citizens.
|
|
16
|
+
*
|
|
17
|
+
* Auth: MOSADD_API_KEY (hub key) for read/post; presence is public.
|
|
18
|
+
*/
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
21
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
22
|
+
|
|
23
|
+
const DEFAULT_EDGE_URL = "https://mosadd-edge.mr-brics-33.workers.dev";
|
|
24
|
+
function edgeUrl(): string {
|
|
25
|
+
return process.env.MOSADD_EDGE_URL?.replace(/\/$/, "") ?? DEFAULT_EDGE_URL;
|
|
26
|
+
}
|
|
27
|
+
function hubKey(): string {
|
|
28
|
+
const key = process.env.MOSADD_API_KEY ?? process.env.MOSADD_HUB_KEY ?? "";
|
|
29
|
+
if (!key) throw new Error("MOSADD_API_KEY required for mURL tools (see `mosadd login` / hub-keys).");
|
|
30
|
+
return key;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A domain OR full URL → { domain, slug }. mURL is keyed PER-URL (RFC-0004):
|
|
34
|
+
* slug = host("."→"-") + each path segment ("/"→"__"); scheme, query, fragment and
|
|
35
|
+
* trailing slash are stripped and segments are sanitised to [a-z0-9-]. A bare domain
|
|
36
|
+
* (no path) yields the DOMAIN room (back-compat); a URL with a path yields a PER-PAGE
|
|
37
|
+
* room. `domain` (host only) is returned for status/branding — a domain block covers
|
|
38
|
+
* every page room under it. */
|
|
39
|
+
function urlToSlug(input: string): { domain: string; slug: string } {
|
|
40
|
+
let s = String(input).trim().toLowerCase();
|
|
41
|
+
s = s.replace(/^[a-z]+:\/\//, "").replace(/[?#].*$/, ""); // strip scheme + query + fragment
|
|
42
|
+
const slash = s.indexOf("/");
|
|
43
|
+
let host = (slash >= 0 ? s.slice(0, slash) : s).replace(/:\d+$/, "");
|
|
44
|
+
if (host.startsWith("www.")) host = host.slice(4);
|
|
45
|
+
if (!host || !/^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(host)) {
|
|
46
|
+
throw new Error(`Invalid URL: "${input}" — pass a domain like "example.com" or a full URL.`);
|
|
47
|
+
}
|
|
48
|
+
const segs = (slash >= 0 ? s.slice(slash) : "")
|
|
49
|
+
.split("/")
|
|
50
|
+
.map((seg) => seg.replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, ""))
|
|
51
|
+
.filter(Boolean);
|
|
52
|
+
const base = host.replace(/\./g, "-");
|
|
53
|
+
return { domain: host, slug: segs.length ? `${base}__${segs.join("__")}` : base };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const DomainArg = z
|
|
57
|
+
.string()
|
|
58
|
+
.min(3)
|
|
59
|
+
.max(2048)
|
|
60
|
+
.describe("A domain ('example.com') or a full URL. Keyed PER-URL: a bare domain → the domain room; a URL with a path (e.g. 'site.com/articles/abc') → that page's own room.");
|
|
61
|
+
|
|
62
|
+
// ── read ──────────────────────────────────────────────────────────────────────
|
|
63
|
+
const mURL_read_channel_input = z.object({
|
|
64
|
+
domain: DomainArg,
|
|
65
|
+
limit: z.number().int().min(1).max(100).optional().describe("Recent messages to return (max 100). Default 50."),
|
|
66
|
+
});
|
|
67
|
+
async function mURL_read_channel(
|
|
68
|
+
input: z.infer<typeof mURL_read_channel_input>,
|
|
69
|
+
ctx: MosaddToolContext,
|
|
70
|
+
): Promise<{ domain: string; messages: Array<{ id: string; from: string | null; text: string; timestamp: number }> }> {
|
|
71
|
+
const { domain, slug } = urlToSlug(input.domain);
|
|
72
|
+
const limit = input.limit ?? 50;
|
|
73
|
+
ctx.log("debug", "mURL_read_channel", { domain });
|
|
74
|
+
const r = await fetch(`${edgeUrl()}/c/${slug}/history?limit=${limit}`, {
|
|
75
|
+
headers: { Authorization: `Bearer ${hubKey()}` },
|
|
76
|
+
});
|
|
77
|
+
if (!r.ok) {
|
|
78
|
+
const e = await r.text().catch(() => "");
|
|
79
|
+
throw new Error(`mosadd-edge ${r.status}: ${e.slice(0, 200)}`);
|
|
80
|
+
}
|
|
81
|
+
const data = (await r.json()) as { messages?: Array<{ id: string; ts: number; from?: string | null; text: string }> };
|
|
82
|
+
return {
|
|
83
|
+
domain,
|
|
84
|
+
messages: (data.messages ?? []).map((m) => ({ id: m.id, from: m.from ?? null, text: m.text, timestamp: m.ts })),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ── post ──────────────────────────────────────────────────────────────────────
|
|
89
|
+
const mURL_post_input = z.object({
|
|
90
|
+
domain: DomainArg,
|
|
91
|
+
text: z.string().min(1).max(2000).describe("Message body (max 2000 chars, up to 3 lines — Worker-enforced)."),
|
|
92
|
+
from: z.string().max(120).optional().describe("Sender hint shown to others (defaults to the hub-key owner / your agent name)."),
|
|
93
|
+
});
|
|
94
|
+
async function mURL_post(
|
|
95
|
+
input: z.infer<typeof mURL_post_input>,
|
|
96
|
+
ctx: MosaddToolContext,
|
|
97
|
+
): Promise<{ domain: string; message_id: string; posted_at: number }> {
|
|
98
|
+
const { domain, slug } = urlToSlug(input.domain);
|
|
99
|
+
ctx.log("debug", "mURL_post", { domain });
|
|
100
|
+
const r = await fetch(`${edgeUrl()}/c/${slug}/send`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${hubKey()}` },
|
|
103
|
+
body: JSON.stringify({ text: input.text, from: input.from }),
|
|
104
|
+
});
|
|
105
|
+
if (!r.ok) {
|
|
106
|
+
const e = await r.text().catch(() => "");
|
|
107
|
+
throw new Error(`mosadd-edge ${r.status}: ${e.slice(0, 200)}`);
|
|
108
|
+
}
|
|
109
|
+
const data = (await r.json()) as { id?: string; ts?: number };
|
|
110
|
+
if (!data?.id) throw new Error("mosadd-edge: send returned no id");
|
|
111
|
+
return { domain, message_id: data.id, posted_at: data.ts ?? Date.now() };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ── list_channels (discovery) ────────────────────────────────────────────────
|
|
115
|
+
const mURL_list_channels_input = z.object({
|
|
116
|
+
action: z
|
|
117
|
+
.enum(["trending", "list", "mine"])
|
|
118
|
+
.optional()
|
|
119
|
+
.describe("'trending' (default) = most active domain channels in the last N hours; 'list' = paginated catalogue filtered by status; 'mine' = channels you own (DNS-verified)."),
|
|
120
|
+
limit: z.number().int().min(1).max(100).optional().describe("Max channels to return. Default 20."),
|
|
121
|
+
hours: z.number().int().min(1).max(168).optional().describe("Trending window in hours. Default 24, max 168 (7d)."),
|
|
122
|
+
status: z.enum(["open", "claimed", "blocked"]).optional().describe("For action='list': filter (default 'open')."),
|
|
123
|
+
cursor: z.string().optional().describe("For action='list': pagination — pass previous response's next_cursor (created_at ISO)."),
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
interface MurlChannel {
|
|
127
|
+
domain: string;
|
|
128
|
+
slug: string;
|
|
129
|
+
status: string;
|
|
130
|
+
branding?: unknown;
|
|
131
|
+
verified_at?: string | null;
|
|
132
|
+
created_at?: string;
|
|
133
|
+
updated_at?: string;
|
|
134
|
+
messages_in_window?: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function mURL_list_channels(
|
|
138
|
+
input: z.infer<typeof mURL_list_channels_input>,
|
|
139
|
+
ctx: MosaddToolContext,
|
|
140
|
+
): Promise<{
|
|
141
|
+
channels: MurlChannel[];
|
|
142
|
+
count: number;
|
|
143
|
+
next_cursor?: string | null;
|
|
144
|
+
hours?: number;
|
|
145
|
+
since?: string;
|
|
146
|
+
}> {
|
|
147
|
+
readSupabaseEnv();
|
|
148
|
+
const action = input.action ?? "trending";
|
|
149
|
+
ctx.log("debug", "mURL_list_channels invoking murl-channels", { action });
|
|
150
|
+
return await invokeFunction("murl-channels", {
|
|
151
|
+
action,
|
|
152
|
+
limit: input.limit ?? null,
|
|
153
|
+
hours: input.hours ?? null,
|
|
154
|
+
status: input.status ?? null,
|
|
155
|
+
cursor: input.cursor ?? null,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ── presence ────────────────────────────────────────────────────────────────────
|
|
160
|
+
const mURL_presence_input = z.object({ domain: DomainArg });
|
|
161
|
+
async function mURL_presence(
|
|
162
|
+
input: z.infer<typeof mURL_presence_input>,
|
|
163
|
+
ctx: MosaddToolContext,
|
|
164
|
+
): Promise<{ domain: string; count: number; roster: string[]; status: string }> {
|
|
165
|
+
const { domain, slug } = urlToSlug(input.domain);
|
|
166
|
+
ctx.log("debug", "mURL_presence", { domain });
|
|
167
|
+
const r = await fetch(`${edgeUrl()}/c/${slug}/presence`);
|
|
168
|
+
if (!r.ok) {
|
|
169
|
+
const e = await r.text().catch(() => "");
|
|
170
|
+
throw new Error(`mosadd-edge ${r.status}: ${e.slice(0, 200)}`);
|
|
171
|
+
}
|
|
172
|
+
const data = (await r.json()) as { count?: number; roster?: string[]; status?: string };
|
|
173
|
+
return { domain, count: data.count ?? 0, roster: data.roster ?? [], status: data.status ?? "open" };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ── create / claim (owner-side) ────────────────────────────────────────────────
|
|
177
|
+
const mURL_create_input = z.object({
|
|
178
|
+
domain: DomainArg,
|
|
179
|
+
branding: z
|
|
180
|
+
.record(z.string(), z.any())
|
|
181
|
+
.optional()
|
|
182
|
+
.describe("Optional branding JSON (e.g. display name, colour, avatar URL) shown on the channel."),
|
|
183
|
+
status: z
|
|
184
|
+
.enum(["open", "claimed", "blocked"])
|
|
185
|
+
.optional()
|
|
186
|
+
.describe("Initial status: 'open' (default) = public agent-native room; 'claimed' = registered as yours; 'blocked' = closed."),
|
|
187
|
+
});
|
|
188
|
+
async function mURL_create(
|
|
189
|
+
input: z.infer<typeof mURL_create_input>,
|
|
190
|
+
ctx: MosaddToolContext,
|
|
191
|
+
): Promise<{ channel: MurlChannel }> {
|
|
192
|
+
readSupabaseEnv();
|
|
193
|
+
const { domain } = urlToSlug(input.domain);
|
|
194
|
+
ctx.log("debug", "mURL_create invoking murl-manage", { domain });
|
|
195
|
+
return await invokeFunction("murl-manage", {
|
|
196
|
+
action: "create",
|
|
197
|
+
domain,
|
|
198
|
+
branding: input.branding ?? null,
|
|
199
|
+
status: input.status ?? null,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ── update (claim status / block / branding) ────────────────────────────────────
|
|
204
|
+
const mURL_update_input = z.object({
|
|
205
|
+
domain: DomainArg,
|
|
206
|
+
branding: z.record(z.string(), z.any()).optional().describe("New branding JSON to set on the channel."),
|
|
207
|
+
status: z
|
|
208
|
+
.enum(["open", "claimed", "blocked"])
|
|
209
|
+
.optional()
|
|
210
|
+
.describe("New status: 'claimed' to mark owned, 'blocked' to close the room, 'open' to reopen."),
|
|
211
|
+
});
|
|
212
|
+
async function mURL_update(
|
|
213
|
+
input: z.infer<typeof mURL_update_input>,
|
|
214
|
+
ctx: MosaddToolContext,
|
|
215
|
+
): Promise<{ channel: MurlChannel }> {
|
|
216
|
+
readSupabaseEnv();
|
|
217
|
+
const { domain } = urlToSlug(input.domain);
|
|
218
|
+
const slug = domain.replace(/\./g, "-");
|
|
219
|
+
ctx.log("debug", "mURL_update invoking murl-manage", { domain });
|
|
220
|
+
return await invokeFunction("murl-manage", {
|
|
221
|
+
action: "update",
|
|
222
|
+
slug,
|
|
223
|
+
branding: input.branding ?? null,
|
|
224
|
+
status: input.status ?? null,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ── delete (owner-side) ─────────────────────────────────────────────────────────
|
|
229
|
+
const mURL_delete_input = z.object({ domain: DomainArg });
|
|
230
|
+
async function mURL_delete(
|
|
231
|
+
input: z.infer<typeof mURL_delete_input>,
|
|
232
|
+
ctx: MosaddToolContext,
|
|
233
|
+
): Promise<{ ok: boolean }> {
|
|
234
|
+
readSupabaseEnv();
|
|
235
|
+
const { domain } = urlToSlug(input.domain);
|
|
236
|
+
const slug = domain.replace(/\./g, "-");
|
|
237
|
+
ctx.log("debug", "mURL_delete invoking murl-manage", { domain });
|
|
238
|
+
return await invokeFunction("murl-manage", { action: "delete", slug });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export const murlTools: MosaddTool[] = [
|
|
242
|
+
{
|
|
243
|
+
name: "mURL_read_channel",
|
|
244
|
+
requires: "network",
|
|
245
|
+
description:
|
|
246
|
+
"Read the live chat channel for a web DOMAIN (mURL — 'IRC for URLs'). Returns the most recent messages humans and agents have posted about that domain. Use it to get real-time, crowd-sourced context before acting on a site — e.g. scam warnings, promo codes, outage reports, 'is this legit?'. The channel auto-exists for any domain. Auth: MOSADD_API_KEY (hub key).",
|
|
247
|
+
inputSchema: mURL_read_channel_input,
|
|
248
|
+
handler: mURL_read_channel as MosaddTool["handler"],
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
name: "mURL_post",
|
|
252
|
+
requires: "network",
|
|
253
|
+
description:
|
|
254
|
+
"Post a message into a web DOMAIN's mURL chat channel — leave context for the humans and agents who visit that site next (a finding, warning, tip, or coordination note). Sub-100ms global fan-out to everyone currently on the channel. Auth: MOSADD_API_KEY (hub key).",
|
|
255
|
+
inputSchema: mURL_post_input,
|
|
256
|
+
handler: mURL_post as MosaddTool["handler"],
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
name: "mURL_presence",
|
|
260
|
+
requires: "network",
|
|
261
|
+
description:
|
|
262
|
+
"Who is on a domain's mURL channel right now: live participant count + roster nicknames + channel status (open/claimed/blocked). Public (no key needed). Use it to gauge whether a domain has an active audience before posting.",
|
|
263
|
+
inputSchema: mURL_presence_input,
|
|
264
|
+
handler: mURL_presence as MosaddTool["handler"],
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
name: "mURL_list_channels",
|
|
268
|
+
requires: "network",
|
|
269
|
+
description:
|
|
270
|
+
"Discovery for mURL domain channels: 'trending' (most active in the last N hours, default 24) / 'list' (paginated catalogue, status-filtered) / 'mine' (channels you DNS-verified). Returns domain, slug, status, branding, verified_at + (for trending) messages_in_window. For who's-on-now, follow up with mURL_presence on the slug.",
|
|
271
|
+
inputSchema: mURL_list_channels_input,
|
|
272
|
+
handler: mURL_list_channels as MosaddTool["handler"],
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
name: "mURL_create",
|
|
276
|
+
requires: "network",
|
|
277
|
+
description:
|
|
278
|
+
"Create / claim the mURL channel for a web DOMAIN and register YOU as its owner (idempotent — returns the existing channel if you already own it; fails with 'claimed_by_other' if another account owns it). Optionally set initial branding + status. Owner-side: needs your mosADD login session (user JWT via `mosadd login`), not just a hub key — same as mURL_list_channels action='mine'. Backend: murl-manage EF.",
|
|
279
|
+
inputSchema: mURL_create_input,
|
|
280
|
+
handler: mURL_create as MosaddTool["handler"],
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
name: "mURL_update",
|
|
284
|
+
requires: "network",
|
|
285
|
+
description:
|
|
286
|
+
"Update a mURL DOMAIN channel you own: set branding, mark it 'claimed', 'blocked' (close the room), or reopen it as 'open'. Owner-scoped — you must own the channel (claim it first with mURL_create). Needs your mosADD login session. Backend: murl-manage EF.",
|
|
287
|
+
inputSchema: mURL_update_input,
|
|
288
|
+
handler: mURL_update as MosaddTool["handler"],
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
name: "mURL_delete",
|
|
292
|
+
requires: "network",
|
|
293
|
+
description:
|
|
294
|
+
"Delete a mURL DOMAIN channel you own (removes its domain_controls row; the room stops being served). Owner-scoped — you must own it. Needs your mosADD login session. Backend: murl-manage EF.",
|
|
295
|
+
inputSchema: mURL_delete_input,
|
|
296
|
+
handler: mURL_delete as MosaddTool["handler"],
|
|
297
|
+
},
|
|
298
|
+
];
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mTALK_ingest_ptt — submit a recorded PTT "over" for transcription + RAG ingest.
|
|
3
|
+
*
|
|
4
|
+
* The actual mIRC/mDM voice-message PERSISTENCE (writing a message_meta row,
|
|
5
|
+
* fanning it out to listeners) is owned by the message-send/send-voice path,
|
|
6
|
+
* NOT by this tool. ptt-ingest exists purely to bridge a finished PTT segment
|
|
7
|
+
* into the user's RAG index — a row in `ptt_transcripts` (status='pending')
|
|
8
|
+
* + a transcribe job that lands in `user_embeddings(source_type='ptt')`.
|
|
9
|
+
*
|
|
10
|
+
* Use this when an agent has a PTT audio buffer it wants searchable in the
|
|
11
|
+
* user's RAG later (e.g. "what did Alice tell me on PTT yesterday?"). The
|
|
12
|
+
* voice message itself is sent separately via mDM_send_voice / mIRC_send_voice.
|
|
13
|
+
*
|
|
14
|
+
* Auth: standard Supabase session JWT (the caller's RAG opt-in is read from
|
|
15
|
+
* their account; opted-out callers get status='skipped_optout').
|
|
16
|
+
* LINEAR-3998 (P4.1 PTT unify) — re-arch 2026-06-27.
|
|
17
|
+
*/
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
20
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
21
|
+
|
|
22
|
+
const mTALK_ingest_ptt_input = z.object({
|
|
23
|
+
audio_base64: z
|
|
24
|
+
.string()
|
|
25
|
+
.min(1)
|
|
26
|
+
.max(20_000_000)
|
|
27
|
+
.describe("Recorded PTT 'over' as base64 (a 'data:audio/…;base64,…' prefix is stripped if present)."),
|
|
28
|
+
mime_type: z
|
|
29
|
+
.string()
|
|
30
|
+
.min(1)
|
|
31
|
+
.max(120)
|
|
32
|
+
.default("audio/webm")
|
|
33
|
+
.describe("MIME type of the audio. Default audio/webm (browser MediaRecorder default)."),
|
|
34
|
+
direction: z
|
|
35
|
+
.enum(["human_to_agent", "agent_to_human"])
|
|
36
|
+
.describe("Direction the PTT crossed: who originated, who received. 'human_to_agent' = user spoke to an agent; 'agent_to_human' = agent spoke to the user."),
|
|
37
|
+
message_id: z
|
|
38
|
+
.string()
|
|
39
|
+
.min(1)
|
|
40
|
+
.max(120)
|
|
41
|
+
.optional()
|
|
42
|
+
.describe("Optional id of the messages_meta row this PTT belongs to. When present, links the transcript back to the message for jump-to-source from RAG."),
|
|
43
|
+
thread_id: z
|
|
44
|
+
.string()
|
|
45
|
+
.min(1)
|
|
46
|
+
.max(120)
|
|
47
|
+
.optional()
|
|
48
|
+
.describe("Optional thread id (e.g. `chat:<channel_id>` or `dm:<a>:<b>`) so the transcript carries its conversational scope into RAG."),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
interface PttIngestResponse {
|
|
52
|
+
ptt_id?: string;
|
|
53
|
+
status?: "pending" | "skipped_optout" | "queued";
|
|
54
|
+
enqueued?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function mTALK_ingest_ptt(
|
|
58
|
+
input: z.infer<typeof mTALK_ingest_ptt_input>,
|
|
59
|
+
ctx: MosaddToolContext,
|
|
60
|
+
): Promise<{ ptt_id: string; status: string; opted_in: boolean }> {
|
|
61
|
+
readSupabaseEnv();
|
|
62
|
+
|
|
63
|
+
// Tolerate a "data:audio/…;base64,…" prefix from MediaRecorder/canvas captures.
|
|
64
|
+
const audio = input.audio_base64.replace(/^data:[^;]+;base64,/, "");
|
|
65
|
+
|
|
66
|
+
ctx.log("debug", "mTALK_ingest_ptt invoking ptt-ingest", {
|
|
67
|
+
direction: input.direction,
|
|
68
|
+
thread_id: input.thread_id ?? null,
|
|
69
|
+
mime_type: input.mime_type,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const res = await invokeFunction<PttIngestResponse>("ptt-ingest", {
|
|
73
|
+
audio_base64: audio,
|
|
74
|
+
mime_type: input.mime_type,
|
|
75
|
+
direction: input.direction,
|
|
76
|
+
message_id: input.message_id ?? null,
|
|
77
|
+
thread_id: input.thread_id ?? null,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const status = res.status ?? "pending";
|
|
81
|
+
return {
|
|
82
|
+
ptt_id: res.ptt_id ?? "",
|
|
83
|
+
status,
|
|
84
|
+
opted_in: status !== "skipped_optout",
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const pttIngestTools: MosaddTool[] = [
|
|
89
|
+
{
|
|
90
|
+
name: "mTALK_ingest_ptt",
|
|
91
|
+
requires: "network",
|
|
92
|
+
description:
|
|
93
|
+
"Submit a finished PTT 'over' (push-to-talk audio segment) for transcription into the caller's RAG index — so 'what did X say on PTT?' is searchable later. The PERSISTENCE of the voice message itself (the message_meta row + fan-out) is handled by mDM_send_voice / mIRC_send_voice; this tool is the transcription bridge only. Honors per-user RAG opt-in: opted-out callers get status='skipped_optout' (no transcript stored). Auth: Supabase session JWT.",
|
|
94
|
+
inputSchema: mTALK_ingest_ptt_input,
|
|
95
|
+
handler: mTALK_ingest_ptt as MosaddTool["handler"],
|
|
96
|
+
},
|
|
97
|
+
];
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* threat_* — defensive threat-event classification, exposed as MCP tools.
|
|
3
|
+
*
|
|
4
|
+
* Wraps `@mosadd/threat-engine` — the canonical mosadd threat-event taxonomy
|
|
5
|
+
* (`THREAT_EVENTS`, the full 160+ event catalog the radar scores against) plus
|
|
6
|
+
* the pure decision engine `evaluateEvent(event) → {action, severity, reason}`.
|
|
7
|
+
*
|
|
8
|
+
* These tools are PURE: deterministic, side-effect-free, no backend, no network.
|
|
9
|
+
* `requires: "any"` — they run anywhere (hosted hub, BYOK, fully offline). The
|
|
10
|
+
* engine DECIDES; the caller carries out any recommended action against its own
|
|
11
|
+
* backend. There is no surveillance, no data collection — it classifies an event
|
|
12
|
+
* the caller already has and returns a defensive recommendation.
|
|
13
|
+
*
|
|
14
|
+
* Founder decision (LINEAR-3498): the catalog is real and must be a real MCP
|
|
15
|
+
* tool, not detoxed away. (The AUDIT-B detox concerns the dead "anti-Pegasus"
|
|
16
|
+
* MARKETING framing, not this classification engine.)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
21
|
+
import {
|
|
22
|
+
THREAT_EVENTS,
|
|
23
|
+
THREAT_EVENT_COUNT,
|
|
24
|
+
evaluateEvent,
|
|
25
|
+
getEventDef,
|
|
26
|
+
type ThreatEventDef,
|
|
27
|
+
} from "@mosadd/threat-engine";
|
|
28
|
+
|
|
29
|
+
// ── threat_catalog ──
|
|
30
|
+
|
|
31
|
+
const threat_catalog_input = z.object({
|
|
32
|
+
category: z
|
|
33
|
+
.enum(["COMINT", "ELINT", "MASINT", "CYBER", "BEHAVIORAL", "PRIVACY", "SIGINT", "OSINT", "MPOST"])
|
|
34
|
+
.optional()
|
|
35
|
+
.describe("Filter the catalog to one intelligence-discipline category. Omit for the full catalog."),
|
|
36
|
+
platform: z
|
|
37
|
+
.enum(["apk", "ios", "pwa", "harmony"])
|
|
38
|
+
.optional()
|
|
39
|
+
.describe("Filter to events detectable on this platform. Omit for all platforms."),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
async function threat_catalog(
|
|
43
|
+
input: z.infer<typeof threat_catalog_input>,
|
|
44
|
+
_ctx: MosaddToolContext,
|
|
45
|
+
): Promise<{ count: number; total: number; events: ThreatEventDef[] }> {
|
|
46
|
+
let events = THREAT_EVENTS;
|
|
47
|
+
if (input.category) events = events.filter((e) => e.category === input.category);
|
|
48
|
+
if (input.platform) events = events.filter((e) => e.platforms.includes(input.platform!));
|
|
49
|
+
return { count: events.length, total: THREAT_EVENT_COUNT, events };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ── threat_classify ──
|
|
53
|
+
|
|
54
|
+
const threat_classify_input = z.object({
|
|
55
|
+
event_type: z
|
|
56
|
+
.string()
|
|
57
|
+
.min(1)
|
|
58
|
+
.max(120)
|
|
59
|
+
.describe("Event type to classify, e.g. \"SIM_SWAP\", \"AUTH_BRUTEFORCE\", \"TOLL_FRAUD\". Case-insensitive."),
|
|
60
|
+
severity: z
|
|
61
|
+
.string()
|
|
62
|
+
.optional()
|
|
63
|
+
.describe("Optional producer severity hint. \"killswitch\" → lock_account; \"critical\" → revoke_sessions; anything else is informational."),
|
|
64
|
+
details: z.string().max(2_000).optional().describe("Optional free-text context (informational only)."),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
async function threat_classify(
|
|
68
|
+
input: z.infer<typeof threat_classify_input>,
|
|
69
|
+
_ctx: MosaddToolContext,
|
|
70
|
+
): Promise<{
|
|
71
|
+
action: string;
|
|
72
|
+
severity: string;
|
|
73
|
+
reason: string;
|
|
74
|
+
known_event: boolean;
|
|
75
|
+
category: string | null;
|
|
76
|
+
label: string | null;
|
|
77
|
+
}> {
|
|
78
|
+
// Known taxonomy event → surface ITS severity immediately. Pegasus-class events
|
|
79
|
+
// (PROCESS_INJECTION, STINGRAY_DETECT, IMSI_CATCHER_SCAN, MEMORY_INJECTION, SS7_PROBE…)
|
|
80
|
+
// classify as critical/killswitch right away. But the engine NEVER auto-acts:
|
|
81
|
+
// action is always "monitor" — it tells you, it never disconnects, locks, or wipes.
|
|
82
|
+
// The taxonomy's own autoActions are what a stricter host policy COULD do; we do
|
|
83
|
+
// not execute them here ("monitoruje, nie działa").
|
|
84
|
+
const def = getEventDef(String(input.event_type).toUpperCase());
|
|
85
|
+
if (def) {
|
|
86
|
+
return {
|
|
87
|
+
action: "monitor",
|
|
88
|
+
severity: def.severity,
|
|
89
|
+
reason: `${def.label} (${def.category}) — classified from the mosadd threat taxonomy. Monitor-only: surfaced for you to act on, no automatic action taken.`,
|
|
90
|
+
known_event: true,
|
|
91
|
+
category: def.category,
|
|
92
|
+
label: def.label,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// Unknown event string → fall back to the pure DECK decision engine (unchanged).
|
|
96
|
+
const decision = evaluateEvent({ eventType: input.event_type, severity: input.severity, details: input.details });
|
|
97
|
+
return { ...decision, known_event: false, category: null, label: null };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ── Registration ──
|
|
101
|
+
|
|
102
|
+
export const threatTools: MosaddTool[] = [
|
|
103
|
+
{
|
|
104
|
+
name: "threat_catalog",
|
|
105
|
+
requires: "any",
|
|
106
|
+
description:
|
|
107
|
+
"List the canonical mosadd threat-event taxonomy (the full radar catalog: id, category, default severity, auto-actions, per-platform availability). Filter by category or platform. Pure/offline — returns { count, total, events }. Use to discover what the defensive engine recognises.",
|
|
108
|
+
inputSchema: threat_catalog_input,
|
|
109
|
+
handler: threat_catalog as MosaddTool["handler"],
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "threat_classify",
|
|
113
|
+
requires: "any",
|
|
114
|
+
description:
|
|
115
|
+
"Classify a single telemetry event against the mosadd threat taxonomy + DECK engine. For a known event type (e.g. PROCESS_INJECTION, STINGRAY_DETECT, IMSI_CATCHER_SCAN, MEMORY_INJECTION) it returns that event's real severity (info|warning|elevated|critical|killswitch) with action=\"monitor\" — Pegasus-class events surface as critical immediately, but the engine NEVER auto-acts (it tells you, it never disconnects/locks). Unknown strings fall back to the pure DECK decision (log_only|revoke_sessions|lock_account|suspend_did). Returns { action, severity, reason, known_event, category, label }. Deterministic, offline, no backend.",
|
|
116
|
+
inputSchema: threat_classify_input,
|
|
117
|
+
handler: threat_classify as MosaddTool["handler"],
|
|
118
|
+
},
|
|
119
|
+
];
|