@mosadd/mcp 3.0.0-alpha.32 → 3.0.0-alpha.34
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 +74 -0
- package/README.md +11 -11
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-KX34DDJB.js → chunk-IWLN5EO5.js} +614 -134
- package/dist/index.d.ts +16 -6
- package/dist/index.js +1 -1
- package/package.json +6 -6
- package/src/__tests__/golden-path.test.ts +137 -137
- package/src/__tests__/integration.test.ts +179 -179
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -212
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +141 -0
- package/src/__tests__/server.test.ts +71 -71
- package/src/__tests__/tool-count-consistency.test.ts +121 -1
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -145
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +502 -502
- package/src/index.ts +19 -19
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +116 -116
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +122 -122
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +148 -148
- package/src/tools/actions.ts +162 -162
- package/src/tools/attachments.ts +268 -268
- package/src/tools/capabilities.ts +68 -68
- package/src/tools/embed.ts +108 -108
- package/src/tools/index.ts +173 -104
- package/src/tools/knowledge-graph.ts +178 -0
- package/src/tools/knowledge.ts +319 -191
- package/src/tools/mail-agentbox.ts +147 -0
- package/src/tools/mail-aliases.ts +25 -25
- package/src/tools/mail-provenance.ts +135 -135
- package/src/tools/mail.ts +381 -381
- package/src/tools/mdm.ts +476 -479
- package/src/tools/mirc-edge.ts +190 -190
- package/src/tools/mirc-members.ts +2 -2
- package/src/tools/mirc-messages.ts +155 -155
- package/src/tools/mirc.ts +2 -2
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +121 -121
- package/src/tools/murl.ts +4 -2
- package/src/tools/presence.ts +208 -0
- package/src/types.ts +94 -94
- package/src/voice/floor.ts +132 -132
package/src/tools/index.ts
CHANGED
|
@@ -1,104 +1,173 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tool registry. Each m* module contributes a set of tools.
|
|
3
|
-
*
|
|
4
|
-
* User-facing modules (4): mDM, mIRC, mURL, mAYL — the re-arch (LINEAR-3938)
|
|
5
|
-
* mAYL replaces the old mp0st name (mp0st_* still registered as back-compat
|
|
6
|
-
* aliases via mail-aliases.ts; will be removed in a later alpha).
|
|
7
|
-
*
|
|
8
|
-
* Capabilities (not modules — cross-cutting agent tooling): mRAG, comms_.
|
|
9
|
-
* mTALK = voice tier inside mIRC, still its own tools for now; folding into
|
|
10
|
-
* mIRC_voice_* in a follow-up release. threat_* (threat_catalog + threat_classify)
|
|
11
|
-
* are REGISTERED (2026-07-15, founder directive + LINEAR-3498): pure/offline
|
|
12
|
-
* defensive classification over the threat taxonomy — the surveillance-era
|
|
13
|
-
* *marketing* was killed, the classification engine is real and wanted.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* (= allTools.length) below; ALWAYS reference that, never re-hard-code a number that drifts.
|
|
17
|
-
* Breakdown (2026-
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
import {
|
|
52
|
-
import {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
import {
|
|
56
|
-
import {
|
|
57
|
-
import {
|
|
58
|
-
import {
|
|
59
|
-
import {
|
|
60
|
-
import {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
...
|
|
79
|
-
...
|
|
80
|
-
...
|
|
81
|
-
...
|
|
82
|
-
...
|
|
83
|
-
// ...
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tool registry. Each m* module contributes a set of tools.
|
|
3
|
+
*
|
|
4
|
+
* User-facing modules (4): mDM, mIRC, mURL, mAYL — the re-arch (LINEAR-3938)
|
|
5
|
+
* mAYL replaces the old mp0st name (mp0st_* still registered as back-compat
|
|
6
|
+
* aliases via mail-aliases.ts; will be removed in a later alpha).
|
|
7
|
+
*
|
|
8
|
+
* Capabilities (not modules — cross-cutting agent tooling): mRAG, comms_.
|
|
9
|
+
* mTALK = voice tier inside mIRC, still its own tools for now; folding into
|
|
10
|
+
* mIRC_voice_* in a follow-up release. threat_* (threat_catalog + threat_classify)
|
|
11
|
+
* are REGISTERED (2026-07-15, founder directive + LINEAR-3498): pure/offline
|
|
12
|
+
* defensive classification over the threat taxonomy — the surveillance-era
|
|
13
|
+
* *marketing* was killed, the classification engine is real and wanted.
|
|
14
|
+
*
|
|
15
|
+
* 82 callable tools registered — the exact live count is exported as TOOL_COUNT
|
|
16
|
+
* (= allTools.length) below; ALWAYS reference that, never re-hard-code a number that drifts.
|
|
17
|
+
* Breakdown (2026-08-14, measured from a live tools/list on the built server):
|
|
18
|
+
* modules mDM 14 · mIRC 24 · mURL 7 · mAYL 16 = 61; capabilities mTALK 6 · mRAG 8 ·
|
|
19
|
+
* comms 5 (action_create/action_frame_get/embed_create/capabilities/session_attach) · Irondome/threat 2
|
|
20
|
+
* = 21. 61 + 21 = 82. (mRAG 8 = 4 search/ingest + 4 mRAG_graph_* traversal tools added 2026-08-11.
|
|
21
|
+
* comms_session_attach added 2026-08-14: a live agent session claims the account's reply lane so
|
|
22
|
+
* the cloud stand-in defers to it — the front door that used to be a script on one PC.)
|
|
23
|
+
* (History of this line drifting: the 2026-07-17 breakdown ended "= 69"
|
|
24
|
+
* — wrong twice over, its own terms summed to 71; the 07-30 rewrite said 73 and still had
|
|
25
|
+
* mAYL at 12 after mAYL_send_as_agent had been re-registered, so it stayed wrong through the
|
|
26
|
+
* four mAYL_agentbox_* tools landing on 08-06. Arithmetic in comments drifts; TOOL_COUNT
|
|
27
|
+
* doesn't — and since 08-11 the anti-drift gate checks the PER-MODULE numbers too, not just
|
|
28
|
+
* the total, so this comment cannot rot alone any more.)
|
|
29
|
+
* (mDM/mIRC each include their 2 attachment tools from attachments.ts.)
|
|
30
|
+
* Modules (4): mDM, mIRC, mURL, mAYL. Capabilities: mTALK (voice/PTT), mRAG, comms_, Irondome (threat_*).
|
|
31
|
+
* mp0st_* stay as DEPRECATED back-compat aliases for mAYL_* (removed in a later alpha).
|
|
32
|
+
* mAYL_send_as_agent + mTALK_ingest_ptt were RE-REGISTERED 2026-07-29: both had been disabled
|
|
33
|
+
* on 07-14 notes that had since become false, and both were re-verified on authenticated calls.
|
|
34
|
+
* comms_embed_create RE-REGISTERED 2026-07-17 (embed.mosadd.com/v1.js live — P3 of mURL→mIRC).
|
|
35
|
+
* mROOM was KILLED (LINEAR-3414, channel re-cut → ephemeral private mIRC): its tools
|
|
36
|
+
* live in tools/mroom.ts + mroom-messages.ts but are NOT registered here.
|
|
37
|
+
* Plus action links: comms_action_create (agent → user one-link browser action,
|
|
38
|
+
* incl. screen_share) + comms_action_frame_get (see the recipient's shared screen).
|
|
39
|
+
* mURL is a FULL registered dev module (revived, founder 2026-06-27) — IRC-for-URLs,
|
|
40
|
+
* agent-native; backends live (mosadd-edge + murl-channels EF). tools/murl.ts.
|
|
41
|
+
* mURL now has full lifecycle: read/post/presence/list_channels (hub-key) PLUS owner-side
|
|
42
|
+
* create/claim + update(branding/status) + delete (mURL_create/update/delete → murl-manage
|
|
43
|
+
* EF, owner-scoped via user JWT — same auth as mURL_list_channels action='mine').
|
|
44
|
+
* mCALL is carrier-pending (no telephony carrier live yet): its tools live in
|
|
45
|
+
* tools/mcall.ts but are NOT registered here, so an agent only ever discovers
|
|
46
|
+
* tools that actually work. Re-register the one line below when a carrier ships.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import type { MosaddTool } from "../types.js";
|
|
50
|
+
import { mdmTools } from "./mdm.js";
|
|
51
|
+
import { mdmVoiceTools } from "./mdm-voice.js";
|
|
52
|
+
import { mircTools } from "./mirc.js";
|
|
53
|
+
import { mircMembersTools } from "./mirc-members.js";
|
|
54
|
+
import { mircMessagesTools } from "./mirc-messages.js";
|
|
55
|
+
import { mircEdgeTools } from "./mirc-edge.js";
|
|
56
|
+
// import { mroomTools } from "./mroom.js"; // mROOM: killed (LINEAR-3414) — not registered
|
|
57
|
+
// import { mroomMessagesTools } from "./mroom-messages.js"; // mROOM: killed (LINEAR-3414) — not registered
|
|
58
|
+
import { mailTools } from "./mail.js";
|
|
59
|
+
// import { mailAliasTools } from "./mail-aliases.js"; // mp0st_* aliases RETIRED 2026-07-15 (founder: unify on mAYL, drop the mp0st codename from every user-visible surface). Source kept for reference.
|
|
60
|
+
import { mailProvenanceTools } from "./mail-provenance.js"; // mAYL_send_as_agent — RE-REGISTERED 2026-07-29, see the note at the registry below
|
|
61
|
+
import { mailAgentboxTools } from "./mail-agentbox.js"; // mAYL_agentbox_*: agent mints/lists/releases its OWN two-way disposable inbox (backs mayl-agentbox EF). Added 2026-08-06 (LINEAR-5201 Part B)
|
|
62
|
+
import { mtalkTools } from "./mtalk.js";
|
|
63
|
+
import { attachmentTools } from "./attachments.js";
|
|
64
|
+
import { pttIngestTools } from "./ptt-ingest.js"; // mTALK_ingest_ptt — RE-REGISTERED 2026-07-29, see the note at the registry below
|
|
65
|
+
// import { mcallTools } from "./mcall.js"; // mCALL: carrier-pending — not registered (see header note)
|
|
66
|
+
import { knowledgeTools } from "./knowledge.js";
|
|
67
|
+
import { knowledgeGraphTools } from "./knowledge-graph.js"; // mRAG_graph_* — 2026-08-11: MCP door onto the kg_* graph RPCs (traverse people/threads, not just search text)
|
|
68
|
+
import { actionTools } from "./actions.js";
|
|
69
|
+
import { presenceTools } from "./presence.js"; // comms_session_attach — 2026-08-14: a live agent session claims the account's reply lane (agent_bridge_heartbeat), so the cloud stand-in defers to it. Replaces the PC-only bridge script as the ONLY writer of that signal.
|
|
70
|
+
import { embedTools } from "./embed.js"; // comms_embed_create RE-REGISTERED 2026-07-17: embed.mosadd.com/v1.js is LIVE (served by the mosadd Vercel project) — the widget + embed-keys/mirc-embed-token backend were already complete.
|
|
71
|
+
import { threatTools } from "./threat.js"; // threat_*: RE-REGISTERED 2026-07-15 (founder directive + LINEAR-3498). Pure/offline classification of the real Pegasus-class threat taxonomy — the engine DECIDES, the caller acts; no surveillance, no backend.
|
|
72
|
+
import { murlTools } from "./murl.js"; // mURL: REGISTERED — revived as a full dev module (founder 2026-06-27, re-arch). IRC-for-URLs, agent-native; backends live (mosadd-edge + murl-channels EF).
|
|
73
|
+
import { makeCapabilitiesTool } from "./capabilities.js";
|
|
74
|
+
|
|
75
|
+
/** The m* channel tools (everything except the meta discovery tool). */
|
|
76
|
+
const channelTools: MosaddTool[] = [
|
|
77
|
+
...mdmTools,
|
|
78
|
+
...mdmVoiceTools,
|
|
79
|
+
...mircTools,
|
|
80
|
+
...mircMembersTools,
|
|
81
|
+
...mircMessagesTools,
|
|
82
|
+
...mircEdgeTools,
|
|
83
|
+
// ...mroomTools, // mROOM: killed (LINEAR-3414) — channel re-cut into ephemeral private mIRC
|
|
84
|
+
// ...mroomMessagesTools, // mROOM: killed (LINEAR-3414)
|
|
85
|
+
...mailTools, // mAYL_*: canonical email module (was the mp0st codename; renamed re-arch 2026-06-27)
|
|
86
|
+
// ...mailAliasTools, // mp0st_* aliases RETIRED 2026-07-15 — mAYL is the one name; the mp0st codename is now backend-internal only
|
|
87
|
+
// ⭐ mAYL_send_as_agent: RE-REGISTERED 2026-07-29 — proven end to end, without sending mail.
|
|
88
|
+
// Unregistered since 07-14 on the note "hub-claim EF 404s every call". That note was stale in
|
|
89
|
+
// the same way ptt-ingest's was: unauthenticated it returns 401 (the auth gate), not 404.
|
|
90
|
+
//
|
|
91
|
+
// Settled by running the whole chain rather than the convenient half:
|
|
92
|
+
// 1. hub-keys with a plain user JWT issued a real `mosadd_sk_live_…` — so a user session
|
|
93
|
+
// mints a hub key and no founder credential was ever needed. I had told the founder it
|
|
94
|
+
// required his key; that was wrong and this is the retraction.
|
|
95
|
+
// 2. hub-claim-mint with that key → 200, valid claim token, aud `mp0st-send`, 600s expiry.
|
|
96
|
+
// 3. mp0st-send with the claim token and a DELIBERATELY EMPTY payload → 400 "Missing to or
|
|
97
|
+
// subject". A 400 on payload validation proves the token was ACCEPTED; 401/403 would have
|
|
98
|
+
// proven it was not. Auth verified end to end with zero mail sent — a diagnostic must not
|
|
99
|
+
// put a real message in someone's inbox.
|
|
100
|
+
// The diagnostic key was then REVOKED (revoked_at set, not deleted, so the audit trail lives).
|
|
101
|
+
...mailProvenanceTools,
|
|
102
|
+
// mAYL_agentbox_provision/list/release/extend — the agent mints/lists/releases/extends its OWN
|
|
103
|
+
// real, two-way, disposable inbox (agent-<hex>@mosadd.com). create_inbox parity with AgentMail,
|
|
104
|
+
// on our own stack + provenance. Backs the live mayl-agentbox EF; receive path landed 2026-08-06
|
|
105
|
+
// in mp0st-inbound-webhook (LINEAR-5201). Same requireUser auth as every other mAYL tool.
|
|
106
|
+
...mailAgentboxTools,
|
|
107
|
+
...mtalkTools,
|
|
108
|
+
...attachmentTools,
|
|
109
|
+
// ⭐ mTALK_ingest_ptt: RE-REGISTERED 2026-07-29 — the backend finally does what the tool promises.
|
|
110
|
+
// Unregistered since 07-14 on the note "ptt-ingest EF 400s every call". Re-measured 07-29: that
|
|
111
|
+
// reason was stale (it returned 401, the auth gate), and an AUTHENTICATED probe then exposed the
|
|
112
|
+
// real fault — 500, `Could not find the table 'public.rag_jobs'`. It authenticated, validated,
|
|
113
|
+
// wrote the transcript row, and died one step later against a table dropped when the rag_events
|
|
114
|
+
// v2 queue landed. Agent push-to-talk had NEVER once reached memory.
|
|
115
|
+
//
|
|
116
|
+
// Fixed by handing the audio to `rag-transcribe` (NOT to the v2 queue — that queue carries TEXT
|
|
117
|
+
// for embedding and cannot transcribe; audio has to become words first). Verified on prod:
|
|
118
|
+
// 200 {"queued":true}, edge log 200 in 2.7s where it used to 500, and the transcript row's
|
|
119
|
+
// updated_at moved 4s after insert, so the backgrounded chain genuinely ran.
|
|
120
|
+
//
|
|
121
|
+
// Registered only AFTER that authenticated 200 — a liveness probe was never enough, because the
|
|
122
|
+
// whole point of this registry is that an agent sees only tools that actually work.
|
|
123
|
+
...pttIngestTools,
|
|
124
|
+
//
|
|
125
|
+
// ✅ RESOLVED 2026-07-29 — both halves of this note are closed; kept as the record of how.
|
|
126
|
+
// The disable note recorded a diagnosis from 2026-07-14: hub-claim "404s every call".
|
|
127
|
+
// Live-probed today, unauthenticated:
|
|
128
|
+
// hub-claim-mint → 401 · ptt-ingest → 401 · mp0st-send → 401 (the control, a tool that
|
|
129
|
+
// IS registered and works)
|
|
130
|
+
// 401 is the auth gate answering. A dead or missing function returns 404; a function that
|
|
131
|
+
// rejects our payload shape returns 400. Neither happens any more, and both are ACTIVE on
|
|
132
|
+
// the project. So whatever was broken in July was fixed at some point and nobody came back
|
|
133
|
+
// to these lines — we advertised two capabilities as unavailable while their backends
|
|
134
|
+
// answered. One of them (ptt) is now proven and registered; this is the other.
|
|
135
|
+
//
|
|
136
|
+
// NOT re-registering hub-claim on this evidence, deliberately. An unauthenticated 401 proves the
|
|
137
|
+
// function is alive; it proves NOTHING about the authenticated path, which is where both
|
|
138
|
+
// originally failed. Re-registering on a liveness probe would swap "wrongly disabled" for
|
|
139
|
+
// "advertised and broken in a user's hands", which is the worse of the two.
|
|
140
|
+
//
|
|
141
|
+
// TO CLOSE: call each with a real hub key / session token. If they succeed, delete these
|
|
142
|
+
// two comment blocks and uncomment the imports — the count moves 71 → 73 and the anti-drift
|
|
143
|
+
// gate will then force every doc surface to follow. (This session cannot mint a session
|
|
144
|
+
// token, so it stops here rather than guess.)
|
|
145
|
+
...knowledgeTools,
|
|
146
|
+
...knowledgeGraphTools, // mRAG_graph_*: traverse the per-user knowledge graph (kg_* RPCs) — search finds text, this walks who/what/when
|
|
147
|
+
...actionTools, // Action links (Tier 1): agent → user one-link browser action via action-create EF
|
|
148
|
+
...presenceTools, // comms_session_attach: THIS session owns the account's reply lane while it runs; the 24/7 cloud stand-in defers. Any session, any machine — no bespoke bridge on one PC.
|
|
149
|
+
...embedTools, // comms_embed_create: RE-REGISTERED 2026-07-17 — embed.mosadd.com/v1.js live; mints an embed key + paste-in snippet (mURL→mIRC rework P3)
|
|
150
|
+
...threatTools, // threat_catalog + threat_classify: RE-REGISTERED 2026-07-15 (founder directive + LINEAR-3498) — pure/offline, makes the Pegasus-class classification claim a live, callable capability
|
|
151
|
+
...murlTools, // mURL: revived as a full module (founder 2026-06-27) — read/post/presence/list_channels over live domain channels
|
|
152
|
+
// ...mcallTools, // mCALL: carrier-pending (no telephony carrier live) — re-register when a carrier is configured
|
|
153
|
+
// mirlTools,
|
|
154
|
+
// mmatrixTools, mdiscordTools, mtelegramTools, mslackTools, msignalTools,
|
|
155
|
+
//
|
|
156
|
+
// Each m* module registers its tools here as it ships.
|
|
157
|
+
// RFC required to add a new m* module (see docs/rfcs/0001-module-naming.md).
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
export const allTools: MosaddTool[] = [
|
|
161
|
+
...channelTools,
|
|
162
|
+
// comms_capabilities: one-call discovery of every tool's `requires` flag,
|
|
163
|
+
// so carrier-aware hosts (cymru-os) can gate the tool list by transport.
|
|
164
|
+
makeCapabilitiesTool(channelTools),
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The exact, live number of callable tools this server registers. Reference THIS
|
|
169
|
+
* (import { TOOL_COUNT }) in any code/doc surface that shows a tool count, instead
|
|
170
|
+
* of hard-coding a number that silently drifts (the "70+/65/68" inconsistency that
|
|
171
|
+
* bit us). Mirrors @mosadd/threat-engine's THREAT_EVENT_COUNT pattern.
|
|
172
|
+
*/
|
|
173
|
+
export const TOOL_COUNT: number = allTools.length;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mRAG_graph — the KNOWLEDGE GRAPH over the user's own mosadd data.
|
|
3
|
+
*
|
|
4
|
+
* mRAG_search recalls WHAT was said; this surface walks WHO/WHAT is connected to whom, and WHEN.
|
|
5
|
+
* The engine already runs inside the mosadd backend as a set of Postgres functions built from the
|
|
6
|
+
* SAME per-user embeddings mRAG_search reads (metadata only — sender/recipient/thread — NEVER the
|
|
7
|
+
* message body, so it does not weaken the E2EE posture). Until now those functions had no MCP door,
|
|
8
|
+
* so an agent could search the corpus but not traverse it. These four tools are that door.
|
|
9
|
+
*
|
|
10
|
+
* ⚠ TRANSPORT: unlike the other mRAG tools (which call Edge Functions via invokeFunction), the
|
|
11
|
+
* graph lives in Postgres RPCs (kg_*), so these call `getSupabase().rpc(...)` directly. The RPCs
|
|
12
|
+
* are `grant execute … to authenticated`, and the graph-building one is SECURITY DEFINER keyed on
|
|
13
|
+
* `auth.uid()`, so under the caller's hub-session JWT every result is scoped to the caller's own
|
|
14
|
+
* account — there is no cross-tenant path.
|
|
15
|
+
*
|
|
16
|
+
* Typical flow for an agent: mRAG_graph_refresh (build/update from the latest ingested data) →
|
|
17
|
+
* mRAG_graph_overview (see the top parties + their ids) → mRAG_graph_neighbors / _timeline on a
|
|
18
|
+
* party id from the overview.
|
|
19
|
+
*
|
|
20
|
+
* Plan note: gated by plan at the hub (the knowledge graph is a paid capability — matches the
|
|
21
|
+
* app's `knowledgeGraph` flag). BYOK/self-host runs it against the user's own backend.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { z } from "zod";
|
|
25
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
26
|
+
import { getSupabase, readSupabaseEnv } from "../providers/supabase.js";
|
|
27
|
+
|
|
28
|
+
/** One authenticated RPC round-trip with normalized errors an agent can reason about. */
|
|
29
|
+
async function callRpc<T>(fn: string, args: Record<string, unknown>, ctx: MosaddToolContext): Promise<T> {
|
|
30
|
+
readSupabaseEnv();
|
|
31
|
+
ctx.log("debug", `mRAG_graph via ${fn}`, args);
|
|
32
|
+
const { data, error } = await getSupabase().rpc(fn, args);
|
|
33
|
+
if (error) {
|
|
34
|
+
// PostgREST surfaces RLS denials, auth failures and function errors here; hand the agent the
|
|
35
|
+
// real message + code rather than a generic sentence, so it can self-correct.
|
|
36
|
+
throw new Error(`${fn} failed: ${error.message}${error.code ? ` (${error.code})` : ""}`);
|
|
37
|
+
}
|
|
38
|
+
return data as T;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ── mRAG_graph_refresh ───────────────────────────────────────────────────────
|
|
42
|
+
const mRAG_graph_refresh_input = z.object({
|
|
43
|
+
max_docs: z
|
|
44
|
+
.number()
|
|
45
|
+
.int()
|
|
46
|
+
.min(1)
|
|
47
|
+
.max(20_000)
|
|
48
|
+
.optional()
|
|
49
|
+
.describe("Cap on source documents scanned when (re)building the graph. Default 20000 (the max)."),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
async function mRAG_graph_refresh(
|
|
53
|
+
input: z.infer<typeof mRAG_graph_refresh_input>,
|
|
54
|
+
ctx: MosaddToolContext,
|
|
55
|
+
): Promise<unknown> {
|
|
56
|
+
// kg_refresh_me rebuilds parties/mentions/edges from the caller's embeddings METADATA only
|
|
57
|
+
// (sender/recipient/thread), never content_text. Returns counts {parties, mentions, edges, …}.
|
|
58
|
+
return await callRpc("kg_refresh_me", { p_max_docs: input.max_docs ?? 20_000 }, ctx);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ── mRAG_graph_overview ──────────────────────────────────────────────────────
|
|
62
|
+
const mRAG_graph_overview_input = z.object({
|
|
63
|
+
limit: z.number().int().min(1).max(200).optional().describe("Max parties to return (busiest first). Default 40, max 200."),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
interface GraphParty {
|
|
67
|
+
party_id: string;
|
|
68
|
+
display_name: string;
|
|
69
|
+
kind: string;
|
|
70
|
+
aliases: string[];
|
|
71
|
+
appearances: number;
|
|
72
|
+
connections: number;
|
|
73
|
+
first_seen_at: string | null;
|
|
74
|
+
last_seen_at: string | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function mRAG_graph_overview(
|
|
78
|
+
input: z.infer<typeof mRAG_graph_overview_input>,
|
|
79
|
+
ctx: MosaddToolContext,
|
|
80
|
+
): Promise<{ parties: GraphParty[]; count: number }> {
|
|
81
|
+
const rows = await callRpc<GraphParty[]>("kg_overview", { p_limit: input.limit ?? 40 }, ctx);
|
|
82
|
+
const parties = rows ?? [];
|
|
83
|
+
return { parties, count: parties.length };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ── mRAG_graph_neighbors ─────────────────────────────────────────────────────
|
|
87
|
+
const mRAG_graph_neighbors_input = z.object({
|
|
88
|
+
party_id: z.string().uuid().describe("The party whose connections to walk (a party_id from mRAG_graph_overview)."),
|
|
89
|
+
depth: z.number().int().min(1).max(2).optional().describe("Hops to walk. Hard-clamped to 2 server-side. Default 1."),
|
|
90
|
+
limit: z.number().int().min(1).max(200).optional().describe("Max neighbours to return. Clamped to 200. Default 50."),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
interface GraphNeighbor {
|
|
94
|
+
party_id: string;
|
|
95
|
+
display_name: string;
|
|
96
|
+
kind: string;
|
|
97
|
+
depth: number;
|
|
98
|
+
edge_type: string;
|
|
99
|
+
observations: number;
|
|
100
|
+
valid_from: string | null;
|
|
101
|
+
last_seen_at: string | null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function mRAG_graph_neighbors(
|
|
105
|
+
input: z.infer<typeof mRAG_graph_neighbors_input>,
|
|
106
|
+
ctx: MosaddToolContext,
|
|
107
|
+
): Promise<{ neighbors: GraphNeighbor[]; count: number }> {
|
|
108
|
+
const rows = await callRpc<GraphNeighbor[]>(
|
|
109
|
+
"kg_neighbors",
|
|
110
|
+
{ p_party_id: input.party_id, p_depth: input.depth ?? 1, p_limit: input.limit ?? 50 },
|
|
111
|
+
ctx,
|
|
112
|
+
);
|
|
113
|
+
const neighbors = rows ?? [];
|
|
114
|
+
return { neighbors, count: neighbors.length };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ── mRAG_graph_timeline ──────────────────────────────────────────────────────
|
|
118
|
+
const mRAG_graph_timeline_input = z.object({
|
|
119
|
+
party_id: z.string().uuid().describe("The party whose dated appearance timeline to return (a party_id from mRAG_graph_overview)."),
|
|
120
|
+
limit: z.number().int().min(1).max(200).optional().describe("Max timeline entries (newest first). Clamped to 200. Default 50."),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
interface GraphTimelineEntry {
|
|
124
|
+
source_type: string;
|
|
125
|
+
source_id: string;
|
|
126
|
+
thread_id: string | null;
|
|
127
|
+
role: string | null;
|
|
128
|
+
occurred_at: string | null;
|
|
129
|
+
evidence_chunk_id: string | null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function mRAG_graph_timeline(
|
|
133
|
+
input: z.infer<typeof mRAG_graph_timeline_input>,
|
|
134
|
+
ctx: MosaddToolContext,
|
|
135
|
+
): Promise<{ timeline: GraphTimelineEntry[]; count: number }> {
|
|
136
|
+
const rows = await callRpc<GraphTimelineEntry[]>(
|
|
137
|
+
"kg_party_timeline",
|
|
138
|
+
{ p_party_id: input.party_id, p_limit: input.limit ?? 50 },
|
|
139
|
+
ctx,
|
|
140
|
+
);
|
|
141
|
+
const timeline = rows ?? [];
|
|
142
|
+
return { timeline, count: timeline.length };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const knowledgeGraphTools: MosaddTool[] = [
|
|
146
|
+
{
|
|
147
|
+
name: "mRAG_graph_refresh",
|
|
148
|
+
requires: "network",
|
|
149
|
+
description:
|
|
150
|
+
"(Re)build the USER'S OWN knowledge graph from their indexed data, then return what it found (counts of parties/mentions/edges). Run this first, or after new data has been ingested, so mRAG_graph_overview/neighbors/timeline reflect the latest. Built from METADATA only (who-sent-to-whom, thread membership) — never message bodies, so it does not weaken E2EE. Owner-scoped.",
|
|
151
|
+
inputSchema: mRAG_graph_refresh_input,
|
|
152
|
+
handler: mRAG_graph_refresh as MosaddTool["handler"],
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "mRAG_graph_overview",
|
|
156
|
+
requires: "network",
|
|
157
|
+
description:
|
|
158
|
+
"List the parties (people / companies / services) in the user's knowledge graph, busiest first, each with its party_id, kind, aliases, appearance count, connection count and first/last-seen dates. This is the entry point: use a returned party_id with mRAG_graph_neighbors or mRAG_graph_timeline. Owner-scoped.",
|
|
159
|
+
inputSchema: mRAG_graph_overview_input,
|
|
160
|
+
handler: mRAG_graph_overview as MosaddTool["handler"],
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name: "mRAG_graph_neighbors",
|
|
164
|
+
requires: "network",
|
|
165
|
+
description:
|
|
166
|
+
"Walk the connections of one party (from mRAG_graph_overview) — who/what it is linked to, with the edge type, how many times observed, and when. Answers 'who is connected to X, and how'. Depth is hard-clamped to 2 hops and breadth to 200 server-side. Owner-scoped.",
|
|
167
|
+
inputSchema: mRAG_graph_neighbors_input,
|
|
168
|
+
handler: mRAG_graph_neighbors as MosaddTool["handler"],
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: "mRAG_graph_timeline",
|
|
172
|
+
requires: "network",
|
|
173
|
+
description:
|
|
174
|
+
"Return the dated appearance timeline of one party (from mRAG_graph_overview) — each source (email/message/call/…), its thread, the party's role, and when it occurred, newest first. Answers 'when and where did X show up'. Owner-scoped.",
|
|
175
|
+
inputSchema: mRAG_graph_timeline_input,
|
|
176
|
+
handler: mRAG_graph_timeline as MosaddTool["handler"],
|
|
177
|
+
},
|
|
178
|
+
];
|