@mosadd/mcp 3.0.0-alpha.0 → 3.0.0-alpha.1
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/dist/bin/login.d.ts +13 -0
- package/dist/bin/login.js +110 -0
- package/dist/bin/mcp.js +8 -1
- package/dist/{chunk-25L536JZ.js → chunk-6IA4QVAJ.js} +13 -4
- package/dist/chunk-Z7WR52GE.js +45 -0
- package/dist/index.js +2 -1
- package/package.json +5 -4
- package/src/bin/login.ts +127 -0
- package/src/bin/mcp.ts +8 -0
- package/src/config.ts +66 -0
- package/src/providers/supabase.ts +89 -80
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mosadd login` / `logout` / `whoami` — session helper.
|
|
3
|
+
*
|
|
4
|
+
* Removes the "copy a JWT from mosadd.com DevTools" hack: signs in with
|
|
5
|
+
* Supabase email+password (BYOK project URL + anon key) and stores the session
|
|
6
|
+
* at ~/.mosadd/session.json, which the MCP server reads automatically.
|
|
7
|
+
*
|
|
8
|
+
* Credentials resolution (first found wins): CLI flags → env → interactive
|
|
9
|
+
* prompt. URL/anon also fall back to a previously saved session.
|
|
10
|
+
*/
|
|
11
|
+
declare function runAuthCommand(cmd: string, args: string[]): Promise<void>;
|
|
12
|
+
|
|
13
|
+
export { runAuthCommand };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clearSession,
|
|
3
|
+
isSessionExpired,
|
|
4
|
+
loadSession,
|
|
5
|
+
saveSession,
|
|
6
|
+
sessionPath
|
|
7
|
+
} from "../chunk-Z7WR52GE.js";
|
|
8
|
+
|
|
9
|
+
// src/bin/login.ts
|
|
10
|
+
import { createInterface } from "readline";
|
|
11
|
+
import { createClient } from "@supabase/supabase-js";
|
|
12
|
+
function parseFlags(args) {
|
|
13
|
+
const out = {};
|
|
14
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
15
|
+
const a = args[i];
|
|
16
|
+
if (a.startsWith("--")) {
|
|
17
|
+
const key = a.slice(2);
|
|
18
|
+
const val = args[i + 1] && !args[i + 1].startsWith("--") ? args[i += 1] : "true";
|
|
19
|
+
out[key] = val;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
function prompt(question, hidden = false) {
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
27
|
+
if (hidden) {
|
|
28
|
+
const ro = rl;
|
|
29
|
+
let first = true;
|
|
30
|
+
ro._writeToOutput = (s) => {
|
|
31
|
+
if (first) {
|
|
32
|
+
process.stdout.write(question);
|
|
33
|
+
first = false;
|
|
34
|
+
} else if (s.includes("\n")) {
|
|
35
|
+
process.stdout.write("\n");
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
rl.question(hidden ? "" : question, (answer) => {
|
|
40
|
+
rl.close();
|
|
41
|
+
resolve(answer.trim());
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async function login(args) {
|
|
46
|
+
const flags = parseFlags(args);
|
|
47
|
+
const prev = loadSession();
|
|
48
|
+
const url = flags.url || process.env.M0SSAD_SUPABASE_URL || prev?.url || await prompt("Supabase URL: ");
|
|
49
|
+
const anonKey = flags.anon || process.env.M0SSAD_SUPABASE_ANON_KEY || prev?.anonKey || await prompt("Supabase anon key: ");
|
|
50
|
+
const email = flags.email || process.env.MOSADD_EMAIL || await prompt("Email: ");
|
|
51
|
+
const password = flags.password || process.env.MOSADD_PASSWORD || await prompt("Password: ", true);
|
|
52
|
+
if (!url || !anonKey || !email || !password) {
|
|
53
|
+
console.error("\nmosadd login: need Supabase URL, anon key, email and password.");
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
const client = createClient(url, anonKey, {
|
|
57
|
+
auth: { persistSession: false, autoRefreshToken: false, detectSessionInUrl: false }
|
|
58
|
+
});
|
|
59
|
+
const { data, error } = await client.auth.signInWithPassword({ email, password });
|
|
60
|
+
if (error || !data.session) {
|
|
61
|
+
console.error(`
|
|
62
|
+
mosadd login failed: ${error?.message ?? "no session returned"}`);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
saveSession({
|
|
66
|
+
url,
|
|
67
|
+
anonKey,
|
|
68
|
+
accessToken: data.session.access_token,
|
|
69
|
+
refreshToken: data.session.refresh_token,
|
|
70
|
+
expiresAt: data.session.expires_at,
|
|
71
|
+
email: data.user?.email ?? email
|
|
72
|
+
});
|
|
73
|
+
const when = data.session.expires_at ? new Date(data.session.expires_at * 1e3).toISOString() : "unknown";
|
|
74
|
+
console.log(`
|
|
75
|
+
\u2713 Signed in as ${data.user?.email ?? email}`);
|
|
76
|
+
console.log(` Session saved to ${sessionPath()} (token expires ${when}).`);
|
|
77
|
+
console.log(` The mosadd MCP server will now use it automatically \u2014 no env vars needed.`);
|
|
78
|
+
}
|
|
79
|
+
function whoami() {
|
|
80
|
+
const s = loadSession();
|
|
81
|
+
if (!s) {
|
|
82
|
+
console.log("Not logged in. Run `mosadd login`.");
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
const expired = isSessionExpired(s);
|
|
86
|
+
console.log(`Logged in as ${s.email ?? "(unknown)"} @ ${s.url}`);
|
|
87
|
+
console.log(`Token: ${expired ? "EXPIRED \u2014 run `mosadd login` again" : "valid"}`);
|
|
88
|
+
}
|
|
89
|
+
function logout() {
|
|
90
|
+
console.log(clearSession() ? "\u2713 Logged out (session cleared)." : "No session to clear.");
|
|
91
|
+
}
|
|
92
|
+
async function runAuthCommand(cmd, args) {
|
|
93
|
+
switch (cmd) {
|
|
94
|
+
case "login":
|
|
95
|
+
await login(args);
|
|
96
|
+
break;
|
|
97
|
+
case "whoami":
|
|
98
|
+
whoami();
|
|
99
|
+
break;
|
|
100
|
+
case "logout":
|
|
101
|
+
logout();
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
console.error(`Unknown command: ${cmd}`);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export {
|
|
109
|
+
runAuthCommand
|
|
110
|
+
};
|
package/dist/bin/mcp.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
createMosaddServer
|
|
4
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-6IA4QVAJ.js";
|
|
5
|
+
import "../chunk-Z7WR52GE.js";
|
|
5
6
|
|
|
6
7
|
// src/bin/mcp.ts
|
|
7
8
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
9
|
async function main() {
|
|
10
|
+
const sub = process.argv[2];
|
|
11
|
+
if (sub === "login" || sub === "logout" || sub === "whoami") {
|
|
12
|
+
const { runAuthCommand } = await import("./login.js");
|
|
13
|
+
await runAuthCommand(sub, process.argv.slice(3));
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
9
16
|
const server = createMosaddServer({
|
|
10
17
|
apiKey: process.env.MOSADD_API_KEY,
|
|
11
18
|
hubUrl: process.env.MOSADD_HUB_URL,
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isSessionExpired,
|
|
3
|
+
loadSession
|
|
4
|
+
} from "./chunk-Z7WR52GE.js";
|
|
5
|
+
|
|
1
6
|
// src/tools/mdm.ts
|
|
2
7
|
import { z } from "zod";
|
|
3
8
|
|
|
@@ -6,19 +11,23 @@ import { createClient } from "@supabase/supabase-js";
|
|
|
6
11
|
var MissingSupabaseEnvError = class extends Error {
|
|
7
12
|
constructor(missing) {
|
|
8
13
|
super(
|
|
9
|
-
`Missing Supabase
|
|
14
|
+
`Missing Supabase credentials: ${missing.join(", ")}. Run \`mosadd login\`, or set M0SSAD_SUPABASE_URL, M0SSAD_SUPABASE_ANON_KEY and M0SSAD_USER_JWT.`
|
|
10
15
|
);
|
|
11
16
|
this.name = "MissingSupabaseEnvError";
|
|
12
17
|
}
|
|
13
18
|
};
|
|
14
19
|
function readSupabaseEnv() {
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
20
|
+
const session = loadSession();
|
|
21
|
+
const url = process.env.M0SSAD_SUPABASE_URL ?? session?.url;
|
|
22
|
+
const anonKey = process.env.M0SSAD_SUPABASE_ANON_KEY ?? session?.anonKey;
|
|
23
|
+
const userJwt = process.env.M0SSAD_USER_JWT ?? session?.accessToken;
|
|
18
24
|
const missing = [];
|
|
19
25
|
if (!url) missing.push("M0SSAD_SUPABASE_URL");
|
|
20
26
|
if (!anonKey) missing.push("M0SSAD_SUPABASE_ANON_KEY");
|
|
21
27
|
if (missing.length) throw new MissingSupabaseEnvError(missing);
|
|
28
|
+
if (!process.env.M0SSAD_USER_JWT && session && isSessionExpired(session)) {
|
|
29
|
+
throw new Error("Your mosadd session has expired. Run `mosadd login` again to refresh it.");
|
|
30
|
+
}
|
|
22
31
|
return { url, anonKey, userJwt };
|
|
23
32
|
}
|
|
24
33
|
function getSupabase(env = readSupabaseEnv()) {
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { mkdirSync, readFileSync, writeFileSync, rmSync, existsSync, chmodSync } from "fs";
|
|
5
|
+
function configDir() {
|
|
6
|
+
return join(homedir(), ".mosadd");
|
|
7
|
+
}
|
|
8
|
+
function sessionPath() {
|
|
9
|
+
return join(configDir(), "session.json");
|
|
10
|
+
}
|
|
11
|
+
function loadSession() {
|
|
12
|
+
try {
|
|
13
|
+
const raw = readFileSync(sessionPath(), "utf8");
|
|
14
|
+
const s = JSON.parse(raw);
|
|
15
|
+
if (s && typeof s.url === "string" && typeof s.anonKey === "string") return s;
|
|
16
|
+
return null;
|
|
17
|
+
} catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function saveSession(session) {
|
|
22
|
+
mkdirSync(configDir(), { recursive: true });
|
|
23
|
+
writeFileSync(sessionPath(), JSON.stringify(session, null, 2), "utf8");
|
|
24
|
+
try {
|
|
25
|
+
chmodSync(sessionPath(), 384);
|
|
26
|
+
} catch {
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function clearSession() {
|
|
30
|
+
if (!existsSync(sessionPath())) return false;
|
|
31
|
+
rmSync(sessionPath());
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
function isSessionExpired(session) {
|
|
35
|
+
if (!session.expiresAt) return false;
|
|
36
|
+
return Date.now() / 1e3 >= session.expiresAt - 30;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
sessionPath,
|
|
41
|
+
loadSession,
|
|
42
|
+
saveSession,
|
|
43
|
+
clearSession,
|
|
44
|
+
isSessionExpired
|
|
45
|
+
};
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mosadd/mcp",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "Model Context Protocol server for mosadd. Exposes m* OS modules (mDM, mTALK, mAIL, mCALL, mIRC, mIRL, mROOM, and bridges) as MCP tools to any agent runtime (Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, Bolt, Goose, Manus, custom).",
|
|
5
5
|
"homepage": "https://github.com/Hei33enberg/mosadd-os/tree/main/packages/mcp",
|
|
6
6
|
"repository": {
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"bin": {
|
|
23
|
-
"mosadd-mcp": "./dist/bin/mcp.js"
|
|
23
|
+
"mosadd-mcp": "./dist/bin/mcp.js",
|
|
24
|
+
"mosadd": "./dist/bin/mcp.js"
|
|
24
25
|
},
|
|
25
26
|
"files": [
|
|
26
27
|
"dist",
|
|
@@ -35,8 +36,8 @@
|
|
|
35
36
|
"@modelcontextprotocol/sdk": "^1.0.4",
|
|
36
37
|
"@supabase/supabase-js": "^2.48.0",
|
|
37
38
|
"zod": "^4.3.6",
|
|
38
|
-
"@mosadd/crypto": "3.0.0-alpha.0",
|
|
39
39
|
"@mosadd/providers": "3.0.0-alpha.0",
|
|
40
|
+
"@mosadd/crypto": "3.0.0-alpha.0",
|
|
40
41
|
"@mosadd/protocol": "3.0.0-alpha.0"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
@@ -57,7 +58,7 @@
|
|
|
57
58
|
"messaging"
|
|
58
59
|
],
|
|
59
60
|
"scripts": {
|
|
60
|
-
"build": "tsup src/index.ts src/bin/mcp.ts --format esm --dts --clean --shims",
|
|
61
|
+
"build": "tsup src/index.ts src/bin/mcp.ts src/bin/login.ts --format esm --dts --clean --shims",
|
|
61
62
|
"dev": "tsx src/bin/mcp.ts",
|
|
62
63
|
"test": "vitest run",
|
|
63
64
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
package/src/bin/login.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mosadd login` / `logout` / `whoami` — session helper.
|
|
3
|
+
*
|
|
4
|
+
* Removes the "copy a JWT from mosadd.com DevTools" hack: signs in with
|
|
5
|
+
* Supabase email+password (BYOK project URL + anon key) and stores the session
|
|
6
|
+
* at ~/.mosadd/session.json, which the MCP server reads automatically.
|
|
7
|
+
*
|
|
8
|
+
* Credentials resolution (first found wins): CLI flags → env → interactive
|
|
9
|
+
* prompt. URL/anon also fall back to a previously saved session.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createInterface } from "node:readline";
|
|
13
|
+
import { createClient } from "@supabase/supabase-js";
|
|
14
|
+
import { loadSession, saveSession, clearSession, sessionPath, isSessionExpired } from "../config.js";
|
|
15
|
+
|
|
16
|
+
function parseFlags(args: string[]): Record<string, string> {
|
|
17
|
+
const out: Record<string, string> = {};
|
|
18
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
19
|
+
const a = args[i];
|
|
20
|
+
if (a.startsWith("--")) {
|
|
21
|
+
const key = a.slice(2);
|
|
22
|
+
const val = args[i + 1] && !args[i + 1].startsWith("--") ? args[(i += 1)] : "true";
|
|
23
|
+
out[key] = val;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function prompt(question: string, hidden = false): Promise<string> {
|
|
30
|
+
return new Promise((resolve) => {
|
|
31
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
32
|
+
if (hidden) {
|
|
33
|
+
// Mute echoed characters for password entry.
|
|
34
|
+
const ro = rl as unknown as { _writeToOutput: (s: string) => void; output?: NodeJS.WriteStream };
|
|
35
|
+
let first = true;
|
|
36
|
+
ro._writeToOutput = (s: string) => {
|
|
37
|
+
if (first) {
|
|
38
|
+
process.stdout.write(question);
|
|
39
|
+
first = false;
|
|
40
|
+
} else if (s.includes("\n")) {
|
|
41
|
+
process.stdout.write("\n");
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
rl.question(hidden ? "" : question, (answer) => {
|
|
46
|
+
rl.close();
|
|
47
|
+
resolve(answer.trim());
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function login(args: string[]): Promise<void> {
|
|
53
|
+
const flags = parseFlags(args);
|
|
54
|
+
const prev = loadSession();
|
|
55
|
+
|
|
56
|
+
const url =
|
|
57
|
+
flags.url || process.env.M0SSAD_SUPABASE_URL || prev?.url || (await prompt("Supabase URL: "));
|
|
58
|
+
const anonKey =
|
|
59
|
+
flags.anon ||
|
|
60
|
+
process.env.M0SSAD_SUPABASE_ANON_KEY ||
|
|
61
|
+
prev?.anonKey ||
|
|
62
|
+
(await prompt("Supabase anon key: "));
|
|
63
|
+
const email = flags.email || process.env.MOSADD_EMAIL || (await prompt("Email: "));
|
|
64
|
+
const password = flags.password || process.env.MOSADD_PASSWORD || (await prompt("Password: ", true));
|
|
65
|
+
|
|
66
|
+
if (!url || !anonKey || !email || !password) {
|
|
67
|
+
console.error("\nmosadd login: need Supabase URL, anon key, email and password.");
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const client = createClient(url, anonKey, {
|
|
72
|
+
auth: { persistSession: false, autoRefreshToken: false, detectSessionInUrl: false },
|
|
73
|
+
});
|
|
74
|
+
const { data, error } = await client.auth.signInWithPassword({ email, password });
|
|
75
|
+
if (error || !data.session) {
|
|
76
|
+
console.error(`\nmosadd login failed: ${error?.message ?? "no session returned"}`);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
saveSession({
|
|
81
|
+
url,
|
|
82
|
+
anonKey,
|
|
83
|
+
accessToken: data.session.access_token,
|
|
84
|
+
refreshToken: data.session.refresh_token,
|
|
85
|
+
expiresAt: data.session.expires_at,
|
|
86
|
+
email: data.user?.email ?? email,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const when = data.session.expires_at
|
|
90
|
+
? new Date(data.session.expires_at * 1000).toISOString()
|
|
91
|
+
: "unknown";
|
|
92
|
+
console.log(`\n✓ Signed in as ${data.user?.email ?? email}`);
|
|
93
|
+
console.log(` Session saved to ${sessionPath()} (token expires ${when}).`);
|
|
94
|
+
console.log(` The mosadd MCP server will now use it automatically — no env vars needed.`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function whoami(): void {
|
|
98
|
+
const s = loadSession();
|
|
99
|
+
if (!s) {
|
|
100
|
+
console.log("Not logged in. Run `mosadd login`.");
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
const expired = isSessionExpired(s);
|
|
104
|
+
console.log(`Logged in as ${s.email ?? "(unknown)"} @ ${s.url}`);
|
|
105
|
+
console.log(`Token: ${expired ? "EXPIRED — run `mosadd login` again" : "valid"}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function logout(): void {
|
|
109
|
+
console.log(clearSession() ? "✓ Logged out (session cleared)." : "No session to clear.");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function runAuthCommand(cmd: string, args: string[]): Promise<void> {
|
|
113
|
+
switch (cmd) {
|
|
114
|
+
case "login":
|
|
115
|
+
await login(args);
|
|
116
|
+
break;
|
|
117
|
+
case "whoami":
|
|
118
|
+
whoami();
|
|
119
|
+
break;
|
|
120
|
+
case "logout":
|
|
121
|
+
logout();
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
console.error(`Unknown command: ${cmd}`);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
}
|
package/src/bin/mcp.ts
CHANGED
|
@@ -13,6 +13,14 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
13
13
|
import { createMosaddServer } from "../server.js";
|
|
14
14
|
|
|
15
15
|
async function main(): Promise<void> {
|
|
16
|
+
// Auth subcommands: `mosadd login | logout | whoami`. Everything else starts the server.
|
|
17
|
+
const sub = process.argv[2];
|
|
18
|
+
if (sub === "login" || sub === "logout" || sub === "whoami") {
|
|
19
|
+
const { runAuthCommand } = await import("./login.js");
|
|
20
|
+
await runAuthCommand(sub, process.argv.slice(3));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
16
24
|
const server = createMosaddServer({
|
|
17
25
|
apiKey: process.env.MOSADD_API_KEY,
|
|
18
26
|
hubUrl: process.env.MOSADD_HUB_URL,
|
package/src/config.ts
ADDED
|
@@ -0,0 +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,80 +1,89 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Supabase provider for the alpha MCP server.
|
|
3
|
-
*
|
|
4
|
-
* Local BYOK mode: user supplies their own Supabase URL + anon key + user JWT
|
|
5
|
-
* via env vars. The MCP server creates a per-request Supabase client and
|
|
6
|
-
* invokes the m0ssad-3 Edge Functions on the user's behalf.
|
|
7
|
-
*
|
|
8
|
-
* In Phase 2 this is replaced by the hosted gateway (mcp.mosadd.com) which
|
|
9
|
-
* holds keys server-side via the BYOK broker.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { createClient, type SupabaseClient } from "@supabase/supabase-js";
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Supabase provider for the alpha MCP server.
|
|
3
|
+
*
|
|
4
|
+
* Local BYOK mode: user supplies their own Supabase URL + anon key + user JWT
|
|
5
|
+
* via env vars. The MCP server creates a per-request Supabase client and
|
|
6
|
+
* invokes the m0ssad-3 Edge Functions on the user's behalf.
|
|
7
|
+
*
|
|
8
|
+
* In Phase 2 this is replaced by the hosted gateway (mcp.mosadd.com) which
|
|
9
|
+
* holds keys server-side via the BYOK broker.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createClient, type SupabaseClient } from "@supabase/supabase-js";
|
|
13
|
+
import { loadSession, isSessionExpired } from "../config.js";
|
|
14
|
+
|
|
15
|
+
export interface SupabaseEnv {
|
|
16
|
+
url: string;
|
|
17
|
+
anonKey: string;
|
|
18
|
+
userJwt?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class MissingSupabaseEnvError extends Error {
|
|
22
|
+
constructor(missing: string[]) {
|
|
23
|
+
super(
|
|
24
|
+
`Missing Supabase credentials: ${missing.join(", ")}. ` +
|
|
25
|
+
`Run \`mosadd login\`, or set M0SSAD_SUPABASE_URL, M0SSAD_SUPABASE_ANON_KEY and M0SSAD_USER_JWT.`,
|
|
26
|
+
);
|
|
27
|
+
this.name = "MissingSupabaseEnvError";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function readSupabaseEnv(): SupabaseEnv {
|
|
32
|
+
// Env vars take precedence; fall back to the session saved by `mosadd login`.
|
|
33
|
+
const session = loadSession();
|
|
34
|
+
const url = process.env.M0SSAD_SUPABASE_URL ?? session?.url;
|
|
35
|
+
const anonKey = process.env.M0SSAD_SUPABASE_ANON_KEY ?? session?.anonKey;
|
|
36
|
+
const userJwt = process.env.M0SSAD_USER_JWT ?? session?.accessToken;
|
|
37
|
+
|
|
38
|
+
const missing: string[] = [];
|
|
39
|
+
if (!url) missing.push("M0SSAD_SUPABASE_URL");
|
|
40
|
+
if (!anonKey) missing.push("M0SSAD_SUPABASE_ANON_KEY");
|
|
41
|
+
if (missing.length) throw new MissingSupabaseEnvError(missing);
|
|
42
|
+
|
|
43
|
+
// Surface an expired `mosadd login` token early with an actionable message.
|
|
44
|
+
if (!process.env.M0SSAD_USER_JWT && session && isSessionExpired(session)) {
|
|
45
|
+
throw new Error("Your mosadd session has expired. Run `mosadd login` again to refresh it.");
|
|
46
|
+
}
|
|
47
|
+
return { url: url!, anonKey: anonKey!, userJwt };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getSupabase(env: SupabaseEnv = readSupabaseEnv()): SupabaseClient {
|
|
51
|
+
return createClient(env.url, env.anonKey, {
|
|
52
|
+
auth: {
|
|
53
|
+
persistSession: false,
|
|
54
|
+
autoRefreshToken: false,
|
|
55
|
+
detectSessionInUrl: false,
|
|
56
|
+
},
|
|
57
|
+
global: env.userJwt
|
|
58
|
+
? {
|
|
59
|
+
headers: { Authorization: `Bearer ${env.userJwt}` },
|
|
60
|
+
}
|
|
61
|
+
: undefined,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Invoke a m0ssad-3 Edge Function. Wraps `supabase.functions.invoke` with
|
|
67
|
+
* normalized error handling so MCP tool handlers can throw clean errors that
|
|
68
|
+
* agents can reason about.
|
|
69
|
+
*/
|
|
70
|
+
export async function invokeFunction<T = unknown>(
|
|
71
|
+
fn: string,
|
|
72
|
+
body: Record<string, unknown>,
|
|
73
|
+
): Promise<T> {
|
|
74
|
+
const client = getSupabase();
|
|
75
|
+
const { data, error } = await client.functions.invoke<T>(fn, { body });
|
|
76
|
+
if (error) {
|
|
77
|
+
const msg =
|
|
78
|
+
error instanceof Error
|
|
79
|
+
? error.message
|
|
80
|
+
: typeof (error as { message?: unknown })?.message === "string"
|
|
81
|
+
? (error as { message: string }).message
|
|
82
|
+
: `Edge function ${fn} failed`;
|
|
83
|
+
throw new Error(msg);
|
|
84
|
+
}
|
|
85
|
+
if (data == null) {
|
|
86
|
+
throw new Error(`Edge function ${fn} returned no data`);
|
|
87
|
+
}
|
|
88
|
+
return data;
|
|
89
|
+
}
|