@mastra/client-js 0.0.0-generate-message-id-20250512171942 → 0.0.0-inject-middleware-20250528222017
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 +180 -4
- package/dist/index.cjs +253 -92
- package/dist/index.d.cts +190 -80
- package/dist/index.d.ts +190 -80
- package/dist/index.js +253 -92
- package/package.json +6 -5
- package/src/client.ts +85 -19
- package/src/example.ts +29 -30
- package/src/index.test.ts +121 -1
- package/src/resources/agent.ts +9 -8
- package/src/resources/base.ts +1 -1
- package/src/resources/index.ts +3 -2
- package/src/resources/{vnext-workflow.ts → legacy-workflow.ts} +124 -143
- package/src/resources/mcp-tool.ts +48 -0
- package/src/resources/memory-thread.ts +13 -3
- package/src/resources/tool.ts +4 -3
- package/src/resources/workflow.ts +202 -100
- package/src/types.ts +70 -18
- package/src/utils/index.ts +11 -0
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
|
|
@@ -257,6 +271,58 @@ export class MastraClient extends BaseResource {
|
|
|
257
271
|
return new Network(this.options, networkId);
|
|
258
272
|
}
|
|
259
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
|
+
|
|
260
326
|
/**
|
|
261
327
|
* Gets an A2A client for interacting with an agent via the A2A protocol
|
|
262
328
|
* @param agentId - ID of the agent to interact with
|
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
|
});
|
package/src/resources/agent.ts
CHANGED
|
@@ -14,7 +14,8 @@ import type {
|
|
|
14
14
|
} from '../types';
|
|
15
15
|
|
|
16
16
|
import { BaseResource } from './base';
|
|
17
|
-
import type { RuntimeContext } from '@mastra/core/
|
|
17
|
+
import type { RuntimeContext } from '@mastra/core/runtime-context';
|
|
18
|
+
import { parseClientRuntimeContext } from '../utils';
|
|
18
19
|
|
|
19
20
|
export class AgentVoice extends BaseResource {
|
|
20
21
|
constructor(
|
|
@@ -48,7 +49,7 @@ export class AgentVoice extends BaseResource {
|
|
|
48
49
|
* @param options - Optional provider-specific options
|
|
49
50
|
* @returns Promise containing the transcribed text
|
|
50
51
|
*/
|
|
51
|
-
listen(audio: Blob, options?: Record<string, any>): Promise<
|
|
52
|
+
listen(audio: Blob, options?: Record<string, any>): Promise<{ text: string }> {
|
|
52
53
|
const formData = new FormData();
|
|
53
54
|
formData.append('audio', audio);
|
|
54
55
|
|
|
@@ -100,9 +101,9 @@ export class Agent extends BaseResource {
|
|
|
100
101
|
): Promise<GenerateReturn<T>> {
|
|
101
102
|
const processedParams = {
|
|
102
103
|
...params,
|
|
103
|
-
output: zodToJsonSchema(params.output),
|
|
104
|
-
experimental_output: zodToJsonSchema(params.experimental_output),
|
|
105
|
-
runtimeContext:
|
|
104
|
+
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
105
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
|
|
106
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
106
107
|
};
|
|
107
108
|
|
|
108
109
|
return this.request(`/api/agents/${this.agentId}/generate`, {
|
|
@@ -125,9 +126,9 @@ export class Agent extends BaseResource {
|
|
|
125
126
|
> {
|
|
126
127
|
const processedParams = {
|
|
127
128
|
...params,
|
|
128
|
-
output: zodToJsonSchema(params.output),
|
|
129
|
-
experimental_output: zodToJsonSchema(params.experimental_output),
|
|
130
|
-
runtimeContext:
|
|
129
|
+
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
130
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
|
|
131
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
131
132
|
};
|
|
132
133
|
|
|
133
134
|
const response: Response & {
|
package/src/resources/base.ts
CHANGED
|
@@ -21,7 +21,7 @@ export class BaseResource {
|
|
|
21
21
|
|
|
22
22
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
23
23
|
try {
|
|
24
|
-
const response = await fetch(`${baseUrl}${path}`, {
|
|
24
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, '')}${path}`, {
|
|
25
25
|
...options,
|
|
26
26
|
headers: {
|
|
27
27
|
...headers,
|
package/src/resources/index.ts
CHANGED
|
@@ -2,8 +2,9 @@ export * from './agent';
|
|
|
2
2
|
export * from './network';
|
|
3
3
|
export * from './memory-thread';
|
|
4
4
|
export * from './vector';
|
|
5
|
-
export * from './workflow';
|
|
5
|
+
export * from './legacy-workflow';
|
|
6
6
|
export * from './tool';
|
|
7
7
|
export * from './base';
|
|
8
|
-
export * from './
|
|
8
|
+
export * from './workflow';
|
|
9
9
|
export * from './a2a';
|
|
10
|
+
export * from './mcp-tool';
|