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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  MCP server for [mosadd](https://mosadd.dev) — exposes the OS modules (m\*) as Model Context Protocol tools so any agent runtime can talk to mosadd: Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, Bolt, Goose, Manus, custom.
4
4
 
5
- > **3.0.0-alpha.4** — **52 live tools across 6 live modules** (mDM incl. voice, mIRC, mROOM, mAIL, mTALK, mRAG) + the `comms_capabilities` discovery tool, wired to the mosadd backend (BYOK) as a strangler-fig step. Phase 2 routes through the hosted gateway at `mcp.mosadd.com` with the 167-event radar in front.
5
+ > **3.0.0-alpha.4** — **57 live tools across 6 live modules** (mDM incl. voice, mIRC, mROOM, mAIL, mTALK, mRAG) + the `comms_capabilities` discovery tool, wired to the mosadd backend (BYOK) as a strangler-fig step. Phase 2 routes through the hosted gateway at `mcp.mosadd.com` with the 167-event radar in front.
6
6
 
7
7
  ## Install
8
8
 
@@ -53,7 +53,7 @@ In Phase 2, run `mosadd login` to OAuth into hub.mosadd.com — no JWT-juggling
53
53
 
54
54
  ## Tools shipped in alpha
55
55
 
56
- **52 live tools across 6 live modules** (mDM, mIRC, mROOM, mAIL, mTALK, mRAG) + the `comms_capabilities` discovery tool. Highlights per module:
56
+ **57 live tools across 6 live modules** (mDM, mIRC, mROOM, mAIL, mTALK, mRAG) + the `comms_capabilities` discovery tool. Highlights per module:
57
57
 
58
58
  | Module | Tools | What it does |
59
59
  |---|---|---|
@@ -61,8 +61,8 @@ In Phase 2, run `mosadd login` to OAuth into hub.mosadd.com — no JWT-juggling
61
61
  | **mIRC** (20) | `mIRC_create/list/get/update/delete`, member RBAC ops, `mIRC_post_message`, `mIRC_list_messages` + admin | Persistent Discord/Slack-style channels |
62
62
  | **mROOM** (9) | `mROOM_create`, **`mROOM_create_guest_link`**, `mROOM_join/leave/close/list`, `mROOM_send_message`, `mROOM_list_messages` | Ephemeral rooms + single-call no-account guest links |
63
63
  | **mTALK** (5) | `mTALK_open`, `mTALK_join`, `mTALK_press`, `mTALK_release`, `mTALK_state` | Half-duplex push-to-talk: one speaker, FIFO queue, anti-hog auto-release |
64
- | **mAIL** (4) | `mAIL_send`, `mAIL_view`, `mAIL_list`, `mAIL_delete` | Mail; every user gets `<id>@mosadd.com` |
65
- | **mRAG** (2) | `mRAG_ingest`, `mRAG_search` | RAG recall over the user's own data (hybrid vector + BM25) |
64
+ | **mAIL** (7) | `mAIL_send`, `mAIL_view`, `mAIL_list`, `mAIL_delete`, `mAIL_stats`, `mAIL_events`, `mAIL_metrics` | Mail; every user gets `<id>@mosadd.com` |
65
+ | **mRAG** (4) | `mRAG_ingest`, `mRAG_search`, `mRAG_list_sources`, `mRAG_delete` | RAG recall over the user's own data (hybrid vector + BM25) |
66
66
 
67
67
  All tool names follow [RFC 0001](https://github.com/Hei33enberg/mosadd-os/blob/main/docs/rfcs/0001-module-naming.md) — `m<MODULE>_<operation>` snake_case.
68
68
 
package/dist/bin/mcp.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  createMosaddServer
4
- } from "../chunk-4C36JDTJ.js";
4
+ } from "../chunk-EFTB743G.js";
5
5
  import "../chunk-Z7WR52GE.js";
6
6
 
7
7
  // src/bin/mcp.ts
@@ -3,11 +3,13 @@ import {
3
3
  loadSession
4
4
  } from "./chunk-Z7WR52GE.js";
5
5
 
6
- // src/tools/mdm.ts
7
- import { z } from "zod";
8
-
9
6
  // src/providers/supabase.ts
7
+ import { AsyncLocalStorage } from "async_hooks";
10
8
  import { createClient } from "@supabase/supabase-js";
9
+ var requestEnv = new AsyncLocalStorage();
10
+ function runWithSupabaseEnv(env, fn) {
11
+ return requestEnv.run(env, fn);
12
+ }
11
13
  var MissingSupabaseEnvError = class extends Error {
12
14
  constructor(missing) {
13
15
  super(
@@ -17,6 +19,8 @@ var MissingSupabaseEnvError = class extends Error {
17
19
  }
18
20
  };
19
21
  function readSupabaseEnv() {
22
+ const perRequest = requestEnv.getStore();
23
+ if (perRequest) return perRequest;
20
24
  const env = (canonical, legacy) => process.env[canonical] ?? process.env[legacy];
21
25
  const userJwtEnv = env("MOSADD_USER_JWT", "M0SSAD_USER_JWT");
22
26
  const session = loadSession();
@@ -57,6 +61,9 @@ async function invokeFunction(fn, body) {
57
61
  return data;
58
62
  }
59
63
 
64
+ // src/tools/mdm.ts
65
+ import { z } from "zod";
66
+
60
67
  // src/crypto/mdm-session.ts
61
68
  import {
62
69
  decryptBytes,
@@ -1375,6 +1382,13 @@ var mAIL_list_input = z9.object({
1375
1382
  limit: z9.number().int().min(1).max(200).optional().describe("Max emails to return. Default 50."),
1376
1383
  before: z9.string().optional().describe("Pagination cursor: pass the previous response's next_cursor to get the next (older) page.")
1377
1384
  });
1385
+ var mAIL_stats_input = z9.object({
1386
+ message_id: MessageId
1387
+ });
1388
+ var mAIL_events_input = z9.object({
1389
+ message_id: MessageId
1390
+ });
1391
+ var mAIL_metrics_input = z9.object({});
1378
1392
  async function mAIL_send(input, ctx) {
1379
1393
  readSupabaseEnv();
1380
1394
  if (!input.body_text && !input.body_html) {
@@ -1404,6 +1418,23 @@ async function mAIL_delete(input, ctx) {
1404
1418
  message_id: input.message_id
1405
1419
  });
1406
1420
  }
1421
+ async function mAIL_stats(input, ctx) {
1422
+ readSupabaseEnv();
1423
+ ctx.log("debug", "mAIL_stats invoking mp0st-stats", { message_id: input.message_id });
1424
+ return await invokeFunction("mp0st-stats", {
1425
+ message_id: input.message_id
1426
+ });
1427
+ }
1428
+ async function mAIL_events(input, ctx) {
1429
+ readSupabaseEnv();
1430
+ ctx.log("debug", "mAIL_events invoking mp0st-stats", { message_id: input.message_id });
1431
+ return await invokeFunction("mp0st-stats", { message_id: input.message_id, events: true });
1432
+ }
1433
+ async function mAIL_metrics(_input, ctx) {
1434
+ readSupabaseEnv();
1435
+ ctx.log("debug", "mAIL_metrics invoking mp0st-stats");
1436
+ return await invokeFunction("mp0st-stats", {});
1437
+ }
1407
1438
  var mailTools = [
1408
1439
  {
1409
1440
  name: "mAIL_list",
@@ -1432,6 +1463,27 @@ var mailTools = [
1432
1463
  description: "Delete one of the user's emails by message_id (from mAIL_list). Soft-delete \u2014 it stops showing in mAIL_list. Owner-scoped: only your own mail.",
1433
1464
  inputSchema: mAIL_delete_input,
1434
1465
  handler: mAIL_delete
1466
+ },
1467
+ {
1468
+ name: "mAIL_stats",
1469
+ requires: "network",
1470
+ description: "Engagement summary for one sent email (by message_id): open_count, unique_opens, click_count, whether it was likely forwarded, first/last opened time, and a breakdown of tracked event types. Opens/clicks are real (tracking pixel + link-wrap); forward detection is best-effort (IP-subnet diversity). Owner-scoped.",
1471
+ inputSchema: mAIL_stats_input,
1472
+ handler: mAIL_stats
1473
+ },
1474
+ {
1475
+ name: "mAIL_events",
1476
+ requires: "network",
1477
+ description: "Raw engagement timeline for one email (by message_id): every tracked event (pixel_open, link_click, forwarded, page_view, copy/print/focus signals) with timestamp, device type and link URL. Use mAIL_stats for the rollup; use this for the per-event detail. Owner-scoped.",
1478
+ inputSchema: mAIL_events_input,
1479
+ handler: mAIL_events
1480
+ },
1481
+ {
1482
+ name: "mAIL_metrics",
1483
+ requires: "network",
1484
+ description: "Mailbox-wide engagement aggregate across the user's sent mail: total_sent, opened_emails, open_rate_pct, total_opens, total_clicks, forwarded_emails. Owner-scoped.",
1485
+ inputSchema: mAIL_metrics_input,
1486
+ handler: mAIL_metrics
1435
1487
  }
1436
1488
  ];
1437
1489
 
@@ -1551,6 +1603,27 @@ async function mRAG_ingest(input, ctx) {
1551
1603
  });
1552
1604
  return { indexed: data.indexed ?? 0, chunk_count: data.chunk_count ?? 0 };
1553
1605
  }
1606
+ var mRAG_list_sources_input = z11.object({});
1607
+ async function mRAG_list_sources(_input, ctx) {
1608
+ readSupabaseEnv();
1609
+ ctx.log("debug", "mRAG_list_sources via rag-sources");
1610
+ return await invokeFunction("rag-sources", { action: "list" });
1611
+ }
1612
+ var mRAG_delete_input = z11.object({
1613
+ source_id: z11.string().optional().describe("Delete every indexed chunk with this source_id (from mRAG_list_sources)."),
1614
+ thread_id: z11.string().optional().describe("Delete every indexed chunk associated with this thread_id.")
1615
+ }).refine((v) => v.source_id || v.thread_id, {
1616
+ message: "Provide source_id or thread_id \u2014 mRAG_delete refuses to purge the whole index."
1617
+ });
1618
+ async function mRAG_delete(input, ctx) {
1619
+ readSupabaseEnv();
1620
+ ctx.log("debug", "mRAG_delete via rag-sources", { source_id: input.source_id, thread_id: input.thread_id });
1621
+ return await invokeFunction("rag-sources", {
1622
+ action: "delete",
1623
+ source_id: input.source_id ?? null,
1624
+ thread_id: input.thread_id ?? null
1625
+ });
1626
+ }
1554
1627
  var knowledgeTools = [
1555
1628
  {
1556
1629
  name: "mRAG_ingest",
@@ -1565,6 +1638,20 @@ var knowledgeTools = [
1565
1638
  description: "Search and answer questions over the USER'S OWN mosadd data (messages, emails, calls, notes) with a private per-user RAG index \u2014 hybrid vector + keyword retrieval, reranked, grounded in cited sources (no fabrication). Use it to recall 'what did X say about Y', summarize a thread, or pull facts the user has received. Returns an answer plus the source snippets it used. This is agent memory (mRAG = RAG over your own data) over the user's communications, available outside the mosadd app. NOTE: RAG requires content to be indexed server-side in plaintext, so anything searchable here is NOT covered by the zero-knowledge / E2EE guarantee \u2014 only data the user has explicitly opted into indexing is searchable. See docs/security/e2ee-posture.md.",
1566
1639
  inputSchema: mRAG_search_input,
1567
1640
  handler: mRAG_search
1641
+ },
1642
+ {
1643
+ name: "mRAG_list_sources",
1644
+ requires: "network",
1645
+ description: "List what is currently indexed in the user's private knowledge base, grouped by source (source_type, source_id, thread_id, title) with per-source chunk counts and first/last indexed timestamps. Use it to see what mRAG_search can recall, and to find a source_id to remove with mRAG_delete. Owner-scoped.",
1646
+ inputSchema: mRAG_list_sources_input,
1647
+ handler: mRAG_list_sources
1648
+ },
1649
+ {
1650
+ name: "mRAG_delete",
1651
+ requires: "network",
1652
+ description: "Remove indexed content from the user's knowledge base by source_id or thread_id (from mRAG_list_sources). Deletes every embedded chunk for that source so mRAG_search can no longer recall it. Requires source_id or thread_id (will not purge the whole index). Returns the number of chunks deleted. Owner-scoped.",
1653
+ inputSchema: mRAG_delete_input,
1654
+ handler: mRAG_delete
1568
1655
  }
1569
1656
  ];
1570
1657
 
@@ -1830,6 +1917,7 @@ function shouldLog(level, threshold) {
1830
1917
  }
1831
1918
 
1832
1919
  export {
1920
+ runWithSupabaseEnv,
1833
1921
  allTools,
1834
1922
  defaultProviders,
1835
1923
  createMosaddServer
package/dist/index.d.ts CHANGED
@@ -186,7 +186,7 @@ declare function createMosaddServer(options?: MosaddServerOptions): McpServer;
186
186
  /**
187
187
  * Tool registry. Each m* module contributes a set of tools.
188
188
  *
189
- * Live modules (6): mDM, mIRC, mROOM, mTALK, mAIL, mRAG — 52 tools + comms_capabilities.
189
+ * Live modules (6): mDM, mIRC, mROOM, mTALK, mAIL, mRAG — 57 tools + comms_capabilities.
190
190
  * mCALL is carrier-pending (no telephony carrier live yet): its tools live in
191
191
  * tools/mcall.ts but are NOT registered here, so an agent only ever discovers
192
192
  * tools that actually work. Re-register the one line below when a carrier ships.
@@ -194,4 +194,23 @@ declare function createMosaddServer(options?: MosaddServerOptions): McpServer;
194
194
 
195
195
  declare const allTools: MosaddTool[];
196
196
 
197
- export { type MosaddServerOptions, type MosaddTool, type MosaddToolContext, type ProviderRegistry, allTools, createMosaddServer, defaultProviders };
197
+ /**
198
+ * Supabase provider for the alpha MCP server.
199
+ *
200
+ * Local BYOK mode: user supplies their own Supabase URL + anon key + user JWT
201
+ * via env vars. The MCP server creates a per-request Supabase client and
202
+ * invokes the mosadd Edge Functions on the user's behalf.
203
+ *
204
+ * In Phase 2 this is replaced by the hosted gateway (mcp.mosadd.com) which
205
+ * holds keys server-side via the BYOK broker.
206
+ */
207
+
208
+ interface SupabaseEnv {
209
+ url: string;
210
+ anonKey: string;
211
+ userJwt?: string;
212
+ }
213
+ /** Run `fn` (and every tool call it triggers) with a per-request Supabase session. */
214
+ declare function runWithSupabaseEnv<T>(env: SupabaseEnv, fn: () => T): T;
215
+
216
+ export { type MosaddServerOptions, type MosaddTool, type MosaddToolContext, type ProviderRegistry, type SupabaseEnv, allTools, createMosaddServer, defaultProviders, runWithSupabaseEnv };
package/dist/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  allTools,
3
3
  createMosaddServer,
4
- defaultProviders
5
- } from "./chunk-4C36JDTJ.js";
4
+ defaultProviders,
5
+ runWithSupabaseEnv
6
+ } from "./chunk-EFTB743G.js";
6
7
  import "./chunk-Z7WR52GE.js";
7
8
  export {
8
9
  allTools,
9
10
  createMosaddServer,
10
- defaultProviders
11
+ defaultProviders,
12
+ runWithSupabaseEnv
11
13
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mosadd/mcp",
3
- "version": "3.0.0-alpha.5",
4
- "description": "Model Context Protocol server for mosadd. Exposes the 6 live m* modules (mDM, mIRC, mROOM, mTALK, mAIL, mRAG) — 52 tools — to any agent runtime (Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, Bolt, Goose, Manus, custom).",
3
+ "version": "3.0.0-alpha.7",
4
+ "description": "Model Context Protocol server for mosadd. Exposes the 6 live m* modules (mDM, mIRC, mROOM, mTALK, mAIL, mRAG) — 57 tools — to any agent runtime (Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, Bolt, Goose, Manus, custom).",
5
5
  "homepage": "https://github.com/Hei33enberg/mosadd-os/tree/main/packages/mcp",
6
6
  "repository": {
7
7
  "type": "git",
@@ -37,8 +37,8 @@
37
37
  "@supabase/supabase-js": "^2.48.0",
38
38
  "zod": "^4.3.6",
39
39
  "@mosadd/crypto": "3.0.0-alpha.4",
40
- "@mosadd/providers": "3.0.0-alpha.4",
41
- "@mosadd/protocol": "3.0.0-alpha.4"
40
+ "@mosadd/protocol": "3.0.0-alpha.4",
41
+ "@mosadd/providers": "3.0.0-alpha.4"
42
42
  },
43
43
  "devDependencies": {
44
44
  "tsup": "^8.3.5",
package/src/index.ts CHANGED
@@ -15,3 +15,5 @@ export type {
15
15
  ProviderRegistry,
16
16
  } from "./types.js";
17
17
  export { allTools } from "./tools/index.js";
18
+ // Per-request session context for the hosted HTTP gateway (mcp.mosadd.com).
19
+ export { runWithSupabaseEnv, type SupabaseEnv } from "./providers/supabase.js";
@@ -9,6 +9,7 @@
9
9
  * holds keys server-side via the BYOK broker.
10
10
  */
11
11
 
12
+ import { AsyncLocalStorage } from "node:async_hooks";
12
13
  import { createClient, type SupabaseClient } from "@supabase/supabase-js";
13
14
  import { loadSession, isSessionExpired } from "../config.js";
14
15
 
@@ -18,6 +19,23 @@ export interface SupabaseEnv {
18
19
  userJwt?: string;
19
20
  }
20
21
 
22
+ /**
23
+ * Per-request session context for the hosted HTTP gateway (mcp.mosadd.com).
24
+ *
25
+ * stdio runs one user per process, so credentials live in process.env. The
26
+ * hosted gateway serves MANY users from one process — sharing process.env would
27
+ * leak user A's JWT into user B's concurrent request. The gateway instead
28
+ * resolves the caller's session (Bearer api key → hub-key-exchange) and runs the
29
+ * whole request inside `runWithSupabaseEnv(env, …)`; `readSupabaseEnv()` reads
30
+ * this store first and falls back to process.env, so stdio is unaffected.
31
+ */
32
+ const requestEnv = new AsyncLocalStorage<SupabaseEnv>();
33
+
34
+ /** Run `fn` (and every tool call it triggers) with a per-request Supabase session. */
35
+ export function runWithSupabaseEnv<T>(env: SupabaseEnv, fn: () => T): T {
36
+ return requestEnv.run(env, fn);
37
+ }
38
+
21
39
  export class MissingSupabaseEnvError extends Error {
22
40
  constructor(missing: string[]) {
23
41
  super(
@@ -29,6 +47,12 @@ export class MissingSupabaseEnvError extends Error {
29
47
  }
30
48
 
31
49
  export function readSupabaseEnv(): SupabaseEnv {
50
+ // Hosted HTTP gateway: a per-request session set via runWithSupabaseEnv wins
51
+ // over any process-global env (multi-tenant safety). stdio has no store → falls
52
+ // through to the env/login resolution below.
53
+ const perRequest = requestEnv.getStore();
54
+ if (perRequest) return perRequest;
55
+
32
56
  // Canonical env vars are MOSADD_*; the legacy M0SSAD_* names are still
33
57
  // honored (deprecated) so anyone who set them before the brand sweep keeps
34
58
  // working. Env vars take precedence; fall back to the session saved by
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Tool registry. Each m* module contributes a set of tools.
3
3
  *
4
- * Live modules (6): mDM, mIRC, mROOM, mTALK, mAIL, mRAG — 52 tools + comms_capabilities.
4
+ * Live modules (6): mDM, mIRC, mROOM, mTALK, mAIL, mRAG — 57 tools + comms_capabilities.
5
5
  * mCALL is carrier-pending (no telephony carrier live yet): its tools live in
6
6
  * tools/mcall.ts but are NOT registered here, so an agent only ever discovers
7
7
  * tools that actually work. Re-register the one line below when a carrier ships.
@@ -112,6 +112,49 @@ async function mRAG_ingest(
112
112
  return { indexed: data.indexed ?? 0, chunk_count: data.chunk_count ?? 0 };
113
113
  }
114
114
 
115
+ const mRAG_list_sources_input = z.object({});
116
+
117
+ interface RagSourceEntry {
118
+ source_type: string;
119
+ source_id: string | null;
120
+ thread_id: string | null;
121
+ title: string | null;
122
+ chunks: number;
123
+ first_indexed: string;
124
+ last_indexed: string;
125
+ }
126
+
127
+ async function mRAG_list_sources(
128
+ _input: z.infer<typeof mRAG_list_sources_input>,
129
+ ctx: MosaddToolContext,
130
+ ): Promise<{ sources: RagSourceEntry[]; total_sources: number; total_chunks: number }> {
131
+ readSupabaseEnv();
132
+ ctx.log("debug", "mRAG_list_sources via rag-sources");
133
+ return await invokeFunction("rag-sources", { action: "list" });
134
+ }
135
+
136
+ const mRAG_delete_input = z
137
+ .object({
138
+ source_id: z.string().optional().describe("Delete every indexed chunk with this source_id (from mRAG_list_sources)."),
139
+ thread_id: z.string().optional().describe("Delete every indexed chunk associated with this thread_id."),
140
+ })
141
+ .refine((v) => v.source_id || v.thread_id, {
142
+ message: "Provide source_id or thread_id — mRAG_delete refuses to purge the whole index.",
143
+ });
144
+
145
+ async function mRAG_delete(
146
+ input: z.infer<typeof mRAG_delete_input>,
147
+ ctx: MosaddToolContext,
148
+ ): Promise<{ ok: true; deleted: number; source_id: string | null; thread_id: string | null }> {
149
+ readSupabaseEnv();
150
+ ctx.log("debug", "mRAG_delete via rag-sources", { source_id: input.source_id, thread_id: input.thread_id });
151
+ return await invokeFunction("rag-sources", {
152
+ action: "delete",
153
+ source_id: input.source_id ?? null,
154
+ thread_id: input.thread_id ?? null,
155
+ });
156
+ }
157
+
115
158
  export const knowledgeTools: MosaddTool[] = [
116
159
  {
117
160
  name: "mRAG_ingest",
@@ -129,4 +172,20 @@ export const knowledgeTools: MosaddTool[] = [
129
172
  inputSchema: mRAG_search_input,
130
173
  handler: mRAG_search as MosaddTool["handler"],
131
174
  },
175
+ {
176
+ name: "mRAG_list_sources",
177
+ requires: "network",
178
+ description:
179
+ "List what is currently indexed in the user's private knowledge base, grouped by source (source_type, source_id, thread_id, title) with per-source chunk counts and first/last indexed timestamps. Use it to see what mRAG_search can recall, and to find a source_id to remove with mRAG_delete. Owner-scoped.",
180
+ inputSchema: mRAG_list_sources_input,
181
+ handler: mRAG_list_sources as MosaddTool["handler"],
182
+ },
183
+ {
184
+ name: "mRAG_delete",
185
+ requires: "network",
186
+ description:
187
+ "Remove indexed content from the user's knowledge base by source_id or thread_id (from mRAG_list_sources). Deletes every embedded chunk for that source so mRAG_search can no longer recall it. Requires source_id or thread_id (will not purge the whole index). Returns the number of chunks deleted. Owner-scoped.",
188
+ inputSchema: mRAG_delete_input,
189
+ handler: mRAG_delete as MosaddTool["handler"],
190
+ },
132
191
  ];
package/src/tools/mail.ts CHANGED
@@ -53,6 +53,16 @@ const mAIL_list_input = z.object({
53
53
  .describe("Pagination cursor: pass the previous response's next_cursor to get the next (older) page."),
54
54
  });
55
55
 
56
+ const mAIL_stats_input = z.object({
57
+ message_id: MessageId,
58
+ });
59
+
60
+ const mAIL_events_input = z.object({
61
+ message_id: MessageId,
62
+ });
63
+
64
+ const mAIL_metrics_input = z.object({});
65
+
56
66
  interface MailListItem {
57
67
  message_id: string;
58
68
  direction: "inbound" | "outbound";
@@ -120,6 +130,64 @@ async function mAIL_delete(
120
130
  });
121
131
  }
122
132
 
133
+ interface MailEngagement {
134
+ message_id: string;
135
+ subject: string;
136
+ to: string;
137
+ direction: string;
138
+ status: string;
139
+ sent_at: string;
140
+ open_count: number;
141
+ unique_opens: number;
142
+ click_count: number;
143
+ forwarded: boolean;
144
+ first_opened_at: string | null;
145
+ last_opened_at: string | null;
146
+ events_by_type: Record<string, number>;
147
+ total_events: number;
148
+ }
149
+
150
+ async function mAIL_stats(
151
+ input: z.infer<typeof mAIL_stats_input>,
152
+ ctx: MosaddToolContext,
153
+ ): Promise<{ summary: MailEngagement }> {
154
+ readSupabaseEnv();
155
+ ctx.log("debug", "mAIL_stats invoking mp0st-stats", { message_id: input.message_id });
156
+ return await invokeFunction<{ summary: MailEngagement }>("mp0st-stats", {
157
+ message_id: input.message_id,
158
+ });
159
+ }
160
+
161
+ async function mAIL_events(
162
+ input: z.infer<typeof mAIL_events_input>,
163
+ ctx: MosaddToolContext,
164
+ ): Promise<{
165
+ summary: MailEngagement;
166
+ events: Array<{ event_type: string; at: string; device: string | null; ip_hash: string | null; link_url: string | null }>;
167
+ }> {
168
+ readSupabaseEnv();
169
+ ctx.log("debug", "mAIL_events invoking mp0st-stats", { message_id: input.message_id });
170
+ return await invokeFunction("mp0st-stats", { message_id: input.message_id, events: true });
171
+ }
172
+
173
+ async function mAIL_metrics(
174
+ _input: z.infer<typeof mAIL_metrics_input>,
175
+ ctx: MosaddToolContext,
176
+ ): Promise<{
177
+ mailbox: {
178
+ total_sent: number;
179
+ opened_emails: number;
180
+ open_rate_pct: number;
181
+ total_opens: number;
182
+ total_clicks: number;
183
+ forwarded_emails: number;
184
+ };
185
+ }> {
186
+ readSupabaseEnv();
187
+ ctx.log("debug", "mAIL_metrics invoking mp0st-stats");
188
+ return await invokeFunction("mp0st-stats", {});
189
+ }
190
+
123
191
  // ---- Registration ----
124
192
 
125
193
  export const mailTools: MosaddTool[] = [
@@ -154,4 +222,28 @@ export const mailTools: MosaddTool[] = [
154
222
  inputSchema: mAIL_delete_input,
155
223
  handler: mAIL_delete as MosaddTool["handler"],
156
224
  },
225
+ {
226
+ name: "mAIL_stats",
227
+ requires: "network",
228
+ description:
229
+ "Engagement summary for one sent email (by message_id): open_count, unique_opens, click_count, whether it was likely forwarded, first/last opened time, and a breakdown of tracked event types. Opens/clicks are real (tracking pixel + link-wrap); forward detection is best-effort (IP-subnet diversity). Owner-scoped.",
230
+ inputSchema: mAIL_stats_input,
231
+ handler: mAIL_stats as MosaddTool["handler"],
232
+ },
233
+ {
234
+ name: "mAIL_events",
235
+ requires: "network",
236
+ description:
237
+ "Raw engagement timeline for one email (by message_id): every tracked event (pixel_open, link_click, forwarded, page_view, copy/print/focus signals) with timestamp, device type and link URL. Use mAIL_stats for the rollup; use this for the per-event detail. Owner-scoped.",
238
+ inputSchema: mAIL_events_input,
239
+ handler: mAIL_events as MosaddTool["handler"],
240
+ },
241
+ {
242
+ name: "mAIL_metrics",
243
+ requires: "network",
244
+ description:
245
+ "Mailbox-wide engagement aggregate across the user's sent mail: total_sent, opened_emails, open_rate_pct, total_opens, total_clicks, forwarded_emails. Owner-scoped.",
246
+ inputSchema: mAIL_metrics_input,
247
+ handler: mAIL_metrics as MosaddTool["handler"],
248
+ },
157
249
  ];