@mastra/client-js 0.0.0-agui-20250501191909 → 0.0.0-cli-debug-20250611094457
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/.turbo/turbo-build.log +19 -0
- package/CHANGELOG.md +465 -2
- package/dist/index.cjs +558 -109
- package/dist/index.d.cts +304 -98
- package/dist/index.d.ts +304 -98
- package/dist/index.js +554 -109
- package/package.json +9 -9
- package/src/adapters/agui.test.ts +19 -6
- package/src/adapters/agui.ts +31 -11
- package/src/client.ts +182 -24
- package/src/example.ts +29 -30
- package/src/index.test.ts +121 -1
- package/src/resources/a2a.ts +88 -0
- package/src/resources/agent.ts +48 -35
- package/src/resources/base.ts +1 -1
- package/src/resources/index.ts +4 -2
- package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +140 -132
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.ts +13 -3
- package/src/resources/network.ts +5 -11
- package/src/resources/tool.ts +9 -2
- package/src/resources/workflow.ts +230 -100
- package/src/types.ts +104 -22
- package/src/utils/index.ts +11 -0
- package/src/utils/process-client-tools.ts +31 -0
- package/src/utils/zod-to-json-schema.ts +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/client-js",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-cli-debug-20250611094457",
|
|
4
4
|
"description": "The official TypeScript library for the Mastra Client API",
|
|
5
5
|
"author": "",
|
|
6
6
|
"type": "module",
|
|
@@ -26,23 +26,23 @@
|
|
|
26
26
|
"@ai-sdk/ui-utils": "^1.1.19",
|
|
27
27
|
"json-schema": "^0.4.0",
|
|
28
28
|
"rxjs": "7.8.1",
|
|
29
|
-
"zod": "^3.
|
|
30
|
-
"zod-to-json-schema": "^3.24.
|
|
31
|
-
"@mastra/core": "0.0.0-
|
|
29
|
+
"zod": "^3.25.56",
|
|
30
|
+
"zod-to-json-schema": "^3.24.5",
|
|
31
|
+
"@mastra/core": "0.0.0-cli-debug-20250611094457"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"zod": "^3.
|
|
34
|
+
"zod": "^3.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@babel/preset-env": "^7.26.9",
|
|
38
38
|
"@babel/preset-typescript": "^7.27.0",
|
|
39
39
|
"@tsconfig/recommended": "^1.0.8",
|
|
40
40
|
"@types/json-schema": "^7.0.15",
|
|
41
|
-
"@types/node": "^20.17.
|
|
42
|
-
"tsup": "^8.
|
|
41
|
+
"@types/node": "^20.17.57",
|
|
42
|
+
"tsup": "^8.5.0",
|
|
43
43
|
"typescript": "^5.8.2",
|
|
44
|
-
"vitest": "^3.
|
|
45
|
-
"@internal/lint": "0.0.
|
|
44
|
+
"vitest": "^3.2.2",
|
|
45
|
+
"@internal/lint": "0.0.0-cli-debug-20250611094457"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting",
|
|
@@ -91,6 +91,17 @@ describe('convertMessagesToMastraMessages', () => {
|
|
|
91
91
|
},
|
|
92
92
|
],
|
|
93
93
|
},
|
|
94
|
+
{
|
|
95
|
+
role: 'tool',
|
|
96
|
+
content: [
|
|
97
|
+
{
|
|
98
|
+
type: 'tool-result',
|
|
99
|
+
toolCallId: 'tool-call-1',
|
|
100
|
+
toolName: 'getWeather',
|
|
101
|
+
result: { location: 'San Francisco' },
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
94
105
|
]);
|
|
95
106
|
});
|
|
96
107
|
|
|
@@ -143,12 +154,6 @@ describe('convertMessagesToMastraMessages', () => {
|
|
|
143
154
|
},
|
|
144
155
|
],
|
|
145
156
|
},
|
|
146
|
-
{
|
|
147
|
-
id: '3',
|
|
148
|
-
role: 'tool',
|
|
149
|
-
toolCallId: 'tool-call-1',
|
|
150
|
-
content: '{"temperature":72,"unit":"F"}',
|
|
151
|
-
},
|
|
152
157
|
{
|
|
153
158
|
id: '4',
|
|
154
159
|
role: 'assistant',
|
|
@@ -162,6 +167,14 @@ describe('convertMessagesToMastraMessages', () => {
|
|
|
162
167
|
expect(result[0].role).toBe('user');
|
|
163
168
|
expect(result[1].role).toBe('assistant');
|
|
164
169
|
expect(result[2].role).toBe('tool');
|
|
170
|
+
expect(result[2].content).toEqual([
|
|
171
|
+
{
|
|
172
|
+
type: 'tool-result',
|
|
173
|
+
toolCallId: 'tool-call-1',
|
|
174
|
+
toolName: 'getWeather',
|
|
175
|
+
result: { location: 'San Francisco' },
|
|
176
|
+
},
|
|
177
|
+
]);
|
|
165
178
|
expect(result[3].role).toBe('assistant');
|
|
166
179
|
});
|
|
167
180
|
});
|
package/src/adapters/agui.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
// Cross-platform UUID generation function
|
|
2
|
-
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
3
2
|
import type {
|
|
3
|
+
AgentConfig,
|
|
4
4
|
BaseEvent,
|
|
5
|
+
Message,
|
|
5
6
|
RunAgentInput,
|
|
6
|
-
AgentConfig,
|
|
7
|
-
RunStartedEvent,
|
|
8
7
|
RunFinishedEvent,
|
|
9
|
-
|
|
8
|
+
RunStartedEvent,
|
|
10
9
|
TextMessageContentEvent,
|
|
11
10
|
TextMessageEndEvent,
|
|
12
|
-
|
|
13
|
-
ToolCallStartEvent,
|
|
11
|
+
TextMessageStartEvent,
|
|
14
12
|
ToolCallArgsEvent,
|
|
15
13
|
ToolCallEndEvent,
|
|
14
|
+
ToolCallStartEvent,
|
|
16
15
|
} from '@ag-ui/client';
|
|
16
|
+
import { AbstractAgent, EventType } from '@ag-ui/client';
|
|
17
17
|
import type { CoreMessage } from '@mastra/core';
|
|
18
18
|
import { Observable } from 'rxjs';
|
|
19
19
|
import type { Agent } from '../resources/agent';
|
|
@@ -39,7 +39,6 @@ export class AGUIAdapter extends AbstractAgent {
|
|
|
39
39
|
protected run(input: RunAgentInput): Observable<BaseEvent> {
|
|
40
40
|
return new Observable<BaseEvent>(subscriber => {
|
|
41
41
|
const convertedMessages = convertMessagesToMastraMessages(input.messages);
|
|
42
|
-
|
|
43
42
|
subscriber.next({
|
|
44
43
|
type: EventType.RUN_STARTED,
|
|
45
44
|
threadId: input.threadId,
|
|
@@ -66,17 +65,18 @@ export class AGUIAdapter extends AbstractAgent {
|
|
|
66
65
|
})
|
|
67
66
|
.then(response => {
|
|
68
67
|
let currentMessageId: string | undefined = undefined;
|
|
68
|
+
let isInTextMessage = false;
|
|
69
69
|
return response.processDataStream({
|
|
70
70
|
onTextPart: text => {
|
|
71
71
|
if (currentMessageId === undefined) {
|
|
72
72
|
currentMessageId = generateUUID();
|
|
73
|
-
|
|
74
73
|
const message: TextMessageStartEvent = {
|
|
75
74
|
type: EventType.TEXT_MESSAGE_START,
|
|
76
75
|
messageId: currentMessageId,
|
|
77
76
|
role: 'assistant',
|
|
78
77
|
};
|
|
79
78
|
subscriber.next(message);
|
|
79
|
+
isInTextMessage = true;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
const message: TextMessageContentEvent = {
|
|
@@ -86,14 +86,14 @@ export class AGUIAdapter extends AbstractAgent {
|
|
|
86
86
|
};
|
|
87
87
|
subscriber.next(message);
|
|
88
88
|
},
|
|
89
|
-
onFinishMessagePart:
|
|
90
|
-
console.log('onFinishMessagePart', message);
|
|
89
|
+
onFinishMessagePart: () => {
|
|
91
90
|
if (currentMessageId !== undefined) {
|
|
92
91
|
const message: TextMessageEndEvent = {
|
|
93
92
|
type: EventType.TEXT_MESSAGE_END,
|
|
94
93
|
messageId: currentMessageId,
|
|
95
94
|
};
|
|
96
95
|
subscriber.next(message);
|
|
96
|
+
isInTextMessage = false;
|
|
97
97
|
}
|
|
98
98
|
// Emit run finished event
|
|
99
99
|
subscriber.next({
|
|
@@ -107,6 +107,15 @@ export class AGUIAdapter extends AbstractAgent {
|
|
|
107
107
|
},
|
|
108
108
|
onToolCallPart(streamPart) {
|
|
109
109
|
const parentMessageId = currentMessageId || generateUUID();
|
|
110
|
+
if (isInTextMessage) {
|
|
111
|
+
const message: TextMessageEndEvent = {
|
|
112
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
113
|
+
messageId: parentMessageId,
|
|
114
|
+
};
|
|
115
|
+
subscriber.next(message);
|
|
116
|
+
isInTextMessage = false;
|
|
117
|
+
}
|
|
118
|
+
|
|
110
119
|
subscriber.next({
|
|
111
120
|
type: EventType.TOOL_CALL_START,
|
|
112
121
|
toolCallId: streamPart.toolCallId,
|
|
@@ -130,7 +139,7 @@ export class AGUIAdapter extends AbstractAgent {
|
|
|
130
139
|
});
|
|
131
140
|
})
|
|
132
141
|
.catch(error => {
|
|
133
|
-
console.
|
|
142
|
+
console.error('error', error);
|
|
134
143
|
// Handle error
|
|
135
144
|
subscriber.error(error);
|
|
136
145
|
});
|
|
@@ -195,6 +204,17 @@ export function convertMessagesToMastraMessages(messages: Message[]): CoreMessag
|
|
|
195
204
|
role: 'assistant',
|
|
196
205
|
content: parts,
|
|
197
206
|
});
|
|
207
|
+
if (message.toolCalls?.length) {
|
|
208
|
+
result.push({
|
|
209
|
+
role: 'tool',
|
|
210
|
+
content: message.toolCalls.map(toolCall => ({
|
|
211
|
+
type: 'tool-result',
|
|
212
|
+
toolCallId: toolCall.id,
|
|
213
|
+
toolName: toolCall.function.name,
|
|
214
|
+
result: JSON.parse(toolCall.function.arguments),
|
|
215
|
+
})),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
198
218
|
} else if (message.role === 'user') {
|
|
199
219
|
result.push({
|
|
200
220
|
role: 'user',
|
package/src/client.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
+
import type { AbstractAgent } from '@ag-ui/client';
|
|
2
|
+
import type { ServerDetailInfo } from '@mastra/core/mcp';
|
|
1
3
|
import { AGUIAdapter } from './adapters/agui';
|
|
2
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
Agent,
|
|
6
|
+
MemoryThread,
|
|
7
|
+
Tool,
|
|
8
|
+
Workflow,
|
|
9
|
+
Vector,
|
|
10
|
+
BaseResource,
|
|
11
|
+
Network,
|
|
12
|
+
A2A,
|
|
13
|
+
MCPTool,
|
|
14
|
+
LegacyWorkflow,
|
|
15
|
+
} from './resources';
|
|
3
16
|
import type {
|
|
4
17
|
ClientOptions,
|
|
5
18
|
CreateMemoryThreadParams,
|
|
@@ -14,10 +27,12 @@ import type {
|
|
|
14
27
|
GetTelemetryParams,
|
|
15
28
|
GetTelemetryResponse,
|
|
16
29
|
GetToolResponse,
|
|
17
|
-
GetVNextWorkflowResponse,
|
|
18
30
|
GetWorkflowResponse,
|
|
19
31
|
SaveMessageToMemoryParams,
|
|
20
32
|
SaveMessageToMemoryResponse,
|
|
33
|
+
McpServerListResponse,
|
|
34
|
+
McpServerToolListResponse,
|
|
35
|
+
GetLegacyWorkflowResponse,
|
|
21
36
|
} from './types';
|
|
22
37
|
|
|
23
38
|
export class MastraClient extends BaseResource {
|
|
@@ -33,7 +48,7 @@ export class MastraClient extends BaseResource {
|
|
|
33
48
|
return this.request('/api/agents');
|
|
34
49
|
}
|
|
35
50
|
|
|
36
|
-
public async getAGUI({ resourceId }: { resourceId: string }): Promise<Record<string,
|
|
51
|
+
public async getAGUI({ resourceId }: { resourceId: string }): Promise<Record<string, AbstractAgent>> {
|
|
37
52
|
const agents = await this.getAgents();
|
|
38
53
|
|
|
39
54
|
return Object.entries(agents).reduce(
|
|
@@ -48,7 +63,7 @@ export class MastraClient extends BaseResource {
|
|
|
48
63
|
|
|
49
64
|
return acc;
|
|
50
65
|
},
|
|
51
|
-
{} as Record<string,
|
|
66
|
+
{} as Record<string, AbstractAgent>,
|
|
52
67
|
);
|
|
53
68
|
}
|
|
54
69
|
|
|
@@ -125,6 +140,23 @@ export class MastraClient extends BaseResource {
|
|
|
125
140
|
return new Tool(this.options, toolId);
|
|
126
141
|
}
|
|
127
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Retrieves all available legacy workflows
|
|
145
|
+
* @returns Promise containing map of legacy workflow IDs to legacy workflow details
|
|
146
|
+
*/
|
|
147
|
+
public getLegacyWorkflows(): Promise<Record<string, GetLegacyWorkflowResponse>> {
|
|
148
|
+
return this.request('/api/workflows/legacy');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Gets a legacy workflow instance by ID
|
|
153
|
+
* @param workflowId - ID of the legacy workflow to retrieve
|
|
154
|
+
* @returns Legacy Workflow instance
|
|
155
|
+
*/
|
|
156
|
+
public getLegacyWorkflow(workflowId: string) {
|
|
157
|
+
return new LegacyWorkflow(this.options, workflowId);
|
|
158
|
+
}
|
|
159
|
+
|
|
128
160
|
/**
|
|
129
161
|
* Retrieves all available workflows
|
|
130
162
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
@@ -142,23 +174,6 @@ export class MastraClient extends BaseResource {
|
|
|
142
174
|
return new Workflow(this.options, workflowId);
|
|
143
175
|
}
|
|
144
176
|
|
|
145
|
-
/**
|
|
146
|
-
* Retrieves all available vNext workflows
|
|
147
|
-
* @returns Promise containing map of vNext workflow IDs to vNext workflow details
|
|
148
|
-
*/
|
|
149
|
-
public getVNextWorkflows(): Promise<Record<string, GetVNextWorkflowResponse>> {
|
|
150
|
-
return this.request('/api/workflows/v-next');
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Gets a vNext workflow instance by ID
|
|
155
|
-
* @param workflowId - ID of the vNext workflow to retrieve
|
|
156
|
-
* @returns vNext Workflow instance
|
|
157
|
-
*/
|
|
158
|
-
public getVNextWorkflow(workflowId: string) {
|
|
159
|
-
return new VNextWorkflow(this.options, workflowId);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
177
|
/**
|
|
163
178
|
* Gets a vector instance by name
|
|
164
179
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -174,7 +189,43 @@ export class MastraClient extends BaseResource {
|
|
|
174
189
|
* @returns Promise containing array of log messages
|
|
175
190
|
*/
|
|
176
191
|
public getLogs(params: GetLogsParams): Promise<GetLogsResponse> {
|
|
177
|
-
|
|
192
|
+
const { transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
193
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
194
|
+
|
|
195
|
+
const searchParams = new URLSearchParams();
|
|
196
|
+
if (transportId) {
|
|
197
|
+
searchParams.set('transportId', transportId);
|
|
198
|
+
}
|
|
199
|
+
if (fromDate) {
|
|
200
|
+
searchParams.set('fromDate', fromDate.toISOString());
|
|
201
|
+
}
|
|
202
|
+
if (toDate) {
|
|
203
|
+
searchParams.set('toDate', toDate.toISOString());
|
|
204
|
+
}
|
|
205
|
+
if (logLevel) {
|
|
206
|
+
searchParams.set('logLevel', logLevel);
|
|
207
|
+
}
|
|
208
|
+
if (page) {
|
|
209
|
+
searchParams.set('page', String(page));
|
|
210
|
+
}
|
|
211
|
+
if (perPage) {
|
|
212
|
+
searchParams.set('perPage', String(perPage));
|
|
213
|
+
}
|
|
214
|
+
if (_filters) {
|
|
215
|
+
if (Array.isArray(_filters)) {
|
|
216
|
+
for (const filter of _filters) {
|
|
217
|
+
searchParams.append('filters', filter);
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
searchParams.set('filters', _filters);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (searchParams.size) {
|
|
225
|
+
return this.request(`/api/logs?${searchParams}`);
|
|
226
|
+
} else {
|
|
227
|
+
return this.request(`/api/logs`);
|
|
228
|
+
}
|
|
178
229
|
}
|
|
179
230
|
|
|
180
231
|
/**
|
|
@@ -183,7 +234,47 @@ export class MastraClient extends BaseResource {
|
|
|
183
234
|
* @returns Promise containing array of log messages
|
|
184
235
|
*/
|
|
185
236
|
public getLogForRun(params: GetLogParams): Promise<GetLogsResponse> {
|
|
186
|
-
|
|
237
|
+
const { runId, transportId, fromDate, toDate, logLevel, filters, page, perPage } = params;
|
|
238
|
+
|
|
239
|
+
const _filters = filters ? Object.entries(filters).map(([key, value]) => `${key}:${value}`) : [];
|
|
240
|
+
const searchParams = new URLSearchParams();
|
|
241
|
+
if (runId) {
|
|
242
|
+
searchParams.set('runId', runId);
|
|
243
|
+
}
|
|
244
|
+
if (transportId) {
|
|
245
|
+
searchParams.set('transportId', transportId);
|
|
246
|
+
}
|
|
247
|
+
if (fromDate) {
|
|
248
|
+
searchParams.set('fromDate', fromDate.toISOString());
|
|
249
|
+
}
|
|
250
|
+
if (toDate) {
|
|
251
|
+
searchParams.set('toDate', toDate.toISOString());
|
|
252
|
+
}
|
|
253
|
+
if (logLevel) {
|
|
254
|
+
searchParams.set('logLevel', logLevel);
|
|
255
|
+
}
|
|
256
|
+
if (page) {
|
|
257
|
+
searchParams.set('page', String(page));
|
|
258
|
+
}
|
|
259
|
+
if (perPage) {
|
|
260
|
+
searchParams.set('perPage', String(perPage));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (_filters) {
|
|
264
|
+
if (Array.isArray(_filters)) {
|
|
265
|
+
for (const filter of _filters) {
|
|
266
|
+
searchParams.append('filters', filter);
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
searchParams.set('filters', _filters);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (searchParams.size) {
|
|
274
|
+
return this.request(`/api/logs/${runId}?${searchParams}`);
|
|
275
|
+
} else {
|
|
276
|
+
return this.request(`/api/logs/${runId}`);
|
|
277
|
+
}
|
|
187
278
|
}
|
|
188
279
|
|
|
189
280
|
/**
|
|
@@ -200,7 +291,7 @@ export class MastraClient extends BaseResource {
|
|
|
200
291
|
* @returns Promise containing telemetry data
|
|
201
292
|
*/
|
|
202
293
|
public getTelemetry(params?: GetTelemetryParams): Promise<GetTelemetryResponse> {
|
|
203
|
-
const { name, scope, page, perPage, attribute } = params || {};
|
|
294
|
+
const { name, scope, page, perPage, attribute, fromDate, toDate } = params || {};
|
|
204
295
|
const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
|
|
205
296
|
|
|
206
297
|
const searchParams = new URLSearchParams();
|
|
@@ -225,6 +316,12 @@ export class MastraClient extends BaseResource {
|
|
|
225
316
|
searchParams.set('attribute', _attribute);
|
|
226
317
|
}
|
|
227
318
|
}
|
|
319
|
+
if (fromDate) {
|
|
320
|
+
searchParams.set('fromDate', fromDate.toISOString());
|
|
321
|
+
}
|
|
322
|
+
if (toDate) {
|
|
323
|
+
searchParams.set('toDate', toDate.toISOString());
|
|
324
|
+
}
|
|
228
325
|
|
|
229
326
|
if (searchParams.size) {
|
|
230
327
|
return this.request(`/api/telemetry?${searchParams}`);
|
|
@@ -249,4 +346,65 @@ export class MastraClient extends BaseResource {
|
|
|
249
346
|
public getNetwork(networkId: string) {
|
|
250
347
|
return new Network(this.options, networkId);
|
|
251
348
|
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Retrieves a list of available MCP servers.
|
|
352
|
+
* @param params - Optional parameters for pagination (limit, offset).
|
|
353
|
+
* @returns Promise containing the list of MCP servers and pagination info.
|
|
354
|
+
*/
|
|
355
|
+
public getMcpServers(params?: { limit?: number; offset?: number }): Promise<McpServerListResponse> {
|
|
356
|
+
const searchParams = new URLSearchParams();
|
|
357
|
+
if (params?.limit !== undefined) {
|
|
358
|
+
searchParams.set('limit', String(params.limit));
|
|
359
|
+
}
|
|
360
|
+
if (params?.offset !== undefined) {
|
|
361
|
+
searchParams.set('offset', String(params.offset));
|
|
362
|
+
}
|
|
363
|
+
const queryString = searchParams.toString();
|
|
364
|
+
return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ''}`);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Retrieves detailed information for a specific MCP server.
|
|
369
|
+
* @param serverId - The ID of the MCP server to retrieve.
|
|
370
|
+
* @param params - Optional parameters, e.g., specific version.
|
|
371
|
+
* @returns Promise containing the detailed MCP server information.
|
|
372
|
+
*/
|
|
373
|
+
public getMcpServerDetails(serverId: string, params?: { version?: string }): Promise<ServerDetailInfo> {
|
|
374
|
+
const searchParams = new URLSearchParams();
|
|
375
|
+
if (params?.version) {
|
|
376
|
+
searchParams.set('version', params.version);
|
|
377
|
+
}
|
|
378
|
+
const queryString = searchParams.toString();
|
|
379
|
+
return this.request(`/api/mcp/v0/servers/${serverId}${queryString ? `?${queryString}` : ''}`);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Retrieves a list of tools for a specific MCP server.
|
|
384
|
+
* @param serverId - The ID of the MCP server.
|
|
385
|
+
* @returns Promise containing the list of tools.
|
|
386
|
+
*/
|
|
387
|
+
public getMcpServerTools(serverId: string): Promise<McpServerToolListResponse> {
|
|
388
|
+
return this.request(`/api/mcp/${serverId}/tools`);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Gets an MCPTool resource instance for a specific tool on an MCP server.
|
|
393
|
+
* This instance can then be used to fetch details or execute the tool.
|
|
394
|
+
* @param serverId - The ID of the MCP server.
|
|
395
|
+
* @param toolId - The ID of the tool.
|
|
396
|
+
* @returns MCPTool instance.
|
|
397
|
+
*/
|
|
398
|
+
public getMcpServerTool(serverId: string, toolId: string): MCPTool {
|
|
399
|
+
return new MCPTool(this.options, serverId, toolId);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
404
|
+
* @param agentId - ID of the agent to interact with
|
|
405
|
+
* @returns A2A client instance
|
|
406
|
+
*/
|
|
407
|
+
public getA2A(agentId: string) {
|
|
408
|
+
return new A2A(this.options, agentId);
|
|
409
|
+
}
|
|
252
410
|
}
|
package/src/example.ts
CHANGED
|
@@ -1,40 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
import { MastraClient } from './client';
|
|
2
2
|
// import type { WorkflowRunResult } from './types';
|
|
3
3
|
|
|
4
4
|
// Agent
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
(async () => {
|
|
7
|
+
const client = new MastraClient({
|
|
8
|
+
baseUrl: 'http://localhost:4111',
|
|
9
|
+
});
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
console.log('Starting agent...');
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
try {
|
|
14
|
+
const agent = client.getAgent('weatherAgent');
|
|
15
|
+
const response = await agent.stream({
|
|
16
|
+
messages: 'what is the weather in new york?',
|
|
17
|
+
});
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// })();
|
|
19
|
+
response.processDataStream({
|
|
20
|
+
onTextPart: text => {
|
|
21
|
+
process.stdout.write(text);
|
|
22
|
+
},
|
|
23
|
+
onFilePart: file => {
|
|
24
|
+
console.log(file);
|
|
25
|
+
},
|
|
26
|
+
onDataPart: data => {
|
|
27
|
+
console.log(data);
|
|
28
|
+
},
|
|
29
|
+
onErrorPart: error => {
|
|
30
|
+
console.error(error);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error(error);
|
|
35
|
+
}
|
|
36
|
+
})();
|
|
38
37
|
|
|
39
38
|
// Workflow
|
|
40
39
|
// (async () => {
|
package/src/index.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
2
|
-
|
|
3
2
|
import { MastraClient } from './client';
|
|
3
|
+
import type { McpServerListResponse, ServerDetailInfo } from './types';
|
|
4
4
|
|
|
5
5
|
// Mock fetch globally
|
|
6
6
|
global.fetch = vi.fn();
|
|
@@ -236,6 +236,7 @@ describe('MastraClient Resources', () => {
|
|
|
236
236
|
model: 'gpt-4',
|
|
237
237
|
instructions: 'Test instructions',
|
|
238
238
|
tools: {},
|
|
239
|
+
workflows: {},
|
|
239
240
|
};
|
|
240
241
|
mockFetchResponse(mockResponse);
|
|
241
242
|
|
|
@@ -552,6 +553,35 @@ describe('MastraClient Resources', () => {
|
|
|
552
553
|
}),
|
|
553
554
|
);
|
|
554
555
|
});
|
|
556
|
+
|
|
557
|
+
it('should get thread messages with limit', async () => {
|
|
558
|
+
const mockResponse = {
|
|
559
|
+
messages: [
|
|
560
|
+
{
|
|
561
|
+
id: '1',
|
|
562
|
+
content: 'test',
|
|
563
|
+
threadId,
|
|
564
|
+
role: 'user',
|
|
565
|
+
type: 'text',
|
|
566
|
+
resourceId: 'test-resource',
|
|
567
|
+
createdAt: new Date(),
|
|
568
|
+
},
|
|
569
|
+
],
|
|
570
|
+
uiMessages: [],
|
|
571
|
+
};
|
|
572
|
+
mockFetchResponse(mockResponse);
|
|
573
|
+
|
|
574
|
+
const limit = 5;
|
|
575
|
+
const result = await memoryThread.getMessages({ limit });
|
|
576
|
+
|
|
577
|
+
expect(result).toEqual(mockResponse);
|
|
578
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
579
|
+
`${clientOptions.baseUrl}/api/memory/threads/${threadId}/messages?agentId=${agentId}&limit=${limit}`,
|
|
580
|
+
expect.objectContaining({
|
|
581
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
582
|
+
}),
|
|
583
|
+
);
|
|
584
|
+
});
|
|
555
585
|
});
|
|
556
586
|
|
|
557
587
|
describe('Tool Resource', () => {
|
|
@@ -707,4 +737,94 @@ describe('MastraClient Resources', () => {
|
|
|
707
737
|
);
|
|
708
738
|
});
|
|
709
739
|
});
|
|
740
|
+
|
|
741
|
+
describe('MCP Server Registry Client Methods', () => {
|
|
742
|
+
const mockServerInfo1 = {
|
|
743
|
+
id: 'mcp-server-1',
|
|
744
|
+
name: 'Test MCP Server 1',
|
|
745
|
+
version_detail: { version: '1.0.0', release_date: '2023-01-01T00:00:00Z', is_latest: true },
|
|
746
|
+
};
|
|
747
|
+
const mockServerInfo2 = {
|
|
748
|
+
id: 'mcp-server-2',
|
|
749
|
+
name: 'Test MCP Server 2',
|
|
750
|
+
version_detail: { version: '1.1.0', release_date: '2023-02-01T00:00:00Z', is_latest: true },
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
const mockServerDetail1: ServerDetailInfo = {
|
|
754
|
+
...mockServerInfo1,
|
|
755
|
+
description: 'Detailed description for server 1',
|
|
756
|
+
package_canonical: 'npm',
|
|
757
|
+
packages: [{ registry_name: 'npm', name: '@example/server1', version: '1.0.0' }],
|
|
758
|
+
remotes: [{ transport_type: 'sse', url: 'http://localhost/sse1' }],
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
describe('getMcpServers()', () => {
|
|
762
|
+
it('should fetch a list of MCP servers', async () => {
|
|
763
|
+
const mockResponse: McpServerListResponse = {
|
|
764
|
+
servers: [mockServerInfo1, mockServerInfo2],
|
|
765
|
+
total_count: 2,
|
|
766
|
+
next: null,
|
|
767
|
+
};
|
|
768
|
+
mockFetchResponse(mockResponse);
|
|
769
|
+
|
|
770
|
+
const result = await client.getMcpServers();
|
|
771
|
+
expect(result).toEqual(mockResponse);
|
|
772
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
773
|
+
`${clientOptions.baseUrl}/api/mcp/v0/servers`,
|
|
774
|
+
expect.objectContaining({
|
|
775
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
776
|
+
}),
|
|
777
|
+
);
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
it('should fetch MCP servers with limit and offset parameters', async () => {
|
|
781
|
+
const mockResponse: McpServerListResponse = {
|
|
782
|
+
servers: [mockServerInfo1],
|
|
783
|
+
total_count: 2,
|
|
784
|
+
next: '/api/mcp/v0/servers?limit=1&offset=1',
|
|
785
|
+
};
|
|
786
|
+
mockFetchResponse(mockResponse);
|
|
787
|
+
|
|
788
|
+
const result = await client.getMcpServers({ limit: 1, offset: 0 });
|
|
789
|
+
expect(result).toEqual(mockResponse);
|
|
790
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
791
|
+
`${clientOptions.baseUrl}/api/mcp/v0/servers?limit=1&offset=0`,
|
|
792
|
+
expect.objectContaining({
|
|
793
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
794
|
+
}),
|
|
795
|
+
);
|
|
796
|
+
});
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
describe('getMcpServerDetails()', () => {
|
|
800
|
+
const serverId = 'mcp-server-1';
|
|
801
|
+
|
|
802
|
+
it('should fetch details for a specific MCP server', async () => {
|
|
803
|
+
mockFetchResponse(mockServerDetail1);
|
|
804
|
+
|
|
805
|
+
const result = await client.getMcpServerDetails(serverId);
|
|
806
|
+
expect(result).toEqual(mockServerDetail1);
|
|
807
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
808
|
+
`${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}`,
|
|
809
|
+
expect.objectContaining({
|
|
810
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
811
|
+
}),
|
|
812
|
+
);
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
it('should fetch MCP server details with a version parameter', async () => {
|
|
816
|
+
mockFetchResponse(mockServerDetail1);
|
|
817
|
+
const version = '1.0.0';
|
|
818
|
+
|
|
819
|
+
const result = await client.getMcpServerDetails(serverId, { version });
|
|
820
|
+
expect(result).toEqual(mockServerDetail1);
|
|
821
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
822
|
+
`${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}?version=${version}`,
|
|
823
|
+
expect.objectContaining({
|
|
824
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
825
|
+
}),
|
|
826
|
+
);
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
});
|
|
710
830
|
});
|