@xmcp-dev/supabase 0.0.0-stage → 0.1.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.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @xmcp-dev/supabase
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Use existing Supabase Auth access tokens and query your database as the authenticated user. `@xmcp-dev/supabase` verifies every HTTP request and provides an isolated Supabase client for row-level security (RLS).
4
+
5
+ ```sh
6
+ pnpm add @xmcp-dev/supabase @supabase/supabase-js
7
+ ```
8
+
9
+ ```ts
10
+ import { supabaseAuth } from "@xmcp-dev/supabase";
11
+
12
+ export default supabaseAuth({
13
+ url: process.env.SUPABASE_URL!,
14
+ publishableKey: process.env.SUPABASE_PUBLISHABLE_KEY!,
15
+ });
16
+ ```
17
+
18
+ Use a publishable key or a legacy `anon` key. Secret and `service_role` keys are rejected. Send `Authorization: Bearer <supabase-access-token>` on every request, including initialize and discovery. The plugin checks the token with the configured project's Auth server using [`getUser(token)`](https://supabase.com/docs/reference/javascript/auth-getuser); it never trusts a decoded session alone. Missing or invalid tokens receive HTTP 401 and a Bearer challenge. Authentication failures cannot fall back to anonymous access.
19
+
20
+ ```ts
21
+ import { getSupabase } from "@xmcp-dev/supabase";
22
+
23
+ export default async function myNotes() {
24
+ const { data, error } = await getSupabase()
25
+ .from("notes")
26
+ .select("id, body")
27
+ .limit(20);
28
+ if (error) throw new Error("Unable to read notes");
29
+ return { content: [{ type: "text", text: JSON.stringify(data) }] };
30
+ }
31
+ ```
32
+
33
+ The request client sends the verified user's token for database queries. It disables persistent sessions and token refresh. `getSupabaseUser()` returns the verified user; both helpers throw outside the active request. User data is never recovered from an earlier stateless HTTP request.
34
+
35
+ Enable RLS and write policies such as `auth.uid() = user_id`. The plugin preserves the user token; it does not create policies or grant table access for you. See [Supabase RLS guidance](https://supabase.com/docs/guides/database/postgres/row-level-security).
36
+
37
+ This integration targets the standalone HTTP transport and Express middleware adapters. It does not implement an OAuth authorization server, refresh clients' tokens, or supply HTTP authentication for STDIO or Fetch-only adapters. Configure clients that already have a Supabase access token; use your existing sign-in flow to obtain and renew it.
38
+
39
+ The [runnable example](https://github.com/basementstudio/xmcp/tree/main/examples/supabase-http) includes a notes table, RLS policy, and instructions for testing two users.
@@ -0,0 +1,11 @@
1
+ import { type SupabaseClient, type User } from "@supabase/supabase-js";
2
+ import { type Middleware } from "xmcp";
3
+ export interface SupabaseAuthOptions {
4
+ url: string;
5
+ /** A publishable key or legacy anon key, never a secret/service-role key. */
6
+ publishableKey: string;
7
+ }
8
+ export declare function getSupabase(): SupabaseClient;
9
+ export declare function getSupabaseUser(): User;
10
+ /** Verify each HTTP request and run downstream tools with its user's RLS client. */
11
+ export declare function supabaseAuth(options: SupabaseAuthOptions): Middleware;
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ import { createClient, } from "@supabase/supabase-js";
2
+ import xmcp from "xmcp";
3
+ const { createContext } = xmcp;
4
+ const session = createContext({
5
+ name: "supabase-request",
6
+ fallback: false,
7
+ });
8
+ export function getSupabase() {
9
+ return session.getContext().client;
10
+ }
11
+ export function getSupabaseUser() {
12
+ return session.getContext().user;
13
+ }
14
+ function isPublicKey(key) {
15
+ if (typeof key !== "string")
16
+ return false;
17
+ if (key.startsWith("sb_publishable_"))
18
+ return true;
19
+ try {
20
+ return (JSON.parse(Buffer.from(key.split(".")[1], "base64url").toString())
21
+ .role === "anon");
22
+ }
23
+ catch {
24
+ return false;
25
+ }
26
+ }
27
+ /** Verify each HTTP request and run downstream tools with its user's RLS client. */
28
+ export function supabaseAuth(options) {
29
+ if (!isPublicKey(options.publishableKey))
30
+ throw new Error("Supabase requires a publishable or legacy anon key.");
31
+ const auth = {
32
+ persistSession: false,
33
+ autoRefreshToken: false,
34
+ detectSessionInUrl: false,
35
+ };
36
+ const verifier = createClient(options.url, options.publishableKey, { auth });
37
+ return async (request, response, next) => {
38
+ const authorization = request.headers.authorization;
39
+ const token = typeof authorization === "string"
40
+ ? /^Bearer ([^\s]+)$/i.exec(authorization)?.[1]
41
+ : undefined;
42
+ const deny = () => {
43
+ response.setHeader("WWW-Authenticate", 'Bearer realm="xmcp"');
44
+ response.status(401).json({ error: "Invalid or missing access token" });
45
+ };
46
+ if (!token) {
47
+ deny();
48
+ return;
49
+ }
50
+ let user;
51
+ try {
52
+ // getUser checks the supplied token with this project's Auth server;
53
+ // getSession alone would only decode untrusted request data.
54
+ const result = await verifier.auth.getUser(token);
55
+ if (result.error || !result.data.user) {
56
+ deny();
57
+ return;
58
+ }
59
+ user = result.data.user;
60
+ }
61
+ catch {
62
+ response
63
+ .status(503)
64
+ .json({ error: "Authentication service unavailable" });
65
+ return;
66
+ }
67
+ const client = createClient(options.url, options.publishableKey, {
68
+ auth,
69
+ // This immutable per-request token is used for every PostgREST query.
70
+ accessToken: async () => token,
71
+ });
72
+ return session.provider({ client, user }, () => next());
73
+ };
74
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@xmcp-dev/supabase",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Supabase integration for xmcp",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "peerDependencies": {
20
+ "@supabase/supabase-js": "^2.117.2",
21
+ "xmcp": "^1.6.0"
22
+ },
23
+ "devDependencies": {
24
+ "@supabase/supabase-js": "^2.117.2",
25
+ "@types/node": "^22.19.2",
26
+ "typescript": "^5.9.3",
27
+ "tsx": "^4.21.0",
28
+ "xmcp": "1.7.1"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/basementstudio/xmcp",
33
+ "directory": "packages/plugins/supabase"
34
+ },
35
+ "scripts": {
36
+ "build": "rm -rf dist && tsc",
37
+ "test": "node --import tsx --test test/*.test.ts"
38
+ }
6
39
  }