@mosadd/mcp 3.0.0-alpha.4 → 3.0.0-alpha.40
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 +298 -0
- package/README.md +70 -36
- package/dist/bin/mcp.js +42 -3
- package/dist/chunk-7ZAAJ52G.js +3475 -0
- package/dist/index.d.ts +115 -6
- package/dist/index.js +7 -3
- package/package.json +8 -5
- package/src/__tests__/golden-path.test.ts +139 -137
- package/src/__tests__/integration.test.ts +179 -178
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -178
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +178 -0
- package/src/__tests__/server.test.ts +71 -67
- package/src/__tests__/tool-count-consistency.test.ts +164 -0
- 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 -403
- 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 -79
- package/src/providers/supabase-dm.ts +122 -117
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +155 -96
- package/src/server.ts +60 -9
- package/src/tools/actions.ts +166 -0
- package/src/tools/attachments.ts +276 -0
- package/src/tools/capabilities.ts +70 -68
- package/src/tools/embed.ts +110 -0
- package/src/tools/index.ts +177 -43
- package/src/tools/knowledge-graph.ts +186 -0
- package/src/tools/knowledge.ts +327 -91
- package/src/tools/mail-agentbox.ts +155 -0
- package/src/tools/mail-aliases.ts +25 -0
- package/src/tools/mail-provenance.ts +137 -0
- package/src/tools/mail.ts +403 -89
- package/src/tools/mcall.ts +216 -0
- package/src/tools/mdm-as-agent.ts +126 -0
- package/src/tools/mdm-voice.ts +220 -0
- package/src/tools/mdm.ts +494 -392
- package/src/tools/mirc-edge.ts +196 -0
- package/src/tools/mirc-members.ts +61 -20
- package/src/tools/mirc-messages.ts +185 -151
- package/src/tools/mirc.ts +104 -10
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -189
- package/src/tools/mtalk.ts +131 -121
- package/src/tools/murl.ts +314 -0
- package/src/tools/presence.ts +333 -0
- package/src/tools/ptt-ingest.ts +99 -0
- package/src/tools/threat.ts +123 -0
- package/src/types.ts +121 -85
- package/src/version.ts +8 -0
- package/src/voice/floor.ts +132 -132
- package/dist/chunk-YDFU6S5E.js +0 -1471
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Symmetric voice + file attachments across channels (mDM, mIRC).
|
|
3
|
+
*
|
|
4
|
+
* GOAL (Lane C): an AGENT must be able to participate in every channel the same
|
|
5
|
+
* way a human does — including sending VOICE notes and FILES, not just text.
|
|
6
|
+
* Today only mDM has a half-measure (`mDM_voice_note`, which takes a pre-uploaded
|
|
7
|
+
* URL and posts a control envelope, NOT a first-class `voice` message via the
|
|
8
|
+
* `message-send` attachments contract). mIRC has no voice/file send at all
|
|
9
|
+
* from the toolkit.
|
|
10
|
+
*
|
|
11
|
+
* These tools accept the blob as base64 (MCP is JSON-only), upload it to Storage
|
|
12
|
+
* via the shared `uploadBlob` helper, then call `message-send` with
|
|
13
|
+
* `message_type: 'voice' | 'file'` and the `attachments[]` descriptor.
|
|
14
|
+
*
|
|
15
|
+
* STATUS: scaffold against the Lane A contract. Compiles and uploads, but the
|
|
16
|
+
* `message-send` call is the not-yet-final shape — see TODO(Lane A) markers.
|
|
17
|
+
* mDM voice additionally needs client-side E2EE sealing before upload (TODO).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
22
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
23
|
+
import { uploadBlob, decodeBase64Body, type AttachmentDescriptor } from "../providers/blob-upload.js";
|
|
24
|
+
|
|
25
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
26
|
+
|
|
27
|
+
// ---- Shared schema fragments ----
|
|
28
|
+
|
|
29
|
+
const Base64Body = z
|
|
30
|
+
.string()
|
|
31
|
+
.min(1)
|
|
32
|
+
.max(20_000_000)
|
|
33
|
+
.describe("Attachment bytes as base64 (or a data: URL). MCP is JSON-only, so binary is base64-encoded. Max ~15 MB decoded.");
|
|
34
|
+
|
|
35
|
+
const Mime = z.string().min(1).max(120).describe("MIME type, e.g. audio/ogg, application/pdf, image/png.");
|
|
36
|
+
const DurationMs = z.number().int().positive().optional().describe("Audio clip length in ms (voice only), if known.");
|
|
37
|
+
const Filename = z.string().min(1).max(255).optional().describe("Original filename (file only).");
|
|
38
|
+
const ReplyTo = z.string().optional().describe("Optional id of the message being replied to.");
|
|
39
|
+
|
|
40
|
+
const ContactRef = z.string().min(1).describe("Recipient identity id (UUID). Use mDM_list_contacts to find it.");
|
|
41
|
+
const RoomId = z.string().min(1).describe("Room/channel id (UUID).");
|
|
42
|
+
|
|
43
|
+
const ThreadLabel = z
|
|
44
|
+
.string()
|
|
45
|
+
.max(120)
|
|
46
|
+
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
47
|
+
.optional()
|
|
48
|
+
.describe("Optional thread label within the conversation (mDM USP: multiple threads per contact).");
|
|
49
|
+
|
|
50
|
+
// ---- Helpers ----
|
|
51
|
+
|
|
52
|
+
/** Deterministic DM thread id (sorted pair) — must match mdm.ts dmThreadId. */
|
|
53
|
+
function dmThreadId(selfId: string, otherId: string, label?: string): string {
|
|
54
|
+
const [a, b] = [selfId, otherId].sort();
|
|
55
|
+
return label ? `dm:${a}:${b}:${label}` : `dm:${a}:${b}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Post an attachment message through `message-send`.
|
|
60
|
+
*
|
|
61
|
+
* TODO(Lane A): lock the exact body shape. Current best-guess from the contract:
|
|
62
|
+
* { space_id, thread_id, message_type: 'voice'|'file', attachments: [desc],
|
|
63
|
+
* encrypted_payload: <caption envelope>, protocol_version, reply_to_id }
|
|
64
|
+
* Open questions for Lane A:
|
|
65
|
+
* - is `attachments` a top-level field on message-send, or nested under the
|
|
66
|
+
* payload? (assumed top-level here)
|
|
67
|
+
* - does voice/file still require a (possibly empty) encrypted_payload caption?
|
|
68
|
+
* - mDM: who seals the blob + mints key_ref — this tool, or the EF?
|
|
69
|
+
*/
|
|
70
|
+
async function sendAttachmentMessage(
|
|
71
|
+
ctx: MosaddToolContext,
|
|
72
|
+
args: {
|
|
73
|
+
space_id: string;
|
|
74
|
+
thread_id: string;
|
|
75
|
+
message_type: "voice" | "file";
|
|
76
|
+
attachment: AttachmentDescriptor;
|
|
77
|
+
caption?: string;
|
|
78
|
+
reply_to_id?: string;
|
|
79
|
+
recipient_account_id?: string;
|
|
80
|
+
},
|
|
81
|
+
): Promise<{ message_id: string; delivered_at: string; thread_id: string }> {
|
|
82
|
+
readSupabaseEnv();
|
|
83
|
+
const captionEnvelope = {
|
|
84
|
+
v: PROTOCOL_VERSION,
|
|
85
|
+
type: args.message_type,
|
|
86
|
+
text: args.caption ?? "",
|
|
87
|
+
sent_at: new Date().toISOString(),
|
|
88
|
+
};
|
|
89
|
+
const encrypted_payload = Buffer.from(JSON.stringify(captionEnvelope), "utf8").toString("base64");
|
|
90
|
+
|
|
91
|
+
ctx.log("debug", "sendAttachmentMessage", {
|
|
92
|
+
thread_id: args.thread_id,
|
|
93
|
+
message_type: args.message_type,
|
|
94
|
+
bucket: args.attachment.bucket,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const data = await invokeFunction<{ message?: { id: string; created_at: string } }>("message-send", {
|
|
98
|
+
space_id: args.space_id,
|
|
99
|
+
thread_id: args.thread_id,
|
|
100
|
+
message_type: args.message_type,
|
|
101
|
+
attachments: [args.attachment], // TODO(Lane A): confirm field placement
|
|
102
|
+
encrypted_payload,
|
|
103
|
+
protocol_version: PROTOCOL_VERSION,
|
|
104
|
+
recipient_account_id: args.recipient_account_id ?? null,
|
|
105
|
+
reply_to_id: args.reply_to_id ?? null,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (!data?.message?.id) throw new Error("message-send returned no message id (attachment)");
|
|
109
|
+
return {
|
|
110
|
+
message_id: data.message.id,
|
|
111
|
+
delivered_at: data.message.created_at ?? new Date().toISOString(),
|
|
112
|
+
thread_id: args.thread_id,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Resolve a channel's backing space id — mirrors mirc-messages.ts. */
|
|
117
|
+
async function resolveLinkedSpaceId(channelId: string): Promise<string> {
|
|
118
|
+
type GetResponse = { channel?: { metadata?: { linked_space_id?: string } | null }; linked_space_id?: string };
|
|
119
|
+
const data = await invokeFunction<GetResponse>("channel-manage", { action: "get", channel_id: channelId });
|
|
120
|
+
const spaceId = data?.linked_space_id ?? data?.channel?.metadata?.linked_space_id;
|
|
121
|
+
if (!spaceId) throw new Error(`Channel ${channelId} has no linked space yet — run mIRC_join/mIRC_list first or pass space_id.`);
|
|
122
|
+
return spaceId;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ====================================================================
|
|
126
|
+
// mDM voice/file
|
|
127
|
+
// ====================================================================
|
|
128
|
+
|
|
129
|
+
const mDM_send_voice_input = z.object({
|
|
130
|
+
to: ContactRef,
|
|
131
|
+
audio_base64: Base64Body,
|
|
132
|
+
mime: Mime.default("audio/ogg"),
|
|
133
|
+
duration_ms: DurationMs,
|
|
134
|
+
caption: z.string().max(2_000).optional(),
|
|
135
|
+
thread_label: ThreadLabel,
|
|
136
|
+
reply_to_id: ReplyTo,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const mDM_send_file_input = z.object({
|
|
140
|
+
to: ContactRef,
|
|
141
|
+
file_base64: Base64Body,
|
|
142
|
+
filename: Filename,
|
|
143
|
+
mime: Mime,
|
|
144
|
+
caption: z.string().max(2_000).optional(),
|
|
145
|
+
thread_label: ThreadLabel,
|
|
146
|
+
reply_to_id: ReplyTo,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
async function mDM_send_voice(input: z.infer<typeof mDM_send_voice_input>, ctx: MosaddToolContext) {
|
|
150
|
+
const dm = ctx.providers.dm;
|
|
151
|
+
const selfId = await dm.selfId();
|
|
152
|
+
const thread_id = dmThreadId(selfId, input.to, input.thread_label);
|
|
153
|
+
// TODO(Lane A/E2EE): mDM is client-encrypted — seal these bytes per-recipient
|
|
154
|
+
// via @mosadd/crypto and pass the wrapped content key as key_ref BEFORE upload.
|
|
155
|
+
// Uploading plaintext here is a scaffold-only shortcut.
|
|
156
|
+
const attachment = await uploadBlob({
|
|
157
|
+
kind: "voice",
|
|
158
|
+
bytes: decodeBase64Body(input.audio_base64),
|
|
159
|
+
mime: input.mime,
|
|
160
|
+
prefix: thread_id,
|
|
161
|
+
duration_ms: input.duration_ms,
|
|
162
|
+
});
|
|
163
|
+
return sendAttachmentMessage(ctx, {
|
|
164
|
+
space_id: "dm",
|
|
165
|
+
thread_id,
|
|
166
|
+
message_type: "voice",
|
|
167
|
+
attachment,
|
|
168
|
+
caption: input.caption,
|
|
169
|
+
reply_to_id: input.reply_to_id,
|
|
170
|
+
recipient_account_id: input.to,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function mDM_send_file(input: z.infer<typeof mDM_send_file_input>, ctx: MosaddToolContext) {
|
|
175
|
+
const dm = ctx.providers.dm;
|
|
176
|
+
const selfId = await dm.selfId();
|
|
177
|
+
const thread_id = dmThreadId(selfId, input.to, input.thread_label);
|
|
178
|
+
// TODO(Lane A/E2EE): seal as above for mDM.
|
|
179
|
+
const attachment = await uploadBlob({
|
|
180
|
+
kind: "file",
|
|
181
|
+
bytes: decodeBase64Body(input.file_base64),
|
|
182
|
+
mime: input.mime,
|
|
183
|
+
prefix: thread_id,
|
|
184
|
+
filename: input.filename,
|
|
185
|
+
});
|
|
186
|
+
return sendAttachmentMessage(ctx, {
|
|
187
|
+
space_id: "dm",
|
|
188
|
+
thread_id,
|
|
189
|
+
message_type: "file",
|
|
190
|
+
attachment,
|
|
191
|
+
caption: input.caption,
|
|
192
|
+
reply_to_id: input.reply_to_id,
|
|
193
|
+
recipient_account_id: input.to,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ====================================================================
|
|
198
|
+
// mIRC voice/file
|
|
199
|
+
// ====================================================================
|
|
200
|
+
|
|
201
|
+
const mIRC_send_voice_input = z.object({
|
|
202
|
+
channel_id: RoomId,
|
|
203
|
+
audio_base64: Base64Body,
|
|
204
|
+
mime: Mime.default("audio/ogg"),
|
|
205
|
+
duration_ms: DurationMs,
|
|
206
|
+
caption: z.string().max(2_000).optional(),
|
|
207
|
+
reply_to_id: ReplyTo,
|
|
208
|
+
space_id: z.string().optional().describe("Backing space id. Resolved automatically if omitted."),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const mIRC_send_file_input = z.object({
|
|
212
|
+
channel_id: RoomId,
|
|
213
|
+
file_base64: Base64Body,
|
|
214
|
+
filename: Filename,
|
|
215
|
+
mime: Mime,
|
|
216
|
+
caption: z.string().max(2_000).optional(),
|
|
217
|
+
reply_to_id: ReplyTo,
|
|
218
|
+
space_id: z.string().optional(),
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
async function mIRC_send_voice(input: z.infer<typeof mIRC_send_voice_input>, ctx: MosaddToolContext) {
|
|
222
|
+
const space_id = input.space_id ?? (await resolveLinkedSpaceId(input.channel_id));
|
|
223
|
+
const thread_id = `chat:${input.channel_id}`;
|
|
224
|
+
const attachment = await uploadBlob({ kind: "voice", bytes: decodeBase64Body(input.audio_base64), mime: input.mime, prefix: thread_id, duration_ms: input.duration_ms });
|
|
225
|
+
return sendAttachmentMessage(ctx, { space_id, thread_id, message_type: "voice", attachment, caption: input.caption, reply_to_id: input.reply_to_id });
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function mIRC_send_file(input: z.infer<typeof mIRC_send_file_input>, ctx: MosaddToolContext) {
|
|
229
|
+
const space_id = input.space_id ?? (await resolveLinkedSpaceId(input.channel_id));
|
|
230
|
+
const thread_id = `chat:${input.channel_id}`;
|
|
231
|
+
const attachment = await uploadBlob({ kind: "file", bytes: decodeBase64Body(input.file_base64), mime: input.mime, prefix: thread_id, filename: input.filename });
|
|
232
|
+
return sendAttachmentMessage(ctx, { space_id, thread_id, message_type: "file", attachment, caption: input.caption, reply_to_id: input.reply_to_id });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ---- Registration ----
|
|
236
|
+
|
|
237
|
+
export const attachmentTools: MosaddTool[] = [
|
|
238
|
+
{
|
|
239
|
+
name: "mDM_send_voice",
|
|
240
|
+
title: "Send a voice message",
|
|
241
|
+
annotations: { readOnlyHint: false },
|
|
242
|
+
requires: "network",
|
|
243
|
+
description:
|
|
244
|
+
"Send a VOICE note to a contact as a first-class mDM `voice` message (not a URL stub). Pass audio bytes as base64; the tool uploads to Storage and posts via message-send with the voice attachment descriptor. SCAFFOLD: mDM is client-encrypted — until the seal step ships, bytes are uploaded unsealed (TODO Lane A).",
|
|
245
|
+
inputSchema: mDM_send_voice_input,
|
|
246
|
+
handler: mDM_send_voice as MosaddTool["handler"],
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
name: "mDM_send_file",
|
|
250
|
+
title: "Send a file in DM",
|
|
251
|
+
annotations: { readOnlyHint: false },
|
|
252
|
+
requires: "network",
|
|
253
|
+
description:
|
|
254
|
+
"Send a FILE to a contact as a first-class mDM `file` message. Base64 bytes → Storage → message-send file attachment. SCAFFOLD: mDM E2EE seal is TODO (Lane A).",
|
|
255
|
+
inputSchema: mDM_send_file_input,
|
|
256
|
+
handler: mDM_send_file as MosaddTool["handler"],
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
name: "mIRC_send_voice",
|
|
260
|
+
title: "Send voice note to channel",
|
|
261
|
+
annotations: { readOnlyHint: false },
|
|
262
|
+
requires: "network",
|
|
263
|
+
description: "Send a VOICE note into a persistent channel as a first-class `voice` message. Base64 audio → Storage → message-send (backing space resolved automatically).",
|
|
264
|
+
inputSchema: mIRC_send_voice_input,
|
|
265
|
+
handler: mIRC_send_voice as MosaddTool["handler"],
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
name: "mIRC_send_file",
|
|
269
|
+
title: "Send file to channel",
|
|
270
|
+
annotations: { readOnlyHint: false },
|
|
271
|
+
requires: "network",
|
|
272
|
+
description: "Send a FILE into a persistent channel as a first-class `file` message. Base64 bytes → Storage → message-send.",
|
|
273
|
+
inputSchema: mIRC_send_file_input,
|
|
274
|
+
handler: mIRC_send_file as MosaddTool["handler"],
|
|
275
|
+
},
|
|
276
|
+
];
|
|
@@ -1,68 +1,70 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* comms_capabilities — capability discovery for consuming hosts.
|
|
3
|
-
*
|
|
4
|
-
* THE contract surface between mosadd-os and carrier-aware hosts (e.g.
|
|
5
|
-
* cymru-os, which filters tools by available transports — see cymru-os RFC
|
|
6
|
-
* 0002 D-Bus "Capability-flag bridge"). A host spawns `@mosadd/mcp` over stdio
|
|
7
|
-
* and calls `comms_capabilities` once to learn which tools are usable on which
|
|
8
|
-
* carrier, then gates the tool list accordingly (e.g. off-grid radio →
|
|
9
|
-
* only `requires: "any" | "radio"` tools are offered).
|
|
10
|
-
*
|
|
11
|
-
* `requires: "any"` here too — discovery must work on any transport, including
|
|
12
|
-
* off-grid radio, otherwise a host couldn't learn what it's allowed to call.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { z } from "zod";
|
|
16
|
-
import type { CapabilityRequirement, MosaddTool } from "../types.js";
|
|
17
|
-
|
|
18
|
-
export interface CapabilityEntry {
|
|
19
|
-
name: string;
|
|
20
|
-
requires: CapabilityRequirement;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CapabilityManifest {
|
|
24
|
-
/** Stable contract version. Bump on any enum/shape change (requires:cymru-input). */
|
|
25
|
-
contract_version: string;
|
|
26
|
-
/** The finalized capability enum, echoed so hosts can validate against it. */
|
|
27
|
-
requirements: CapabilityRequirement[];
|
|
28
|
-
/** Flat list of every tool and its transport requirement. */
|
|
29
|
-
tools: CapabilityEntry[];
|
|
30
|
-
/** Pre-grouped by requirement for cheap host-side filtering. */
|
|
31
|
-
by_requirement: Record<CapabilityRequirement, string[]>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const CONTRACT_VERSION = "1.0.0";
|
|
35
|
-
const ALL_REQUIREMENTS: CapabilityRequirement[] = ["any", "radio", "network"];
|
|
36
|
-
|
|
37
|
-
/** Build the capability manifest from a tool list. Pure, host-agnostic. */
|
|
38
|
-
export function buildCapabilityManifest(tools: MosaddTool[]): CapabilityManifest {
|
|
39
|
-
const entries: CapabilityEntry[] = tools.map((t) => ({ name: t.name, requires: t.requires }));
|
|
40
|
-
const by_requirement: Record<CapabilityRequirement, string[]> = {
|
|
41
|
-
any: [],
|
|
42
|
-
radio: [],
|
|
43
|
-
network: [],
|
|
44
|
-
};
|
|
45
|
-
for (const e of entries) by_requirement[e.requires].push(e.name);
|
|
46
|
-
return {
|
|
47
|
-
contract_version: CONTRACT_VERSION,
|
|
48
|
-
requirements: ALL_REQUIREMENTS,
|
|
49
|
-
tools: entries,
|
|
50
|
-
by_requirement,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Factory: build the comms_capabilities tool over a given tool set. The tool
|
|
56
|
-
* itself is appended to that set, so it self-reports too (requires: "any").
|
|
57
|
-
*/
|
|
58
|
-
export function makeCapabilitiesTool(channelTools: MosaddTool[]): MosaddTool {
|
|
59
|
-
const tool: MosaddTool = {
|
|
60
|
-
name: "comms_capabilities",
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* comms_capabilities — capability discovery for consuming hosts.
|
|
3
|
+
*
|
|
4
|
+
* THE contract surface between mosadd-os and carrier-aware hosts (e.g.
|
|
5
|
+
* cymru-os, which filters tools by available transports — see cymru-os RFC
|
|
6
|
+
* 0002 D-Bus "Capability-flag bridge"). A host spawns `@mosadd/mcp` over stdio
|
|
7
|
+
* and calls `comms_capabilities` once to learn which tools are usable on which
|
|
8
|
+
* carrier, then gates the tool list accordingly (e.g. off-grid radio →
|
|
9
|
+
* only `requires: "any" | "radio"` tools are offered).
|
|
10
|
+
*
|
|
11
|
+
* `requires: "any"` here too — discovery must work on any transport, including
|
|
12
|
+
* off-grid radio, otherwise a host couldn't learn what it's allowed to call.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import type { CapabilityRequirement, MosaddTool } from "../types.js";
|
|
17
|
+
|
|
18
|
+
export interface CapabilityEntry {
|
|
19
|
+
name: string;
|
|
20
|
+
requires: CapabilityRequirement;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CapabilityManifest {
|
|
24
|
+
/** Stable contract version. Bump on any enum/shape change (requires:cymru-input). */
|
|
25
|
+
contract_version: string;
|
|
26
|
+
/** The finalized capability enum, echoed so hosts can validate against it. */
|
|
27
|
+
requirements: CapabilityRequirement[];
|
|
28
|
+
/** Flat list of every tool and its transport requirement. */
|
|
29
|
+
tools: CapabilityEntry[];
|
|
30
|
+
/** Pre-grouped by requirement for cheap host-side filtering. */
|
|
31
|
+
by_requirement: Record<CapabilityRequirement, string[]>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const CONTRACT_VERSION = "1.0.0";
|
|
35
|
+
const ALL_REQUIREMENTS: CapabilityRequirement[] = ["any", "radio", "network"];
|
|
36
|
+
|
|
37
|
+
/** Build the capability manifest from a tool list. Pure, host-agnostic. */
|
|
38
|
+
export function buildCapabilityManifest(tools: MosaddTool[]): CapabilityManifest {
|
|
39
|
+
const entries: CapabilityEntry[] = tools.map((t) => ({ name: t.name, requires: t.requires }));
|
|
40
|
+
const by_requirement: Record<CapabilityRequirement, string[]> = {
|
|
41
|
+
any: [],
|
|
42
|
+
radio: [],
|
|
43
|
+
network: [],
|
|
44
|
+
};
|
|
45
|
+
for (const e of entries) by_requirement[e.requires].push(e.name);
|
|
46
|
+
return {
|
|
47
|
+
contract_version: CONTRACT_VERSION,
|
|
48
|
+
requirements: ALL_REQUIREMENTS,
|
|
49
|
+
tools: entries,
|
|
50
|
+
by_requirement,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Factory: build the comms_capabilities tool over a given tool set. The tool
|
|
56
|
+
* itself is appended to that set, so it self-reports too (requires: "any").
|
|
57
|
+
*/
|
|
58
|
+
export function makeCapabilitiesTool(channelTools: MosaddTool[]): MosaddTool {
|
|
59
|
+
const tool: MosaddTool = {
|
|
60
|
+
name: "comms_capabilities",
|
|
61
|
+
title: "List tool capabilities",
|
|
62
|
+
annotations: { readOnlyHint: true },
|
|
63
|
+
requires: "any",
|
|
64
|
+
description:
|
|
65
|
+
"Discover which mosadd tools are available and what transport each requires (any | radio | network). Carrier-aware hosts (e.g. off-grid radio devices) call this once to gate the tool list to what the current carrier supports. Returns a manifest with per-tool requirements grouped by transport.",
|
|
66
|
+
inputSchema: z.object({}),
|
|
67
|
+
handler: async () => buildCapabilityManifest([...channelTools, tool]),
|
|
68
|
+
};
|
|
69
|
+
return tool;
|
|
70
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* comms_embed — let a builder-agent drop a LIVE mosadd channel into the app/site
|
|
3
|
+
* it is generating.
|
|
4
|
+
*
|
|
5
|
+
* Mints a publishable embed key (`m_pk_live_…`) scoped to a channel + the app's
|
|
6
|
+
* domain(s) via the `embed-keys` Edge Function, and returns a paste-in HTML
|
|
7
|
+
* snippet that loads the mosadd widget from the CDN (embed.mosadd.com/v1.js).
|
|
8
|
+
*
|
|
9
|
+
* Security: the publishable key is safe to ship in HTML — it can ONLY mint
|
|
10
|
+
* channel-scoped JWTs (the widget exchanges it at runtime via `mirc-embed-token`,
|
|
11
|
+
* validated against the key's origin allow-list). Your hub key (`mosadd_sk_live_…`)
|
|
12
|
+
* never enters the browser. The action is owned by the calling user (user-JWT
|
|
13
|
+
* scoped — `mosadd login` / MOSADD_USER_JWT / hub-key exchange).
|
|
14
|
+
*
|
|
15
|
+
* Reuses the shipped embed infra (apps/embed widget + embed-keys + mirc-embed-token).
|
|
16
|
+
* RFC 0001: channel-agnostic capability → `comms_` namespace (not an m<MODULE>_ prefix).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
21
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
22
|
+
|
|
23
|
+
/** Widget CDN base. Override with MOSADD_EMBED_CDN for self-host / dev. */
|
|
24
|
+
const EMBED_CDN = (process.env.MOSADD_EMBED_CDN ?? "https://embed.mosadd.com").replace(/\/+$/, "");
|
|
25
|
+
|
|
26
|
+
const POSITIONS = ["inline", "sidebar-left", "sidebar-right", "floating-bl", "floating-br", "fullscreen"] as const;
|
|
27
|
+
const SKINS = ["default", "retro-irc-1990", "terminal", "minimal-dark", "minimal-light"] as const;
|
|
28
|
+
|
|
29
|
+
const embed_create_input = z.object({
|
|
30
|
+
channel: z
|
|
31
|
+
.string().min(1).max(128).regex(/^[A-Za-z0-9_-]+$/)
|
|
32
|
+
.describe("The mIRC channel id everyone on the embed shares (e.g. 'acme-support'). Auto-created on first message if new."),
|
|
33
|
+
allowed_origins: z
|
|
34
|
+
.array(z.string().min(1)).min(1).max(10)
|
|
35
|
+
.describe("REQUIRED. Domain(s) where the widget will be served — the key only works from these origins. Use the host(s) of the app you're building, e.g. ['app.acme.com','*.acme.com']. For local testing use mode:'test' and e.g. 'localhost:3000'."),
|
|
36
|
+
name: z.string().max(64).optional().describe("Label for the key in the owner's dashboard. Default 'embed: <channel>'."),
|
|
37
|
+
position: z.enum(POSITIONS).optional().describe("Where the widget renders. Default 'inline'."),
|
|
38
|
+
skin: z.enum(SKINS).optional().describe("Visual skin. Default 'default' (mosadd-branded retro mIRC)."),
|
|
39
|
+
title: z.string().max(60).optional().describe("Header title shown in the widget. Default 'mIRC'."),
|
|
40
|
+
anon: z.boolean().optional().describe("Allow visitors to join with just a nick (no mosadd account). Default true."),
|
|
41
|
+
locale: z.enum(["en", "pl"]).optional().describe("Widget UI language. Default 'en'."),
|
|
42
|
+
mode: z.enum(["live", "test"]).optional().describe("Key mode — 'test' for local/dev origins, 'live' for production. Default 'live'."),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
interface EmbedKeyResult {
|
|
46
|
+
id?: string;
|
|
47
|
+
key?: string;
|
|
48
|
+
pk_prefix?: string;
|
|
49
|
+
allowed_channels?: string[];
|
|
50
|
+
error?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface EmbedCreateResult {
|
|
54
|
+
embed_key: string;
|
|
55
|
+
channel: string;
|
|
56
|
+
allowed_origins: string[];
|
|
57
|
+
snippet: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** HTML-attr-safe: drop quotes/angle brackets (inputs are already length-bounded). */
|
|
61
|
+
const attrSafe = (s: string): string => s.replace(/["'<>]/g, "");
|
|
62
|
+
|
|
63
|
+
function buildSnippet(input: z.infer<typeof embed_create_input>, key: string): string {
|
|
64
|
+
const attrs = [`data-channel="${attrSafe(input.channel)}"`, `data-position="${input.position ?? "inline"}"`];
|
|
65
|
+
if (input.skin) attrs.push(`data-skin="${input.skin}"`);
|
|
66
|
+
if (input.title) attrs.push(`data-title="${attrSafe(input.title)}"`);
|
|
67
|
+
if (input.anon === false) attrs.push(`data-anon="false"`);
|
|
68
|
+
if (input.locale) attrs.push(`data-locale="${input.locale}"`);
|
|
69
|
+
return (
|
|
70
|
+
`<div id="mosadd-mirc" ${attrs.join(" ")}></div>\n` +
|
|
71
|
+
`<script src="${EMBED_CDN}/v1.js" data-key="${key}"></script>`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function embed_create(
|
|
76
|
+
input: z.infer<typeof embed_create_input>,
|
|
77
|
+
ctx: MosaddToolContext,
|
|
78
|
+
): Promise<EmbedCreateResult> {
|
|
79
|
+
readSupabaseEnv();
|
|
80
|
+
ctx.log("debug", "comms_embed_create minting embed key", { channel: input.channel, origins: input.allowed_origins.length });
|
|
81
|
+
|
|
82
|
+
const res = await invokeFunction<EmbedKeyResult>("embed-keys", {
|
|
83
|
+
name: input.name ?? `embed: ${input.channel}`,
|
|
84
|
+
channels: [input.channel],
|
|
85
|
+
allowed_origins: input.allowed_origins,
|
|
86
|
+
mode: input.mode ?? "live",
|
|
87
|
+
});
|
|
88
|
+
if (res.error) throw new Error(res.error);
|
|
89
|
+
if (!res.key) throw new Error("embed-keys returned no publishable key (missing `key`).");
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
embed_key: res.key,
|
|
93
|
+
channel: input.channel,
|
|
94
|
+
allowed_origins: input.allowed_origins,
|
|
95
|
+
snippet: buildSnippet(input, res.key),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const embedTools: MosaddTool[] = [
|
|
100
|
+
{
|
|
101
|
+
name: "comms_embed_create",
|
|
102
|
+
title: "Create website embed",
|
|
103
|
+
annotations: { readOnlyHint: false },
|
|
104
|
+
requires: "network",
|
|
105
|
+
description:
|
|
106
|
+
"Embed a LIVE mosadd chat channel into an app/site you're building (great for builder-agents on Lovable/Bolt/Cursor/v0). Mints a publishable embed key scoped to a channel + the app's domain(s) and returns a ready-to-paste HTML snippet (a <div> + <script src=embed.mosadd.com/v1.js data-key=…>). Visitors of the host page chat in real time (anonymous nick or mosadd login). REQUIRES `allowed_origins` — the domain(s) the app is served from (the key only works there). The publishable key is browser-safe (mints only channel-scoped JWTs); your hub key never leaves the server. Returns { embed_key, channel, allowed_origins, snippet }.",
|
|
107
|
+
inputSchema: embed_create_input,
|
|
108
|
+
handler: embed_create as MosaddTool["handler"],
|
|
109
|
+
},
|
|
110
|
+
];
|