@mastra/mcp-docs-server 0.13.22-alpha.2 → 0.13.22-alpha.4

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 (104) hide show
  1. package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +20 -0
  2. package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +9 -0
  3. package/.docs/organized/changelogs/%40mastra%2Fastra.md +11 -11
  4. package/.docs/organized/changelogs/%40mastra%2Fchroma.md +13 -13
  5. package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +10 -10
  6. package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +23 -23
  7. package/.docs/organized/changelogs/%40mastra%2Fcloud.md +21 -21
  8. package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +10 -10
  9. package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +10 -10
  10. package/.docs/organized/changelogs/%40mastra%2Fcore.md +56 -56
  11. package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +10 -10
  12. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +33 -33
  13. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +28 -28
  14. package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +22 -22
  15. package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +23 -23
  16. package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +61 -61
  17. package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +19 -19
  18. package/.docs/organized/changelogs/%40mastra%2Fevals.md +10 -10
  19. package/.docs/organized/changelogs/%40mastra%2Flance.md +11 -11
  20. package/.docs/organized/changelogs/%40mastra%2Flibsql.md +11 -11
  21. package/.docs/organized/changelogs/%40mastra%2Floggers.md +11 -11
  22. package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +19 -19
  23. package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +10 -10
  24. package/.docs/organized/changelogs/%40mastra%2Fmcp.md +21 -21
  25. package/.docs/organized/changelogs/%40mastra%2Fmemory.md +21 -21
  26. package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +10 -10
  27. package/.docs/organized/changelogs/%40mastra%2Fmssql.md +10 -10
  28. package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
  29. package/.docs/organized/changelogs/%40mastra%2Fpg.md +13 -13
  30. package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
  31. package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +39 -39
  32. package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +11 -11
  33. package/.docs/organized/changelogs/%40mastra%2Frag.md +16 -16
  34. package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +11 -0
  35. package/.docs/organized/changelogs/%40mastra%2Fserver.md +21 -21
  36. package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
  37. package/.docs/organized/changelogs/%40mastra%2Fupstash.md +11 -11
  38. package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
  39. package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +10 -10
  40. package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +11 -11
  41. package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +10 -10
  42. package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +10 -10
  43. package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +9 -0
  44. package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +9 -0
  45. package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +10 -10
  46. package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +10 -10
  47. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +10 -10
  48. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +10 -10
  49. package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +10 -10
  50. package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +10 -10
  51. package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +19 -19
  52. package/.docs/organized/changelogs/create-mastra.md +9 -9
  53. package/.docs/organized/changelogs/mastra.md +58 -58
  54. package/.docs/organized/code-examples/agent-network.md +2 -1
  55. package/.docs/organized/code-examples/quick-start.md +2 -1
  56. package/.docs/organized/code-examples/weather-agent.md +11 -4
  57. package/.docs/organized/code-examples/workflow-ai-recruiter.md +2 -1
  58. package/.docs/raw/agents/overview.mdx +1 -1
  59. package/.docs/raw/auth/firebase.mdx +285 -0
  60. package/.docs/raw/auth/index.mdx +3 -3
  61. package/.docs/raw/course/04-workflows/08-running-workflows-programmatically.md +2 -2
  62. package/.docs/raw/deployment/overview.mdx +7 -1
  63. package/.docs/raw/frameworks/agentic-uis/cedar-os.mdx +108 -0
  64. package/.docs/raw/getting-started/installation.mdx +0 -1
  65. package/.docs/raw/memory/overview.mdx +1 -1
  66. package/.docs/raw/reference/agents/network.mdx +258 -0
  67. package/.docs/raw/reference/auth/firebase.mdx +128 -0
  68. package/.docs/raw/reference/cli/dev.mdx +7 -1
  69. package/.docs/raw/reference/client-js/agents.mdx +3 -3
  70. package/.docs/raw/reference/client-js/memory.mdx +1 -1
  71. package/.docs/raw/reference/client-js/tools.mdx +1 -1
  72. package/.docs/raw/reference/core/getWorkflow.mdx +0 -1
  73. package/.docs/raw/reference/core/getWorkflows.mdx +0 -1
  74. package/.docs/raw/reference/core/setTelemetry.mdx +2 -2
  75. package/.docs/raw/reference/storage/dynamodb.mdx +0 -4
  76. package/.docs/raw/reference/{agents → streaming}/ChunkType.mdx +12 -12
  77. package/.docs/raw/reference/{agents → streaming}/MastraModelOutput.mdx +10 -10
  78. package/.docs/raw/reference/tools/mcp-server.mdx +1 -1
  79. package/.docs/raw/reference/workflows/workflow-methods/branch.mdx +2 -2
  80. package/.docs/raw/reference/workflows/workflow-methods/commit.mdx +1 -1
  81. package/.docs/raw/reference/workflows/workflow-methods/create-run.mdx +3 -3
  82. package/.docs/raw/reference/workflows/workflow-methods/dountil.mdx +1 -1
  83. package/.docs/raw/reference/workflows/workflow-methods/dowhile.mdx +1 -1
  84. package/.docs/raw/reference/workflows/workflow-methods/foreach.mdx +1 -1
  85. package/.docs/raw/reference/workflows/workflow-methods/map.mdx +1 -1
  86. package/.docs/raw/reference/workflows/workflow-methods/parallel.mdx +1 -1
  87. package/.docs/raw/reference/workflows/workflow-methods/sendEvent.mdx +1 -1
  88. package/.docs/raw/reference/workflows/workflow-methods/sleep.mdx +1 -1
  89. package/.docs/raw/reference/workflows/workflow-methods/sleepUntil.mdx +1 -1
  90. package/.docs/raw/reference/workflows/workflow-methods/then.mdx +1 -1
  91. package/.docs/raw/reference/workflows/workflow-methods/waitForEvent.mdx +1 -1
  92. package/.docs/raw/server-db/local-dev-playground.mdx +31 -9
  93. package/.docs/raw/server-db/mastra-client.mdx +189 -73
  94. package/.docs/raw/server-db/snapshots.mdx +155 -114
  95. package/.docs/raw/workflows/inngest-workflow.mdx +82 -0
  96. package/CHANGELOG.md +18 -0
  97. package/package.json +5 -5
  98. package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +0 -302
  99. package/.docs/organized/changelogs/%40mastra%2Fgithub.md +0 -302
  100. package/.docs/organized/changelogs/%40mastra%2Fmem0.md +0 -302
  101. package/.docs/organized/changelogs/%40mastra%2Fragie.md +0 -302
  102. package/.docs/organized/code-examples/memory-with-mem0.md +0 -108
  103. /package/.docs/raw/reference/{agents → streaming}/stream.mdx +0 -0
  104. /package/.docs/raw/reference/{agents → streaming}/streamVNext.mdx +0 -0
@@ -40,4 +40,4 @@ workflow.parallel([step1, step2]);
40
40
 
41
41
  ## Related
42
42
 
43
- - [Parallel workflow example](../../examples/workflows/parallel-steps.mdx)
43
+ - [Parallel Workflow Example](../../../examples/workflows/parallel-steps.mdx)
@@ -46,4 +46,4 @@ workflow.sendEvent('event-name', step1);
46
46
 
47
47
  ## Related
48
48
 
49
- - [Sleep & Events](../../docs/workflows/pausing-execution.mdx)
49
+ - [Sleep & Events](../../../docs/workflows/pausing-execution.mdx)
@@ -40,4 +40,4 @@ workflow.sleep(5000);
40
40
 
41
41
  ## Related
42
42
 
43
- - [Sleep & Events](../../docs/workflows/pausing-execution.mdx)
43
+ - [Sleep & Events](../../../docs/workflows/pausing-execution.mdx)
@@ -48,4 +48,4 @@ workflow.sleepUntil(async ({ inputData }) => {
48
48
 
49
49
  ## Related
50
50
 
51
- - [Sleep & Events](../../docs/workflows/pausing-execution.mdx)
51
+ - [Sleep & Events](../../../docs/workflows/pausing-execution.mdx)
@@ -41,4 +41,4 @@ workflow.then(step1).then(step2);
41
41
 
42
42
  ## Related
43
43
 
44
- - [Control flow](../../docs/workflows/control-flow.mdx)
44
+ - [Control flow](../../../docs/workflows/control-flow.mdx)
@@ -46,4 +46,4 @@ workflow.waitForEvent('event-name', step1);
46
46
 
47
47
  ## Related
48
48
 
49
- - [Sleep & Events](../../docs/workflows/pausing-execution.mdx)
49
+ - [Sleep & Events](../../../docs/workflows/pausing-execution.mdx)
@@ -126,11 +126,10 @@ The local development server includes an OpenAPI specification available at: [ht
126
126
 
127
127
  To include OpenAPI documentation in your production server, enable it in the Mastra instance:
128
128
 
129
- ```typescript {7} filename="src/mastra/index.ts" showLineNumbers copy
129
+ ```typescript {6} filename="src/mastra/index.ts" copy
130
130
  import { Mastra } from "@mastra/core/mastra";
131
131
 
132
132
  export const mastra = new Mastra({
133
- // ...
134
133
  server: {
135
134
  build: {
136
135
  openAPIDocs: true
@@ -145,11 +144,10 @@ The local development server includes an interactive Swagger UI - API explorer a
145
144
 
146
145
  To include Swagger UI in your production server, enable it in the Mastra instance:
147
146
 
148
- ```typescript {7} filename="src/mastra/index.ts" showLineNumbers copy
147
+ ```typescript {6} filename="src/mastra/index.ts" copy
149
148
  import { Mastra } from "@mastra/core/mastra";
150
149
 
151
150
  export const mastra = new Mastra({
152
- // ...
153
151
  server: {
154
152
  build: {
155
153
  swaggerUI: true
@@ -170,13 +168,12 @@ This setup lets you start developing immediately with production-like behavior,
170
168
 
171
169
  ## Configuration
172
170
 
173
- By default, the server runs on port `4111`. You can customize the host and port through the Mastra server configuration.
171
+ By default, the server runs on port `4111`. You can customize the `host` and `port` through the Mastra server configuration.
174
172
 
175
- ```typescript {6,7} filename="src/mastra/index.ts" showLineNumbers copy
173
+ ```typescript {5,6} filename="src/mastra/index.ts" copy
176
174
  import { Mastra } from "@mastra/core/mastra";
177
175
 
178
176
  export const mastra = new Mastra({
179
- // ...
180
177
  server: {
181
178
  port: 8080,
182
179
  host: "0.0.0.0",
@@ -184,15 +181,40 @@ export const mastra = new Mastra({
184
181
  });
185
182
  ```
186
183
 
184
+ ### Local HTTPS
185
+
186
+ Mastra provides a way to use a local HTTPS server for `mastra dev` (through [expo/devcert](https://github.com/expo/devcert)). When you use the `--https` flag, a private key and certificate will be created and used for your project. By default, certificates are issued for `localhost` unless you defined another `host` value.
187
+
188
+ ```bash
189
+ mastra dev --https
190
+ ```
191
+
192
+ You can provide your own key and cert file by specifying the `server.https` option in the Mastra server configuration.
193
+
194
+ ```typescript {2,6-9} filename="src/mastra/index.ts" copy
195
+ import { Mastra } from "@mastra/core/mastra";
196
+ import fs from 'node:fs'
197
+
198
+ export const mastra = new Mastra({
199
+ server: {
200
+ https: {
201
+ key: fs.readFileSync('path/to/key.pem'),
202
+ cert: fs.readFileSync('path/to/cert.pem')
203
+ }
204
+ },
205
+ });
206
+ ```
207
+
208
+ When you provide both `--https` and `server.https` the latter will take precedence.
209
+
187
210
  ## Bundler options
188
211
 
189
212
  Use `transpilePackages` to compile TypeScript packages or libraries. Use `externals` to exclude dependencies resolved at runtime, and `sourcemap` to emit readable stack traces.
190
213
 
191
- ```typescript filename="src/mastra/index.ts" showLineNumbers copy
214
+ ```typescript filename="src/mastra/index.ts" copy
192
215
  import { Mastra } from "@mastra/core/mastra";
193
216
 
194
217
  export const mastra = new Mastra({
195
- // ...
196
218
  bundler: {
197
219
  transpilePackages: ["utils"],
198
220
  externals: ["ui"],
@@ -1,24 +1,29 @@
1
1
  ---
2
- title: "MastraClient"
2
+ title: "Mastra Client SDK | Mastra Docs"
3
3
  description: "Learn how to set up and use the Mastra Client SDK"
4
4
  ---
5
5
 
6
+ import { Tabs } from "nextra/components";
7
+
6
8
  # Mastra Client SDK
7
9
 
8
10
  The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](/docs/deployment/server) from your client environment.
9
11
 
10
- ## Development Requirements
12
+ ## Prerequisites
11
13
 
12
14
  To ensure smooth local development, make sure you have:
13
15
 
14
- - Node.js 18.x or later installed
15
- - TypeScript 4.7+ (if using TypeScript)
16
- - A modern browser environment with Fetch API support
17
- - Your local Mastra server running (typically on port 4111)
16
+ - Node.js `v18` or higher
17
+ - TypeScript `v4.7` or higher (if using TypeScript)
18
+ - Your local Mastra server running (typically on port `4111`)
19
+
20
+ ## Usage
21
+
22
+ The Mastra Client SDK is designed for browser environments and uses the native `fetch` API for making HTTP requests to your Mastra server.
18
23
 
19
24
  ## Installation
20
25
 
21
- import { Tabs } from "nextra/components";
26
+ To use the Mastra Client SDK, install the required dependencies:
22
27
 
23
28
  {/*
24
29
  LLM CONTEXT:
@@ -28,7 +33,7 @@ This helps users install the client SDK with their preferred package manager.
28
33
  All commands install the same @mastra/client-js package but use different package manager syntax.
29
34
  */}
30
35
 
31
- <Tabs items={["npm", "yarn", "pnpm"]}>
36
+ <Tabs items={["npm", "yarn", "pnpm", "bun"]}>
32
37
  <Tabs.Tab>
33
38
  ```bash copy
34
39
  npm install @mastra/client-js@latest
@@ -44,115 +49,226 @@ All commands install the same @mastra/client-js package but use different packag
44
49
  pnpm add @mastra/client-js@latest
45
50
  ```
46
51
  </Tabs.Tab>
52
+ <Tabs.Tab>
53
+ ```bash copy
54
+ bun add @mastra/client-js@latest
55
+ ```
56
+ </Tabs.Tab>
47
57
  </Tabs>
48
58
 
49
- ## Initialize Mastra Client
59
+ ### Initialize the `MastraClient`
50
60
 
51
- To get started you'll need to initialize your MastraClient with necessary parameters:
61
+ Once initialized with a `baseUrl`, `MastraClient` exposes a type-safe interface for calling agents, tools, and workflows.
52
62
 
53
63
  ```typescript filename="lib/mastra-client.ts" showLineNumbers copy
54
64
  import { MastraClient } from "@mastra/client-js";
55
65
 
56
66
  export const mastraClient = new MastraClient({
57
- baseUrl: "http://localhost:4111", // Default Mastra development server port
67
+ baseUrl: process.env.MASTRA_API_URL || "http://localhost:4111"
58
68
  });
59
69
  ```
60
70
 
61
- ### Configuration Options
71
+ ## Core APIs
72
+
73
+ The Mastra Client SDK exposes all resources served by the Mastra Server
62
74
 
63
- You can customize the client with various options:
75
+ - **[Agents](/reference/client-js/agents.mdx)**: Generate responses and stream conversations.
76
+ - **[Memory](/reference/client-js/memory.mdx)**: Manage conversation threads and message history.
77
+ - **[Tools](/reference/client-js/tools.mdx)**: Executed and managed tools.
78
+ - **[Workflows](/reference/client-js/workflows.mdx)**: Trigger workflows and track their execution.
79
+ - **[Vectors](/reference/client-js/vectors.mdx)**: Use vector embeddings for semantic search.
80
+ - **[Logs](/reference/client-js/logs.mdx)**: View logs and debug system behavior.
81
+ - **[Telemetry](/reference/client-js/telemetry.mdx)**: Monitor app performance and trace activity.
82
+
83
+ ## Generating responses
84
+
85
+ Call `.generate()` with an array of message objects that include `role` and `content`:
86
+
87
+ ```typescript showLineNumbers copy
88
+ import { mastraClient } from "lib/mastra-client";
89
+
90
+ const testAgent = async () => {
91
+ try {
92
+ const agent = mastraClient.getAgent("testAgent");
93
+
94
+ const response = await agent.generate({
95
+ messages: [
96
+ {
97
+ role: "user",
98
+ content: "Hello"
99
+ }
100
+ ]
101
+ });
102
+
103
+ console.log(response.text);
104
+ } catch (error) {
105
+ return "Error occurred while generating response";
106
+ }
107
+ };
108
+ ```
109
+
110
+ > See [.generate()](../../reference/client-js/agents.mdx#generate-response) for more information.
111
+
112
+ ## Streaming responses
113
+
114
+ Use `.stream()` for real-time responses with an array of message objects that include `role` and `content`:
115
+
116
+ ```typescript showLineNumbers copy
117
+ import { mastraClient } from "lib/mastra-client";
118
+
119
+ const testAgent = async () => {
120
+ try {
121
+ const agent = mastraClient.getAgent("testAgent");
122
+
123
+ const stream = await agent.stream({
124
+ messages: [
125
+ {
126
+ role: "user",
127
+ content: "Hello"
128
+ }
129
+ ]
130
+ });
131
+
132
+ stream.processDataStream({
133
+ onTextPart: (text) => {
134
+ console.log(text);
135
+ }
136
+ });
137
+ } catch (error) {
138
+ return "Error occurred while generating response";
139
+ }
140
+ };
141
+ ```
142
+
143
+ > See [.stream()](../../reference/client-js/agents.mdx#stream-response) for more information.
144
+
145
+ ## Configuration options
146
+
147
+ `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.
64
148
 
65
149
  ```typescript filename="lib/mastra-client.ts" showLineNumbers copy
66
150
  import { MastraClient } from "@mastra/client-js";
67
151
 
68
152
  export const mastraClient = new MastraClient({
69
- // Required
70
- baseUrl: "http://localhost:4111",
71
-
72
- // Optional configurations for development
73
- retries: 3, // Number of retry attempts
74
- backoffMs: 300, // Initial retry backoff time
75
- maxBackoffMs: 5000, // Maximum retry backoff time
153
+ // ...
154
+ retries: 3,
155
+ backoffMs: 300,
156
+ maxBackoffMs: 5000,
76
157
  headers: {
77
- // Custom headers for development
78
158
  "X-Development": "true",
79
159
  },
80
160
  });
81
161
  ```
82
162
 
83
- ## AbortSignal
163
+ > See [MastraClient](../../reference/client-js/mastra-client.mdx) for more configuration options.
84
164
 
85
- The Mastra Client SDK supports request cancellation using the standard Web API `AbortSignal`. Pass an `AbortSignal` to the client constructor to enable cancellation for all requests:
165
+ ## Adding request cancelling
86
166
 
87
- ```typescript filename="lib/mastra-client.ts" showLineNumbers copy
167
+ `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.
168
+
169
+ Pass an `AbortSignal` to the client constructor to enable cancellation across all requests.
170
+
171
+ ```typescript {3,7} filename="lib/mastra-client.ts" showLineNumbers copy
88
172
  import { MastraClient } from "@mastra/client-js";
89
173
 
90
- const controller = new AbortController();
174
+ export const controller = new AbortController();
91
175
 
92
176
  export const mastraClient = new MastraClient({
93
- baseUrl: "http://localhost:4111",
94
- abortSignal: controller.signal,
177
+ baseUrl: process.env.MASTRA_API_URL || "http://localhost:4111",
178
+ abortSignal: controller.signal
95
179
  });
96
-
97
- // Cancel all requests from this client
98
- controller.abort();
99
180
  ```
100
181
 
101
- ## Example
182
+ ### Using the `AbortController`
102
183
 
103
- Once your MastraClient is initialized you can start making client calls via the type-safe
104
- interface
184
+ Calling `.abort()` will cancel any ongoing requests tied to that signal.
105
185
 
106
- ```typescript
107
- // Get a reference to your local agent
108
- const agent = client.getAgent("dev-agent-id");
186
+ ```typescript {4} showLineNumbers copy
187
+ import { mastraClient, controller } from "lib/mastra-client";
109
188
 
110
- // Generate responses
111
- const response = await agent.generate({
112
- messages: [
113
- {
114
- role: "user",
115
- content: "Hello, I'm testing the local development setup!",
116
- },
117
- ],
118
- });
189
+ const handleAbort = () => {
190
+ controller.abort();
191
+ };
119
192
  ```
120
193
 
121
- ## Available Features
194
+ ## Client tools
195
+
196
+ Define tools directly in client-side applications using the `createTool()` function. Pass them to agents via the `clientTools` parameter in `.generate()` or `.stream()` calls.
197
+
198
+ 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.
199
+
200
+ ```typescript {27} showLineNumbers copy
201
+ import { createTool } from '@mastra/client-js';
202
+ import { z } from 'zod';
203
+
204
+ const handleClientTool = async () => {
205
+ try {
206
+ const agent = mastraClient.getAgent("colorAgent");
207
+
208
+ const colorChangeTool = createTool({
209
+ id: "color-change-tool",
210
+ description: "Changes the HTML background color",
211
+ inputSchema: z.object({
212
+ color: z.string()
213
+ }),
214
+ outputSchema: z.object({
215
+ success: z.boolean()
216
+ }),
217
+ execute: async ({ context }) => {
218
+ const { color } = context
219
+
220
+ document.body.style.backgroundColor = color;
221
+ return { success: true };
222
+ }
223
+ });
224
+
225
+ const response = await agent.generate({
226
+ messages: "Change the background to blue",
227
+ clientTools: { colorChangeTool }
228
+ });
229
+
230
+ console.log(response);
231
+ } catch (error) {
232
+ console.error(error);
233
+ }
234
+ };
235
+ ```
236
+ ### Client tool's agent
237
+
238
+ This is a standard Mastra [agent](../agents/overview#creating-an-agent) configured to return hex color codes, intended to work with the browser-based client tool defined above.
122
239
 
123
- Mastra client exposes all resources served by the Mastra Server
240
+ ```typescript filename="src/mastra/agents/color-agent" showLineNumbers copy
241
+ import { openai } from "@ai-sdk/openai";
242
+ import { Agent } from "@mastra/core/agent";
243
+
244
+ export const colorAgent = new Agent({
245
+ name: "test-agent",
246
+ instructions: `You are a helpful CSS assistant.
247
+ You can change the background color of web pages.
248
+ Respond with a hex reference for the color requested by the user`,
249
+ model: openai("gpt-4o-mini")
250
+ });
251
+ ```
124
252
 
125
- - [**Agents**](/reference/client-js/agents): Create and manage AI agents, generate responses, and handle streaming interactions
126
- - [**Memory**](/reference/client-js/memory): Manage conversation threads and message history
127
- - [**Tools**](/reference/client-js/tools): Access and execute tools available to agents
128
- - [**Workflows**](/reference/client-js/workflows): Create and manage automated workflows
129
- - [**Vectors**](/reference/client-js/vectors): Handle vector operations for semantic search and similarity matching
253
+ ## Server-side environments
130
254
 
131
- ## Best Practices
255
+ 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:
132
256
 
133
- 1. **Error Handling**: Implement proper error handling for development scenarios
134
- 2. **Environment Variables**: Use environment variables for configuration
135
- 3. **Debugging**: Enable detailed logging when needed
257
+ ```typescript {8} showLineNumbers
258
+ export async function action() {
259
+ const agent = mastraClient.getAgent("testAgent");
136
260
 
137
- ```typescript
138
- // Example with error handling and logging
139
- try {
140
- const agent = client.getAgent("dev-agent-id");
141
- const response = await agent.generate({
142
- messages: [{ role: "user", content: "Test message" }],
261
+ const stream = await agent.stream({
262
+ messages: [{ role: "user", content: "Hello" }]
143
263
  });
144
- console.log("Response:", response);
145
- } catch (error) {
146
- console.error("Development error:", error);
264
+
265
+ return new Response(stream.body);
147
266
  }
148
267
  ```
149
268
 
150
- ## Debug
269
+ ## Best practices
151
270
 
152
- - Sometimes when using MastraClient on the server instead of the client e.g `/api/chat`,
153
- you might need to recreate the response to your client:
154
-
155
- ```typescript
156
- const result = agent.stream(/* get your agent stream */);
157
- return new Response(result.body);
158
- ```
271
+ 1. **Error Handling**: Implement proper [error handling](/reference/client-js/error-handling) for development scenarios.
272
+ 2. **Environment Variables**: Use environment variables for configuration.
273
+ 3. **Debugging**: Enable detailed [logging](/reference/client-js/logs) when needed.
274
+ 4. **Performance**: Monitor application performance, [telemetry](/reference/client-js/telemetry) and traces.