@mastra/client-js 0.0.0-vnextWorkflows-20250422142014 → 0.0.0-workflow-deno-20250616130925
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 +662 -2
- package/dist/index.cjs +889 -55
- package/dist/index.d.cts +408 -41
- package/dist/index.d.ts +408 -41
- package/dist/index.js +885 -55
- package/package.json +23 -13
- package/src/adapters/agui.test.ts +180 -0
- package/src/adapters/agui.ts +239 -0
- package/src/client.ts +200 -13
- package/src/example.ts +30 -31
- package/src/index.test.ts +125 -5
- package/src/resources/a2a.ts +88 -0
- package/src/resources/agent.ts +49 -37
- package/src/resources/base.ts +2 -2
- package/src/resources/index.ts +4 -1
- package/src/resources/legacy-workflow.ts +242 -0
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.ts +8 -5
- package/src/resources/network.ts +6 -12
- package/src/resources/tool.ts +16 -3
- package/src/resources/workflow.ts +254 -96
- package/src/types.ts +134 -19
- 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/src/client.ts
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { AbstractAgent } from '@ag-ui/client';
|
|
2
|
+
import type { ServerDetailInfo } from '@mastra/core/mcp';
|
|
3
|
+
import { AGUIAdapter } from './adapters/agui';
|
|
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';
|
|
2
16
|
import type {
|
|
3
17
|
ClientOptions,
|
|
4
18
|
CreateMemoryThreadParams,
|
|
@@ -14,9 +28,11 @@ import type {
|
|
|
14
28
|
GetTelemetryResponse,
|
|
15
29
|
GetToolResponse,
|
|
16
30
|
GetWorkflowResponse,
|
|
17
|
-
RequestOptions,
|
|
18
31
|
SaveMessageToMemoryParams,
|
|
19
32
|
SaveMessageToMemoryResponse,
|
|
33
|
+
McpServerListResponse,
|
|
34
|
+
McpServerToolListResponse,
|
|
35
|
+
GetLegacyWorkflowResponse,
|
|
20
36
|
} from './types';
|
|
21
37
|
|
|
22
38
|
export class MastraClient extends BaseResource {
|
|
@@ -32,6 +48,25 @@ export class MastraClient extends BaseResource {
|
|
|
32
48
|
return this.request('/api/agents');
|
|
33
49
|
}
|
|
34
50
|
|
|
51
|
+
public async getAGUI({ resourceId }: { resourceId: string }): Promise<Record<string, AbstractAgent>> {
|
|
52
|
+
const agents = await this.getAgents();
|
|
53
|
+
|
|
54
|
+
return Object.entries(agents).reduce(
|
|
55
|
+
(acc, [agentId]) => {
|
|
56
|
+
const agent = this.getAgent(agentId);
|
|
57
|
+
|
|
58
|
+
acc[agentId] = new AGUIAdapter({
|
|
59
|
+
agentId,
|
|
60
|
+
agent,
|
|
61
|
+
resourceId,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return acc;
|
|
65
|
+
},
|
|
66
|
+
{} as Record<string, AbstractAgent>,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
35
70
|
/**
|
|
36
71
|
* Gets an agent instance by ID
|
|
37
72
|
* @param agentId - ID of the agent to retrieve
|
|
@@ -105,6 +140,23 @@ export class MastraClient extends BaseResource {
|
|
|
105
140
|
return new Tool(this.options, toolId);
|
|
106
141
|
}
|
|
107
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
|
+
|
|
108
160
|
/**
|
|
109
161
|
* Retrieves all available workflows
|
|
110
162
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
@@ -137,7 +189,43 @@ export class MastraClient extends BaseResource {
|
|
|
137
189
|
* @returns Promise containing array of log messages
|
|
138
190
|
*/
|
|
139
191
|
public getLogs(params: GetLogsParams): Promise<GetLogsResponse> {
|
|
140
|
-
|
|
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
|
+
}
|
|
141
229
|
}
|
|
142
230
|
|
|
143
231
|
/**
|
|
@@ -146,7 +234,47 @@ export class MastraClient extends BaseResource {
|
|
|
146
234
|
* @returns Promise containing array of log messages
|
|
147
235
|
*/
|
|
148
236
|
public getLogForRun(params: GetLogParams): Promise<GetLogsResponse> {
|
|
149
|
-
|
|
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
|
+
}
|
|
150
278
|
}
|
|
151
279
|
|
|
152
280
|
/**
|
|
@@ -163,17 +291,9 @@ export class MastraClient extends BaseResource {
|
|
|
163
291
|
* @returns Promise containing telemetry data
|
|
164
292
|
*/
|
|
165
293
|
public getTelemetry(params?: GetTelemetryParams): Promise<GetTelemetryResponse> {
|
|
166
|
-
const { name, scope, page, perPage, attribute } = params || {};
|
|
294
|
+
const { name, scope, page, perPage, attribute, fromDate, toDate } = params || {};
|
|
167
295
|
const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
|
|
168
296
|
|
|
169
|
-
const queryObj = {
|
|
170
|
-
...(name ? { name } : {}),
|
|
171
|
-
...(scope ? { scope } : {}),
|
|
172
|
-
...(page ? { page: String(page) } : {}),
|
|
173
|
-
...(perPage ? { perPage: String(perPage) } : {}),
|
|
174
|
-
...(_attribute?.length ? { attribute: _attribute } : {}),
|
|
175
|
-
} as const;
|
|
176
|
-
|
|
177
297
|
const searchParams = new URLSearchParams();
|
|
178
298
|
if (name) {
|
|
179
299
|
searchParams.set('name', name);
|
|
@@ -196,6 +316,12 @@ export class MastraClient extends BaseResource {
|
|
|
196
316
|
searchParams.set('attribute', _attribute);
|
|
197
317
|
}
|
|
198
318
|
}
|
|
319
|
+
if (fromDate) {
|
|
320
|
+
searchParams.set('fromDate', fromDate.toISOString());
|
|
321
|
+
}
|
|
322
|
+
if (toDate) {
|
|
323
|
+
searchParams.set('toDate', toDate.toISOString());
|
|
324
|
+
}
|
|
199
325
|
|
|
200
326
|
if (searchParams.size) {
|
|
201
327
|
return this.request(`/api/telemetry?${searchParams}`);
|
|
@@ -220,4 +346,65 @@ export class MastraClient extends BaseResource {
|
|
|
220
346
|
public getNetwork(networkId: string) {
|
|
221
347
|
return new Network(this.options, networkId);
|
|
222
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
|
+
}
|
|
223
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 () => {
|
|
@@ -46,7 +45,7 @@
|
|
|
46
45
|
// const workflowId = 'myWorkflow';
|
|
47
46
|
// const workflow = client.getWorkflow(workflowId);
|
|
48
47
|
|
|
49
|
-
// const { runId } =
|
|
48
|
+
// const { runId } = await workflow.createRun();
|
|
50
49
|
|
|
51
50
|
// workflow.watch({ runId }, record => {
|
|
52
51
|
// console.log(new Date().toTimeString(), record);
|
package/src/index.test.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { MessageType } from '@mastra/core';
|
|
2
1
|
import { describe, expect, beforeEach, it, vi } from 'vitest';
|
|
3
|
-
|
|
4
2
|
import { MastraClient } from './client';
|
|
3
|
+
import type { McpServerListResponse, ServerDetailInfo } from './types';
|
|
5
4
|
|
|
6
5
|
// Mock fetch globally
|
|
7
6
|
global.fetch = vi.fn();
|
|
@@ -237,6 +236,7 @@ describe('MastraClient Resources', () => {
|
|
|
237
236
|
model: 'gpt-4',
|
|
238
237
|
instructions: 'Test instructions',
|
|
239
238
|
tools: {},
|
|
239
|
+
workflows: {},
|
|
240
240
|
};
|
|
241
241
|
mockFetchResponse(mockResponse);
|
|
242
242
|
|
|
@@ -489,7 +489,7 @@ describe('MastraClient Resources', () => {
|
|
|
489
489
|
const result = await memoryThread.update({
|
|
490
490
|
title: 'Updated Thread',
|
|
491
491
|
metadata: { updated: true },
|
|
492
|
-
|
|
492
|
+
resourceId: 'test-resource',
|
|
493
493
|
});
|
|
494
494
|
expect(result).toEqual(mockResponse);
|
|
495
495
|
expect(global.fetch).toHaveBeenCalledWith(
|
|
@@ -536,6 +536,7 @@ describe('MastraClient Resources', () => {
|
|
|
536
536
|
content: 'test',
|
|
537
537
|
role: 'user' as const,
|
|
538
538
|
threadId: 'test-thread',
|
|
539
|
+
resourceId: 'test-resource',
|
|
539
540
|
createdAt: new Date('2025-03-26T10:40:55.116Z'),
|
|
540
541
|
},
|
|
541
542
|
];
|
|
@@ -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', () => {
|
|
@@ -584,10 +614,10 @@ describe('MastraClient Resources', () => {
|
|
|
584
614
|
it('should execute tool', async () => {
|
|
585
615
|
const mockResponse = { data: 'test' };
|
|
586
616
|
mockFetchResponse(mockResponse);
|
|
587
|
-
const result = await tool.execute({ data: '' });
|
|
617
|
+
const result = await tool.execute({ data: '', runId: 'test-run-id' });
|
|
588
618
|
expect(result).toEqual(mockResponse);
|
|
589
619
|
expect(global.fetch).toHaveBeenCalledWith(
|
|
590
|
-
`${clientOptions.baseUrl}/api/tools/test-tool/execute`,
|
|
620
|
+
`${clientOptions.baseUrl}/api/tools/test-tool/execute?runId=test-run-id`,
|
|
591
621
|
expect.objectContaining({
|
|
592
622
|
method: 'POST',
|
|
593
623
|
headers: expect.objectContaining({
|
|
@@ -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
|
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { TaskSendParams, TaskQueryParams, TaskIdParams, Task, AgentCard, JSONRPCResponse } from '@mastra/core/a2a';
|
|
2
|
+
import type { ClientOptions } from '../types';
|
|
3
|
+
import { BaseResource } from './base';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Class for interacting with an agent via the A2A protocol
|
|
7
|
+
*/
|
|
8
|
+
export class A2A extends BaseResource {
|
|
9
|
+
constructor(
|
|
10
|
+
options: ClientOptions,
|
|
11
|
+
private agentId: string,
|
|
12
|
+
) {
|
|
13
|
+
super(options);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the agent card with metadata about the agent
|
|
18
|
+
* @returns Promise containing the agent card information
|
|
19
|
+
*/
|
|
20
|
+
async getCard(): Promise<AgentCard> {
|
|
21
|
+
return this.request(`/.well-known/${this.agentId}/agent.json`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Send a message to the agent and get a response
|
|
26
|
+
* @param params - Parameters for the task
|
|
27
|
+
* @returns Promise containing the task response
|
|
28
|
+
*/
|
|
29
|
+
async sendMessage(params: TaskSendParams): Promise<{ task: Task }> {
|
|
30
|
+
const response = await this.request<JSONRPCResponse<Task>>(`/a2a/${this.agentId}`, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
body: {
|
|
33
|
+
method: 'tasks/send',
|
|
34
|
+
params,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return { task: response.result! };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get the status and result of a task
|
|
43
|
+
* @param params - Parameters for querying the task
|
|
44
|
+
* @returns Promise containing the task response
|
|
45
|
+
*/
|
|
46
|
+
async getTask(params: TaskQueryParams): Promise<Task> {
|
|
47
|
+
const response = await this.request<JSONRPCResponse<Task>>(`/a2a/${this.agentId}`, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
body: {
|
|
50
|
+
method: 'tasks/get',
|
|
51
|
+
params,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return response.result!;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Cancel a running task
|
|
60
|
+
* @param params - Parameters identifying the task to cancel
|
|
61
|
+
* @returns Promise containing the task response
|
|
62
|
+
*/
|
|
63
|
+
async cancelTask(params: TaskIdParams): Promise<{ task: Task }> {
|
|
64
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
body: {
|
|
67
|
+
method: 'tasks/cancel',
|
|
68
|
+
params,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Send a message and subscribe to streaming updates (not fully implemented)
|
|
75
|
+
* @param params - Parameters for the task
|
|
76
|
+
* @returns Promise containing the task response
|
|
77
|
+
*/
|
|
78
|
+
async sendAndSubscribe(params: TaskSendParams): Promise<Response> {
|
|
79
|
+
return this.request(`/a2a/${this.agentId}`, {
|
|
80
|
+
method: 'POST',
|
|
81
|
+
body: {
|
|
82
|
+
method: 'tasks/sendSubscribe',
|
|
83
|
+
params,
|
|
84
|
+
},
|
|
85
|
+
stream: true,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|