@mastra/mcp-docs-server 0.13.23 → 0.13.24-alpha.2
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/.docs/organized/changelogs/%40internal%2Fstorage-test-utils.md +8 -8
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +2 -0
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +19 -0
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +35 -35
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +46 -46
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +38 -38
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +37 -37
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +37 -37
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Flance.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +30 -30
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +51 -51
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Frag.md +14 -14
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +10 -10
- package/.docs/organized/changelogs/create-mastra.md +14 -14
- package/.docs/organized/changelogs/mastra.md +46 -46
- package/.docs/raw/agents/dynamic-agents.mdx +19 -0
- package/.docs/raw/agents/overview.mdx +46 -0
- package/.docs/raw/auth/auth0.mdx +235 -0
- package/.docs/raw/auth/index.mdx +2 -7
- package/.docs/raw/auth/workos.mdx +198 -0
- package/.docs/raw/reference/agents/agent.mdx +82 -5
- package/.docs/raw/reference/agents/getInstructions.mdx +2 -2
- package/.docs/raw/reference/auth/auth0.mdx +117 -0
- package/.docs/raw/reference/auth/workos.mdx +128 -0
- package/.docs/raw/reference/workflows/step.mdx +1 -1
- package/CHANGELOG.md +22 -0
- package/package.json +4 -4
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "MastraAuthAuth0 Class"
|
|
3
|
+
description: "Documentation for the MastraAuthAuth0 class, which authenticates Mastra applications using Auth0 authentication."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Tabs, Tab } from "@/components/tabs";
|
|
7
|
+
|
|
8
|
+
# MastraAuthAuth0 Class
|
|
9
|
+
|
|
10
|
+
The `MastraAuthAuth0` class provides authentication for Mastra using Auth0. It verifies incoming requests using Auth0-issued JWT tokens and integrates with the Mastra server using the `experimental_auth` option.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
This example uses Auth0 authentication. Make sure to:
|
|
15
|
+
|
|
16
|
+
1. Create an Auth0 account at [auth0.com](https://auth0.com/)
|
|
17
|
+
2. Set up an Application in your Auth0 Dashboard
|
|
18
|
+
3. Configure an API in your Auth0 Dashboard with an identifier (audience)
|
|
19
|
+
4. Configure your application's allowed callback URLs, web origins, and logout URLs
|
|
20
|
+
|
|
21
|
+
```env filename=".env" copy
|
|
22
|
+
AUTH0_DOMAIN=your-tenant.auth0.com
|
|
23
|
+
AUTH0_AUDIENCE=your-api-identifier
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> **Note:** You can find your domain in the Auth0 Dashboard under Applications > Settings. The audience is the identifier of your API configured in Auth0 Dashboard > APIs.
|
|
27
|
+
|
|
28
|
+
> For detailed setup instructions, refer to the [Auth0 quickstarts](https://auth0.com/docs/quickstarts) for your specific platform.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
Before you can use the `MastraAuthAuth0` class you have to install the `@mastra/auth-auth0` package.
|
|
33
|
+
|
|
34
|
+
```bash copy
|
|
35
|
+
npm install @mastra/auth-auth0@latest
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage examples
|
|
39
|
+
|
|
40
|
+
### Basic usage with environment variables
|
|
41
|
+
|
|
42
|
+
```typescript {2,7} filename="src/mastra/index.ts" showLineNumbers copy
|
|
43
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
44
|
+
import { MastraAuthAuth0 } from '@mastra/auth-auth0';
|
|
45
|
+
|
|
46
|
+
export const mastra = new Mastra({
|
|
47
|
+
// ..
|
|
48
|
+
server: {
|
|
49
|
+
experimental_auth: new MastraAuthAuth0(),
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Custom configuration
|
|
55
|
+
|
|
56
|
+
```typescript {2,7-10} filename="src/mastra/index.ts" showLineNumbers copy
|
|
57
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
58
|
+
import { MastraAuthAuth0 } from '@mastra/auth-auth0';
|
|
59
|
+
|
|
60
|
+
export const mastra = new Mastra({
|
|
61
|
+
// ..
|
|
62
|
+
server: {
|
|
63
|
+
experimental_auth: new MastraAuthAuth0({
|
|
64
|
+
domain: process.env.AUTH0_DOMAIN,
|
|
65
|
+
audience: process.env.AUTH0_AUDIENCE
|
|
66
|
+
}),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Configuration
|
|
72
|
+
|
|
73
|
+
### User Authorization
|
|
74
|
+
|
|
75
|
+
By default, `MastraAuthAuth0` allows all authenticated users who have valid Auth0 tokens for the specified audience. The token verification ensures that:
|
|
76
|
+
|
|
77
|
+
1. The token is properly signed by Auth0
|
|
78
|
+
2. The token is not expired
|
|
79
|
+
3. The token audience matches your configured audience
|
|
80
|
+
4. The token issuer matches your Auth0 domain
|
|
81
|
+
|
|
82
|
+
To customize user authorization, provide a custom `authorizeUser` function:
|
|
83
|
+
|
|
84
|
+
```typescript filename="src/mastra/auth.ts" showLineNumbers copy
|
|
85
|
+
import { MastraAuthAuth0 } from '@mastra/auth-auth0';
|
|
86
|
+
|
|
87
|
+
const auth0Provider = new MastraAuthAuth0({
|
|
88
|
+
authorizeUser: async (user) => {
|
|
89
|
+
// Custom authorization logic
|
|
90
|
+
return user.email?.endsWith('@yourcompany.com') || false;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
> See the [MastraAuthAuth0](/reference/auth/auth0.mdx) API reference for all available configuration options.
|
|
96
|
+
|
|
97
|
+
## Client-side setup
|
|
98
|
+
|
|
99
|
+
When using Auth0 auth, you'll need to set up the Auth0 React SDK, authenticate users, and retrieve their access tokens to pass to your Mastra requests.
|
|
100
|
+
|
|
101
|
+
### Setting up Auth0 React SDK
|
|
102
|
+
|
|
103
|
+
First, install and configure the Auth0 React SDK in your application:
|
|
104
|
+
|
|
105
|
+
```bash copy
|
|
106
|
+
npm install @auth0/auth0-react
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
```typescript filename="src/auth0-provider.tsx" showLineNumbers copy
|
|
110
|
+
import React from 'react';
|
|
111
|
+
import { Auth0Provider } from '@auth0/auth0-react';
|
|
112
|
+
|
|
113
|
+
const Auth0ProviderWithHistory = ({ children }) => {
|
|
114
|
+
return (
|
|
115
|
+
<Auth0Provider
|
|
116
|
+
domain={process.env.REACT_APP_AUTH0_DOMAIN}
|
|
117
|
+
clientId={process.env.REACT_APP_AUTH0_CLIENT_ID}
|
|
118
|
+
authorizationParams={{
|
|
119
|
+
redirect_uri: window.location.origin,
|
|
120
|
+
audience: process.env.REACT_APP_AUTH0_AUDIENCE,
|
|
121
|
+
scope: "read:current_user update:current_user_metadata"
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
{children}
|
|
125
|
+
</Auth0Provider>
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export default Auth0ProviderWithHistory;
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Retrieving access tokens
|
|
133
|
+
|
|
134
|
+
Use the Auth0 React SDK to authenticate users and retrieve their access tokens:
|
|
135
|
+
|
|
136
|
+
```typescript filename="lib/auth.ts" showLineNumbers copy
|
|
137
|
+
import { useAuth0 } from '@auth0/auth0-react';
|
|
138
|
+
|
|
139
|
+
export const useAuth0Token = () => {
|
|
140
|
+
const { getAccessTokenSilently } = useAuth0();
|
|
141
|
+
|
|
142
|
+
const getAccessToken = async () => {
|
|
143
|
+
const token = await getAccessTokenSilently();
|
|
144
|
+
return token;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return { getAccessToken };
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
> Refer to the [Auth0 React SDK documentation](https://auth0.com/docs/libraries/auth0-react) for more authentication methods and configuration options.
|
|
152
|
+
|
|
153
|
+
## Configuring `MastraClient`
|
|
154
|
+
|
|
155
|
+
When `experimental_auth` is enabled, all requests made with `MastraClient` must include a valid Auth0 access token in the `Authorization` header:
|
|
156
|
+
|
|
157
|
+
```typescript filename="lib/mastra/mastra-client.ts" showLineNumbers copy
|
|
158
|
+
import { MastraClient } from "@mastra/client-js";
|
|
159
|
+
|
|
160
|
+
export const createMastraClient = (accessToken: string) => {
|
|
161
|
+
return new MastraClient({
|
|
162
|
+
baseUrl: "https://<mastra-api-url>",
|
|
163
|
+
headers: {
|
|
164
|
+
Authorization: `Bearer ${accessToken}`
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
> **Note:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
171
|
+
|
|
172
|
+
> See [Mastra Client SDK](/docs/server-db/mastra-client.mdx) for more configuration options.
|
|
173
|
+
|
|
174
|
+
### Making authenticated requests
|
|
175
|
+
|
|
176
|
+
Once `MastraClient` is configured with the Auth0 access token, you can send authenticated requests:
|
|
177
|
+
|
|
178
|
+
<Tabs items={["React Component", "curl"]}>
|
|
179
|
+
<Tab>
|
|
180
|
+
```tsx filename="src/components/mastra-api-test.tsx" showLineNumbers copy
|
|
181
|
+
import React, { useState } from 'react';
|
|
182
|
+
import { useAuth0 } from '@auth0/auth0-react';
|
|
183
|
+
import { MastraClient } from '@mastra/client-js';
|
|
184
|
+
|
|
185
|
+
export const MastraApiTest = () => {
|
|
186
|
+
const { getAccessTokenSilently } = useAuth0();
|
|
187
|
+
const [result, setResult] = useState(null);
|
|
188
|
+
|
|
189
|
+
const callMastraApi = async () => {
|
|
190
|
+
const token = await getAccessTokenSilently();
|
|
191
|
+
|
|
192
|
+
const mastra = new MastraClient({
|
|
193
|
+
baseUrl: "http://localhost:4111",
|
|
194
|
+
headers: {
|
|
195
|
+
Authorization: `Bearer ${token}`
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const weatherAgent = mastra.getAgent("weatherAgent");
|
|
200
|
+
const response = await weatherAgent.generate({
|
|
201
|
+
messages: "What's the weather like in New York"
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
setResult(response.text);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
<div>
|
|
209
|
+
<button onClick={callMastraApi}>
|
|
210
|
+
Test Mastra API
|
|
211
|
+
</button>
|
|
212
|
+
|
|
213
|
+
{result && (
|
|
214
|
+
<div className="result">
|
|
215
|
+
<h6>Result:</h6>
|
|
216
|
+
<pre>{result}</pre>
|
|
217
|
+
</div>
|
|
218
|
+
)}
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
};
|
|
222
|
+
```
|
|
223
|
+
</Tab>
|
|
224
|
+
<Tab>
|
|
225
|
+
```bash copy
|
|
226
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
227
|
+
-H "Content-Type: application/json" \
|
|
228
|
+
-H "Authorization: Bearer <your-auth0-access-token>" \
|
|
229
|
+
-d '{
|
|
230
|
+
"messages": "Weather in London"
|
|
231
|
+
}'
|
|
232
|
+
```
|
|
233
|
+
</Tab>
|
|
234
|
+
</Tabs>
|
|
235
|
+
|
package/.docs/raw/auth/index.mdx
CHANGED
|
@@ -15,10 +15,5 @@ You can start with simple shared secret JWT authentication and switch to provide
|
|
|
15
15
|
- [Clerk](/docs/auth/clerk)
|
|
16
16
|
- [Supabase](/docs/auth/supabase)
|
|
17
17
|
- [Firebase](/docs/auth/firebase)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
The following providers will be available soon:
|
|
22
|
-
|
|
23
|
-
- Auth0
|
|
24
|
-
- WorkOS
|
|
18
|
+
- [WorkOS](/docs/auth/workos)
|
|
19
|
+
- [Auth0](/docs/auth/auth0)
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "MastraAuthWorkos Class"
|
|
3
|
+
description: "Documentation for the MastraAuthWorkos class, which authenticates Mastra applications using WorkOS authentication."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Tabs, Tab } from "@/components/tabs";
|
|
7
|
+
|
|
8
|
+
# MastraAuthWorkos Class
|
|
9
|
+
|
|
10
|
+
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 `experimental_auth` option.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
This example uses WorkOS authentication. Make sure to:
|
|
15
|
+
|
|
16
|
+
1. Create a WorkOS account at [workos.com](https://workos.com/)
|
|
17
|
+
2. Set up an Application in your WorkOS Dashboard
|
|
18
|
+
3. Configure your redirect URIs and allowed origins
|
|
19
|
+
4. Set up Organizations and configure user roles as needed
|
|
20
|
+
|
|
21
|
+
```env filename=".env" copy
|
|
22
|
+
WORKOS_API_KEY=sk_live_...
|
|
23
|
+
WORKOS_CLIENT_ID=client_...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> **Note:** You can find your API key and Client ID in the WorkOS Dashboard under API Keys and Applications respectively.
|
|
27
|
+
|
|
28
|
+
> For detailed setup instructions, refer to the [WorkOS documentation](https://workos.com/docs) for your specific platform.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
Before you can use the `MastraAuthWorkos` class you have to install the `@mastra/auth-workos` package.
|
|
33
|
+
|
|
34
|
+
```bash copy
|
|
35
|
+
npm install @mastra/auth-workos@latest
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage examples
|
|
39
|
+
|
|
40
|
+
### Basic usage with environment variables
|
|
41
|
+
|
|
42
|
+
```typescript {2,7} filename="src/mastra/index.ts" showLineNumbers copy
|
|
43
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
44
|
+
import { MastraAuthWorkos } from '@mastra/auth-workos';
|
|
45
|
+
|
|
46
|
+
export const mastra = new Mastra({
|
|
47
|
+
// ..
|
|
48
|
+
server: {
|
|
49
|
+
experimental_auth: new MastraAuthWorkos(),
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Custom configuration
|
|
55
|
+
|
|
56
|
+
```typescript {2,7-10} filename="src/mastra/index.ts" showLineNumbers copy
|
|
57
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
58
|
+
import { MastraAuthWorkos } from '@mastra/auth-workos';
|
|
59
|
+
|
|
60
|
+
export const mastra = new Mastra({
|
|
61
|
+
// ..
|
|
62
|
+
server: {
|
|
63
|
+
experimental_auth: new MastraAuthWorkos({
|
|
64
|
+
apiKey: process.env.WORKOS_API_KEY,
|
|
65
|
+
clientId: process.env.WORKOS_CLIENT_ID
|
|
66
|
+
}),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Configuration
|
|
72
|
+
|
|
73
|
+
### User Authorization
|
|
74
|
+
|
|
75
|
+
By default, `MastraAuthWorkos` checks whether the authenticated user has an 'admin' role in any of their organization memberships. The authorization process:
|
|
76
|
+
|
|
77
|
+
1. Retrieves the user's organization memberships using their user ID
|
|
78
|
+
2. Extracts all roles from their memberships
|
|
79
|
+
3. Checks if any role has the slug 'admin'
|
|
80
|
+
4. Grants access only if the user has admin role in at least one organization
|
|
81
|
+
|
|
82
|
+
To customize user authorization, provide a custom `authorizeUser` function:
|
|
83
|
+
|
|
84
|
+
```typescript filename="src/mastra/auth.ts" showLineNumbers copy
|
|
85
|
+
import { MastraAuthWorkos } from '@mastra/auth-workos';
|
|
86
|
+
|
|
87
|
+
const workosAuth = new MastraAuthWorkos({
|
|
88
|
+
apiKey: process.env.WORKOS_API_KEY,
|
|
89
|
+
clientId: process.env.WORKOS_CLIENT_ID,
|
|
90
|
+
authorizeUser: async (user) => {
|
|
91
|
+
return !!user;
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
> See the [MastraAuthWorkos](/reference/auth/workos) API reference for all available configuration options.
|
|
97
|
+
|
|
98
|
+
## Client-side setup
|
|
99
|
+
|
|
100
|
+
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.
|
|
101
|
+
|
|
102
|
+
### Installing WorkOS SDK
|
|
103
|
+
|
|
104
|
+
First, install the WorkOS SDK in your application:
|
|
105
|
+
|
|
106
|
+
```bash copy
|
|
107
|
+
npm install @workos-inc/node
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Exchanging code for access token
|
|
111
|
+
|
|
112
|
+
After users complete the WorkOS authentication flow and return with an authorization code, exchange it for an access token:
|
|
113
|
+
|
|
114
|
+
```typescript filename="lib/auth.ts" showLineNumbers copy
|
|
115
|
+
import { WorkOS } from '@workos-inc/node';
|
|
116
|
+
|
|
117
|
+
const workos = new WorkOS(process.env.WORKOS_API_KEY);
|
|
118
|
+
|
|
119
|
+
export const authenticateWithWorkos = async (code: string, clientId: string) => {
|
|
120
|
+
const authenticationResponse = await workos.userManagement.authenticateWithCode({
|
|
121
|
+
code,
|
|
122
|
+
clientId,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return authenticationResponse.accessToken;
|
|
126
|
+
};
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
> Refer to the [WorkOS User Management documentation](https://workos.com/docs/authkit/vanilla/nodejs) for more authentication methods and configuration options.
|
|
130
|
+
|
|
131
|
+
## Configuring `MastraClient`
|
|
132
|
+
|
|
133
|
+
When `experimental_auth` is enabled, all requests made with `MastraClient` must include a valid WorkOS access token in the `Authorization` header:
|
|
134
|
+
|
|
135
|
+
```typescript filename="lib/mastra/mastra-client.ts" showLineNumbers copy
|
|
136
|
+
import { MastraClient } from "@mastra/client-js";
|
|
137
|
+
|
|
138
|
+
export const createMastraClient = (accessToken: string) => {
|
|
139
|
+
return new MastraClient({
|
|
140
|
+
baseUrl: "https://<mastra-api-url>",
|
|
141
|
+
headers: {
|
|
142
|
+
Authorization: `Bearer ${accessToken}`
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
> **Note:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
149
|
+
|
|
150
|
+
> See [Mastra Client SDK](/docs/server-db/mastra-client) for more configuration options.
|
|
151
|
+
|
|
152
|
+
### Making authenticated requests
|
|
153
|
+
|
|
154
|
+
Once `MastraClient` is configured with the WorkOS access token, you can send authenticated requests:
|
|
155
|
+
|
|
156
|
+
<Tabs items={["JavaScript/Node.js", "curl"]}>
|
|
157
|
+
<Tab>
|
|
158
|
+
```typescript filename="src/api/agents.ts" showLineNumbers copy
|
|
159
|
+
import { WorkOS } from '@workos-inc/node';
|
|
160
|
+
import { MastraClient } from '@mastra/client-js';
|
|
161
|
+
|
|
162
|
+
const workos = new WorkOS(process.env.WORKOS_API_KEY);
|
|
163
|
+
|
|
164
|
+
export const callMastraWithWorkos = async (code: string, clientId: string) => {
|
|
165
|
+
const authenticationResponse = await workos.userManagement.authenticateWithCode({
|
|
166
|
+
code,
|
|
167
|
+
clientId,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const token = authenticationResponse.accessToken;
|
|
171
|
+
|
|
172
|
+
const mastra = new MastraClient({
|
|
173
|
+
baseUrl: "http://localhost:4111",
|
|
174
|
+
headers: {
|
|
175
|
+
Authorization: `Bearer ${token}`,
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const weatherAgent = mastra.getAgent("weatherAgent");
|
|
180
|
+
const response = await weatherAgent.generate({
|
|
181
|
+
messages: "What's the weather like in Nairobi",
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return response.text;
|
|
185
|
+
};
|
|
186
|
+
```
|
|
187
|
+
</Tab>
|
|
188
|
+
<Tab>
|
|
189
|
+
```bash copy
|
|
190
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
191
|
+
-H "Content-Type: application/json" \
|
|
192
|
+
-H "Authorization: Bearer <your-workos-access-token>" \
|
|
193
|
+
-d '{
|
|
194
|
+
"messages": "Weather in London"
|
|
195
|
+
}'
|
|
196
|
+
```
|
|
197
|
+
</Tab>
|
|
198
|
+
</Tabs>
|
|
@@ -7,19 +7,94 @@ description: "Documentation for the `Agent` class in Mastra, which provides the
|
|
|
7
7
|
|
|
8
8
|
The `Agent` class is the foundation for creating AI agents in Mastra. It provides methods for generating responses, streaming interactions, and handling voice capabilities.
|
|
9
9
|
|
|
10
|
-
## Usage
|
|
10
|
+
## Usage examples
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
### Basic string instructions
|
|
13
|
+
|
|
14
|
+
```typescript filename="src/mastra/agents/string-agent.ts" showLineNumbers copy
|
|
13
15
|
import { openai } from "@ai-sdk/openai";
|
|
14
16
|
import { Agent } from "@mastra/core/agent";
|
|
15
17
|
|
|
18
|
+
// String instructions
|
|
16
19
|
export const agent = new Agent({
|
|
17
20
|
name: "test-agent",
|
|
18
|
-
instructions: '
|
|
21
|
+
instructions: 'You are a helpful assistant that provides concise answers.',
|
|
22
|
+
model: openai("gpt-4o")
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// System message object
|
|
26
|
+
export const agent2 = new Agent({
|
|
27
|
+
name: "test-agent-2",
|
|
28
|
+
instructions: {
|
|
29
|
+
role: "system",
|
|
30
|
+
content: "You are an expert programmer"
|
|
31
|
+
},
|
|
32
|
+
model: openai("gpt-4o")
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Array of system messages
|
|
36
|
+
export const agent3 = new Agent({
|
|
37
|
+
name: "test-agent-3",
|
|
38
|
+
instructions: [
|
|
39
|
+
{ role: "system", content: "You are a helpful assistant" },
|
|
40
|
+
{ role: "system", content: "You have expertise in TypeScript" }
|
|
41
|
+
],
|
|
19
42
|
model: openai("gpt-4o")
|
|
20
43
|
});
|
|
21
44
|
```
|
|
22
45
|
|
|
46
|
+
### Single CoreSystemMessage
|
|
47
|
+
|
|
48
|
+
Use CoreSystemMessage format to access additional properties like `providerOptions` for provider-specific configurations:
|
|
49
|
+
|
|
50
|
+
```typescript filename="src/mastra/agents/core-message-agent.ts" showLineNumbers copy
|
|
51
|
+
import { openai } from "@ai-sdk/openai";
|
|
52
|
+
import { Agent } from "@mastra/core/agent";
|
|
53
|
+
|
|
54
|
+
export const agent = new Agent({
|
|
55
|
+
name: "core-message-agent",
|
|
56
|
+
instructions: {
|
|
57
|
+
role: 'system',
|
|
58
|
+
content: 'You are a helpful assistant specialized in technical documentation.',
|
|
59
|
+
providerOptions: {
|
|
60
|
+
openai: {
|
|
61
|
+
reasoningEffort: 'low'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
model: openai("gpt-5")
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Multiple CoreSystemMessages
|
|
70
|
+
|
|
71
|
+
```typescript filename="src/mastra/agents/multi-message-agent.ts" showLineNumbers copy
|
|
72
|
+
import { anthropic } from "@ai-sdk/anthropic";
|
|
73
|
+
import { Agent } from "@mastra/core/agent";
|
|
74
|
+
|
|
75
|
+
// This could be customizable based on the user
|
|
76
|
+
const preferredTone = {
|
|
77
|
+
role: 'system',
|
|
78
|
+
content: 'Always maintain a professional and empathetic tone.',
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const agent = new Agent({
|
|
82
|
+
name: "multi-message-agent",
|
|
83
|
+
instructions: [
|
|
84
|
+
{ role: 'system', content: 'You are a customer service representative.' },
|
|
85
|
+
preferredTone,
|
|
86
|
+
{
|
|
87
|
+
role: 'system',
|
|
88
|
+
content: 'Escalate complex issues to human agents when needed.',
|
|
89
|
+
providerOptions: {
|
|
90
|
+
anthropic: { cacheControl: { type: 'ephemeral' } },
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
model: anthropic('claude-sonnet-4-20250514'),
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
23
98
|
## Constructor parameters
|
|
24
99
|
|
|
25
100
|
<PropertiesTable
|
|
@@ -44,9 +119,11 @@ export const agent = new Agent({
|
|
|
44
119
|
},
|
|
45
120
|
{
|
|
46
121
|
name: "instructions",
|
|
47
|
-
type: "
|
|
122
|
+
type: "SystemMessage | ({ runtimeContext: RuntimeContext }) => SystemMessage | Promise<SystemMessage>",
|
|
48
123
|
isOptional: false,
|
|
49
|
-
description:
|
|
124
|
+
description: `Instructions that guide the agent's behavior. Can be a string, array of strings, system message object,
|
|
125
|
+
array of system messages, or a function that returns any of these types dynamically.
|
|
126
|
+
SystemMessage types: string | string[] | CoreSystemMessage | CoreSystemMessage[] | SystemModelMessage | SystemModelMessage[]`,
|
|
50
127
|
},
|
|
51
128
|
{
|
|
52
129
|
name: "model",
|
|
@@ -33,8 +33,8 @@ await agent.getInstructions();
|
|
|
33
33
|
content={[
|
|
34
34
|
{
|
|
35
35
|
name: "instructions",
|
|
36
|
-
type: "
|
|
37
|
-
description: "The instructions configured for the agent
|
|
36
|
+
type: "SystemMessage | Promise<SystemMessage>",
|
|
37
|
+
description: "The instructions configured for the agent. SystemMessage can be: string | string[] | CoreSystemMessage | CoreSystemMessage[] | SystemModelMessage | SystemModelMessage[]. Returns either directly or as a promise that resolves to the instructions.",
|
|
38
38
|
},
|
|
39
39
|
]}
|
|
40
40
|
/>
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "MastraAuthAuth0 Class"
|
|
3
|
+
description: "API reference for the MastraAuthAuth0 class, which authenticates Mastra applications using Auth0 authentication."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# MastraAuthAuth0 Class
|
|
7
|
+
|
|
8
|
+
The `MastraAuthAuth0` class provides authentication for Mastra using Auth0. It verifies incoming requests using Auth0-issued JWT tokens and integrates with the Mastra server using the `experimental_auth` option.
|
|
9
|
+
|
|
10
|
+
## Usage example
|
|
11
|
+
|
|
12
|
+
```typescript filename="src/mastra/index.ts" showLineNumbers copy
|
|
13
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
14
|
+
import { MastraAuthAuth0 } from '@mastra/auth-auth0';
|
|
15
|
+
|
|
16
|
+
export const mastra = new Mastra({
|
|
17
|
+
// ..
|
|
18
|
+
server: {
|
|
19
|
+
experimental_auth: new MastraAuthAuth0({
|
|
20
|
+
domain: process.env.AUTH0_DOMAIN,
|
|
21
|
+
audience: process.env.AUTH0_AUDIENCE
|
|
22
|
+
}),
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
> **Note:** You can omit the constructor parameters if you have the appropriately named environment variables (`AUTH0_DOMAIN` and `AUTH0_AUDIENCE`) set. In that case, simply use `new MastraAuthAuth0()` without any arguments.
|
|
28
|
+
|
|
29
|
+
## Constructor parameters
|
|
30
|
+
|
|
31
|
+
<PropertiesTable
|
|
32
|
+
content={[
|
|
33
|
+
{
|
|
34
|
+
name: "domain",
|
|
35
|
+
type: "string",
|
|
36
|
+
description: "Your Auth0 domain (e.g., your-tenant.auth0.com). This is used to verify JWT tokens issued by your Auth0 tenant.",
|
|
37
|
+
isOptional: true,
|
|
38
|
+
defaultValue: "process.env.AUTH0_DOMAIN"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "audience",
|
|
42
|
+
type: "string",
|
|
43
|
+
description: "Your Auth0 API identifier/audience. This ensures tokens are intended for your specific API.",
|
|
44
|
+
isOptional: true,
|
|
45
|
+
defaultValue: "process.env.AUTH0_AUDIENCE"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "name",
|
|
49
|
+
type: "string",
|
|
50
|
+
description: "Custom name for the auth provider instance.",
|
|
51
|
+
isOptional: true,
|
|
52
|
+
defaultValue: '"auth0"'
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "authorizeUser",
|
|
56
|
+
type: "(user: Auth0User) => Promise<boolean> | boolean",
|
|
57
|
+
description: "Custom authorization function to determine if a user should be granted access. Called after token verification. By default, allows all authenticated users with valid tokens.",
|
|
58
|
+
isOptional: true,
|
|
59
|
+
},
|
|
60
|
+
]}
|
|
61
|
+
/>
|
|
62
|
+
|
|
63
|
+
## Environment Variables
|
|
64
|
+
|
|
65
|
+
The following environment variables are automatically used when constructor options are not provided:
|
|
66
|
+
|
|
67
|
+
<PropertiesTable
|
|
68
|
+
content={[
|
|
69
|
+
{
|
|
70
|
+
name: "AUTH0_DOMAIN",
|
|
71
|
+
type: "string",
|
|
72
|
+
description: "Your Auth0 domain. Can be found in your Auth0 Dashboard under Applications > Settings.",
|
|
73
|
+
isOptional: true,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: "AUTH0_AUDIENCE",
|
|
77
|
+
type: "string",
|
|
78
|
+
description: "Your Auth0 API identifier. This is the identifier you set when creating an API in your Auth0 Dashboard.",
|
|
79
|
+
isOptional: true,
|
|
80
|
+
},
|
|
81
|
+
]}
|
|
82
|
+
/>
|
|
83
|
+
|
|
84
|
+
## Default Authorization Behavior
|
|
85
|
+
|
|
86
|
+
By default, `MastraAuthAuth0` validates Auth0 JWT tokens and allows access to all authenticated users:
|
|
87
|
+
|
|
88
|
+
1. **Token Verification**: The JWT token is verified using Auth0's public keys (JWKS)
|
|
89
|
+
2. **Signature Validation**: Ensures the token was signed by your Auth0 tenant
|
|
90
|
+
3. **Expiration Check**: Verifies the token has not expired
|
|
91
|
+
4. **Audience Validation**: Confirms the token was issued for your specific API (audience)
|
|
92
|
+
5. **Issuer Validation**: Ensures the token was issued by your Auth0 domain
|
|
93
|
+
|
|
94
|
+
If all validations pass, the user is considered authorized. To implement custom authorization logic (e.g., role-based access control), provide a custom `authorizeUser` function.
|
|
95
|
+
|
|
96
|
+
## Auth0 User Type
|
|
97
|
+
|
|
98
|
+
The `Auth0User` type used in the `authorizeUser` function corresponds to the decoded JWT token payload, which typically includes:
|
|
99
|
+
|
|
100
|
+
- `sub`: The user's unique identifier (subject)
|
|
101
|
+
- `email`: The user's email address (if included in token)
|
|
102
|
+
- `email_verified`: Whether the email is verified
|
|
103
|
+
- `name`: The user's display name (if available)
|
|
104
|
+
- `picture`: URL to the user's profile picture (if available)
|
|
105
|
+
- `iss`: Token issuer (your Auth0 domain)
|
|
106
|
+
- `aud`: Token audience (your API identifier)
|
|
107
|
+
- `iat`: Token issued at timestamp
|
|
108
|
+
- `exp`: Token expiration timestamp
|
|
109
|
+
- `scope`: Granted scopes for the token
|
|
110
|
+
- Custom claims and app metadata configured in your Auth0 tenant
|
|
111
|
+
|
|
112
|
+
The exact properties available depend on your Auth0 configuration, scopes requested, and any custom claims you've configured.
|
|
113
|
+
|
|
114
|
+
## Related
|
|
115
|
+
|
|
116
|
+
[MastraAuthAuth0 Class](/docs/auth/auth0.mdx)
|
|
117
|
+
|