@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.
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +20 -0
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +13 -13
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +56 -56
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +22 -22
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +61 -61
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Flance.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +21 -21
- 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 +13 -13
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Frag.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +11 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +10 -10
- 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 +10 -10
- 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 +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +19 -19
- package/.docs/organized/changelogs/create-mastra.md +9 -9
- package/.docs/organized/changelogs/mastra.md +58 -58
- package/.docs/organized/code-examples/agent-network.md +2 -1
- package/.docs/organized/code-examples/quick-start.md +2 -1
- package/.docs/organized/code-examples/weather-agent.md +11 -4
- package/.docs/organized/code-examples/workflow-ai-recruiter.md +2 -1
- package/.docs/raw/agents/overview.mdx +1 -1
- package/.docs/raw/auth/firebase.mdx +285 -0
- package/.docs/raw/auth/index.mdx +3 -3
- package/.docs/raw/course/04-workflows/08-running-workflows-programmatically.md +2 -2
- package/.docs/raw/deployment/overview.mdx +7 -1
- package/.docs/raw/frameworks/agentic-uis/cedar-os.mdx +108 -0
- package/.docs/raw/getting-started/installation.mdx +0 -1
- package/.docs/raw/memory/overview.mdx +1 -1
- package/.docs/raw/reference/agents/network.mdx +258 -0
- package/.docs/raw/reference/auth/firebase.mdx +128 -0
- package/.docs/raw/reference/cli/dev.mdx +7 -1
- package/.docs/raw/reference/client-js/agents.mdx +3 -3
- package/.docs/raw/reference/client-js/memory.mdx +1 -1
- package/.docs/raw/reference/client-js/tools.mdx +1 -1
- package/.docs/raw/reference/core/getWorkflow.mdx +0 -1
- package/.docs/raw/reference/core/getWorkflows.mdx +0 -1
- package/.docs/raw/reference/core/setTelemetry.mdx +2 -2
- package/.docs/raw/reference/storage/dynamodb.mdx +0 -4
- package/.docs/raw/reference/{agents → streaming}/ChunkType.mdx +12 -12
- package/.docs/raw/reference/{agents → streaming}/MastraModelOutput.mdx +10 -10
- package/.docs/raw/reference/tools/mcp-server.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/branch.mdx +2 -2
- package/.docs/raw/reference/workflows/workflow-methods/commit.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/create-run.mdx +3 -3
- package/.docs/raw/reference/workflows/workflow-methods/dountil.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/dowhile.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/foreach.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/map.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/parallel.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/sendEvent.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/sleep.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/sleepUntil.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/then.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/waitForEvent.mdx +1 -1
- package/.docs/raw/server-db/local-dev-playground.mdx +31 -9
- package/.docs/raw/server-db/mastra-client.mdx +189 -73
- package/.docs/raw/server-db/snapshots.mdx +155 -114
- package/.docs/raw/workflows/inngest-workflow.mdx +82 -0
- package/CHANGELOG.md +18 -0
- package/package.json +5 -5
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +0 -302
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +0 -302
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +0 -302
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +0 -302
- package/.docs/organized/code-examples/memory-with-mem0.md +0 -108
- /package/.docs/raw/reference/{agents → streaming}/stream.mdx +0 -0
- /package/.docs/raw/reference/{agents → streaming}/streamVNext.mdx +0 -0
|
@@ -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 {
|
|
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 {
|
|
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
|
|
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"
|
|
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: "
|
|
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
|
-
##
|
|
12
|
+
## Prerequisites
|
|
11
13
|
|
|
12
14
|
To ensure smooth local development, make sure you have:
|
|
13
15
|
|
|
14
|
-
- Node.js
|
|
15
|
-
- TypeScript
|
|
16
|
-
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
+
### Initialize the `MastraClient`
|
|
50
60
|
|
|
51
|
-
|
|
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"
|
|
67
|
+
baseUrl: process.env.MASTRA_API_URL || "http://localhost:4111"
|
|
58
68
|
});
|
|
59
69
|
```
|
|
60
70
|
|
|
61
|
-
|
|
71
|
+
## Core APIs
|
|
72
|
+
|
|
73
|
+
The Mastra Client SDK exposes all resources served by the Mastra Server
|
|
62
74
|
|
|
63
|
-
|
|
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
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
163
|
+
> See [MastraClient](../../reference/client-js/mastra-client.mdx) for more configuration options.
|
|
84
164
|
|
|
85
|
-
|
|
165
|
+
## Adding request cancelling
|
|
86
166
|
|
|
87
|
-
|
|
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
|
-
|
|
182
|
+
### Using the `AbortController`
|
|
102
183
|
|
|
103
|
-
|
|
104
|
-
interface
|
|
184
|
+
Calling `.abort()` will cancel any ongoing requests tied to that signal.
|
|
105
185
|
|
|
106
|
-
```typescript
|
|
107
|
-
|
|
108
|
-
const agent = client.getAgent("dev-agent-id");
|
|
186
|
+
```typescript {4} showLineNumbers copy
|
|
187
|
+
import { mastraClient, controller } from "lib/mastra-client";
|
|
109
188
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
##
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
257
|
+
```typescript {8} showLineNumbers
|
|
258
|
+
export async function action() {
|
|
259
|
+
const agent = mastraClient.getAgent("testAgent");
|
|
136
260
|
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
console.error("Development error:", error);
|
|
264
|
+
|
|
265
|
+
return new Response(stream.body);
|
|
147
266
|
}
|
|
148
267
|
```
|
|
149
268
|
|
|
150
|
-
##
|
|
269
|
+
## Best practices
|
|
151
270
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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.
|