@mastra/client-js 1.0.0-beta.9 → 1.0.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +1638 -0
  2. package/README.md +1 -3
  3. package/dist/_types/@ai-sdk_ui-utils/dist/index.d.ts +820 -0
  4. package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +8511 -0
  5. package/dist/client.d.ts +56 -11
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/docs/README.md +33 -0
  8. package/dist/docs/SKILL.md +34 -0
  9. package/dist/docs/SOURCE_MAP.json +6 -0
  10. package/dist/docs/ai-sdk/01-reference.md +358 -0
  11. package/dist/docs/client-js/01-reference.md +1180 -0
  12. package/dist/docs/server/01-mastra-client.md +256 -0
  13. package/dist/docs/server/02-jwt.md +99 -0
  14. package/dist/docs/server/03-clerk.md +143 -0
  15. package/dist/docs/server/04-supabase.md +128 -0
  16. package/dist/docs/server/05-firebase.md +286 -0
  17. package/dist/docs/server/06-workos.md +201 -0
  18. package/dist/docs/server/07-auth0.md +233 -0
  19. package/dist/index.cjs +1690 -596
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.js +1688 -594
  22. package/dist/index.js.map +1 -1
  23. package/dist/resources/agent-builder.d.ts +10 -26
  24. package/dist/resources/agent-builder.d.ts.map +1 -1
  25. package/dist/resources/agent.d.ts +43 -8
  26. package/dist/resources/agent.d.ts.map +1 -1
  27. package/dist/resources/index.d.ts +1 -0
  28. package/dist/resources/index.d.ts.map +1 -1
  29. package/dist/resources/memory-thread.d.ts +18 -3
  30. package/dist/resources/memory-thread.d.ts.map +1 -1
  31. package/dist/resources/observability.d.ts +58 -15
  32. package/dist/resources/observability.d.ts.map +1 -1
  33. package/dist/resources/processor.d.ts +20 -0
  34. package/dist/resources/processor.d.ts.map +1 -0
  35. package/dist/resources/run.d.ts +210 -0
  36. package/dist/resources/run.d.ts.map +1 -0
  37. package/dist/resources/workflow.d.ts +19 -224
  38. package/dist/resources/workflow.d.ts.map +1 -1
  39. package/dist/tools.d.ts +2 -2
  40. package/dist/tools.d.ts.map +1 -1
  41. package/dist/types.d.ts +141 -36
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/utils/index.d.ts +26 -0
  44. package/dist/utils/index.d.ts.map +1 -1
  45. package/package.json +13 -12
@@ -0,0 +1,286 @@
1
+ > Documentation for the MastraAuthFirebase class, which authenticates Mastra applications using Firebase Authentication.
2
+
3
+ # MastraAuthFirebase Class
4
+
5
+ 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.
6
+
7
+ ## Prerequisites
8
+
9
+ This example uses Firebase Authentication. Make sure to:
10
+
11
+ 1. Create a Firebase project in the [Firebase Console](https://console.firebase.google.com/)
12
+ 2. Enable Authentication and configure your preferred sign-in methods (Google, Email/Password, etc.)
13
+ 3. Generate a service account key from Project Settings > Service Accounts
14
+ 4. Download the service account JSON file
15
+
16
+ ```env title=".env"
17
+ FIREBASE_SERVICE_ACCOUNT=/path/to/your/service-account-key.json
18
+ FIRESTORE_DATABASE_ID=(default)
19
+ # Alternative environment variable names:
20
+ # FIREBASE_DATABASE_ID=(default)
21
+ ```
22
+
23
+ > **Note:**
24
+
25
+ Store your service account JSON file securely and never commit it to version control.
26
+
27
+ ## Installation
28
+
29
+ Before you can use the `MastraAuthFirebase` class you have to install the `@mastra/auth-firebase` package.
30
+
31
+ ```bash
32
+ npm install @mastra/auth-firebase@beta
33
+ ```
34
+
35
+ ## Usage examples
36
+
37
+ ### Basic usage with environment variables
38
+
39
+ 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
+
41
+ ```typescript {2,7} title="src/mastra/index.ts"
42
+ import { Mastra } from "@mastra/core";
43
+ import { MastraAuthFirebase } from "@mastra/auth-firebase";
44
+
45
+ // Automatically uses FIREBASE_SERVICE_ACCOUNT and FIRESTORE_DATABASE_ID env vars
46
+ export const mastra = new Mastra({
47
+ server: {
48
+ auth: new MastraAuthFirebase(),
49
+ },
50
+ });
51
+ ```
52
+
53
+ ### Custom configuration
54
+
55
+ ```typescript {2,6-9} title="src/mastra/index.ts"
56
+ import { Mastra } from "@mastra/core";
57
+ import { MastraAuthFirebase } from "@mastra/auth-firebase";
58
+
59
+ export const mastra = new Mastra({
60
+ server: {
61
+ auth: new MastraAuthFirebase({
62
+ serviceAccount: "/path/to/service-account.json",
63
+ databaseId: "your-database-id",
64
+ }),
65
+ },
66
+ });
67
+ ```
68
+
69
+ ## Configuration
70
+
71
+ The `MastraAuthFirebase` class can be configured through constructor options or environment variables.
72
+
73
+ ### Environment Variables
74
+
75
+ - `FIREBASE_SERVICE_ACCOUNT`: Path to Firebase service account JSON file
76
+ - `FIRESTORE_DATABASE_ID` or `FIREBASE_DATABASE_ID`: Firestore database ID
77
+
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.
81
+
82
+ ### User Authorization
83
+
84
+ 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
+
86
+ ```typescript title="firestore-structure.txt"
87
+ user_access/
88
+ {user_uid_1}/ // Document exists = user authorized
89
+ {user_uid_2}/ // Document exists = user authorized
90
+ ```
91
+
92
+ To customize user authorization, provide a custom `authorizeUser` function:
93
+
94
+ ```typescript title="src/mastra/auth.ts"
95
+ import { MastraAuthFirebase } from "@mastra/auth-firebase";
96
+
97
+ const firebaseAuth = new MastraAuthFirebase({
98
+ authorizeUser: async (user) => {
99
+ // Custom authorization logic
100
+ return user.email?.endsWith("@yourcompany.com") || false;
101
+ },
102
+ });
103
+ ```
104
+
105
+ > **Note:**
106
+
107
+ Visit [MastraAuthFirebase](https://mastra.ai/reference/v1/auth/firebase) for all available configuration options.
108
+
109
+ ## Client-side setup
110
+
111
+ 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.
112
+
113
+ ### Setting up Firebase on the client
114
+
115
+ First, initialize Firebase in your client application:
116
+
117
+ ```typescript title="lib/firebase.ts"
118
+ import { initializeApp } from "firebase/app";
119
+ import { getAuth, GoogleAuthProvider } from "firebase/auth";
120
+
121
+ const firebaseConfig = {
122
+ apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
123
+ authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
124
+ projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
125
+ };
126
+
127
+ const app = initializeApp(firebaseConfig);
128
+ export const auth = getAuth(app);
129
+ export const googleProvider = new GoogleAuthProvider();
130
+ ```
131
+
132
+ ### Authenticating users and retrieving tokens
133
+
134
+ Use Firebase authentication to sign in users and retrieve their ID tokens:
135
+
136
+ ```typescript title="lib/auth.ts"
137
+ import { signInWithPopup, signOut, User } from "firebase/auth";
138
+ import { auth, googleProvider } from "./firebase";
139
+
140
+ export const signInWithGoogle = async () => {
141
+ try {
142
+ const result = await signInWithPopup(auth, googleProvider);
143
+ return result.user;
144
+ } catch (error) {
145
+ console.error("Error signing in:", error);
146
+ throw error;
147
+ }
148
+ };
149
+
150
+ export const getIdToken = async (user: User) => {
151
+ try {
152
+ const idToken = await user.getIdToken();
153
+ return idToken;
154
+ } catch (error) {
155
+ console.error("Error getting ID token:", error);
156
+ throw error;
157
+ }
158
+ };
159
+
160
+ export const signOutUser = async () => {
161
+ try {
162
+ await signOut(auth);
163
+ } catch (error) {
164
+ console.error("Error signing out:", error);
165
+ throw error;
166
+ }
167
+ };
168
+ ```
169
+
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.
173
+
174
+ ## Configuring `MastraClient`
175
+
176
+ When `auth` is enabled, all requests made with `MastraClient` must include a valid Firebase ID token in the `Authorization` header:
177
+
178
+ ```typescript {6} title="lib/mastra/mastra-client.ts"
179
+ import { MastraClient } from "@mastra/client-js";
180
+
181
+ export const createMastraClient = (idToken: string) => {
182
+ return new MastraClient({
183
+ baseUrl: "https://<mastra-api-url>",
184
+ headers: {
185
+ Authorization: `Bearer ${idToken}`,
186
+ },
187
+ });
188
+ };
189
+ ```
190
+
191
+ > **Note:**
192
+
193
+ The ID token must be prefixed with `Bearer` in the Authorization header.
194
+
195
+ Visit [Mastra Client SDK](https://mastra.ai/docs/v1/server/mastra-client) for more configuration options.
196
+
197
+ ### Making authenticated requests
198
+
199
+ Once `MastraClient` is configured with the Firebase ID token, you can send authenticated requests:
200
+
201
+ **react:**
202
+
203
+ ```tsx title="src/components/test-agent.tsx" copy
204
+ "use client";
205
+
206
+ import { useAuthState } from 'react-firebase-hooks/auth';
207
+ import { MastraClient } from "@mastra/client-js";
208
+ import { auth } from '../lib/firebase';
209
+ import { getIdToken } from '../lib/auth';
210
+
211
+ export const TestAgent = () => {
212
+ const [user] = useAuthState(auth);
213
+
214
+ async function handleClick() {
215
+ if (!user) return;
216
+
217
+ const token = await getIdToken(user);
218
+ const client = createMastraClient(token);
219
+
220
+ const weatherAgent = client.getAgent("weatherAgent");
221
+ const response = await weatherAgent.generate("What's the weather like in New York");
222
+
223
+ console.log({ response });
224
+ }
225
+
226
+ return (
227
+ <button onClick={handleClick} disabled={!user}>
228
+ Test Agent
229
+ </button>
230
+ );
231
+ };
232
+ ```
233
+
234
+
235
+ **nodejs:**
236
+
237
+ ```typescript title="server.js" copy
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
+ });
248
+
249
+ const app = express();
250
+ app.use(express.json());
251
+
252
+ app.post('/generate', async (req, res) => {
253
+ try {
254
+ const { idToken } = req.body;
255
+
256
+ // Verify the token
257
+ await admin.auth().verifyIdToken(idToken);
258
+
259
+ const mastra = new MastraClient({
260
+ baseUrl: "http://localhost:4111",
261
+ headers: {
262
+ Authorization: `Bearer ${idToken}`
263
+ }
264
+ });
265
+
266
+ const weatherAgent = mastra.getAgent("weatherAgent");
267
+ const response = await weatherAgent.generate("What's the weather like in Nairobi");
268
+
269
+ res.json({ response: response.text });
270
+ } catch (error) {
271
+ res.status(401).json({ error: 'Unauthorized' });
272
+ }
273
+ });
274
+ ```
275
+
276
+
277
+ **curl:**
278
+
279
+ ```bash
280
+ curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
281
+ -H "Content-Type: application/json" \
282
+ -H "Authorization: Bearer <your-firebase-id-token>" \
283
+ -d '{
284
+ "messages": "Weather in London"
285
+ }'
286
+ ```
@@ -0,0 +1,201 @@
1
+ > Documentation for the MastraAuthWorkos class, which authenticates Mastra applications using WorkOS authentication.
2
+
3
+ # MastraAuthWorkos Class
4
+
5
+ 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.
6
+
7
+ ## Prerequisites
8
+
9
+ This example uses WorkOS authentication. Make sure to:
10
+
11
+ 1. Create a WorkOS account at [workos.com](https://workos.com/)
12
+ 2. Set up an Application in your WorkOS Dashboard
13
+ 3. Configure your redirect URIs and allowed origins
14
+ 4. Set up Organizations and configure user roles as needed
15
+
16
+ ```env title=".env"
17
+ WORKOS_API_KEY=sk_live_...
18
+ WORKOS_CLIENT_ID=client_...
19
+ ```
20
+
21
+ > **Note:**
22
+
23
+ You can find your API key and Client ID in the WorkOS Dashboard under API Keys and Applications respectively.
24
+
25
+ For detailed setup instructions, refer to the [WorkOS documentation](https://workos.com/docs) for your specific platform.
26
+
27
+ ## Installation
28
+
29
+ Before you can use the `MastraAuthWorkos` class you have to install the `@mastra/auth-workos` package.
30
+
31
+ ```bash
32
+ npm install @mastra/auth-workos@beta
33
+ ```
34
+
35
+ ## Usage examples
36
+
37
+ ### Basic usage with environment variables
38
+
39
+ ```typescript {2,6} title="src/mastra/index.ts"
40
+ import { Mastra } from "@mastra/core";
41
+ import { MastraAuthWorkos } from "@mastra/auth-workos";
42
+
43
+ export const mastra = new Mastra({
44
+ server: {
45
+ auth: new MastraAuthWorkos(),
46
+ },
47
+ });
48
+ ```
49
+
50
+ ### Custom configuration
51
+
52
+ ```typescript {2,6-9} title="src/mastra/index.ts"
53
+ import { Mastra } from "@mastra/core";
54
+ import { MastraAuthWorkos } from "@mastra/auth-workos";
55
+
56
+ export const mastra = new Mastra({
57
+ server: {
58
+ auth: new MastraAuthWorkos({
59
+ apiKey: process.env.WORKOS_API_KEY,
60
+ clientId: process.env.WORKOS_CLIENT_ID,
61
+ }),
62
+ },
63
+ });
64
+ ```
65
+
66
+ ## Configuration
67
+
68
+ ### User Authorization
69
+
70
+ By default, `MastraAuthWorkos` checks whether the authenticated user has an 'admin' role in any of their organization memberships. The authorization process:
71
+
72
+ 1. Retrieves the user's organization memberships using their user ID
73
+ 2. Extracts all roles from their memberships
74
+ 3. Checks if any role has the slug 'admin'
75
+ 4. Grants access only if the user has admin role in at least one organization
76
+
77
+ To customize user authorization, provide a custom `authorizeUser` function:
78
+
79
+ ```typescript title="src/mastra/auth.ts"
80
+ import { MastraAuthWorkos } from "@mastra/auth-workos";
81
+
82
+ const workosAuth = new MastraAuthWorkos({
83
+ apiKey: process.env.WORKOS_API_KEY,
84
+ clientId: process.env.WORKOS_CLIENT_ID,
85
+ authorizeUser: async (user) => {
86
+ return !!user;
87
+ },
88
+ });
89
+ ```
90
+
91
+ > **Note:**
92
+
93
+ Visit [MastraAuthWorkos](https://mastra.ai/reference/v1/auth/workos) for all available configuration options.
94
+
95
+ ## Client-side setup
96
+
97
+ 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.
98
+
99
+ ### Installing WorkOS SDK
100
+
101
+ First, install the WorkOS SDK in your application:
102
+
103
+ ```bash
104
+ npm install @workos-inc/node
105
+ ```
106
+
107
+ ### Exchanging code for access token
108
+
109
+ After users complete the WorkOS authentication flow and return with an authorization code, exchange it for an access token:
110
+
111
+ ```typescript title="lib/auth.ts"
112
+ import { WorkOS } from "@workos-inc/node";
113
+
114
+ const workos = new WorkOS(process.env.WORKOS_API_KEY);
115
+
116
+ export const authenticateWithWorkos = async (
117
+ code: string,
118
+ clientId: string,
119
+ ) => {
120
+ const authenticationResponse =
121
+ await workos.userManagement.authenticateWithCode({
122
+ code,
123
+ clientId,
124
+ });
125
+
126
+ return authenticationResponse.accessToken;
127
+ };
128
+ ```
129
+
130
+ > **Note:**
131
+
132
+ Refer to the [WorkOS User Management documentation](https://workos.com/docs/authkit/vanilla/nodejs) for more authentication methods and configuration options.
133
+
134
+ ## Configuring `MastraClient`
135
+
136
+ When `auth` is enabled, all requests made with `MastraClient` must include a valid WorkOS access token in the `Authorization` header:
137
+
138
+ ```typescript title="lib/mastra/mastra-client.ts"
139
+ import { MastraClient } from "@mastra/client-js";
140
+
141
+ export const createMastraClient = (accessToken: string) => {
142
+ return new MastraClient({
143
+ baseUrl: "https://<mastra-api-url>",
144
+ headers: {
145
+ Authorization: `Bearer ${accessToken}`,
146
+ },
147
+ });
148
+ };
149
+ ```
150
+
151
+ > **Note:**
152
+
153
+ The access token must be prefixed with `Bearer` in the Authorization header.
154
+
155
+ Visit [Mastra Client SDK](https://mastra.ai/docs/v1/server/mastra-client) for more configuration options.
156
+
157
+ ### Making authenticated requests
158
+
159
+ Once `MastraClient` is configured with the WorkOS access token, you can send authenticated requests:
160
+
161
+ **react:**
162
+
163
+ ```typescript title="src/api/agents.ts" copy
164
+ import { WorkOS } from '@workos-inc/node';
165
+ import { MastraClient } from '@mastra/client-js';
166
+
167
+ const workos = new WorkOS(process.env.WORKOS_API_KEY);
168
+
169
+ export const callMastraWithWorkos = async (code: string, clientId: string) => {
170
+ const authenticationResponse = await workos.userManagement.authenticateWithCode({
171
+ code,
172
+ clientId,
173
+ });
174
+
175
+ const token = authenticationResponse.accessToken;
176
+
177
+ const mastra = new MastraClient({
178
+ baseUrl: "http://localhost:4111",
179
+ headers: {
180
+ Authorization: `Bearer ${token}`,
181
+ },
182
+ });
183
+
184
+ const weatherAgent = mastra.getAgent("weatherAgent");
185
+ const response = await weatherAgent.generate("What's the weather like in Nairobi");
186
+
187
+ return response.text;
188
+ };
189
+ ```
190
+
191
+
192
+ **curl:**
193
+
194
+ ```bash
195
+ curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
196
+ -H "Content-Type: application/json" \
197
+ -H "Authorization: Bearer <your-workos-access-token>" \
198
+ -d '{
199
+ "messages": "Weather in London"
200
+ }'
201
+ ```