@mastra/client-js 0.0.0-vnext-inngest-20250508131921 → 0.0.0-vnextAgentNetwork-20250527091247
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/CHANGELOG.md +206 -2
- package/dist/index.cjs +394 -102
- package/dist/index.d.cts +244 -77
- package/dist/index.d.ts +244 -77
- package/dist/index.js +390 -102
- package/package.json +6 -5
- package/src/adapters/agui.test.ts +19 -6
- package/src/adapters/agui.ts +31 -11
- package/src/client.ts +94 -19
- 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 +27 -36
- package/src/resources/base.ts +1 -1
- package/src/resources/index.ts +4 -2
- package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +124 -139
- 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 +197 -99
- package/src/types.ts +66 -17
- package/src/utils/index.ts +11 -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-vnextAgentNetwork-20250527091247",
|
|
4
4
|
"description": "The official TypeScript library for the Mastra Client API",
|
|
5
5
|
"author": "",
|
|
6
6
|
"type": "module",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"json-schema": "^0.4.0",
|
|
28
28
|
"rxjs": "7.8.1",
|
|
29
29
|
"zod": "^3.24.3",
|
|
30
|
-
"zod-to-json-schema": "^3.24.5"
|
|
31
|
-
"@mastra/core": "0.0.0-vnext-inngest-20250508131921"
|
|
30
|
+
"zod-to-json-schema": "^3.24.5"
|
|
32
31
|
},
|
|
33
32
|
"peerDependencies": {
|
|
34
|
-
"zod": "^3.
|
|
33
|
+
"zod": "^3.0.0",
|
|
34
|
+
"@mastra/core": "^0.10.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@babel/preset-env": "^7.26.9",
|
|
@@ -42,7 +42,8 @@
|
|
|
42
42
|
"tsup": "^8.4.0",
|
|
43
43
|
"typescript": "^5.8.2",
|
|
44
44
|
"vitest": "^3.1.2",
|
|
45
|
-
"@internal/lint": "0.0.0-
|
|
45
|
+
"@internal/lint": "0.0.0-vnextAgentNetwork-20250527091247",
|
|
46
|
+
"@mastra/core": "0.0.0-vnextAgentNetwork-20250527091247"
|
|
46
47
|
},
|
|
47
48
|
"scripts": {
|
|
48
49
|
"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,6 +1,18 @@
|
|
|
1
1
|
import type { AbstractAgent } from '@ag-ui/client';
|
|
2
|
+
import type { ServerDetailInfo } from '@mastra/core/mcp';
|
|
2
3
|
import { AGUIAdapter } from './adapters/agui';
|
|
3
|
-
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';
|
|
4
16
|
import type {
|
|
5
17
|
ClientOptions,
|
|
6
18
|
CreateMemoryThreadParams,
|
|
@@ -15,10 +27,12 @@ import type {
|
|
|
15
27
|
GetTelemetryParams,
|
|
16
28
|
GetTelemetryResponse,
|
|
17
29
|
GetToolResponse,
|
|
18
|
-
GetVNextWorkflowResponse,
|
|
19
30
|
GetWorkflowResponse,
|
|
20
31
|
SaveMessageToMemoryParams,
|
|
21
32
|
SaveMessageToMemoryResponse,
|
|
33
|
+
McpServerListResponse,
|
|
34
|
+
McpServerToolListResponse,
|
|
35
|
+
GetLegacyWorkflowResponse,
|
|
22
36
|
} from './types';
|
|
23
37
|
|
|
24
38
|
export class MastraClient extends BaseResource {
|
|
@@ -126,6 +140,23 @@ export class MastraClient extends BaseResource {
|
|
|
126
140
|
return new Tool(this.options, toolId);
|
|
127
141
|
}
|
|
128
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
|
+
|
|
129
160
|
/**
|
|
130
161
|
* Retrieves all available workflows
|
|
131
162
|
* @returns Promise containing map of workflow IDs to workflow details
|
|
@@ -143,23 +174,6 @@ export class MastraClient extends BaseResource {
|
|
|
143
174
|
return new Workflow(this.options, workflowId);
|
|
144
175
|
}
|
|
145
176
|
|
|
146
|
-
/**
|
|
147
|
-
* Retrieves all available vNext workflows
|
|
148
|
-
* @returns Promise containing map of vNext workflow IDs to vNext workflow details
|
|
149
|
-
*/
|
|
150
|
-
public getVNextWorkflows(): Promise<Record<string, GetVNextWorkflowResponse>> {
|
|
151
|
-
return this.request('/api/workflows/v-next');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Gets a vNext workflow instance by ID
|
|
156
|
-
* @param workflowId - ID of the vNext workflow to retrieve
|
|
157
|
-
* @returns vNext Workflow instance
|
|
158
|
-
*/
|
|
159
|
-
public getVNextWorkflow(workflowId: string) {
|
|
160
|
-
return new VNextWorkflow(this.options, workflowId);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
177
|
/**
|
|
164
178
|
* Gets a vector instance by name
|
|
165
179
|
* @param vectorName - Name of the vector to retrieve
|
|
@@ -256,4 +270,65 @@ export class MastraClient extends BaseResource {
|
|
|
256
270
|
public getNetwork(networkId: string) {
|
|
257
271
|
return new Network(this.options, networkId);
|
|
258
272
|
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Retrieves a list of available MCP servers.
|
|
276
|
+
* @param params - Optional parameters for pagination (limit, offset).
|
|
277
|
+
* @returns Promise containing the list of MCP servers and pagination info.
|
|
278
|
+
*/
|
|
279
|
+
public getMcpServers(params?: { limit?: number; offset?: number }): Promise<McpServerListResponse> {
|
|
280
|
+
const searchParams = new URLSearchParams();
|
|
281
|
+
if (params?.limit !== undefined) {
|
|
282
|
+
searchParams.set('limit', String(params.limit));
|
|
283
|
+
}
|
|
284
|
+
if (params?.offset !== undefined) {
|
|
285
|
+
searchParams.set('offset', String(params.offset));
|
|
286
|
+
}
|
|
287
|
+
const queryString = searchParams.toString();
|
|
288
|
+
return this.request(`/api/mcp/v0/servers${queryString ? `?${queryString}` : ''}`);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Retrieves detailed information for a specific MCP server.
|
|
293
|
+
* @param serverId - The ID of the MCP server to retrieve.
|
|
294
|
+
* @param params - Optional parameters, e.g., specific version.
|
|
295
|
+
* @returns Promise containing the detailed MCP server information.
|
|
296
|
+
*/
|
|
297
|
+
public getMcpServerDetails(serverId: string, params?: { version?: string }): Promise<ServerDetailInfo> {
|
|
298
|
+
const searchParams = new URLSearchParams();
|
|
299
|
+
if (params?.version) {
|
|
300
|
+
searchParams.set('version', params.version);
|
|
301
|
+
}
|
|
302
|
+
const queryString = searchParams.toString();
|
|
303
|
+
return this.request(`/api/mcp/v0/servers/${serverId}${queryString ? `?${queryString}` : ''}`);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Retrieves a list of tools for a specific MCP server.
|
|
308
|
+
* @param serverId - The ID of the MCP server.
|
|
309
|
+
* @returns Promise containing the list of tools.
|
|
310
|
+
*/
|
|
311
|
+
public getMcpServerTools(serverId: string): Promise<McpServerToolListResponse> {
|
|
312
|
+
return this.request(`/api/mcp/${serverId}/tools`);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Gets an MCPTool resource instance for a specific tool on an MCP server.
|
|
317
|
+
* This instance can then be used to fetch details or execute the tool.
|
|
318
|
+
* @param serverId - The ID of the MCP server.
|
|
319
|
+
* @param toolId - The ID of the tool.
|
|
320
|
+
* @returns MCPTool instance.
|
|
321
|
+
*/
|
|
322
|
+
public getMcpServerTool(serverId: string, toolId: string): MCPTool {
|
|
323
|
+
return new MCPTool(this.options, serverId, toolId);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
328
|
+
* @param agentId - ID of the agent to interact with
|
|
329
|
+
* @returns A2A client instance
|
|
330
|
+
*/
|
|
331
|
+
public getA2A(agentId: string) {
|
|
332
|
+
return new A2A(this.options, agentId);
|
|
333
|
+
}
|
|
259
334
|
}
|
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
|
});
|
|
@@ -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
|
+
}
|