@mosadd/mcp 3.0.0-alpha.0

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.
@@ -0,0 +1,170 @@
1
+ /**
2
+ * mIRC — Persistent Channels.
3
+ *
4
+ * IRC-style persistent channels (Discord/Slack semantics). Channels live forever
5
+ * until deleted, members come and go. Distinct from mROOM (ephemeral, no-account).
6
+ *
7
+ * Phase 1 alpha: wired to m0ssad-3 `channel-manage` Edge Function (strangler-fig).
8
+ * The single edge fn dispatches on `action` — we expose each action as its own
9
+ * MCP tool for clarity.
10
+ */
11
+
12
+ import { z } from "zod";
13
+ import type { MosaddTool, MosaddToolContext } from "../types.js";
14
+ import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
15
+
16
+ // ---- Schemas ----
17
+
18
+ const ChannelId = z
19
+ .string()
20
+ .min(1)
21
+ .max(120)
22
+ .describe("Channel id (UUID). Use mIRC_list to discover available channels.");
23
+
24
+ const AccessMode = z
25
+ .enum(["public", "private", "invite_only"])
26
+ .describe("public: anyone joins · private: requires invite + password · invite_only: invite only.");
27
+
28
+ const Capabilities = z
29
+ .object({
30
+ txt: z.boolean().default(true),
31
+ ptt: z.boolean().default(false),
32
+ live: z.boolean().default(false),
33
+ })
34
+ .describe("Which modes are enabled in this channel: txt (text), ptt (push-to-talk voice), live (live stream).")
35
+ .optional();
36
+
37
+ const mIRC_create_input = z.object({
38
+ name: z.string().min(1).max(80).describe("Channel display name."),
39
+ topic: z.string().max(500).optional().describe("Optional channel topic/description."),
40
+ access_mode: AccessMode.default("public"),
41
+ password: z.string().min(6).max(120).optional().describe("Password (only for access_mode=private)."),
42
+ capabilities: Capabilities,
43
+ });
44
+
45
+ const mIRC_list_input = z.object({
46
+ limit: z.number().int().min(1).max(500).default(100).optional(),
47
+ offset: z.number().int().min(0).default(0).optional(),
48
+ access_mode: AccessMode.optional().describe("Filter to a single access mode."),
49
+ });
50
+
51
+ const mIRC_get_input = z.object({
52
+ channel_id: ChannelId,
53
+ });
54
+
55
+ const mIRC_update_input = z.object({
56
+ channel_id: ChannelId,
57
+ name: z.string().min(1).max(80).optional(),
58
+ topic: z.string().max(500).optional(),
59
+ access_mode: AccessMode.optional(),
60
+ capabilities: Capabilities,
61
+ });
62
+
63
+ const mIRC_delete_input = z.object({
64
+ channel_id: ChannelId,
65
+ });
66
+
67
+ // ---- Handlers ----
68
+
69
+ async function mIRC_create(
70
+ input: z.infer<typeof mIRC_create_input>,
71
+ ctx: MosaddToolContext,
72
+ ): Promise<{ channel: unknown }> {
73
+ readSupabaseEnv();
74
+ ctx.log("debug", "mIRC_create invoking channel-manage", { name: input.name });
75
+ return await invokeFunction<{ channel: unknown }>("channel-manage", {
76
+ action: "create",
77
+ ...input,
78
+ });
79
+ }
80
+
81
+ async function mIRC_list(
82
+ input: z.infer<typeof mIRC_list_input>,
83
+ ctx: MosaddToolContext,
84
+ ): Promise<{ channels: unknown[] }> {
85
+ readSupabaseEnv();
86
+ ctx.log("debug", "mIRC_list invoking channel-manage");
87
+ return await invokeFunction<{ channels: unknown[] }>("channel-manage", {
88
+ action: "list",
89
+ ...input,
90
+ });
91
+ }
92
+
93
+ async function mIRC_get(
94
+ input: z.infer<typeof mIRC_get_input>,
95
+ ctx: MosaddToolContext,
96
+ ): Promise<{ channel: unknown }> {
97
+ readSupabaseEnv();
98
+ ctx.log("debug", "mIRC_get invoking channel-manage", { channel_id: input.channel_id });
99
+ return await invokeFunction<{ channel: unknown }>("channel-manage", {
100
+ action: "get",
101
+ channel_id: input.channel_id,
102
+ });
103
+ }
104
+
105
+ async function mIRC_update(
106
+ input: z.infer<typeof mIRC_update_input>,
107
+ ctx: MosaddToolContext,
108
+ ): Promise<{ channel: unknown }> {
109
+ readSupabaseEnv();
110
+ ctx.log("debug", "mIRC_update invoking channel-manage", { channel_id: input.channel_id });
111
+ return await invokeFunction<{ channel: unknown }>("channel-manage", {
112
+ action: "update",
113
+ ...input,
114
+ });
115
+ }
116
+
117
+ async function mIRC_delete(
118
+ input: z.infer<typeof mIRC_delete_input>,
119
+ ctx: MosaddToolContext,
120
+ ): Promise<{ deleted: true }> {
121
+ readSupabaseEnv();
122
+ ctx.log("debug", "mIRC_delete invoking channel-manage", { channel_id: input.channel_id });
123
+ await invokeFunction<{ deleted: boolean }>("channel-manage", {
124
+ action: "delete",
125
+ channel_id: input.channel_id,
126
+ });
127
+ return { deleted: true };
128
+ }
129
+
130
+ // ---- Registration ----
131
+
132
+ export const mircTools: MosaddTool[] = [
133
+ {
134
+ name: "mIRC_create",
135
+ requires: "network",
136
+ description:
137
+ "Create a new persistent channel (Discord/Slack-style). Set access_mode to public for open join, private for password-gated, invite_only for explicit invites. capabilities controls modes (txt/ptt/live).",
138
+ inputSchema: mIRC_create_input,
139
+ handler: mIRC_create as MosaddTool["handler"],
140
+ },
141
+ {
142
+ name: "mIRC_list",
143
+ requires: "network",
144
+ description:
145
+ "List channels available to the current user. Optionally filter by access_mode.",
146
+ inputSchema: mIRC_list_input,
147
+ handler: mIRC_list as MosaddTool["handler"],
148
+ },
149
+ {
150
+ name: "mIRC_get",
151
+ requires: "network",
152
+ description: "Get full details of a single channel by id.",
153
+ inputSchema: mIRC_get_input,
154
+ handler: mIRC_get as MosaddTool["handler"],
155
+ },
156
+ {
157
+ name: "mIRC_update",
158
+ requires: "network",
159
+ description: "Update channel metadata (name, topic, access_mode, capabilities). Owner only.",
160
+ inputSchema: mIRC_update_input,
161
+ handler: mIRC_update as MosaddTool["handler"],
162
+ },
163
+ {
164
+ name: "mIRC_delete",
165
+ requires: "network",
166
+ description: "Delete a channel and cascade-remove its members. Owner only.",
167
+ inputSchema: mIRC_delete_input,
168
+ handler: mIRC_delete as MosaddTool["handler"],
169
+ },
170
+ ];
@@ -0,0 +1,142 @@
1
+ /**
2
+ * mROOM text mode — send + list messages inside a private room.
3
+ *
4
+ * mROOM_send_message and mROOM_list_messages route through the same
5
+ * m0ssad-3 `message-send` / `message-list` Edge Functions that mDM uses,
6
+ * but with `space_id: 'rooms'` and `thread_id: 'privroom:<room_id>'` so the
7
+ * RLS policies apply room-membership checks rather than DM contact checks.
8
+ *
9
+ * Voice in rooms (PTT, audio) lands separately via mTALK_start_session
10
+ * over the WebRTC daemon — see LINEAR-2145.
11
+ */
12
+
13
+ import { z } from "zod";
14
+ import type { MosaddTool, MosaddToolContext } from "../types.js";
15
+ import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
16
+
17
+ const PROTOCOL_VERSION = "mosadd.chat.v1";
18
+
19
+ const RoomId = z.string().min(1).describe("Private room id (UUID). From mROOM_create / mROOM_list.");
20
+
21
+ const mROOM_send_message_input = z.object({
22
+ room_id: RoomId,
23
+ text: z.string().min(1).max(64_000).describe("Message body (UTF-8). Max 64 KB."),
24
+ reply_to_id: z.string().optional().describe("Optional id of the message being replied to."),
25
+ });
26
+
27
+ const mROOM_list_messages_input = z.object({
28
+ room_id: RoomId,
29
+ limit: z.number().int().min(1).max(200).default(50).optional(),
30
+ cursor: z.string().optional().describe("Opaque pagination cursor from a prior list response."),
31
+ });
32
+
33
+ function packPlaintextPayload(text: string, replyToId?: string): string {
34
+ const envelope = {
35
+ v: PROTOCOL_VERSION,
36
+ type: "text",
37
+ text,
38
+ reply_to: replyToId ?? null,
39
+ sent_at: new Date().toISOString(),
40
+ };
41
+ return Buffer.from(JSON.stringify(envelope), "utf8").toString("base64");
42
+ }
43
+
44
+ function unpackPayload(payload: string): { text: string } {
45
+ try {
46
+ const json = Buffer.from(payload, "base64").toString("utf8");
47
+ const obj = JSON.parse(json);
48
+ if (typeof obj?.text === "string") return obj;
49
+ } catch {
50
+ /* fall through */
51
+ }
52
+ return { text: "<ciphertext>" };
53
+ }
54
+
55
+ async function mROOM_send_message(
56
+ input: z.infer<typeof mROOM_send_message_input>,
57
+ ctx: MosaddToolContext,
58
+ ): Promise<{ message_id: string; delivered_at: string; thread_id: string }> {
59
+ readSupabaseEnv();
60
+ const thread_id = `privroom:${input.room_id}`;
61
+ ctx.log("debug", "mROOM_send_message", { thread_id });
62
+
63
+ const data = await invokeFunction<{ message?: { id: string; created_at: string } }>(
64
+ "message-send",
65
+ {
66
+ space_id: "rooms",
67
+ thread_id,
68
+ encrypted_payload: packPlaintextPayload(input.text, input.reply_to_id),
69
+ message_type: "text",
70
+ protocol_version: PROTOCOL_VERSION,
71
+ reply_to_id: input.reply_to_id ?? null,
72
+ },
73
+ );
74
+
75
+ if (!data?.message?.id) {
76
+ throw new Error("message-send returned no message id");
77
+ }
78
+
79
+ return {
80
+ message_id: data.message.id,
81
+ delivered_at: data.message.created_at ?? new Date().toISOString(),
82
+ thread_id,
83
+ };
84
+ }
85
+
86
+ async function mROOM_list_messages(
87
+ input: z.infer<typeof mROOM_list_messages_input>,
88
+ ctx: MosaddToolContext,
89
+ ): Promise<{
90
+ messages: Array<{ id: string; sender_identity_id: string; text: string; timestamp: string }>;
91
+ next_cursor: string | null;
92
+ }> {
93
+ readSupabaseEnv();
94
+ const thread_id = `privroom:${input.room_id}`;
95
+ ctx.log("debug", "mROOM_list_messages", { thread_id });
96
+
97
+ type MessageListResponse = {
98
+ messages?: Array<{
99
+ id: string;
100
+ sender_identity_id: string;
101
+ encrypted_payload: string;
102
+ created_at: string;
103
+ }>;
104
+ next_cursor?: string | null;
105
+ };
106
+
107
+ const data = await invokeFunction<MessageListResponse>("message-list", {
108
+ space_id: "rooms",
109
+ thread_id,
110
+ limit: input.limit ?? 50,
111
+ cursor: input.cursor,
112
+ });
113
+
114
+ return {
115
+ messages: (data?.messages ?? []).map((m) => ({
116
+ id: m.id,
117
+ sender_identity_id: m.sender_identity_id,
118
+ text: unpackPayload(m.encrypted_payload).text,
119
+ timestamp: m.created_at,
120
+ })),
121
+ next_cursor: data?.next_cursor ?? null,
122
+ };
123
+ }
124
+
125
+ export const mroomMessagesTools: MosaddTool[] = [
126
+ {
127
+ name: "mROOM_send_message",
128
+ requires: "network",
129
+ description:
130
+ "Send a text message into a private room. Authorisation: must be a member (or guest with a valid token). Alpha payload is plaintext base64; v0.2 wraps with Double Ratchet.",
131
+ inputSchema: mROOM_send_message_input,
132
+ handler: mROOM_send_message as MosaddTool["handler"],
133
+ },
134
+ {
135
+ name: "mROOM_list_messages",
136
+ requires: "network",
137
+ description:
138
+ "List recent text messages in a private room. Cursor-paginated, newest first.",
139
+ inputSchema: mROOM_list_messages_input,
140
+ handler: mROOM_list_messages as MosaddTool["handler"],
141
+ },
142
+ ];
@@ -0,0 +1,189 @@
1
+ /**
2
+ * mROOM — Ephemeral rooms with no-account join links.
3
+ *
4
+ * USP: anyone can join via a short-lived link without signing up. Distinct from
5
+ * mIRC (persistent channels for known users). Use cases: customer support drop-in
6
+ * call, live-stream after-party, anonymous tipline, incident bridge.
7
+ *
8
+ * Phase 1 alpha: wired to m0ssad-3 `privroom-manage` + `privroom-guest-token`
9
+ * Edge Functions.
10
+ */
11
+
12
+ import { z } from "zod";
13
+ import type { MosaddTool, MosaddToolContext } from "../types.js";
14
+ import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
15
+
16
+ // ---- Schemas ----
17
+
18
+ const RoomId = z.string().min(1).describe("Private room id (UUID).");
19
+
20
+ const mROOM_create_input = z.object({
21
+ ttl_seconds: z
22
+ .number()
23
+ .int()
24
+ .min(60)
25
+ .max(86_400 * 30)
26
+ .default(86_400)
27
+ .optional()
28
+ .describe("Time-to-live in seconds. Defaults to 24 h. Max 30 days."),
29
+ });
30
+
31
+ const mROOM_join_input = z.object({
32
+ room_id: RoomId,
33
+ });
34
+
35
+ const mROOM_close_input = z.object({
36
+ room_id: RoomId,
37
+ });
38
+
39
+ const mROOM_leave_input = z.object({
40
+ room_id: RoomId,
41
+ });
42
+
43
+ const mROOM_list_input = z.object({});
44
+
45
+ const mROOM_create_guest_link_input = z.object({
46
+ room_id: RoomId,
47
+ display_name: z
48
+ .string()
49
+ .min(1)
50
+ .max(80)
51
+ .describe("Display name the guest will appear under. They never sign up — this is the only identifier."),
52
+ ttl_seconds: z
53
+ .number()
54
+ .int()
55
+ .min(60)
56
+ .max(86_400 * 7)
57
+ .default(3600)
58
+ .optional()
59
+ .describe("How long the guest token is valid. Default 1 h, max 7 days."),
60
+ });
61
+
62
+ // ---- Handlers ----
63
+
64
+ async function mROOM_create(
65
+ input: z.infer<typeof mROOM_create_input>,
66
+ ctx: MosaddToolContext,
67
+ ): Promise<{ room: unknown }> {
68
+ readSupabaseEnv();
69
+ const expires_at = new Date(Date.now() + (input.ttl_seconds ?? 86_400) * 1000).toISOString();
70
+ ctx.log("debug", "mROOM_create invoking privroom-manage", { expires_at });
71
+ return await invokeFunction<{ room: unknown }>("privroom-manage", {
72
+ action: "create",
73
+ expires_at,
74
+ });
75
+ }
76
+
77
+ async function mROOM_join(
78
+ input: z.infer<typeof mROOM_join_input>,
79
+ ctx: MosaddToolContext,
80
+ ): Promise<{ joined: true; room_id: string }> {
81
+ readSupabaseEnv();
82
+ ctx.log("debug", "mROOM_join invoking privroom-manage", { room_id: input.room_id });
83
+ return await invokeFunction<{ joined: true; room_id: string }>("privroom-manage", {
84
+ action: "join",
85
+ room_id: input.room_id,
86
+ });
87
+ }
88
+
89
+ async function mROOM_close(
90
+ input: z.infer<typeof mROOM_close_input>,
91
+ ctx: MosaddToolContext,
92
+ ): Promise<{ closed: true; room_id: string }> {
93
+ readSupabaseEnv();
94
+ ctx.log("debug", "mROOM_close invoking privroom-manage", { room_id: input.room_id });
95
+ return await invokeFunction<{ closed: true; room_id: string }>("privroom-manage", {
96
+ action: "close",
97
+ room_id: input.room_id,
98
+ });
99
+ }
100
+
101
+ async function mROOM_leave(
102
+ input: z.infer<typeof mROOM_leave_input>,
103
+ ctx: MosaddToolContext,
104
+ ): Promise<{ left: true; room_id: string }> {
105
+ readSupabaseEnv();
106
+ ctx.log("debug", "mROOM_leave invoking privroom-manage", { room_id: input.room_id });
107
+ return await invokeFunction<{ left: true; room_id: string }>("privroom-manage", {
108
+ action: "leave",
109
+ room_id: input.room_id,
110
+ });
111
+ }
112
+
113
+ async function mROOM_list(
114
+ _input: z.infer<typeof mROOM_list_input>,
115
+ ctx: MosaddToolContext,
116
+ ): Promise<{ rooms: unknown[] }> {
117
+ readSupabaseEnv();
118
+ ctx.log("debug", "mROOM_list invoking privroom-manage");
119
+ return await invokeFunction<{ rooms: unknown[] }>("privroom-manage", { action: "list" });
120
+ }
121
+
122
+ async function mROOM_create_guest_link(
123
+ input: z.infer<typeof mROOM_create_guest_link_input>,
124
+ ctx: MosaddToolContext,
125
+ ): Promise<{ token: string; join_url: string; expires_at: string }> {
126
+ readSupabaseEnv();
127
+ ctx.log("debug", "mROOM_create_guest_link invoking privroom-guest-token", {
128
+ room_id: input.room_id,
129
+ });
130
+ const result = await invokeFunction<{ token: string; expires_at: string }>("privroom-guest-token", {
131
+ room_id: input.room_id,
132
+ display_name: input.display_name,
133
+ ttl_seconds: input.ttl_seconds ?? 3600,
134
+ });
135
+ return {
136
+ token: result.token,
137
+ join_url: `https://mosadd.com/r/${input.room_id}?t=${result.token}`,
138
+ expires_at: result.expires_at,
139
+ };
140
+ }
141
+
142
+ // ---- Registration ----
143
+
144
+ export const mroomTools: MosaddTool[] = [
145
+ {
146
+ name: "mROOM_create",
147
+ requires: "network",
148
+ description:
149
+ "Create an ephemeral private room. Auto-joins the creator. Defaults to 24 h TTL — pass ttl_seconds to override.",
150
+ inputSchema: mROOM_create_input,
151
+ handler: mROOM_create as MosaddTool["handler"],
152
+ },
153
+ {
154
+ name: "mROOM_create_guest_link",
155
+ requires: "network",
156
+ description:
157
+ "USP: Generate a short-lived join link that lets a person enter the room WITHOUT a mosadd account. Returns { token, join_url, expires_at }. Share the join_url out-of-band (email, SMS, QR). Use case: support call, live-stream after-party, anonymous tipline.",
158
+ inputSchema: mROOM_create_guest_link_input,
159
+ handler: mROOM_create_guest_link as MosaddTool["handler"],
160
+ },
161
+ {
162
+ name: "mROOM_join",
163
+ requires: "network",
164
+ description: "Join an existing private room (current user, not a guest).",
165
+ inputSchema: mROOM_join_input,
166
+ handler: mROOM_join as MosaddTool["handler"],
167
+ },
168
+ {
169
+ name: "mROOM_leave",
170
+ requires: "network",
171
+ description: "Leave a private room.",
172
+ inputSchema: mROOM_leave_input,
173
+ handler: mROOM_leave as MosaddTool["handler"],
174
+ },
175
+ {
176
+ name: "mROOM_close",
177
+ requires: "network",
178
+ description: "Close a private room and remove all members. Founder only.",
179
+ inputSchema: mROOM_close_input,
180
+ handler: mROOM_close as MosaddTool["handler"],
181
+ },
182
+ {
183
+ name: "mROOM_list",
184
+ requires: "network",
185
+ description: "List private rooms the current user belongs to.",
186
+ inputSchema: mROOM_list_input,
187
+ handler: mROOM_list as MosaddTool["handler"],
188
+ },
189
+ ];
package/src/types.ts ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Internal types for the mosadd MCP server.
3
+ */
4
+
5
+ import type { z } from "zod";
6
+ import type { DmProvider } from "@mosadd/providers";
7
+ import type { MdmKeyStore } from "./crypto/mdm-session.js";
8
+
9
+ export type Mode = "cloud" | "local" | "self-host";
10
+
11
+ /**
12
+ * Transport backends per channel. The server fills any missing entry with its
13
+ * default network adapter; a host (e.g. cymru-os) injects alternatives —
14
+ * notably a radio DmProvider — via `MosaddServerOptions.providers`. This is
15
+ * the DI seam that keeps mosadd-os transport-agnostic.
16
+ *
17
+ * `keys` is LOCAL crypto custody (identity + prekeys + ratchet sessions) for
18
+ * mDM E2EE — distinct from transport. Defaults to an in-process keystore; a
19
+ * host may inject a file/keychain-backed one.
20
+ */
21
+ export interface ProviderRegistry {
22
+ dm: DmProvider;
23
+ keys: MdmKeyStore;
24
+ }
25
+
26
+ export interface MosaddServerOptions {
27
+ /** API key for hosted mode. Required if mode === "cloud". */
28
+ apiKey?: string;
29
+ /** Override the default hub URL (https://mcp.mosadd.com). */
30
+ hubUrl?: string;
31
+ /** Operating mode. Defaults to "cloud" if apiKey is present, else "local". */
32
+ mode?: Mode;
33
+ /** Log level. */
34
+ logLevel?: "debug" | "info" | "warn" | "error";
35
+ /**
36
+ * Host-injected channel providers. Any omitted channel uses the default
37
+ * network adapter. A carrier-aware host supplies e.g. a radio DmProvider here.
38
+ */
39
+ providers?: Partial<ProviderRegistry>;
40
+ }
41
+
42
+ export interface MosaddToolContext {
43
+ options: Required<Pick<MosaddServerOptions, "mode" | "hubUrl">> & MosaddServerOptions;
44
+ /** Resolved channel providers (defaults merged with host-injected ones). */
45
+ providers: ProviderRegistry;
46
+ /** Per-request logger. */
47
+ log: (level: "debug" | "info" | "warn" | "error", msg: string, extra?: unknown) => void;
48
+ }
49
+
50
+ /**
51
+ * Transport capability a tool requires to function. This is the ONE contract
52
+ * string shared with consuming hosts. e.g. cymru-os gates tools by carrier
53
+ * availability via this flag (cymru-os RFC 0002 D-Bus "Capability-flag bridge").
54
+ * The enum is FINAL and normalized with the cymru-os host; changing these
55
+ * values must be coordinated (Linear label `requires:cymru-input`).
56
+ *
57
+ * - "any" works over any transport, incl. off-grid radio (small,
58
+ * packet-friendly payloads). e.g. text DM.
59
+ * - "radio" radio-specific path (e.g. Codec2 audio frames). Off-grid OK.
60
+ * - "network" needs IP connectivity (server-hosted channels, email, public
61
+ * join links, bridges). NOT available off-grid.
62
+ */
63
+ export type CapabilityRequirement = "any" | "radio" | "network";
64
+
65
+ export interface MosaddTool<TInput = unknown, TOutput = unknown> {
66
+ /** Tool name. MUST follow the m{MODULE}_{operation} convention. */
67
+ name: string;
68
+ /** Short human-readable description. Shown to the model. */
69
+ description: string;
70
+ /**
71
+ * Transport this tool requires. Consuming hosts read this to gate tools by
72
+ * available carriers. Every tool MUST declare it explicitly.
73
+ */
74
+ requires: CapabilityRequirement;
75
+ /** Zod schema for input. */
76
+ inputSchema: z.ZodType<TInput>;
77
+ /** Tool handler. */
78
+ handler: (input: TInput, ctx: MosaddToolContext) => Promise<TOutput>;
79
+ }