@mastra/client-js 0.0.0-separate-trace-data-from-component-20250501141108 → 0.0.0-share-agent-metadata-with-cloud-20250718110128

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/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();
@@ -27,7 +27,13 @@ describe('MastraClient Resources', () => {
27
27
  } else {
28
28
  responseBody = new ReadableStream({
29
29
  start(controller) {
30
- controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
30
+ if (typeof data === 'string') {
31
+ controller.enqueue(new TextEncoder().encode(data));
32
+ } else if (typeof data === 'object' && data !== null) {
33
+ controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
34
+ } else {
35
+ controller.enqueue(new TextEncoder().encode(String(data)));
36
+ }
31
37
  controller.close();
32
38
  },
33
39
  });
@@ -236,6 +242,7 @@ describe('MastraClient Resources', () => {
236
242
  model: 'gpt-4',
237
243
  instructions: 'Test instructions',
238
244
  tools: {},
245
+ workflows: {},
239
246
  };
240
247
  mockFetchResponse(mockResponse);
241
248
 
@@ -278,7 +285,7 @@ describe('MastraClient Resources', () => {
278
285
  });
279
286
 
280
287
  it('should stream responses', async () => {
281
- const mockChunk = { content: 'test response' };
288
+ const mockChunk = `0:"test response"\n`;
282
289
  mockFetchResponse(mockChunk, { isStream: true });
283
290
 
284
291
  const response = await agent.stream({
@@ -297,7 +304,7 @@ describe('MastraClient Resources', () => {
297
304
  if (reader) {
298
305
  const { value, done } = await reader.read();
299
306
  expect(done).toBe(false);
300
- expect(new TextDecoder().decode(value)).toBe(JSON.stringify(mockChunk));
307
+ expect(new TextDecoder().decode(value)).toBe(mockChunk);
301
308
  }
302
309
  });
303
310
 
@@ -552,6 +559,35 @@ describe('MastraClient Resources', () => {
552
559
  }),
553
560
  );
554
561
  });
562
+
563
+ it('should get thread messages with limit', async () => {
564
+ const mockResponse = {
565
+ messages: [
566
+ {
567
+ id: '1',
568
+ content: 'test',
569
+ threadId,
570
+ role: 'user',
571
+ type: 'text',
572
+ resourceId: 'test-resource',
573
+ createdAt: new Date(),
574
+ },
575
+ ],
576
+ uiMessages: [],
577
+ };
578
+ mockFetchResponse(mockResponse);
579
+
580
+ const limit = 5;
581
+ const result = await memoryThread.getMessages({ limit });
582
+
583
+ expect(result).toEqual(mockResponse);
584
+ expect(global.fetch).toHaveBeenCalledWith(
585
+ `${clientOptions.baseUrl}/api/memory/threads/${threadId}/messages?agentId=${agentId}&limit=${limit}`,
586
+ expect.objectContaining({
587
+ headers: expect.objectContaining(clientOptions.headers),
588
+ }),
589
+ );
590
+ });
555
591
  });
556
592
 
557
593
  describe('Tool Resource', () => {
@@ -632,14 +668,14 @@ describe('MastraClient Resources', () => {
632
668
  };
633
669
  mockFetchResponse(mockResponse);
634
670
 
635
- const result = await workflow.startAsync({ triggerData: { test: 'test' } });
671
+ const result = await workflow.startAsync({ inputData: { test: 'test' } });
636
672
  expect(result).toEqual(mockResponse);
637
673
  expect(global.fetch).toHaveBeenCalledWith(
638
674
  `${clientOptions.baseUrl}/api/workflows/test-workflow/start-async?`,
639
675
  expect.objectContaining({
640
676
  method: 'POST',
641
677
  headers: expect.objectContaining(clientOptions.headers),
642
- body: JSON.stringify({ test: 'test' }),
678
+ body: JSON.stringify({ inputData: { test: 'test' } }),
643
679
  }),
644
680
  );
645
681
  });
@@ -707,4 +743,94 @@ describe('MastraClient Resources', () => {
707
743
  );
708
744
  });
709
745
  });
746
+
747
+ describe('MCP Server Registry Client Methods', () => {
748
+ const mockServerInfo1 = {
749
+ id: 'mcp-server-1',
750
+ name: 'Test MCP Server 1',
751
+ version_detail: { version: '1.0.0', release_date: '2023-01-01T00:00:00Z', is_latest: true },
752
+ };
753
+ const mockServerInfo2 = {
754
+ id: 'mcp-server-2',
755
+ name: 'Test MCP Server 2',
756
+ version_detail: { version: '1.1.0', release_date: '2023-02-01T00:00:00Z', is_latest: true },
757
+ };
758
+
759
+ const mockServerDetail1: ServerDetailInfo = {
760
+ ...mockServerInfo1,
761
+ description: 'Detailed description for server 1',
762
+ package_canonical: 'npm',
763
+ packages: [{ registry_name: 'npm', name: '@example/server1', version: '1.0.0' }],
764
+ remotes: [{ transport_type: 'sse', url: 'http://localhost/sse1' }],
765
+ };
766
+
767
+ describe('getMcpServers()', () => {
768
+ it('should fetch a list of MCP servers', async () => {
769
+ const mockResponse: McpServerListResponse = {
770
+ servers: [mockServerInfo1, mockServerInfo2],
771
+ total_count: 2,
772
+ next: null,
773
+ };
774
+ mockFetchResponse(mockResponse);
775
+
776
+ const result = await client.getMcpServers();
777
+ expect(result).toEqual(mockResponse);
778
+ expect(global.fetch).toHaveBeenCalledWith(
779
+ `${clientOptions.baseUrl}/api/mcp/v0/servers`,
780
+ expect.objectContaining({
781
+ headers: expect.objectContaining(clientOptions.headers),
782
+ }),
783
+ );
784
+ });
785
+
786
+ it('should fetch MCP servers with limit and offset parameters', async () => {
787
+ const mockResponse: McpServerListResponse = {
788
+ servers: [mockServerInfo1],
789
+ total_count: 2,
790
+ next: '/api/mcp/v0/servers?limit=1&offset=1',
791
+ };
792
+ mockFetchResponse(mockResponse);
793
+
794
+ const result = await client.getMcpServers({ limit: 1, offset: 0 });
795
+ expect(result).toEqual(mockResponse);
796
+ expect(global.fetch).toHaveBeenCalledWith(
797
+ `${clientOptions.baseUrl}/api/mcp/v0/servers?limit=1&offset=0`,
798
+ expect.objectContaining({
799
+ headers: expect.objectContaining(clientOptions.headers),
800
+ }),
801
+ );
802
+ });
803
+ });
804
+
805
+ describe('getMcpServerDetails()', () => {
806
+ const serverId = 'mcp-server-1';
807
+
808
+ it('should fetch details for a specific MCP server', async () => {
809
+ mockFetchResponse(mockServerDetail1);
810
+
811
+ const result = await client.getMcpServerDetails(serverId);
812
+ expect(result).toEqual(mockServerDetail1);
813
+ expect(global.fetch).toHaveBeenCalledWith(
814
+ `${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}`,
815
+ expect.objectContaining({
816
+ headers: expect.objectContaining(clientOptions.headers),
817
+ }),
818
+ );
819
+ });
820
+
821
+ it('should fetch MCP server details with a version parameter', async () => {
822
+ mockFetchResponse(mockServerDetail1);
823
+ const version = '1.0.0';
824
+
825
+ const result = await client.getMcpServerDetails(serverId, { version });
826
+ expect(result).toEqual(mockServerDetail1);
827
+ expect(global.fetch).toHaveBeenCalledWith(
828
+ `${clientOptions.baseUrl}/api/mcp/v0/servers/${serverId}?version=${version}`,
829
+ expect.objectContaining({
830
+ headers: expect.objectContaining(clientOptions.headers),
831
+ }),
832
+ );
833
+ });
834
+ });
835
+ });
710
836
  });
@@ -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
+ }