@mosadd/mcp 3.0.0-alpha.32 → 3.0.0-alpha.33
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.js +1 -1
- package/package.json +15 -15
- 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/LICENSE +0 -201
- package/dist/bin/login.d.ts +0 -13
- package/dist/bin/mcp.d.ts +0 -1
- package/dist/index.d.ts +0 -266
package/src/bin/mcp.ts
CHANGED
|
@@ -1,145 +1,145 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* `mosadd-mcp` (also runnable as `npx @mosadd/mcp`)
|
|
4
|
-
*
|
|
5
|
-
* Starts a mosadd MCP server over stdio. The standard transport for local
|
|
6
|
-
* MCP clients (Claude Code, Cursor, Cline, Windsurf, Goose, ...).
|
|
7
|
-
*
|
|
8
|
-
* For HTTP/SSE transport (hosted mcp.mosadd.com), see packages/mcp-gateway
|
|
9
|
-
* in the proprietary repo.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
13
|
-
import { createClient } from "@supabase/supabase-js";
|
|
14
|
-
import { createMosaddServer } from "../server.js";
|
|
15
|
-
import { loadSession, saveSession, isSessionExpired } from "../config.js";
|
|
16
|
-
|
|
17
|
-
const DEFAULT_EXCHANGE =
|
|
18
|
-
"https://rooffhgbxafyjcwmwpsy.supabase.co/functions/v1/hub-key-exchange";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Hosted-hub path: when MOSADD_API_KEY is set, exchange it for a short-lived
|
|
22
|
-
* session (URL + anon key + access token) so the tools act as the key's owner.
|
|
23
|
-
* Removes the DevTools-JWT wall — paste one stable key, done. Falls through
|
|
24
|
-
* silently if the key is unset or the exchange is unreachable (so BYOK/self-host
|
|
25
|
-
* keep working).
|
|
26
|
-
*/
|
|
27
|
-
async function bootstrapFromApiKey(): Promise<void> {
|
|
28
|
-
const apiKey = process.env.MOSADD_API_KEY;
|
|
29
|
-
if (!apiKey || process.env.MOSADD_USER_JWT) return;
|
|
30
|
-
const endpoint = process.env.MOSADD_HUB_URL
|
|
31
|
-
? `${process.env.MOSADD_HUB_URL.replace(/\/$/, "")}/hub-key-exchange`
|
|
32
|
-
: DEFAULT_EXCHANGE;
|
|
33
|
-
try {
|
|
34
|
-
const res = await fetch(endpoint, {
|
|
35
|
-
method: "POST",
|
|
36
|
-
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
|
|
37
|
-
body: "{}",
|
|
38
|
-
});
|
|
39
|
-
if (!res.ok) {
|
|
40
|
-
process.stderr.write(
|
|
41
|
-
JSON.stringify({ level: "warn", msg: "mosadd hub key exchange failed", status: res.status }) + "\n",
|
|
42
|
-
);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const d = (await res.json()) as { url?: string; anon_key?: string; access_token?: string };
|
|
46
|
-
if (d.url) process.env.MOSADD_SUPABASE_URL = d.url;
|
|
47
|
-
if (d.anon_key) process.env.MOSADD_SUPABASE_ANON_KEY = d.anon_key;
|
|
48
|
-
if (d.access_token) process.env.MOSADD_USER_JWT = d.access_token;
|
|
49
|
-
} catch (err) {
|
|
50
|
-
process.stderr.write(
|
|
51
|
-
JSON.stringify({ level: "warn", msg: "mosadd hub unreachable", error: String(err) }) + "\n",
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* `mosadd login` path: refresh the saved session at boot so a stored token that
|
|
58
|
-
* expired since the last launch (Supabase access tokens last ~1h) is renewed
|
|
59
|
-
* silently from its refresh token — instead of failing the first tool call with
|
|
60
|
-
* "session expired, run `mosadd login` again". This makes a single `mosadd login`
|
|
61
|
-
* durable across restarts (the founder's "set once, forget" expectation). No-op
|
|
62
|
-
* when an explicit env JWT or hub key already resolved a session, when there is no
|
|
63
|
-
* saved session, or when the saved token is still valid.
|
|
64
|
-
*/
|
|
65
|
-
async function bootstrapFromSavedSession(): Promise<void> {
|
|
66
|
-
if (process.env.MOSADD_USER_JWT) return;
|
|
67
|
-
const session = loadSession();
|
|
68
|
-
if (!session?.refreshToken) return;
|
|
69
|
-
// Still-valid token → the provider uses it as-is; don't churn the refresh token.
|
|
70
|
-
if (!isSessionExpired(session)) return;
|
|
71
|
-
try {
|
|
72
|
-
const client = createClient(session.url, session.anonKey, {
|
|
73
|
-
auth: { persistSession: false, autoRefreshToken: false, detectSessionInUrl: false },
|
|
74
|
-
});
|
|
75
|
-
const { data, error } = await client.auth.refreshSession({ refresh_token: session.refreshToken });
|
|
76
|
-
if (error || !data.session) {
|
|
77
|
-
process.stderr.write(
|
|
78
|
-
JSON.stringify({ level: "warn", msg: "mosadd session refresh failed — run `mosadd login`", error: error?.message }) + "\n",
|
|
79
|
-
);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
saveSession({
|
|
83
|
-
url: session.url,
|
|
84
|
-
anonKey: session.anonKey,
|
|
85
|
-
accessToken: data.session.access_token,
|
|
86
|
-
refreshToken: data.session.refresh_token ?? session.refreshToken,
|
|
87
|
-
expiresAt: data.session.expires_at,
|
|
88
|
-
email: data.user?.email ?? session.email,
|
|
89
|
-
});
|
|
90
|
-
} catch (err) {
|
|
91
|
-
process.stderr.write(
|
|
92
|
-
JSON.stringify({ level: "warn", msg: "mosadd session refresh error — run `mosadd login`", error: String(err) }) + "\n",
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async function main(): Promise<void> {
|
|
98
|
-
// Auth subcommands: `mosadd login | logout | whoami`. Everything else starts the server.
|
|
99
|
-
const sub = process.argv[2];
|
|
100
|
-
if (sub === "login" || sub === "logout" || sub === "whoami") {
|
|
101
|
-
const { runAuthCommand } = await import("./login.js");
|
|
102
|
-
await runAuthCommand(sub, process.argv.slice(3));
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Hosted: trade MOSADD_API_KEY for a live session before the server reads env.
|
|
107
|
-
await bootstrapFromApiKey();
|
|
108
|
-
// `mosadd login`: renew a saved session whose access token expired since last launch.
|
|
109
|
-
await bootstrapFromSavedSession();
|
|
110
|
-
|
|
111
|
-
const server = createMosaddServer({
|
|
112
|
-
apiKey: process.env.MOSADD_API_KEY,
|
|
113
|
-
hubUrl: process.env.MOSADD_HUB_URL,
|
|
114
|
-
mode: (process.env.MOSADD_MODE as "cloud" | "local" | "self-host" | undefined),
|
|
115
|
-
logLevel: (process.env.MOSADD_LOG_LEVEL as "debug" | "info" | "warn" | "error" | undefined),
|
|
116
|
-
// stdio is single-tenant: the session lives in env (set by bootstrapFromApiKey
|
|
117
|
-
// above), so auto-publish this identity's prekeys on boot → mDM is reachable.
|
|
118
|
-
autoPublishKeys: true,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
const transport = new StdioServerTransport();
|
|
122
|
-
await server.connect(transport);
|
|
123
|
-
|
|
124
|
-
// The server now runs until stdin closes.
|
|
125
|
-
process.stderr.write(
|
|
126
|
-
JSON.stringify({
|
|
127
|
-
level: "info",
|
|
128
|
-
msg: "mosadd MCP server ready",
|
|
129
|
-
version: "3.0.0-alpha.0",
|
|
130
|
-
ts: new Date().toISOString(),
|
|
131
|
-
}) + "\n",
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
main().catch((err) => {
|
|
136
|
-
process.stderr.write(
|
|
137
|
-
JSON.stringify({
|
|
138
|
-
level: "error",
|
|
139
|
-
msg: "mosadd MCP server failed to start",
|
|
140
|
-
error: err instanceof Error ? err.message : String(err),
|
|
141
|
-
stack: err instanceof Error ? err.stack : undefined,
|
|
142
|
-
}) + "\n",
|
|
143
|
-
);
|
|
144
|
-
process.exit(1);
|
|
145
|
-
});
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `mosadd-mcp` (also runnable as `npx @mosadd/mcp`)
|
|
4
|
+
*
|
|
5
|
+
* Starts a mosadd MCP server over stdio. The standard transport for local
|
|
6
|
+
* MCP clients (Claude Code, Cursor, Cline, Windsurf, Goose, ...).
|
|
7
|
+
*
|
|
8
|
+
* For HTTP/SSE transport (hosted mcp.mosadd.com), see packages/mcp-gateway
|
|
9
|
+
* in the proprietary repo.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
13
|
+
import { createClient } from "@supabase/supabase-js";
|
|
14
|
+
import { createMosaddServer } from "../server.js";
|
|
15
|
+
import { loadSession, saveSession, isSessionExpired } from "../config.js";
|
|
16
|
+
|
|
17
|
+
const DEFAULT_EXCHANGE =
|
|
18
|
+
"https://rooffhgbxafyjcwmwpsy.supabase.co/functions/v1/hub-key-exchange";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Hosted-hub path: when MOSADD_API_KEY is set, exchange it for a short-lived
|
|
22
|
+
* session (URL + anon key + access token) so the tools act as the key's owner.
|
|
23
|
+
* Removes the DevTools-JWT wall — paste one stable key, done. Falls through
|
|
24
|
+
* silently if the key is unset or the exchange is unreachable (so BYOK/self-host
|
|
25
|
+
* keep working).
|
|
26
|
+
*/
|
|
27
|
+
async function bootstrapFromApiKey(): Promise<void> {
|
|
28
|
+
const apiKey = process.env.MOSADD_API_KEY;
|
|
29
|
+
if (!apiKey || process.env.MOSADD_USER_JWT) return;
|
|
30
|
+
const endpoint = process.env.MOSADD_HUB_URL
|
|
31
|
+
? `${process.env.MOSADD_HUB_URL.replace(/\/$/, "")}/hub-key-exchange`
|
|
32
|
+
: DEFAULT_EXCHANGE;
|
|
33
|
+
try {
|
|
34
|
+
const res = await fetch(endpoint, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
|
|
37
|
+
body: "{}",
|
|
38
|
+
});
|
|
39
|
+
if (!res.ok) {
|
|
40
|
+
process.stderr.write(
|
|
41
|
+
JSON.stringify({ level: "warn", msg: "mosadd hub key exchange failed", status: res.status }) + "\n",
|
|
42
|
+
);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const d = (await res.json()) as { url?: string; anon_key?: string; access_token?: string };
|
|
46
|
+
if (d.url) process.env.MOSADD_SUPABASE_URL = d.url;
|
|
47
|
+
if (d.anon_key) process.env.MOSADD_SUPABASE_ANON_KEY = d.anon_key;
|
|
48
|
+
if (d.access_token) process.env.MOSADD_USER_JWT = d.access_token;
|
|
49
|
+
} catch (err) {
|
|
50
|
+
process.stderr.write(
|
|
51
|
+
JSON.stringify({ level: "warn", msg: "mosadd hub unreachable", error: String(err) }) + "\n",
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* `mosadd login` path: refresh the saved session at boot so a stored token that
|
|
58
|
+
* expired since the last launch (Supabase access tokens last ~1h) is renewed
|
|
59
|
+
* silently from its refresh token — instead of failing the first tool call with
|
|
60
|
+
* "session expired, run `mosadd login` again". This makes a single `mosadd login`
|
|
61
|
+
* durable across restarts (the founder's "set once, forget" expectation). No-op
|
|
62
|
+
* when an explicit env JWT or hub key already resolved a session, when there is no
|
|
63
|
+
* saved session, or when the saved token is still valid.
|
|
64
|
+
*/
|
|
65
|
+
async function bootstrapFromSavedSession(): Promise<void> {
|
|
66
|
+
if (process.env.MOSADD_USER_JWT) return;
|
|
67
|
+
const session = loadSession();
|
|
68
|
+
if (!session?.refreshToken) return;
|
|
69
|
+
// Still-valid token → the provider uses it as-is; don't churn the refresh token.
|
|
70
|
+
if (!isSessionExpired(session)) return;
|
|
71
|
+
try {
|
|
72
|
+
const client = createClient(session.url, session.anonKey, {
|
|
73
|
+
auth: { persistSession: false, autoRefreshToken: false, detectSessionInUrl: false },
|
|
74
|
+
});
|
|
75
|
+
const { data, error } = await client.auth.refreshSession({ refresh_token: session.refreshToken });
|
|
76
|
+
if (error || !data.session) {
|
|
77
|
+
process.stderr.write(
|
|
78
|
+
JSON.stringify({ level: "warn", msg: "mosadd session refresh failed — run `mosadd login`", error: error?.message }) + "\n",
|
|
79
|
+
);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
saveSession({
|
|
83
|
+
url: session.url,
|
|
84
|
+
anonKey: session.anonKey,
|
|
85
|
+
accessToken: data.session.access_token,
|
|
86
|
+
refreshToken: data.session.refresh_token ?? session.refreshToken,
|
|
87
|
+
expiresAt: data.session.expires_at,
|
|
88
|
+
email: data.user?.email ?? session.email,
|
|
89
|
+
});
|
|
90
|
+
} catch (err) {
|
|
91
|
+
process.stderr.write(
|
|
92
|
+
JSON.stringify({ level: "warn", msg: "mosadd session refresh error — run `mosadd login`", error: String(err) }) + "\n",
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function main(): Promise<void> {
|
|
98
|
+
// Auth subcommands: `mosadd login | logout | whoami`. Everything else starts the server.
|
|
99
|
+
const sub = process.argv[2];
|
|
100
|
+
if (sub === "login" || sub === "logout" || sub === "whoami") {
|
|
101
|
+
const { runAuthCommand } = await import("./login.js");
|
|
102
|
+
await runAuthCommand(sub, process.argv.slice(3));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Hosted: trade MOSADD_API_KEY for a live session before the server reads env.
|
|
107
|
+
await bootstrapFromApiKey();
|
|
108
|
+
// `mosadd login`: renew a saved session whose access token expired since last launch.
|
|
109
|
+
await bootstrapFromSavedSession();
|
|
110
|
+
|
|
111
|
+
const server = createMosaddServer({
|
|
112
|
+
apiKey: process.env.MOSADD_API_KEY,
|
|
113
|
+
hubUrl: process.env.MOSADD_HUB_URL,
|
|
114
|
+
mode: (process.env.MOSADD_MODE as "cloud" | "local" | "self-host" | undefined),
|
|
115
|
+
logLevel: (process.env.MOSADD_LOG_LEVEL as "debug" | "info" | "warn" | "error" | undefined),
|
|
116
|
+
// stdio is single-tenant: the session lives in env (set by bootstrapFromApiKey
|
|
117
|
+
// above), so auto-publish this identity's prekeys on boot → mDM is reachable.
|
|
118
|
+
autoPublishKeys: true,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const transport = new StdioServerTransport();
|
|
122
|
+
await server.connect(transport);
|
|
123
|
+
|
|
124
|
+
// The server now runs until stdin closes.
|
|
125
|
+
process.stderr.write(
|
|
126
|
+
JSON.stringify({
|
|
127
|
+
level: "info",
|
|
128
|
+
msg: "mosadd MCP server ready",
|
|
129
|
+
version: "3.0.0-alpha.0",
|
|
130
|
+
ts: new Date().toISOString(),
|
|
131
|
+
}) + "\n",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
main().catch((err) => {
|
|
136
|
+
process.stderr.write(
|
|
137
|
+
JSON.stringify({
|
|
138
|
+
level: "error",
|
|
139
|
+
msg: "mosadd MCP server failed to start",
|
|
140
|
+
error: err instanceof Error ? err.message : String(err),
|
|
141
|
+
stack: err instanceof Error ? err.stack : undefined,
|
|
142
|
+
}) + "\n",
|
|
143
|
+
);
|
|
144
|
+
process.exit(1);
|
|
145
|
+
});
|
package/src/config.ts
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local session config for the mosadd CLI (`mosadd login`).
|
|
3
|
-
*
|
|
4
|
-
* Stores the authenticated session at `~/.mosadd/session.json` so the MCP
|
|
5
|
-
* server can pick up credentials without the user pasting an expiring JWT
|
|
6
|
-
* from mosadd.com DevTools into env vars. Env vars still take precedence.
|
|
7
|
-
*
|
|
8
|
-
* The file holds a Supabase session (BYOK): the project URL + anon key (both
|
|
9
|
-
* public, RLS-gated) and the user's access/refresh tokens.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { homedir } from "node:os";
|
|
13
|
-
import { join } from "node:path";
|
|
14
|
-
import { mkdirSync, readFileSync, writeFileSync, rmSync, existsSync, chmodSync } from "node:fs";
|
|
15
|
-
|
|
16
|
-
export interface MosaddSession {
|
|
17
|
-
url: string;
|
|
18
|
-
anonKey: string;
|
|
19
|
-
accessToken: string;
|
|
20
|
-
refreshToken?: string;
|
|
21
|
-
/** Unix seconds when the access token expires. */
|
|
22
|
-
expiresAt?: number;
|
|
23
|
-
email?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function configDir(): string {
|
|
27
|
-
return join(homedir(), ".mosadd");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function sessionPath(): string {
|
|
31
|
-
return join(configDir(), "session.json");
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function loadSession(): MosaddSession | null {
|
|
35
|
-
try {
|
|
36
|
-
const raw = readFileSync(sessionPath(), "utf8");
|
|
37
|
-
const s = JSON.parse(raw) as MosaddSession;
|
|
38
|
-
if (s && typeof s.url === "string" && typeof s.anonKey === "string") return s;
|
|
39
|
-
return null;
|
|
40
|
-
} catch {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function saveSession(session: MosaddSession): void {
|
|
46
|
-
mkdirSync(configDir(), { recursive: true });
|
|
47
|
-
writeFileSync(sessionPath(), JSON.stringify(session, null, 2), "utf8");
|
|
48
|
-
// Best-effort tighten perms (no-op on platforms that ignore it).
|
|
49
|
-
try {
|
|
50
|
-
chmodSync(sessionPath(), 0o600);
|
|
51
|
-
} catch {
|
|
52
|
-
/* ignore */
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function clearSession(): boolean {
|
|
57
|
-
if (!existsSync(sessionPath())) return false;
|
|
58
|
-
rmSync(sessionPath());
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** True if the stored token is past (or within 30s of) its expiry. */
|
|
63
|
-
export function isSessionExpired(session: MosaddSession): boolean {
|
|
64
|
-
if (!session.expiresAt) return false;
|
|
65
|
-
return Date.now() / 1000 >= session.expiresAt - 30;
|
|
66
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Local session config for the mosadd CLI (`mosadd login`).
|
|
3
|
+
*
|
|
4
|
+
* Stores the authenticated session at `~/.mosadd/session.json` so the MCP
|
|
5
|
+
* server can pick up credentials without the user pasting an expiring JWT
|
|
6
|
+
* from mosadd.com DevTools into env vars. Env vars still take precedence.
|
|
7
|
+
*
|
|
8
|
+
* The file holds a Supabase session (BYOK): the project URL + anon key (both
|
|
9
|
+
* public, RLS-gated) and the user's access/refresh tokens.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { mkdirSync, readFileSync, writeFileSync, rmSync, existsSync, chmodSync } from "node:fs";
|
|
15
|
+
|
|
16
|
+
export interface MosaddSession {
|
|
17
|
+
url: string;
|
|
18
|
+
anonKey: string;
|
|
19
|
+
accessToken: string;
|
|
20
|
+
refreshToken?: string;
|
|
21
|
+
/** Unix seconds when the access token expires. */
|
|
22
|
+
expiresAt?: number;
|
|
23
|
+
email?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function configDir(): string {
|
|
27
|
+
return join(homedir(), ".mosadd");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function sessionPath(): string {
|
|
31
|
+
return join(configDir(), "session.json");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function loadSession(): MosaddSession | null {
|
|
35
|
+
try {
|
|
36
|
+
const raw = readFileSync(sessionPath(), "utf8");
|
|
37
|
+
const s = JSON.parse(raw) as MosaddSession;
|
|
38
|
+
if (s && typeof s.url === "string" && typeof s.anonKey === "string") return s;
|
|
39
|
+
return null;
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function saveSession(session: MosaddSession): void {
|
|
46
|
+
mkdirSync(configDir(), { recursive: true });
|
|
47
|
+
writeFileSync(sessionPath(), JSON.stringify(session, null, 2), "utf8");
|
|
48
|
+
// Best-effort tighten perms (no-op on platforms that ignore it).
|
|
49
|
+
try {
|
|
50
|
+
chmodSync(sessionPath(), 0o600);
|
|
51
|
+
} catch {
|
|
52
|
+
/* ignore */
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function clearSession(): boolean {
|
|
57
|
+
if (!existsSync(sessionPath())) return false;
|
|
58
|
+
rmSync(sessionPath());
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** True if the stored token is past (or within 30s of) its expiry. */
|
|
63
|
+
export function isSessionExpired(session: MosaddSession): boolean {
|
|
64
|
+
if (!session.expiresAt) return false;
|
|
65
|
+
return Date.now() / 1000 >= session.expiresAt - 30;
|
|
66
|
+
}
|