@mastra/client-js 1.7.0 → 1.7.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/CHANGELOG.md +16 -0
- package/dist/docs/SKILL.md +44 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-server-auth-auth0.md +220 -0
- package/dist/docs/references/docs-server-auth-clerk.md +132 -0
- package/dist/docs/references/docs-server-auth-firebase.md +272 -0
- package/dist/docs/references/docs-server-auth-jwt.md +110 -0
- package/dist/docs/references/docs-server-auth-supabase.md +117 -0
- package/dist/docs/references/docs-server-auth-workos.md +186 -0
- package/dist/docs/references/docs-server-mastra-client.md +243 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-stream.md +231 -0
- 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 +76 -0
- package/dist/docs/references/reference-client-js-agents.md +437 -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 +221 -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/package.json +7 -7
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# MastraAuthFirebase Class
|
|
2
|
+
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
This example uses Firebase Authentication. Make sure to:
|
|
8
|
+
|
|
9
|
+
1. Create a Firebase project in the [Firebase Console](https://console.firebase.google.com/)
|
|
10
|
+
2. Enable Authentication and configure your preferred sign-in methods (Google, Email/Password, etc.)
|
|
11
|
+
3. Generate a service account key from Project Settings > Service Accounts
|
|
12
|
+
4. Download the service account JSON file
|
|
13
|
+
|
|
14
|
+
```env
|
|
15
|
+
FIREBASE_SERVICE_ACCOUNT=/path/to/your/service-account-key.json
|
|
16
|
+
FIRESTORE_DATABASE_ID=(default)
|
|
17
|
+
# Alternative environment variable names:
|
|
18
|
+
# FIREBASE_DATABASE_ID=(default)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
> **Note:** Store your service account JSON file securely and never commit it to version control.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Before you can use the `MastraAuthFirebase` class you have to install the `@mastra/auth-firebase` package.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @mastra/auth-firebase@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage examples
|
|
32
|
+
|
|
33
|
+
### Basic usage with environment variables
|
|
34
|
+
|
|
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:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { Mastra } from '@mastra/core'
|
|
39
|
+
import { MastraAuthFirebase } from '@mastra/auth-firebase'
|
|
40
|
+
|
|
41
|
+
// Automatically uses FIREBASE_SERVICE_ACCOUNT and FIRESTORE_DATABASE_ID env vars
|
|
42
|
+
export const mastra = new Mastra({
|
|
43
|
+
server: {
|
|
44
|
+
auth: new MastraAuthFirebase(),
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Custom configuration
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { Mastra } from '@mastra/core'
|
|
53
|
+
import { MastraAuthFirebase } from '@mastra/auth-firebase'
|
|
54
|
+
|
|
55
|
+
export const mastra = new Mastra({
|
|
56
|
+
server: {
|
|
57
|
+
auth: new MastraAuthFirebase({
|
|
58
|
+
serviceAccount: '/path/to/service-account.json',
|
|
59
|
+
databaseId: 'your-database-id',
|
|
60
|
+
}),
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
The `MastraAuthFirebase` class can be configured through constructor options or environment variables.
|
|
68
|
+
|
|
69
|
+
### Environment Variables
|
|
70
|
+
|
|
71
|
+
- `FIREBASE_SERVICE_ACCOUNT`: Path to Firebase service account JSON file
|
|
72
|
+
- `FIRESTORE_DATABASE_ID` or `FIREBASE_DATABASE_ID`: Firestore database ID
|
|
73
|
+
|
|
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.
|
|
75
|
+
|
|
76
|
+
### User Authorization
|
|
77
|
+
|
|
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.
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
user_access/
|
|
82
|
+
{user_uid_1}/ // Document exists = user authorized
|
|
83
|
+
{user_uid_2}/ // Document exists = user authorized
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
To customize user authorization, provide a custom `authorizeUser` function:
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { MastraAuthFirebase } from '@mastra/auth-firebase'
|
|
90
|
+
|
|
91
|
+
const firebaseAuth = new MastraAuthFirebase({
|
|
92
|
+
authorizeUser: async user => {
|
|
93
|
+
// Custom authorization logic
|
|
94
|
+
return user.email?.endsWith('@yourcompany.com') || false
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> **Info:** Visit [MastraAuthFirebase](https://mastra.ai/reference/auth/firebase) for all available configuration options.
|
|
100
|
+
|
|
101
|
+
## Client-side setup
|
|
102
|
+
|
|
103
|
+
When using Firebase auth, you'll need to initialize Firebase on the client side, authenticate users, and retrieve their ID tokens to pass to your Mastra requests.
|
|
104
|
+
|
|
105
|
+
### Setting up Firebase on the client
|
|
106
|
+
|
|
107
|
+
First, initialize Firebase in your client application:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { initializeApp } from 'firebase/app'
|
|
111
|
+
import { getAuth, GoogleAuthProvider } from 'firebase/auth'
|
|
112
|
+
|
|
113
|
+
const firebaseConfig = {
|
|
114
|
+
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
|
|
115
|
+
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
|
|
116
|
+
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const app = initializeApp(firebaseConfig)
|
|
120
|
+
export const auth = getAuth(app)
|
|
121
|
+
export const googleProvider = new GoogleAuthProvider()
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Authenticating users and retrieving tokens
|
|
125
|
+
|
|
126
|
+
Use Firebase authentication to sign in users and retrieve their ID tokens:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { signInWithPopup, signOut, User } from 'firebase/auth'
|
|
130
|
+
import { auth, googleProvider } from './firebase'
|
|
131
|
+
|
|
132
|
+
export const signInWithGoogle = async () => {
|
|
133
|
+
try {
|
|
134
|
+
const result = await signInWithPopup(auth, googleProvider)
|
|
135
|
+
return result.user
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('Error signing in:', error)
|
|
138
|
+
throw error
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export const getIdToken = async (user: User) => {
|
|
143
|
+
try {
|
|
144
|
+
const idToken = await user.getIdToken()
|
|
145
|
+
return idToken
|
|
146
|
+
} catch (error) {
|
|
147
|
+
console.error('Error getting ID token:', error)
|
|
148
|
+
throw error
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const signOutUser = async () => {
|
|
153
|
+
try {
|
|
154
|
+
await signOut(auth)
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.error('Error signing out:', error)
|
|
157
|
+
throw error
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
> **Note:** Refer to the [Firebase documentation](https://firebase.google.com/docs/auth) for other authentication methods like email/password, phone authentication, and more.
|
|
163
|
+
|
|
164
|
+
## Configuring `MastraClient`
|
|
165
|
+
|
|
166
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid Firebase ID token in the `Authorization` header:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { MastraClient } from '@mastra/client-js'
|
|
170
|
+
|
|
171
|
+
export const createMastraClient = (idToken: string) => {
|
|
172
|
+
return new MastraClient({
|
|
173
|
+
baseUrl: 'https://<mastra-api-url>',
|
|
174
|
+
headers: {
|
|
175
|
+
Authorization: `Bearer ${idToken}`,
|
|
176
|
+
},
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
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.
|
|
184
|
+
|
|
185
|
+
### Making authenticated requests
|
|
186
|
+
|
|
187
|
+
Once `MastraClient` is configured with the Firebase ID token, you can send authenticated requests:
|
|
188
|
+
|
|
189
|
+
**React**:
|
|
190
|
+
|
|
191
|
+
```tsx
|
|
192
|
+
'use client'
|
|
193
|
+
|
|
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'
|
|
198
|
+
|
|
199
|
+
export const TestAgent = () => {
|
|
200
|
+
const [user] = useAuthState(auth)
|
|
201
|
+
|
|
202
|
+
async function handleClick() {
|
|
203
|
+
if (!user) return
|
|
204
|
+
|
|
205
|
+
const token = await getIdToken(user)
|
|
206
|
+
const client = createMastraClient(token)
|
|
207
|
+
|
|
208
|
+
const weatherAgent = client.getAgent('weatherAgent')
|
|
209
|
+
const response = await weatherAgent.generate("What's the weather like in New York")
|
|
210
|
+
|
|
211
|
+
console.log({ response })
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return (
|
|
215
|
+
<button onClick={handleClick} disabled={!user}>
|
|
216
|
+
Test Agent
|
|
217
|
+
</button>
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Node.js**:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const express = require('express')
|
|
226
|
+
const admin = require('firebase-admin')
|
|
227
|
+
const { MastraClient } = require('@mastra/client-js')
|
|
228
|
+
|
|
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())
|
|
238
|
+
|
|
239
|
+
app.post('/generate', async (req, res) => {
|
|
240
|
+
try {
|
|
241
|
+
const { idToken } = req.body
|
|
242
|
+
|
|
243
|
+
// Verify the token
|
|
244
|
+
await admin.auth().verifyIdToken(idToken)
|
|
245
|
+
|
|
246
|
+
const mastra = new MastraClient({
|
|
247
|
+
baseUrl: 'http://localhost:4111',
|
|
248
|
+
headers: {
|
|
249
|
+
Authorization: `Bearer ${idToken}`,
|
|
250
|
+
},
|
|
251
|
+
})
|
|
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
|
+
```
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# MastraJwtAuth Class
|
|
2
|
+
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Before you can use the `MastraJwtAuth` class you have to install the `@mastra/auth` package.
|
|
8
|
+
|
|
9
|
+
**npm**:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @mastra/auth@latest
|
|
13
|
+
```
|
|
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
|
+
|
|
33
|
+
## Usage example
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { Mastra } from '@mastra/core'
|
|
37
|
+
import { MastraJwtAuth } from '@mastra/auth'
|
|
38
|
+
|
|
39
|
+
export const mastra = new Mastra({
|
|
40
|
+
server: {
|
|
41
|
+
auth: new MastraJwtAuth({
|
|
42
|
+
secret: process.env.MASTRA_JWT_SECRET,
|
|
43
|
+
}),
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> **Info:** Visit [MastraJwtAuth](https://mastra.ai/reference/auth/jwt) for all available configuration options.
|
|
49
|
+
|
|
50
|
+
## Configuring `MastraClient`
|
|
51
|
+
|
|
52
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid JWT in the `Authorization` header:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { MastraClient } from '@mastra/client-js'
|
|
56
|
+
|
|
57
|
+
export const mastraClient = new MastraClient({
|
|
58
|
+
baseUrl: 'https://<mastra-api-url>',
|
|
59
|
+
headers: {
|
|
60
|
+
Authorization: `Bearer ${process.env.MASTRA_JWT_TOKEN}`,
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
> **Info:** Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
66
|
+
|
|
67
|
+
### Making authenticated requests
|
|
68
|
+
|
|
69
|
+
Once `MastraClient` is configured, you can send authenticated requests from your frontend application, or use `curl` for quick local testing:
|
|
70
|
+
|
|
71
|
+
**React**:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { mastraClient } from '../../lib/mastra-client'
|
|
75
|
+
|
|
76
|
+
export const TestAgent = () => {
|
|
77
|
+
async function handleClick() {
|
|
78
|
+
const agent = mastraClient.getAgent('weatherAgent')
|
|
79
|
+
|
|
80
|
+
const response = await agent.generate('Weather in London')
|
|
81
|
+
|
|
82
|
+
console.log(response)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return <button onClick={handleClick}>Test Agent</button>
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**cURL**:
|
|
90
|
+
|
|
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
|
+
```
|
|
99
|
+
|
|
100
|
+
## Creating a JWT
|
|
101
|
+
|
|
102
|
+
To authenticate requests to your Mastra server, you'll need a valid JSON Web Token (JWT) signed with your `MASTRA_JWT_SECRET`.
|
|
103
|
+
|
|
104
|
+
The easiest way to generate one is using [jwt.io](https://www.jwt.io/):
|
|
105
|
+
|
|
106
|
+
1. Select **JWT Encoder**.
|
|
107
|
+
2. Scroll down to the **Sign JWT: Secret** section.
|
|
108
|
+
3. Enter your secret (for example: `supersecretdevkeythatishs256safe!`).
|
|
109
|
+
4. Click **Generate example** to create a valid JWT.
|
|
110
|
+
5. Copy the generated token and set it as `MASTRA_JWT_TOKEN` in your `.env` file.
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# MastraAuthSupabase Class
|
|
2
|
+
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
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.
|
|
8
|
+
|
|
9
|
+
```env
|
|
10
|
+
SUPABASE_URL=https://your-project.supabase.co
|
|
11
|
+
SUPABASE_ANON_KEY=your-anon-key
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
> **Note:** Review your Supabase Row Level Security (RLS) settings to ensure proper data access controls.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Before you can use the `MastraAuthSupabase` class you have to install the `@mastra/auth-supabase` package.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @mastra/auth-supabase@latest
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage example
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { Mastra } from '@mastra/core'
|
|
28
|
+
import { MastraAuthSupabase } from '@mastra/auth-supabase'
|
|
29
|
+
|
|
30
|
+
export const mastra = new Mastra({
|
|
31
|
+
server: {
|
|
32
|
+
auth: new MastraAuthSupabase({
|
|
33
|
+
url: process.env.SUPABASE_URL,
|
|
34
|
+
anonKey: process.env.SUPABASE_ANON_KEY,
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
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.
|
|
43
|
+
|
|
44
|
+
## Client-side setup
|
|
45
|
+
|
|
46
|
+
When using Supabase auth, you'll need to retrieve the access token from Supabase on the client side and pass it to your Mastra requests.
|
|
47
|
+
|
|
48
|
+
### Retrieving the access token
|
|
49
|
+
|
|
50
|
+
Use the Supabase client to authenticate users and retrieve their access token:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { createClient } from '@supabase/supabase-js'
|
|
54
|
+
|
|
55
|
+
const supabase = createClient('<supabase-url>', '<supabase-key>')
|
|
56
|
+
|
|
57
|
+
const authTokenResponse = await supabase.auth.signInWithPassword({
|
|
58
|
+
email: "<user's email>",
|
|
59
|
+
password: "<user's password>",
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const accessToken = authTokenResponse.data?.session?.access_token
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
> **Note:** Refer to the [Supabase documentation](https://supabase.com/docs/guides/auth) for other authentication methods like OAuth, magic links, and more.
|
|
66
|
+
|
|
67
|
+
## Configuring `MastraClient`
|
|
68
|
+
|
|
69
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid Supabase access token in the `Authorization` header:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { MastraClient } from '@mastra/client-js'
|
|
73
|
+
|
|
74
|
+
export const mastraClient = new MastraClient({
|
|
75
|
+
baseUrl: 'https://<mastra-api-url>',
|
|
76
|
+
headers: {
|
|
77
|
+
Authorization: `Bearer ${accessToken}`,
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
```
|
|
81
|
+
|
|
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.
|
|
85
|
+
|
|
86
|
+
### Making authenticated requests
|
|
87
|
+
|
|
88
|
+
Once `MastraClient` is configured with the Supabase access token, you can send authenticated requests:
|
|
89
|
+
|
|
90
|
+
**React**:
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
import { mastraClient } from '../../lib/mastra-client'
|
|
94
|
+
|
|
95
|
+
export const TestAgent = () => {
|
|
96
|
+
async function handleClick() {
|
|
97
|
+
const agent = mastraClient.getAgent('weatherAgent')
|
|
98
|
+
|
|
99
|
+
const response = await agent.generate("What's the weather like in New York")
|
|
100
|
+
|
|
101
|
+
console.log(response)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return <button onClick={handleClick}>Test Agent</button>
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**cURL**:
|
|
109
|
+
|
|
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
|
+
```
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# MastraAuthWorkos Class
|
|
2
|
+
|
|
3
|
+
The `MastraAuthWorkos` class provides authentication for Mastra using WorkOS. It verifies incoming requests using WorkOS access tokens and integrates with the Mastra server using the `auth` option.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
This example uses WorkOS authentication. Make sure to:
|
|
8
|
+
|
|
9
|
+
1. Create a WorkOS account at [workos.com](https://workos.com/)
|
|
10
|
+
2. Set up an Application in your WorkOS Dashboard
|
|
11
|
+
3. Configure your redirect URIs and allowed origins
|
|
12
|
+
4. Set up Organizations and configure user roles as needed
|
|
13
|
+
|
|
14
|
+
```env
|
|
15
|
+
WORKOS_API_KEY=sk_live_...
|
|
16
|
+
WORKOS_CLIENT_ID=client_...
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
> **Note:** You can find your API key and Client ID in the WorkOS Dashboard under API Keys and Applications respectively.
|
|
20
|
+
>
|
|
21
|
+
> For detailed setup instructions, refer to the [WorkOS documentation](https://workos.com/docs) for your specific platform.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Before you can use the `MastraAuthWorkos` class you have to install the `@mastra/auth-workos` package.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @mastra/auth-workos@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage examples
|
|
32
|
+
|
|
33
|
+
### Basic usage with environment variables
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { Mastra } from '@mastra/core'
|
|
37
|
+
import { MastraAuthWorkos } from '@mastra/auth-workos'
|
|
38
|
+
|
|
39
|
+
export const mastra = new Mastra({
|
|
40
|
+
server: {
|
|
41
|
+
auth: new MastraAuthWorkos(),
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Custom configuration
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { Mastra } from '@mastra/core'
|
|
50
|
+
import { MastraAuthWorkos } from '@mastra/auth-workos'
|
|
51
|
+
|
|
52
|
+
export const mastra = new Mastra({
|
|
53
|
+
server: {
|
|
54
|
+
auth: new MastraAuthWorkos({
|
|
55
|
+
apiKey: process.env.WORKOS_API_KEY,
|
|
56
|
+
clientId: process.env.WORKOS_CLIENT_ID,
|
|
57
|
+
}),
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Configuration
|
|
63
|
+
|
|
64
|
+
### User Authorization
|
|
65
|
+
|
|
66
|
+
By default, `MastraAuthWorkos` checks whether the authenticated user has an 'admin' role in any of their organization memberships. The authorization process:
|
|
67
|
+
|
|
68
|
+
1. Retrieves the user's organization memberships using their user ID
|
|
69
|
+
2. Extracts all roles from their memberships
|
|
70
|
+
3. Checks if any role has the slug 'admin'
|
|
71
|
+
4. Grants access only if the user has admin role in at least one organization
|
|
72
|
+
|
|
73
|
+
To customize user authorization, provide a custom `authorizeUser` function:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { MastraAuthWorkos } from '@mastra/auth-workos'
|
|
77
|
+
|
|
78
|
+
const workosAuth = new MastraAuthWorkos({
|
|
79
|
+
apiKey: process.env.WORKOS_API_KEY,
|
|
80
|
+
clientId: process.env.WORKOS_CLIENT_ID,
|
|
81
|
+
authorizeUser: async user => {
|
|
82
|
+
return !!user
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
> **Info:** Visit [MastraAuthWorkos](https://mastra.ai/reference/auth/workos) for all available configuration options.
|
|
88
|
+
|
|
89
|
+
## Client-side setup
|
|
90
|
+
|
|
91
|
+
When using WorkOS auth, you'll need to implement the WorkOS authentication flow to exchange an authorization code for an access token, then use that token with your Mastra requests.
|
|
92
|
+
|
|
93
|
+
### Installing WorkOS SDK
|
|
94
|
+
|
|
95
|
+
First, install the WorkOS SDK in your application:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
npm install @workos-inc/node
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Exchanging code for access token
|
|
102
|
+
|
|
103
|
+
After users complete the WorkOS authentication flow and return with an authorization code, exchange it for an access token:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { WorkOS } from '@workos-inc/node'
|
|
107
|
+
|
|
108
|
+
const workos = new WorkOS(process.env.WORKOS_API_KEY)
|
|
109
|
+
|
|
110
|
+
export const authenticateWithWorkos = async (code: string, clientId: string) => {
|
|
111
|
+
const authenticationResponse = await workos.userManagement.authenticateWithCode({
|
|
112
|
+
code,
|
|
113
|
+
clientId,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
return authenticationResponse.accessToken
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
> **Note:** Refer to the [WorkOS User Management documentation](https://workos.com/docs/authkit/vanilla/nodejs) for more authentication methods and configuration options.
|
|
121
|
+
|
|
122
|
+
## Configuring `MastraClient`
|
|
123
|
+
|
|
124
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid WorkOS access token in the `Authorization` header:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { MastraClient } from '@mastra/client-js'
|
|
128
|
+
|
|
129
|
+
export const createMastraClient = (accessToken: string) => {
|
|
130
|
+
return new MastraClient({
|
|
131
|
+
baseUrl: 'https://<mastra-api-url>',
|
|
132
|
+
headers: {
|
|
133
|
+
Authorization: `Bearer ${accessToken}`,
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
> **Info:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
140
|
+
>
|
|
141
|
+
> Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
142
|
+
|
|
143
|
+
### Making authenticated requests
|
|
144
|
+
|
|
145
|
+
Once `MastraClient` is configured with the WorkOS access token, you can send authenticated requests:
|
|
146
|
+
|
|
147
|
+
**React**:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { WorkOS } from '@workos-inc/node'
|
|
151
|
+
import { MastraClient } from '@mastra/client-js'
|
|
152
|
+
|
|
153
|
+
const workos = new WorkOS(process.env.WORKOS_API_KEY)
|
|
154
|
+
|
|
155
|
+
export const callMastraWithWorkos = async (code: string, clientId: string) => {
|
|
156
|
+
const authenticationResponse = await workos.userManagement.authenticateWithCode({
|
|
157
|
+
code,
|
|
158
|
+
clientId,
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
const token = authenticationResponse.accessToken
|
|
162
|
+
|
|
163
|
+
const mastra = new MastraClient({
|
|
164
|
+
baseUrl: 'http://localhost:4111',
|
|
165
|
+
headers: {
|
|
166
|
+
Authorization: `Bearer ${token}`,
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const weatherAgent = mastra.getAgent('weatherAgent')
|
|
171
|
+
const response = await weatherAgent.generate("What's the weather like in Nairobi")
|
|
172
|
+
|
|
173
|
+
return response.text
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**cURL**:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
181
|
+
-H "Content-Type: application/json" \
|
|
182
|
+
-H "Authorization: Bearer <your-workos-access-token>" \
|
|
183
|
+
-d '{
|
|
184
|
+
"messages": "Weather in London"
|
|
185
|
+
}'
|
|
186
|
+
```
|