@mastra/client-js 1.0.0-beta.8 → 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.
- package/CHANGELOG.md +1647 -0
- package/README.md +1 -3
- package/dist/_types/@ai-sdk_ui-utils/dist/index.d.ts +820 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +8511 -0
- package/dist/client.d.ts +74 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/docs/README.md +33 -0
- package/dist/docs/SKILL.md +34 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/ai-sdk/01-reference.md +358 -0
- package/dist/docs/client-js/01-reference.md +1180 -0
- package/dist/docs/server/01-mastra-client.md +256 -0
- package/dist/docs/server/02-jwt.md +99 -0
- package/dist/docs/server/03-clerk.md +143 -0
- package/dist/docs/server/04-supabase.md +128 -0
- package/dist/docs/server/05-firebase.md +286 -0
- package/dist/docs/server/06-workos.md +201 -0
- package/dist/docs/server/07-auth0.md +233 -0
- package/dist/index.cjs +1839 -664
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1837 -662
- package/dist/index.js.map +1 -1
- package/dist/resources/agent-builder.d.ts +10 -26
- package/dist/resources/agent-builder.d.ts.map +1 -1
- package/dist/resources/agent.d.ts +43 -8
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/index.d.ts +2 -0
- package/dist/resources/index.d.ts.map +1 -1
- package/dist/resources/memory-thread.d.ts +18 -3
- package/dist/resources/memory-thread.d.ts.map +1 -1
- package/dist/resources/observability.d.ts +58 -15
- package/dist/resources/observability.d.ts.map +1 -1
- package/dist/resources/processor.d.ts +20 -0
- package/dist/resources/processor.d.ts.map +1 -0
- package/dist/resources/run.d.ts +210 -0
- package/dist/resources/run.d.ts.map +1 -0
- package/dist/resources/stored-agent.d.ts +26 -0
- package/dist/resources/stored-agent.d.ts.map +1 -0
- package/dist/resources/workflow.d.ts +19 -224
- package/dist/resources/workflow.d.ts.map +1 -1
- package/dist/tools.d.ts +2 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/types.d.ts +240 -37
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +26 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +13 -12
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
> Learn how to set up and use the Mastra Client SDK
|
|
2
|
+
|
|
3
|
+
# Mastra Client SDK
|
|
4
|
+
|
|
5
|
+
The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](https://mastra.ai/docs/v1/server/mastra-server) from your client environment.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
To ensure smooth local development, make sure you have:
|
|
10
|
+
|
|
11
|
+
- Node.js `v22.13.0` or later
|
|
12
|
+
- TypeScript `v4.7` or higher (if using TypeScript)
|
|
13
|
+
- Your local Mastra server running (typically on port `4111`)
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
The Mastra Client SDK is designed for browser environments and uses the native `fetch` API for making HTTP requests to your Mastra server.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
To use the Mastra Client SDK, install the required dependencies:
|
|
22
|
+
|
|
23
|
+
**npm:**
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mastra/client-js@beta
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
**pnpm:**
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pnpm add @mastra/client-js@beta
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
**yarn:**
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
yarn add @mastra/client-js@beta
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
**bun:**
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
bun add @mastra/client-js@beta
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
### Initialize the `MastraClient`
|
|
53
|
+
|
|
54
|
+
Once initialized with a `baseUrl`, `MastraClient` exposes a type-safe interface for calling agents, tools, and workflows.
|
|
55
|
+
|
|
56
|
+
```typescript title="lib/mastra-client.ts"
|
|
57
|
+
import { MastraClient } from "@mastra/client-js";
|
|
58
|
+
|
|
59
|
+
export const mastraClient = new MastraClient({
|
|
60
|
+
baseUrl: process.env.MASTRA_API_URL || "http://localhost:4111",
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Core APIs
|
|
65
|
+
|
|
66
|
+
The Mastra Client SDK exposes all resources served by the Mastra Server
|
|
67
|
+
|
|
68
|
+
- **[Agents](https://mastra.ai/reference/v1/client-js/agents)**: Generate responses and stream conversations.
|
|
69
|
+
- **[Memory](https://mastra.ai/reference/v1/client-js/memory)**: Manage conversation threads and message history.
|
|
70
|
+
- **[Tools](https://mastra.ai/reference/v1/client-js/tools)**: Executed and managed tools.
|
|
71
|
+
- **[Workflows](https://mastra.ai/reference/v1/client-js/workflows)**: Trigger workflows and track their execution.
|
|
72
|
+
- **[Vectors](https://mastra.ai/reference/v1/client-js/vectors)**: Use vector embeddings for semantic search.
|
|
73
|
+
- **[Logs](https://mastra.ai/reference/v1/client-js/logs)**: View logs and debug system behavior.
|
|
74
|
+
- **[Telemetry](https://mastra.ai/reference/v1/client-js/telemetry)**: Monitor app performance and trace activity.
|
|
75
|
+
|
|
76
|
+
## Generating responses
|
|
77
|
+
|
|
78
|
+
Call `.generate()` with a string prompt:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { mastraClient } from "lib/mastra-client";
|
|
82
|
+
|
|
83
|
+
const testAgent = async () => {
|
|
84
|
+
try {
|
|
85
|
+
const agent = mastraClient.getAgent("testAgent");
|
|
86
|
+
|
|
87
|
+
const response = await agent.generate("Hello");
|
|
88
|
+
|
|
89
|
+
console.log(response.text);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return "Error occurred while generating response";
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
> **Note:**
|
|
97
|
+
|
|
98
|
+
You can also call `.generate()` with an array of message objects that include `role` and `content`. Visit the [.generate() reference](https://mastra.ai/reference/v1/client-js/agents#generate) for more information.
|
|
99
|
+
|
|
100
|
+
## Streaming responses
|
|
101
|
+
|
|
102
|
+
Use `.stream()` for real-time responses with a string prompt:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { mastraClient } from "lib/mastra-client";
|
|
106
|
+
|
|
107
|
+
const testAgent = async () => {
|
|
108
|
+
try {
|
|
109
|
+
const agent = mastraClient.getAgent("testAgent");
|
|
110
|
+
|
|
111
|
+
const stream = await agent.stream("Hello");
|
|
112
|
+
|
|
113
|
+
stream.processDataStream({
|
|
114
|
+
onTextPart: (text) => {
|
|
115
|
+
console.log(text);
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return "Error occurred while generating response";
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
> **Note:**
|
|
125
|
+
|
|
126
|
+
You can also call `.stream()` with an array of message objects that include `role` and `content`. Visit the [.stream() reference](https://mastra.ai/reference/v1/client-js/agents#stream) for more information.
|
|
127
|
+
|
|
128
|
+
## Configuration options
|
|
129
|
+
|
|
130
|
+
`MastraClient` accepts optional parameters like `retries`, `backoffMs`, and `headers` to control request behavior. These parameters are useful for controlling retry behavior and including diagnostic metadata.
|
|
131
|
+
|
|
132
|
+
```typescript title="lib/mastra-client.ts"
|
|
133
|
+
import { MastraClient } from "@mastra/client-js";
|
|
134
|
+
|
|
135
|
+
export const mastraClient = new MastraClient({
|
|
136
|
+
retries: 3,
|
|
137
|
+
backoffMs: 300,
|
|
138
|
+
maxBackoffMs: 5000,
|
|
139
|
+
headers: {
|
|
140
|
+
"X-Development": "true",
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
> **Note:**
|
|
146
|
+
|
|
147
|
+
Visit [MastraClient](https://mastra.ai/reference/v1/client-js/mastra-client) for more configuration options.
|
|
148
|
+
|
|
149
|
+
## Adding request cancelling
|
|
150
|
+
|
|
151
|
+
`MastraClient` supports request cancellation using the standard Node.js `AbortSignal` API. Useful for canceling in-flight requests, such as when users abort an operation or to clean up stale network calls.
|
|
152
|
+
|
|
153
|
+
Pass an `AbortSignal` to the client constructor to enable cancellation across all requests.
|
|
154
|
+
|
|
155
|
+
```typescript {3,7} title="lib/mastra-client.ts"
|
|
156
|
+
import { MastraClient } from "@mastra/client-js";
|
|
157
|
+
|
|
158
|
+
export const controller = new AbortController();
|
|
159
|
+
|
|
160
|
+
export const mastraClient = new MastraClient({
|
|
161
|
+
baseUrl: process.env.MASTRA_API_URL || "http://localhost:4111",
|
|
162
|
+
abortSignal: controller.signal,
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Using the `AbortController`
|
|
167
|
+
|
|
168
|
+
Calling `.abort()` will cancel any ongoing requests tied to that signal.
|
|
169
|
+
|
|
170
|
+
```typescript {4}
|
|
171
|
+
import { mastraClient, controller } from "lib/mastra-client";
|
|
172
|
+
|
|
173
|
+
const handleAbort = () => {
|
|
174
|
+
controller.abort();
|
|
175
|
+
};
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Client tools
|
|
179
|
+
|
|
180
|
+
Define tools directly in client-side applications using the `createTool()` function. Pass them to agents via the `clientTools` parameter in `.generate()` or `.stream()` calls.
|
|
181
|
+
|
|
182
|
+
This lets agents trigger browser-side functionality such as DOM manipulation, local storage access, or other Web APIs, enabling tool execution in the user's environment rather than on the server.
|
|
183
|
+
|
|
184
|
+
```typescript {27}
|
|
185
|
+
import { createTool } from "@mastra/client-js";
|
|
186
|
+
import { z } from "zod";
|
|
187
|
+
|
|
188
|
+
const handleClientTool = async () => {
|
|
189
|
+
try {
|
|
190
|
+
const agent = mastraClient.getAgent("colorAgent");
|
|
191
|
+
|
|
192
|
+
const colorChangeTool = createTool({
|
|
193
|
+
id: "color-change-tool",
|
|
194
|
+
description: "Changes the HTML background color",
|
|
195
|
+
inputSchema: z.object({
|
|
196
|
+
color: z.string(),
|
|
197
|
+
}),
|
|
198
|
+
outputSchema: z.object({
|
|
199
|
+
success: z.boolean(),
|
|
200
|
+
}),
|
|
201
|
+
execute: async (inputData) => {
|
|
202
|
+
const { color } = inputData;
|
|
203
|
+
|
|
204
|
+
document.body.style.backgroundColor = color;
|
|
205
|
+
return { success: true };
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const response = await agent.generate("Change the background to blue", {
|
|
210
|
+
clientTools: { colorChangeTool },
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
console.log(response);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.error(error);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Client tool's agent
|
|
221
|
+
|
|
222
|
+
This is a standard Mastra [agent](../agents/overview#setting-up-agents) configured to return hex color codes, intended to work with the browser-based client tool defined above.
|
|
223
|
+
|
|
224
|
+
```typescript title="src/mastra/agents/color-agent"
|
|
225
|
+
import { Agent } from "@mastra/core/agent";
|
|
226
|
+
|
|
227
|
+
export const colorAgent = new Agent({
|
|
228
|
+
id: "color-agent",
|
|
229
|
+
name: "Color Agent",
|
|
230
|
+
instructions: `You are a helpful CSS assistant.
|
|
231
|
+
You can change the background color of web pages.
|
|
232
|
+
Respond with a hex reference for the color requested by the user`,
|
|
233
|
+
model: "openai/gpt-5.1",
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Server-side environments
|
|
238
|
+
|
|
239
|
+
You can also use `MastraClient` in server-side environments such as API routes, serverless functions or actions. The usage will broadly remain the same but you may need to recreate the response to your client:
|
|
240
|
+
|
|
241
|
+
```typescript {8}
|
|
242
|
+
export async function action() {
|
|
243
|
+
const agent = mastraClient.getAgent("testAgent");
|
|
244
|
+
|
|
245
|
+
const stream = await agent.stream("Hello");
|
|
246
|
+
|
|
247
|
+
return new Response(stream.body);
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Best practices
|
|
252
|
+
|
|
253
|
+
1. **Error Handling**: Implement proper [error handling](https://mastra.ai/reference/v1/client-js/error-handling) for development scenarios.
|
|
254
|
+
2. **Environment Variables**: Use environment variables for configuration.
|
|
255
|
+
3. **Debugging**: Enable detailed [logging](https://mastra.ai/reference/v1/client-js/logs) when needed.
|
|
256
|
+
4. **Performance**: Monitor application performance, [telemetry](https://mastra.ai/reference/v1/client-js/telemetry) and traces.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
> Documentation for the MastraJwtAuth class, which authenticates Mastra applications using JSON Web Tokens.
|
|
2
|
+
|
|
3
|
+
# MastraJwtAuth Class
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Before you can use the `MastraJwtAuth` class you have to install the `@mastra/auth` package.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @mastra/auth@beta
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage example
|
|
16
|
+
|
|
17
|
+
```typescript {2,6-8} title="src/mastra/index.ts"
|
|
18
|
+
import { Mastra } from "@mastra/core";
|
|
19
|
+
import { MastraJwtAuth } from "@mastra/auth";
|
|
20
|
+
|
|
21
|
+
export const mastra = new Mastra({
|
|
22
|
+
server: {
|
|
23
|
+
auth: new MastraJwtAuth({
|
|
24
|
+
secret: process.env.MASTRA_JWT_SECRET,
|
|
25
|
+
}),
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
> **Note:**
|
|
31
|
+
|
|
32
|
+
Visit [MastraJwtAuth](https://mastra.ai/reference/v1/auth/jwt) for all available configuration options.
|
|
33
|
+
|
|
34
|
+
## Configuring `MastraClient`
|
|
35
|
+
|
|
36
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid JWT in the `Authorization` header:
|
|
37
|
+
|
|
38
|
+
```typescript {6} title="lib/mastra/mastra-client.ts"
|
|
39
|
+
import { MastraClient } from "@mastra/client-js";
|
|
40
|
+
|
|
41
|
+
export const mastraClient = new MastraClient({
|
|
42
|
+
baseUrl: "https://<mastra-api-url>",
|
|
43
|
+
headers: {
|
|
44
|
+
Authorization: `Bearer ${process.env.MASTRA_JWT_TOKEN}`,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
> **Note:**
|
|
50
|
+
|
|
51
|
+
Visit [Mastra Client SDK](https://mastra.ai/docs/v1/server/mastra-client) for more configuration options.
|
|
52
|
+
|
|
53
|
+
### Making authenticated requests
|
|
54
|
+
|
|
55
|
+
Once `MastraClient` is configured, you can send authenticated requests from your frontend application, or use `curl` for quick local testing:
|
|
56
|
+
|
|
57
|
+
**react:**
|
|
58
|
+
|
|
59
|
+
```tsx title="src/components/test-agent.tsx" copy
|
|
60
|
+
import { mastraClient } from "../../lib/mastra-client";
|
|
61
|
+
|
|
62
|
+
export const TestAgent = () => {
|
|
63
|
+
async function handleClick() {
|
|
64
|
+
const agent = mastraClient.getAgent("weatherAgent");
|
|
65
|
+
|
|
66
|
+
const response = await agent.generate("Weather in London");
|
|
67
|
+
|
|
68
|
+
console.log(response);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return <button onClick={handleClick}>Test Agent</button>;
|
|
72
|
+
};
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
**curl:**
|
|
77
|
+
|
|
78
|
+
```bash
|
|
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
|
+
```
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## Creating a JWT
|
|
90
|
+
|
|
91
|
+
To authenticate requests to your Mastra server, you'll need a valid JSON Web Token (JWT) signed with your `MASTRA_JWT_SECRET`.
|
|
92
|
+
|
|
93
|
+
The easiest way to generate one is using [jwt.io](https://www.jwt.io/):
|
|
94
|
+
|
|
95
|
+
1. Select **JWT Encoder**.
|
|
96
|
+
2. Scroll down to the **Sign JWT: Secret** section.
|
|
97
|
+
3. Enter your secret (for example: `supersecretdevkeythatishs256safe!`).
|
|
98
|
+
4. Click **Generate example** to create a valid JWT.
|
|
99
|
+
5. Copy the generated token and set it as `MASTRA_JWT_TOKEN` in your `.env` file.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
> Documentation for the MastraAuthClerk class, which authenticates Mastra applications using Clerk authentication.
|
|
2
|
+
|
|
3
|
+
# MastraAuthClerk Class
|
|
4
|
+
|
|
5
|
+
The `MastraAuthClerk` class provides authentication for Mastra using Clerk. It verifies incoming requests using Clerk's authentication system and integrates with the Mastra server using the `auth` option.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
This example uses Clerk authentication. Make sure to add your Clerk credentials to your `.env` file and ensure your Clerk project is properly configured.
|
|
10
|
+
|
|
11
|
+
```env title=".env"
|
|
12
|
+
CLERK_PUBLISHABLE_KEY=pk_test_...
|
|
13
|
+
CLERK_SECRET_KEY=sk_test_...
|
|
14
|
+
CLERK_JWKS_URI=https://your-clerk-domain.clerk.accounts.dev/.well-known/jwks.json
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
> **Note:**
|
|
18
|
+
|
|
19
|
+
You can find these keys in your Clerk Dashboard under "API Keys".
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Before you can use the `MastraAuthClerk` class you have to install the `@mastra/auth-clerk` package.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mastra/auth-clerk@beta
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage example
|
|
30
|
+
|
|
31
|
+
```typescript {2,6-10} title="src/mastra/index.ts"
|
|
32
|
+
import { Mastra } from "@mastra/core";
|
|
33
|
+
import { MastraAuthClerk } from "@mastra/auth-clerk";
|
|
34
|
+
|
|
35
|
+
export const mastra = new Mastra({
|
|
36
|
+
server: {
|
|
37
|
+
auth: new MastraAuthClerk({
|
|
38
|
+
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
|
|
39
|
+
secretKey: process.env.CLERK_SECRET_KEY,
|
|
40
|
+
jwksUri: process.env.CLERK_JWKS_URI,
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> **Note:**
|
|
47
|
+
|
|
48
|
+
The default `authorizeUser` method allows all authenticated users. To customize user authorization, provide a custom `authorizeUser` function when constructing the provider.
|
|
49
|
+
|
|
50
|
+
Visit [MastraAuthClerk](https://mastra.ai/reference/v1/auth/clerk) for all available configuration options.
|
|
51
|
+
|
|
52
|
+
## Client-side setup
|
|
53
|
+
|
|
54
|
+
When using Clerk auth, you'll need to retrieve the access token from Clerk on the client side and pass it to your Mastra requests.
|
|
55
|
+
|
|
56
|
+
### Retrieving the access token
|
|
57
|
+
|
|
58
|
+
Use the Clerk React hooks to authenticate users and retrieve their access token:
|
|
59
|
+
|
|
60
|
+
```typescript title="lib/auth.ts"
|
|
61
|
+
import { useAuth } from "@clerk/nextjs";
|
|
62
|
+
|
|
63
|
+
export const useClerkAuth = () => {
|
|
64
|
+
const { getToken } = useAuth();
|
|
65
|
+
|
|
66
|
+
const getAccessToken = async () => {
|
|
67
|
+
const token = await getToken();
|
|
68
|
+
return token;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return { getAccessToken };
|
|
72
|
+
};
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> **Note:**
|
|
76
|
+
|
|
77
|
+
Refer to the [Clerk documentation](https://clerk.com/docs) for more information.
|
|
78
|
+
|
|
79
|
+
## Configuring `MastraClient`
|
|
80
|
+
|
|
81
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid Clerk access token in the `Authorization` header:
|
|
82
|
+
|
|
83
|
+
```typescript {6} title="lib/mastra/mastra-client.ts"
|
|
84
|
+
import { MastraClient } from "@mastra/client-js";
|
|
85
|
+
|
|
86
|
+
export const mastraClient = new MastraClient({
|
|
87
|
+
baseUrl: "https://<mastra-api-url>",
|
|
88
|
+
headers: {
|
|
89
|
+
Authorization: `Bearer ${accessToken}`,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
> **Note:**
|
|
95
|
+
|
|
96
|
+
The access token must be prefixed with `Bearer` in the Authorization header.
|
|
97
|
+
|
|
98
|
+
Visit [Mastra Client SDK](https://mastra.ai/docs/v1/server/mastra-client) for more configuration options.
|
|
99
|
+
|
|
100
|
+
### Making authenticated requests
|
|
101
|
+
|
|
102
|
+
Once `MastraClient` is configured with the Clerk access token, you can send authenticated requests:
|
|
103
|
+
|
|
104
|
+
**react:**
|
|
105
|
+
|
|
106
|
+
```tsx title="src/components/test-agent.tsx" copy
|
|
107
|
+
"use client";
|
|
108
|
+
|
|
109
|
+
import { useAuth } from "@clerk/nextjs";
|
|
110
|
+
import { MastraClient } from "@mastra/client-js";
|
|
111
|
+
|
|
112
|
+
export const TestAgent = () => {
|
|
113
|
+
const { getToken } = useAuth();
|
|
114
|
+
|
|
115
|
+
async function handleClick() {
|
|
116
|
+
const token = await getToken();
|
|
117
|
+
|
|
118
|
+
const client = new MastraClient({
|
|
119
|
+
baseUrl: "http://localhost:4111",
|
|
120
|
+
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const weatherAgent = client.getAgent("weatherAgent");
|
|
124
|
+
const response = await weatherAgent.generate("What's the weather like in New York");
|
|
125
|
+
|
|
126
|
+
console.log({ response });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return <button onClick={handleClick}>Test Agent</button>;
|
|
130
|
+
};
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
**curl:**
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
138
|
+
-H "Content-Type: application/json" \
|
|
139
|
+
-H "Authorization: Bearer <your-clerk-access-token>" \
|
|
140
|
+
-d '{
|
|
141
|
+
"messages": "Weather in London"
|
|
142
|
+
}'
|
|
143
|
+
```
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
> Documentation for the MastraAuthSupabase class, which authenticates Mastra applications using Supabase Auth.
|
|
2
|
+
|
|
3
|
+
# MastraAuthSupabase Class
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
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
|
+
|
|
11
|
+
```env title=".env"
|
|
12
|
+
SUPABASE_URL=https://your-project.supabase.co
|
|
13
|
+
SUPABASE_ANON_KEY=your-anon-key
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
> **Note:**
|
|
17
|
+
|
|
18
|
+
Review your Supabase Row Level Security (RLS) settings to ensure proper data access controls.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Before you can use the `MastraAuthSupabase` class you have to install the `@mastra/auth-supabase` package.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @mastra/auth-supabase@beta
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage example
|
|
29
|
+
|
|
30
|
+
```typescript {2,6-9} title="src/mastra/index.ts"
|
|
31
|
+
import { Mastra } from "@mastra/core";
|
|
32
|
+
import { MastraAuthSupabase } from "@mastra/auth-supabase";
|
|
33
|
+
|
|
34
|
+
export const mastra = new Mastra({
|
|
35
|
+
server: {
|
|
36
|
+
auth: new MastraAuthSupabase({
|
|
37
|
+
url: process.env.SUPABASE_URL,
|
|
38
|
+
anonKey: process.env.SUPABASE_ANON_KEY,
|
|
39
|
+
}),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
> **Note:**
|
|
45
|
+
|
|
46
|
+
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.
|
|
47
|
+
|
|
48
|
+
Visit [MastraAuthSupabase](https://mastra.ai/reference/v1/auth/supabase) for all available configuration options.
|
|
49
|
+
|
|
50
|
+
## Client-side setup
|
|
51
|
+
|
|
52
|
+
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.
|
|
53
|
+
|
|
54
|
+
### Retrieving the access token
|
|
55
|
+
|
|
56
|
+
Use the Supabase client to authenticate users and retrieve their access token:
|
|
57
|
+
|
|
58
|
+
```typescript title="lib/auth.ts"
|
|
59
|
+
import { createClient } from "@supabase/supabase-js";
|
|
60
|
+
|
|
61
|
+
const supabase = createClient("<supabase-url>", "<supabase-key>");
|
|
62
|
+
|
|
63
|
+
const authTokenResponse = await supabase.auth.signInWithPassword({
|
|
64
|
+
email: "<user's email>",
|
|
65
|
+
password: "<user's password>",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const accessToken = authTokenResponse.data?.session?.access_token;
|
|
69
|
+
```
|
|
70
|
+
|
|
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.
|
|
74
|
+
|
|
75
|
+
## Configuring `MastraClient`
|
|
76
|
+
|
|
77
|
+
When `auth` is enabled, all requests made with `MastraClient` must include a valid Supabase access token in the `Authorization` header:
|
|
78
|
+
|
|
79
|
+
```typescript {6} title="lib/mastra/mastra-client.ts"
|
|
80
|
+
import { MastraClient } from "@mastra/client-js";
|
|
81
|
+
|
|
82
|
+
export const mastraClient = new MastraClient({
|
|
83
|
+
baseUrl: "https://<mastra-api-url>",
|
|
84
|
+
headers: {
|
|
85
|
+
Authorization: `Bearer ${accessToken}`,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
> **Note:**
|
|
91
|
+
|
|
92
|
+
The access token must be prefixed with `Bearer` in the Authorization header.
|
|
93
|
+
|
|
94
|
+
Visit [Mastra Client SDK](https://mastra.ai/docs/v1/server/mastra-client) for more configuration options.
|
|
95
|
+
|
|
96
|
+
### Making authenticated requests
|
|
97
|
+
|
|
98
|
+
Once `MastraClient` is configured with the Supabase access token, you can send authenticated requests:
|
|
99
|
+
|
|
100
|
+
**react:**
|
|
101
|
+
|
|
102
|
+
```tsx title="src/components/test-agent.tsx" copy
|
|
103
|
+
import { mastraClient } from "../../lib/mastra-client";
|
|
104
|
+
|
|
105
|
+
export const TestAgent = () => {
|
|
106
|
+
async function handleClick() {
|
|
107
|
+
const agent = mastraClient.getAgent("weatherAgent");
|
|
108
|
+
|
|
109
|
+
const response = await agent.generate("What's the weather like in New York");
|
|
110
|
+
|
|
111
|
+
console.log(response);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return <button onClick={handleClick}>Test Agent</button>;
|
|
115
|
+
};
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
**curl:**
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
123
|
+
-H "Content-Type: application/json" \
|
|
124
|
+
-H "Authorization: Bearer <your-supabase-access-token>" \
|
|
125
|
+
-d '{
|
|
126
|
+
"messages": "Weather in London"
|
|
127
|
+
}'
|
|
128
|
+
```
|