@mastra/client-js 1.2.0 → 1.2.1-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.
- package/CHANGELOG.md +14 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +10 -7
- package/dist/client.d.ts +11 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/docs/SKILL.md +32 -22
- package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +1 -1
- package/dist/docs/{server/07-auth0.md → references/docs-server-auth-auth0.md} +68 -79
- package/dist/docs/{server/03-clerk.md → references/docs-server-auth-clerk.md} +41 -52
- package/dist/docs/{server/05-firebase.md → references/docs-server-auth-firebase.md} +83 -97
- package/dist/docs/{server/02-jwt.md → references/docs-server-auth-jwt.md} +46 -35
- package/dist/docs/{server/04-supabase.md → references/docs-server-auth-supabase.md} +33 -44
- package/dist/docs/{server/06-workos.md → references/docs-server-auth-workos.md} +45 -56
- package/dist/docs/{server/01-mastra-client.md → references/docs-server-mastra-client.md} +32 -20
- package/dist/docs/{ai-sdk/01-reference.md → references/reference-ai-sdk-to-ai-sdk-stream.md} +27 -153
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-v4-messages.md +79 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-v5-messages.md +73 -0
- package/dist/docs/references/reference-client-js-agents.md +438 -0
- package/dist/docs/references/reference-client-js-error-handling.md +16 -0
- package/dist/docs/references/reference-client-js-logs.md +24 -0
- package/dist/docs/references/reference-client-js-mastra-client.md +63 -0
- package/dist/docs/references/reference-client-js-memory.md +225 -0
- package/dist/docs/references/reference-client-js-observability.md +72 -0
- package/dist/docs/references/reference-client-js-telemetry.md +20 -0
- package/dist/docs/references/reference-client-js-tools.md +44 -0
- package/dist/docs/references/reference-client-js-vectors.md +79 -0
- package/dist/docs/references/reference-client-js-workflows.md +199 -0
- package/dist/index.cjs +18 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +18 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +158 -27
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/docs/README.md +0 -33
- package/dist/docs/client-js/01-reference.md +0 -1180
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
> Documentation for the MastraAuthFirebase class, which authenticates Mastra applications using Firebase Authentication.
|
|
2
|
-
|
|
3
1
|
# MastraAuthFirebase Class
|
|
4
2
|
|
|
5
3
|
The `MastraAuthFirebase` class provides authentication for Mastra using Firebase Authentication. It verifies incoming requests using Firebase ID tokens and integrates with the Mastra server using the `auth` option.
|
|
@@ -13,16 +11,14 @@ This example uses Firebase Authentication. Make sure to:
|
|
|
13
11
|
3. Generate a service account key from Project Settings > Service Accounts
|
|
14
12
|
4. Download the service account JSON file
|
|
15
13
|
|
|
16
|
-
```env
|
|
14
|
+
```env
|
|
17
15
|
FIREBASE_SERVICE_ACCOUNT=/path/to/your/service-account-key.json
|
|
18
16
|
FIRESTORE_DATABASE_ID=(default)
|
|
19
17
|
# Alternative environment variable names:
|
|
20
18
|
# FIREBASE_DATABASE_ID=(default)
|
|
21
19
|
```
|
|
22
20
|
|
|
23
|
-
> **Note:**
|
|
24
|
-
|
|
25
|
-
Store your service account JSON file securely and never commit it to version control.
|
|
21
|
+
> **Note:** Store your service account JSON file securely and never commit it to version control.
|
|
26
22
|
|
|
27
23
|
## Installation
|
|
28
24
|
|
|
@@ -38,7 +34,7 @@ npm install @mastra/auth-firebase@latest
|
|
|
38
34
|
|
|
39
35
|
If you set the required environment variables (`FIREBASE_SERVICE_ACCOUNT` and `FIRESTORE_DATABASE_ID`), you can initialize `MastraAuthFirebase` without any constructor arguments. The class will automatically read these environment variables as configuration:
|
|
40
36
|
|
|
41
|
-
```typescript
|
|
37
|
+
```typescript
|
|
42
38
|
import { Mastra } from "@mastra/core";
|
|
43
39
|
import { MastraAuthFirebase } from "@mastra/auth-firebase";
|
|
44
40
|
|
|
@@ -52,7 +48,7 @@ export const mastra = new Mastra({
|
|
|
52
48
|
|
|
53
49
|
### Custom configuration
|
|
54
50
|
|
|
55
|
-
```typescript
|
|
51
|
+
```typescript
|
|
56
52
|
import { Mastra } from "@mastra/core";
|
|
57
53
|
import { MastraAuthFirebase } from "@mastra/auth-firebase";
|
|
58
54
|
|
|
@@ -75,15 +71,13 @@ The `MastraAuthFirebase` class can be configured through constructor options or
|
|
|
75
71
|
- `FIREBASE_SERVICE_ACCOUNT`: Path to Firebase service account JSON file
|
|
76
72
|
- `FIRESTORE_DATABASE_ID` or `FIREBASE_DATABASE_ID`: Firestore database ID
|
|
77
73
|
|
|
78
|
-
> **Note:**
|
|
79
|
-
|
|
80
|
-
When constructor options are not provided, the class automatically reads these environment variables. This means you can simply call `new MastraAuthFirebase()` without any arguments if your environment variables are properly configured.
|
|
74
|
+
> **Note:** When constructor options are not provided, the class automatically reads these environment variables. This means you can simply call `new MastraAuthFirebase()` without any arguments if your environment variables are properly configured.
|
|
81
75
|
|
|
82
76
|
### User Authorization
|
|
83
77
|
|
|
84
78
|
By default, `MastraAuthFirebase` uses Firestore to manage user access. It expects a collection named `user_access` with documents keyed by user UIDs. The presence of a document in this collection determines whether a user is authorized.
|
|
85
79
|
|
|
86
|
-
```typescript
|
|
80
|
+
```typescript
|
|
87
81
|
user_access/
|
|
88
82
|
{user_uid_1}/ // Document exists = user authorized
|
|
89
83
|
{user_uid_2}/ // Document exists = user authorized
|
|
@@ -91,7 +85,7 @@ user_access/
|
|
|
91
85
|
|
|
92
86
|
To customize user authorization, provide a custom `authorizeUser` function:
|
|
93
87
|
|
|
94
|
-
```typescript
|
|
88
|
+
```typescript
|
|
95
89
|
import { MastraAuthFirebase } from "@mastra/auth-firebase";
|
|
96
90
|
|
|
97
91
|
const firebaseAuth = new MastraAuthFirebase({
|
|
@@ -102,9 +96,7 @@ const firebaseAuth = new MastraAuthFirebase({
|
|
|
102
96
|
});
|
|
103
97
|
```
|
|
104
98
|
|
|
105
|
-
> **
|
|
106
|
-
|
|
107
|
-
Visit [MastraAuthFirebase](https://mastra.ai/reference/auth/firebase) for all available configuration options.
|
|
99
|
+
> **Info:** Visit [MastraAuthFirebase](https://mastra.ai/reference/auth/firebase) for all available configuration options.
|
|
108
100
|
|
|
109
101
|
## Client-side setup
|
|
110
102
|
|
|
@@ -114,7 +106,7 @@ When using Firebase auth, you'll need to initialize Firebase on the client side,
|
|
|
114
106
|
|
|
115
107
|
First, initialize Firebase in your client application:
|
|
116
108
|
|
|
117
|
-
```typescript
|
|
109
|
+
```typescript
|
|
118
110
|
import { initializeApp } from "firebase/app";
|
|
119
111
|
import { getAuth, GoogleAuthProvider } from "firebase/auth";
|
|
120
112
|
|
|
@@ -133,7 +125,7 @@ export const googleProvider = new GoogleAuthProvider();
|
|
|
133
125
|
|
|
134
126
|
Use Firebase authentication to sign in users and retrieve their ID tokens:
|
|
135
127
|
|
|
136
|
-
```typescript
|
|
128
|
+
```typescript
|
|
137
129
|
import { signInWithPopup, signOut, User } from "firebase/auth";
|
|
138
130
|
import { auth, googleProvider } from "./firebase";
|
|
139
131
|
|
|
@@ -167,15 +159,13 @@ export const signOutUser = async () => {
|
|
|
167
159
|
};
|
|
168
160
|
```
|
|
169
161
|
|
|
170
|
-
> **Note:**
|
|
171
|
-
|
|
172
|
-
Refer to the [Firebase documentation](https://firebase.google.com/docs/auth) for other authentication methods like email/password, phone authentication, and more.
|
|
162
|
+
> **Note:** Refer to the [Firebase documentation](https://firebase.google.com/docs/auth) for other authentication methods like email/password, phone authentication, and more.
|
|
173
163
|
|
|
174
164
|
## Configuring `MastraClient`
|
|
175
165
|
|
|
176
166
|
When `auth` is enabled, all requests made with `MastraClient` must include a valid Firebase ID token in the `Authorization` header:
|
|
177
167
|
|
|
178
|
-
```typescript
|
|
168
|
+
```typescript
|
|
179
169
|
import { MastraClient } from "@mastra/client-js";
|
|
180
170
|
|
|
181
171
|
export const createMastraClient = (idToken: string) => {
|
|
@@ -188,99 +178,95 @@ export const createMastraClient = (idToken: string) => {
|
|
|
188
178
|
};
|
|
189
179
|
```
|
|
190
180
|
|
|
191
|
-
> **
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
181
|
+
> **Info:** The ID token must be prefixed with `Bearer` in the Authorization header.
|
|
182
|
+
>
|
|
183
|
+
> Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
196
184
|
|
|
197
185
|
### Making authenticated requests
|
|
198
186
|
|
|
199
187
|
Once `MastraClient` is configured with the Firebase ID token, you can send authenticated requests:
|
|
200
188
|
|
|
201
|
-
|
|
189
|
+
**React**:
|
|
202
190
|
|
|
203
|
-
|
|
204
|
-
|
|
191
|
+
```tsx
|
|
192
|
+
"use client";
|
|
205
193
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
194
|
+
import { useAuthState } from 'react-firebase-hooks/auth';
|
|
195
|
+
import { MastraClient } from "@mastra/client-js";
|
|
196
|
+
import { auth } from '../lib/firebase';
|
|
197
|
+
import { getIdToken } from '../lib/auth';
|
|
210
198
|
|
|
211
|
-
|
|
212
|
-
|
|
199
|
+
export const TestAgent = () => {
|
|
200
|
+
const [user] = useAuthState(auth);
|
|
213
201
|
|
|
214
|
-
|
|
215
|
-
|
|
202
|
+
async function handleClick() {
|
|
203
|
+
if (!user) return;
|
|
216
204
|
|
|
217
|
-
|
|
218
|
-
|
|
205
|
+
const token = await getIdToken(user);
|
|
206
|
+
const client = createMastraClient(token);
|
|
219
207
|
|
|
220
|
-
|
|
221
|
-
|
|
208
|
+
const weatherAgent = client.getAgent("weatherAgent");
|
|
209
|
+
const response = await weatherAgent.generate("What's the weather like in New York");
|
|
222
210
|
|
|
223
|
-
|
|
224
|
-
|
|
211
|
+
console.log({ response });
|
|
212
|
+
}
|
|
225
213
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
**nodejs:**
|
|
236
|
-
|
|
237
|
-
```typescript title="server.js"
|
|
238
|
-
const express = require('express');
|
|
239
|
-
const admin = require('firebase-admin');
|
|
240
|
-
const { MastraClient } = require('@mastra/client-js');
|
|
241
|
-
|
|
242
|
-
// Initialize Firebase Admin
|
|
243
|
-
admin.initializeApp({
|
|
244
|
-
credential: admin.credential.cert({
|
|
245
|
-
// Your service account credentials
|
|
246
|
-
})
|
|
247
|
-
});
|
|
214
|
+
return (
|
|
215
|
+
<button onClick={handleClick} disabled={!user}>
|
|
216
|
+
Test Agent
|
|
217
|
+
</button>
|
|
218
|
+
);
|
|
219
|
+
};
|
|
220
|
+
```
|
|
248
221
|
|
|
249
|
-
|
|
250
|
-
app.use(express.json());
|
|
222
|
+
**Node.js**:
|
|
251
223
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
224
|
+
```typescript
|
|
225
|
+
const express = require('express');
|
|
226
|
+
const admin = require('firebase-admin');
|
|
227
|
+
const { MastraClient } = require('@mastra/client-js');
|
|
255
228
|
|
|
256
|
-
|
|
257
|
-
|
|
229
|
+
// Initialize Firebase Admin
|
|
230
|
+
admin.initializeApp({
|
|
231
|
+
credential: admin.credential.cert({
|
|
232
|
+
// Your service account credentials
|
|
233
|
+
})
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const app = express();
|
|
237
|
+
app.use(express.json());
|
|
258
238
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
Authorization: `Bearer ${idToken}`
|
|
263
|
-
}
|
|
264
|
-
});
|
|
239
|
+
app.post('/generate', async (req, res) => {
|
|
240
|
+
try {
|
|
241
|
+
const { idToken } = req.body;
|
|
265
242
|
|
|
266
|
-
|
|
267
|
-
|
|
243
|
+
// Verify the token
|
|
244
|
+
await admin.auth().verifyIdToken(idToken);
|
|
268
245
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
246
|
+
const mastra = new MastraClient({
|
|
247
|
+
baseUrl: "http://localhost:4111",
|
|
248
|
+
headers: {
|
|
249
|
+
Authorization: `Bearer ${idToken}`
|
|
272
250
|
}
|
|
273
251
|
});
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
252
|
+
|
|
253
|
+
const weatherAgent = mastra.getAgent("weatherAgent");
|
|
254
|
+
const response = await weatherAgent.generate("What's the weather like in Nairobi");
|
|
255
|
+
|
|
256
|
+
res.json({ response: response.text });
|
|
257
|
+
} catch (error) {
|
|
258
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**cURL**:
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
267
|
+
-H "Content-Type: application/json" \
|
|
268
|
+
-H "Authorization: Bearer <your-firebase-id-token>" \
|
|
269
|
+
-d '{
|
|
270
|
+
"messages": "Weather in London"
|
|
271
|
+
}'
|
|
272
|
+
```
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
> Documentation for the MastraJwtAuth class, which authenticates Mastra applications using JSON Web Tokens.
|
|
2
|
-
|
|
3
1
|
# MastraJwtAuth Class
|
|
4
2
|
|
|
5
3
|
The `MastraJwtAuth` class provides a lightweight authentication mechanism for Mastra using JSON Web Tokens (JWTs). It verifies incoming requests based on a shared secret and integrates with the Mastra server using the `auth` option.
|
|
@@ -8,13 +6,33 @@ The `MastraJwtAuth` class provides a lightweight authentication mechanism for Ma
|
|
|
8
6
|
|
|
9
7
|
Before you can use the `MastraJwtAuth` class you have to install the `@mastra/auth` package.
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
**npm**:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
12
|
npm install @mastra/auth@latest
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
**pnpm**:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @mastra/auth@latest
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Yarn**:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add @mastra/auth@latest
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Bun**:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
bun add @mastra/auth@latest
|
|
31
|
+
```
|
|
32
|
+
|
|
15
33
|
## Usage example
|
|
16
34
|
|
|
17
|
-
```typescript
|
|
35
|
+
```typescript
|
|
18
36
|
import { Mastra } from "@mastra/core";
|
|
19
37
|
import { MastraJwtAuth } from "@mastra/auth";
|
|
20
38
|
|
|
@@ -27,15 +45,13 @@ export const mastra = new Mastra({
|
|
|
27
45
|
});
|
|
28
46
|
```
|
|
29
47
|
|
|
30
|
-
> **
|
|
31
|
-
|
|
32
|
-
Visit [MastraJwtAuth](https://mastra.ai/reference/auth/jwt) for all available configuration options.
|
|
48
|
+
> **Info:** Visit [MastraJwtAuth](https://mastra.ai/reference/auth/jwt) for all available configuration options.
|
|
33
49
|
|
|
34
50
|
## Configuring `MastraClient`
|
|
35
51
|
|
|
36
52
|
When `auth` is enabled, all requests made with `MastraClient` must include a valid JWT in the `Authorization` header:
|
|
37
53
|
|
|
38
|
-
```typescript
|
|
54
|
+
```typescript
|
|
39
55
|
import { MastraClient } from "@mastra/client-js";
|
|
40
56
|
|
|
41
57
|
export const mastraClient = new MastraClient({
|
|
@@ -46,45 +62,40 @@ export const mastraClient = new MastraClient({
|
|
|
46
62
|
});
|
|
47
63
|
```
|
|
48
64
|
|
|
49
|
-
> **
|
|
50
|
-
|
|
51
|
-
Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
65
|
+
> **Info:** Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
52
66
|
|
|
53
67
|
### Making authenticated requests
|
|
54
68
|
|
|
55
69
|
Once `MastraClient` is configured, you can send authenticated requests from your frontend application, or use `curl` for quick local testing:
|
|
56
70
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
```tsx title="src/components/test-agent.tsx"
|
|
60
|
-
import { mastraClient } from "../../lib/mastra-client";
|
|
71
|
+
**React**:
|
|
61
72
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const agent = mastraClient.getAgent("weatherAgent");
|
|
73
|
+
```tsx
|
|
74
|
+
import { mastraClient } from "../../lib/mastra-client";
|
|
65
75
|
|
|
66
|
-
|
|
76
|
+
export const TestAgent = () => {
|
|
77
|
+
async function handleClick() {
|
|
78
|
+
const agent = mastraClient.getAgent("weatherAgent");
|
|
67
79
|
|
|
68
|
-
|
|
69
|
-
}
|
|
80
|
+
const response = await agent.generate("Weather in London");
|
|
70
81
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
```
|
|
82
|
+
console.log(response);
|
|
83
|
+
}
|
|
74
84
|
|
|
75
|
-
|
|
76
|
-
|
|
85
|
+
return <button onClick={handleClick}>Test Agent</button>;
|
|
86
|
+
};
|
|
87
|
+
```
|
|
77
88
|
|
|
78
|
-
|
|
79
|
-
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
80
|
-
-H "Content-Type: application/json" \
|
|
81
|
-
-H "Authorization: Bearer <your-jwt>" \
|
|
82
|
-
-d '{
|
|
83
|
-
"messages": "Weather in London"
|
|
84
|
-
}'
|
|
85
|
-
```
|
|
89
|
+
**cURL**:
|
|
86
90
|
|
|
87
|
-
|
|
91
|
+
```bash
|
|
92
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
93
|
+
-H "Content-Type: application/json" \
|
|
94
|
+
-H "Authorization: Bearer <your-jwt>" \
|
|
95
|
+
-d '{
|
|
96
|
+
"messages": "Weather in London"
|
|
97
|
+
}'
|
|
98
|
+
```
|
|
88
99
|
|
|
89
100
|
## Creating a JWT
|
|
90
101
|
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
> Documentation for the MastraAuthSupabase class, which authenticates Mastra applications using Supabase Auth.
|
|
2
|
-
|
|
3
1
|
# MastraAuthSupabase Class
|
|
4
2
|
|
|
5
3
|
The `MastraAuthSupabase` class provides authentication for Mastra using Supabase Auth. It verifies incoming requests using Supabase's authentication system and integrates with the Mastra server using the `auth` option.
|
|
@@ -8,14 +6,12 @@ The `MastraAuthSupabase` class provides authentication for Mastra using Supabase
|
|
|
8
6
|
|
|
9
7
|
This example uses Supabase Auth. Make sure to add your Supabase credentials to your `.env` file and ensure your Supabase project is properly configured.
|
|
10
8
|
|
|
11
|
-
```env
|
|
9
|
+
```env
|
|
12
10
|
SUPABASE_URL=https://your-project.supabase.co
|
|
13
11
|
SUPABASE_ANON_KEY=your-anon-key
|
|
14
12
|
```
|
|
15
13
|
|
|
16
|
-
> **Note:**
|
|
17
|
-
|
|
18
|
-
Review your Supabase Row Level Security (RLS) settings to ensure proper data access controls.
|
|
14
|
+
> **Note:** Review your Supabase Row Level Security (RLS) settings to ensure proper data access controls.
|
|
19
15
|
|
|
20
16
|
## Installation
|
|
21
17
|
|
|
@@ -27,7 +23,7 @@ npm install @mastra/auth-supabase@latest
|
|
|
27
23
|
|
|
28
24
|
## Usage example
|
|
29
25
|
|
|
30
|
-
```typescript
|
|
26
|
+
```typescript
|
|
31
27
|
import { Mastra } from "@mastra/core";
|
|
32
28
|
import { MastraAuthSupabase } from "@mastra/auth-supabase";
|
|
33
29
|
|
|
@@ -41,11 +37,9 @@ export const mastra = new Mastra({
|
|
|
41
37
|
});
|
|
42
38
|
```
|
|
43
39
|
|
|
44
|
-
> **
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Visit [MastraAuthSupabase](https://mastra.ai/reference/auth/supabase) for all available configuration options.
|
|
40
|
+
> **Info:** The default `authorizeUser` method checks the `isAdmin` column in the `users` table in the `public` schema. To customize user authorization, provide a custom `authorizeUser` function when constructing the provider.
|
|
41
|
+
>
|
|
42
|
+
> Visit [MastraAuthSupabase](https://mastra.ai/reference/auth/supabase) for all available configuration options.
|
|
49
43
|
|
|
50
44
|
## Client-side setup
|
|
51
45
|
|
|
@@ -55,7 +49,7 @@ When using Supabase auth, you'll need to retrieve the access token from Supabase
|
|
|
55
49
|
|
|
56
50
|
Use the Supabase client to authenticate users and retrieve their access token:
|
|
57
51
|
|
|
58
|
-
```typescript
|
|
52
|
+
```typescript
|
|
59
53
|
import { createClient } from "@supabase/supabase-js";
|
|
60
54
|
|
|
61
55
|
const supabase = createClient("<supabase-url>", "<supabase-key>");
|
|
@@ -68,15 +62,13 @@ const authTokenResponse = await supabase.auth.signInWithPassword({
|
|
|
68
62
|
const accessToken = authTokenResponse.data?.session?.access_token;
|
|
69
63
|
```
|
|
70
64
|
|
|
71
|
-
> **Note:**
|
|
72
|
-
|
|
73
|
-
Refer to the [Supabase documentation](https://supabase.com/docs/guides/auth) for other authentication methods like OAuth, magic links, and more.
|
|
65
|
+
> **Note:** Refer to the [Supabase documentation](https://supabase.com/docs/guides/auth) for other authentication methods like OAuth, magic links, and more.
|
|
74
66
|
|
|
75
67
|
## Configuring `MastraClient`
|
|
76
68
|
|
|
77
69
|
When `auth` is enabled, all requests made with `MastraClient` must include a valid Supabase access token in the `Authorization` header:
|
|
78
70
|
|
|
79
|
-
```typescript
|
|
71
|
+
```typescript
|
|
80
72
|
import { MastraClient } from "@mastra/client-js";
|
|
81
73
|
|
|
82
74
|
export const mastraClient = new MastraClient({
|
|
@@ -87,42 +79,39 @@ export const mastraClient = new MastraClient({
|
|
|
87
79
|
});
|
|
88
80
|
```
|
|
89
81
|
|
|
90
|
-
> **
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
82
|
+
> **Info:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
83
|
+
>
|
|
84
|
+
> Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
95
85
|
|
|
96
86
|
### Making authenticated requests
|
|
97
87
|
|
|
98
88
|
Once `MastraClient` is configured with the Supabase access token, you can send authenticated requests:
|
|
99
89
|
|
|
100
|
-
|
|
90
|
+
**React**:
|
|
101
91
|
|
|
102
|
-
|
|
103
|
-
|
|
92
|
+
```tsx
|
|
93
|
+
import { mastraClient } from "../../lib/mastra-client";
|
|
104
94
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
95
|
+
export const TestAgent = () => {
|
|
96
|
+
async function handleClick() {
|
|
97
|
+
const agent = mastraClient.getAgent("weatherAgent");
|
|
108
98
|
|
|
109
|
-
|
|
99
|
+
const response = await agent.generate("What's the weather like in New York");
|
|
110
100
|
|
|
111
|
-
|
|
112
|
-
|
|
101
|
+
console.log(response);
|
|
102
|
+
}
|
|
113
103
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
104
|
+
return <button onClick={handleClick}>Test Agent</button>;
|
|
105
|
+
};
|
|
106
|
+
```
|
|
117
107
|
|
|
118
|
-
|
|
119
|
-
**curl:**
|
|
108
|
+
**cURL**:
|
|
120
109
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
110
|
+
```bash
|
|
111
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
112
|
+
-H "Content-Type: application/json" \
|
|
113
|
+
-H "Authorization: Bearer <your-supabase-access-token>" \
|
|
114
|
+
-d '{
|
|
115
|
+
"messages": "Weather in London"
|
|
116
|
+
}'
|
|
117
|
+
```
|