@mastra/mcp 0.13.3 → 0.13.4
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 +18 -0
- package/dist/client/client.d.ts +110 -2
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/configuration.d.ts +399 -4
- package/dist/client/configuration.d.ts.map +1 -1
- package/dist/client/elicitationActions.d.ts +46 -2
- package/dist/client/elicitationActions.d.ts.map +1 -1
- package/dist/client/promptActions.d.ts +61 -10
- package/dist/client/promptActions.d.ts.map +1 -1
- package/dist/client/resourceActions.d.ts +105 -15
- package/dist/client/resourceActions.d.ts.map +1 -1
- package/dist/index.cjs +951 -78
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +951 -78
- package/dist/index.js.map +1 -1
- package/dist/server/promptActions.d.ts +21 -2
- package/dist/server/promptActions.d.ts.map +1 -1
- package/dist/server/resourceActions.d.ts +35 -4
- package/dist/server/resourceActions.d.ts.map +1 -1
- package/dist/server/server.d.ts +381 -42
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/types.d.ts +89 -0
- package/dist/server/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -1,24 +1,196 @@
|
|
|
1
1
|
import { MastraBase } from '@mastra/core/base';
|
|
2
2
|
import type { ElicitRequest, ElicitResult, Prompt, Resource, ResourceTemplate } from '@modelcontextprotocol/sdk/types.js';
|
|
3
3
|
import type { MastraMCPServerDefinition } from './client.js';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for creating an MCPClient instance.
|
|
6
|
+
*/
|
|
4
7
|
export interface MCPClientOptions {
|
|
8
|
+
/** Optional unique identifier to prevent memory leaks when creating multiple instances with identical configurations */
|
|
5
9
|
id?: string;
|
|
10
|
+
/** Map of server names to their connection configurations (stdio or HTTP-based) */
|
|
6
11
|
servers: Record<string, MastraMCPServerDefinition>;
|
|
12
|
+
/** Optional global timeout in milliseconds for all servers (default: 60000ms) */
|
|
7
13
|
timeout?: number;
|
|
8
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* MCPClient manages multiple MCP server connections and their tools in a Mastra application.
|
|
17
|
+
*
|
|
18
|
+
* This class handles connection lifecycle, tool namespacing, and provides access to tools,
|
|
19
|
+
* resources, prompts, and elicitation across all configured servers.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { MCPClient } from '@mastra/mcp';
|
|
24
|
+
* import { Agent } from '@mastra/core/agent';
|
|
25
|
+
* import { openai } from '@ai-sdk/openai';
|
|
26
|
+
*
|
|
27
|
+
* const mcp = new MCPClient({
|
|
28
|
+
* servers: {
|
|
29
|
+
* weather: {
|
|
30
|
+
* url: new URL('http://localhost:8080/sse'),
|
|
31
|
+
* },
|
|
32
|
+
* stockPrice: {
|
|
33
|
+
* command: 'npx',
|
|
34
|
+
* args: ['tsx', 'stock-price.ts'],
|
|
35
|
+
* env: { API_KEY: 'your-api-key' },
|
|
36
|
+
* },
|
|
37
|
+
* },
|
|
38
|
+
* timeout: 30000,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* const agent = new Agent({
|
|
42
|
+
* name: 'Multi-tool Agent',
|
|
43
|
+
* instructions: 'You have access to multiple tools.',
|
|
44
|
+
* model: openai('gpt-4'),
|
|
45
|
+
* tools: await mcp.getTools(),
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
9
49
|
export declare class MCPClient extends MastraBase {
|
|
10
50
|
private serverConfigs;
|
|
11
51
|
private id;
|
|
12
52
|
private defaultTimeout;
|
|
13
53
|
private mcpClientsById;
|
|
14
54
|
private disconnectPromise;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new MCPClient instance for managing MCP server connections.
|
|
57
|
+
*
|
|
58
|
+
* The client automatically manages connection lifecycle and prevents memory leaks by
|
|
59
|
+
* caching instances with identical configurations.
|
|
60
|
+
*
|
|
61
|
+
* @param args - Configuration options
|
|
62
|
+
* @param args.id - Optional unique identifier to allow multiple instances with same config
|
|
63
|
+
* @param args.servers - Map of server names to server configurations
|
|
64
|
+
* @param args.timeout - Optional global timeout in milliseconds (default: 60000)
|
|
65
|
+
*
|
|
66
|
+
* @throws {Error} If multiple instances with identical config are created without an ID
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const mcp = new MCPClient({
|
|
71
|
+
* servers: {
|
|
72
|
+
* weatherServer: {
|
|
73
|
+
* url: new URL('http://localhost:8080/sse'),
|
|
74
|
+
* requestInit: {
|
|
75
|
+
* headers: { Authorization: 'Bearer token' }
|
|
76
|
+
* }
|
|
77
|
+
* }
|
|
78
|
+
* },
|
|
79
|
+
* timeout: 30000
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
15
83
|
constructor(args: MCPClientOptions);
|
|
84
|
+
/**
|
|
85
|
+
* Provides access to elicitation-related operations for interactive user input collection.
|
|
86
|
+
*
|
|
87
|
+
* Elicitation allows MCP servers to request structured information from users during tool execution.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* // Set up handler for elicitation requests from a server
|
|
92
|
+
* await mcp.elicitation.onRequest('serverName', async (request) => {
|
|
93
|
+
* console.log(`Server requests: ${request.message}`);
|
|
94
|
+
* console.log('Schema:', request.requestedSchema);
|
|
95
|
+
*
|
|
96
|
+
* // Collect user input and return response
|
|
97
|
+
* return {
|
|
98
|
+
* action: 'accept',
|
|
99
|
+
* content: { name: 'John Doe', email: 'john@example.com' }
|
|
100
|
+
* };
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
16
104
|
get elicitation(): {
|
|
105
|
+
/**
|
|
106
|
+
* Sets up a handler function for elicitation requests from a specific server.
|
|
107
|
+
*
|
|
108
|
+
* The handler receives requests for user input and must return a response with
|
|
109
|
+
* action ('accept', 'decline', or 'cancel') and optional content.
|
|
110
|
+
*
|
|
111
|
+
* @param serverName - Name of the server to handle elicitation requests for
|
|
112
|
+
* @param handler - Function to handle elicitation requests
|
|
113
|
+
* @throws {MastraError} If setting up the handler fails
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* await mcp.elicitation.onRequest('weatherServer', async (request) => {
|
|
118
|
+
* // Prompt user for input
|
|
119
|
+
* const userInput = await promptUser(request.requestedSchema);
|
|
120
|
+
* return { action: 'accept', content: userInput };
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
17
124
|
onRequest: (serverName: string, handler: (request: ElicitRequest["params"]) => Promise<ElicitResult>) => Promise<void>;
|
|
18
125
|
};
|
|
126
|
+
/**
|
|
127
|
+
* Provides access to resource-related operations across all configured servers.
|
|
128
|
+
*
|
|
129
|
+
* Resources represent data exposed by MCP servers (files, database records, API responses, etc.).
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* // List all resources from all servers
|
|
134
|
+
* const allResources = await mcp.resources.list();
|
|
135
|
+
* Object.entries(allResources).forEach(([serverName, resources]) => {
|
|
136
|
+
* console.log(`${serverName}: ${resources.length} resources`);
|
|
137
|
+
* });
|
|
138
|
+
*
|
|
139
|
+
* // Read a specific resource
|
|
140
|
+
* const content = await mcp.resources.read('weatherServer', 'file://data.json');
|
|
141
|
+
*
|
|
142
|
+
* // Subscribe to resource updates
|
|
143
|
+
* await mcp.resources.subscribe('weatherServer', 'file://data.json');
|
|
144
|
+
* await mcp.resources.onUpdated('weatherServer', async (params) => {
|
|
145
|
+
* console.log(`Resource updated: ${params.uri}`);
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
19
149
|
get resources(): {
|
|
150
|
+
/**
|
|
151
|
+
* Lists all available resources from all configured servers.
|
|
152
|
+
*
|
|
153
|
+
* Returns a map of server names to their resource arrays. Errors for individual
|
|
154
|
+
* servers are logged but don't throw - failed servers return empty arrays.
|
|
155
|
+
*
|
|
156
|
+
* @returns Promise resolving to object mapping server names to resource arrays
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const resources = await mcp.resources.list();
|
|
161
|
+
* console.log(resources.weatherServer); // Array of resources
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
20
164
|
list: () => Promise<Record<string, Resource[]>>;
|
|
165
|
+
/**
|
|
166
|
+
* Lists all available resource templates from all configured servers.
|
|
167
|
+
*
|
|
168
|
+
* Resource templates are URI templates (RFC 6570) describing dynamic resources.
|
|
169
|
+
* Errors for individual servers are logged but don't throw.
|
|
170
|
+
*
|
|
171
|
+
* @returns Promise resolving to object mapping server names to template arrays
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const templates = await mcp.resources.templates();
|
|
176
|
+
* console.log(templates.weatherServer); // Array of resource templates
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
21
179
|
templates: () => Promise<Record<string, ResourceTemplate[]>>;
|
|
180
|
+
/**
|
|
181
|
+
* Reads the content of a specific resource from a server.
|
|
182
|
+
*
|
|
183
|
+
* @param serverName - Name of the server to read from
|
|
184
|
+
* @param uri - URI of the resource to read
|
|
185
|
+
* @returns Promise resolving to the resource content
|
|
186
|
+
* @throws {MastraError} If reading the resource fails
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const content = await mcp.resources.read('weatherServer', 'file://config.json');
|
|
191
|
+
* console.log(content.contents[0].text);
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
22
194
|
read: (serverName: string, uri: string) => Promise<import("zod").objectOutputType<{
|
|
23
195
|
_meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
|
|
24
196
|
} & {
|
|
@@ -60,15 +232,131 @@ export declare class MCPClient extends MastraBase {
|
|
|
60
232
|
blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
|
|
61
233
|
}>, import("zod").ZodTypeAny, "passthrough">>]>, "many">;
|
|
62
234
|
}, import("zod").ZodTypeAny, "passthrough">>;
|
|
235
|
+
/**
|
|
236
|
+
* Subscribes to updates for a specific resource on a server.
|
|
237
|
+
*
|
|
238
|
+
* @param serverName - Name of the server
|
|
239
|
+
* @param uri - URI of the resource to subscribe to
|
|
240
|
+
* @returns Promise resolving when subscription is established
|
|
241
|
+
* @throws {MastraError} If subscription fails
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* await mcp.resources.subscribe('weatherServer', 'file://config.json');
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
63
248
|
subscribe: (serverName: string, uri: string) => Promise<{}>;
|
|
249
|
+
/**
|
|
250
|
+
* Unsubscribes from updates for a specific resource on a server.
|
|
251
|
+
*
|
|
252
|
+
* @param serverName - Name of the server
|
|
253
|
+
* @param uri - URI of the resource to unsubscribe from
|
|
254
|
+
* @returns Promise resolving when unsubscription is complete
|
|
255
|
+
* @throws {MastraError} If unsubscription fails
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* await mcp.resources.unsubscribe('weatherServer', 'file://config.json');
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
64
262
|
unsubscribe: (serverName: string, uri: string) => Promise<{}>;
|
|
263
|
+
/**
|
|
264
|
+
* Sets a notification handler for when subscribed resources are updated on a server.
|
|
265
|
+
*
|
|
266
|
+
* @param serverName - Name of the server to monitor
|
|
267
|
+
* @param handler - Callback function receiving the updated resource URI
|
|
268
|
+
* @returns Promise resolving when handler is registered
|
|
269
|
+
* @throws {MastraError} If setting up the handler fails
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* await mcp.resources.onUpdated('weatherServer', async (params) => {
|
|
274
|
+
* console.log(`Resource updated: ${params.uri}`);
|
|
275
|
+
* const content = await mcp.resources.read('weatherServer', params.uri);
|
|
276
|
+
* });
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
65
279
|
onUpdated: (serverName: string, handler: (params: {
|
|
66
280
|
uri: string;
|
|
67
281
|
}) => void) => Promise<void>;
|
|
282
|
+
/**
|
|
283
|
+
* Sets a notification handler for when the resource list changes on a server.
|
|
284
|
+
*
|
|
285
|
+
* @param serverName - Name of the server to monitor
|
|
286
|
+
* @param handler - Callback function invoked when resources are added/removed
|
|
287
|
+
* @returns Promise resolving when handler is registered
|
|
288
|
+
* @throws {MastraError} If setting up the handler fails
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```typescript
|
|
292
|
+
* await mcp.resources.onListChanged('weatherServer', async () => {
|
|
293
|
+
* console.log('Resource list changed, re-fetching...');
|
|
294
|
+
* const resources = await mcp.resources.list();
|
|
295
|
+
* });
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
68
298
|
onListChanged: (serverName: string, handler: () => void) => Promise<void>;
|
|
69
299
|
};
|
|
300
|
+
/**
|
|
301
|
+
* Provides access to prompt-related operations across all configured servers.
|
|
302
|
+
*
|
|
303
|
+
* Prompts are reusable message templates exposed by MCP servers that can be parameterized
|
|
304
|
+
* and used for AI interactions.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```typescript
|
|
308
|
+
* // List all prompts from all servers
|
|
309
|
+
* const allPrompts = await mcp.prompts.list();
|
|
310
|
+
* Object.entries(allPrompts).forEach(([serverName, prompts]) => {
|
|
311
|
+
* console.log(`${serverName}: ${prompts.map(p => p.name).join(', ')}`);
|
|
312
|
+
* });
|
|
313
|
+
*
|
|
314
|
+
* // Get a specific prompt with arguments
|
|
315
|
+
* const prompt = await mcp.prompts.get({
|
|
316
|
+
* serverName: 'weatherServer',
|
|
317
|
+
* name: 'forecast-template',
|
|
318
|
+
* args: { city: 'London', days: 7 }
|
|
319
|
+
* });
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
70
322
|
get prompts(): {
|
|
323
|
+
/**
|
|
324
|
+
* Lists all available prompts from all configured servers.
|
|
325
|
+
*
|
|
326
|
+
* Returns a map of server names to their prompt arrays. Errors for individual
|
|
327
|
+
* servers are logged but don't throw - failed servers return empty arrays.
|
|
328
|
+
*
|
|
329
|
+
* @returns Promise resolving to object mapping server names to prompt arrays
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const prompts = await mcp.prompts.list();
|
|
334
|
+
* console.log(prompts.weatherServer); // Array of prompts
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
71
337
|
list: () => Promise<Record<string, Prompt[]>>;
|
|
338
|
+
/**
|
|
339
|
+
* Retrieves a specific prompt with its messages from a server.
|
|
340
|
+
*
|
|
341
|
+
* @param params - Parameters for the prompt request
|
|
342
|
+
* @param params.serverName - Name of the server to retrieve from
|
|
343
|
+
* @param params.name - Name of the prompt to retrieve
|
|
344
|
+
* @param params.args - Optional arguments to populate the prompt template
|
|
345
|
+
* @param params.version - Optional specific version of the prompt
|
|
346
|
+
* @returns Promise resolving to the prompt result with messages
|
|
347
|
+
* @throws {MastraError} If fetching the prompt fails
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```typescript
|
|
351
|
+
* const prompt = await mcp.prompts.get({
|
|
352
|
+
* serverName: 'weatherServer',
|
|
353
|
+
* name: 'forecast',
|
|
354
|
+
* args: { city: 'London' },
|
|
355
|
+
* version: '1.0'
|
|
356
|
+
* });
|
|
357
|
+
* console.log(prompt.messages);
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
72
360
|
get: ({ serverName, name, args, version, }: {
|
|
73
361
|
serverName: string;
|
|
74
362
|
name: string;
|
|
@@ -143,12 +431,84 @@ export declare class MCPClient extends MastraBase {
|
|
|
143
431
|
} | undefined;
|
|
144
432
|
description?: string | undefined;
|
|
145
433
|
}>;
|
|
434
|
+
/**
|
|
435
|
+
* Sets a notification handler for when the prompt list changes on a server.
|
|
436
|
+
*
|
|
437
|
+
* @param serverName - Name of the server to monitor
|
|
438
|
+
* @param handler - Callback function invoked when prompts are added/removed/modified
|
|
439
|
+
* @returns Promise resolving when handler is registered
|
|
440
|
+
* @throws {MastraError} If setting up the handler fails
|
|
441
|
+
*
|
|
442
|
+
* @example
|
|
443
|
+
* ```typescript
|
|
444
|
+
* await mcp.prompts.onListChanged('weatherServer', async () => {
|
|
445
|
+
* console.log('Prompt list changed, re-fetching...');
|
|
446
|
+
* const prompts = await mcp.prompts.list();
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
146
450
|
onListChanged: (serverName: string, handler: () => void) => Promise<void>;
|
|
147
451
|
};
|
|
148
452
|
private addToInstanceCache;
|
|
149
453
|
private makeId;
|
|
454
|
+
/**
|
|
455
|
+
* Disconnects from all MCP servers and cleans up resources.
|
|
456
|
+
*
|
|
457
|
+
* This method gracefully closes all server connections and clears internal caches.
|
|
458
|
+
* Safe to call multiple times - subsequent calls will wait for the first disconnect to complete.
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```typescript
|
|
462
|
+
* // Cleanup on application shutdown
|
|
463
|
+
* process.on('SIGTERM', async () => {
|
|
464
|
+
* await mcp.disconnect();
|
|
465
|
+
* process.exit(0);
|
|
466
|
+
* });
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
150
469
|
disconnect(): Promise<void>;
|
|
470
|
+
/**
|
|
471
|
+
* Retrieves all tools from all configured servers with namespaced names.
|
|
472
|
+
*
|
|
473
|
+
* Tool names are namespaced as `serverName_toolName` to prevent conflicts between servers.
|
|
474
|
+
* This method is intended to be passed directly to an Agent definition.
|
|
475
|
+
*
|
|
476
|
+
* @returns Object mapping namespaced tool names to tool implementations
|
|
477
|
+
* @throws {MastraError} If retrieving tools fails
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```typescript
|
|
481
|
+
* const agent = new Agent({
|
|
482
|
+
* name: 'Multi-tool Agent',
|
|
483
|
+
* instructions: 'You have access to weather and stock tools.',
|
|
484
|
+
* model: openai('gpt-4'),
|
|
485
|
+
* tools: await mcp.getTools(), // weather_getWeather, stockPrice_getPrice
|
|
486
|
+
* });
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
151
489
|
getTools(): Promise<Record<string, any>>;
|
|
490
|
+
/**
|
|
491
|
+
* Returns toolsets organized by server name for dynamic tool injection.
|
|
492
|
+
*
|
|
493
|
+
* Unlike getTools(), this returns tools grouped by server without namespacing.
|
|
494
|
+
* This is intended to be passed dynamically to the generate() or stream() method.
|
|
495
|
+
*
|
|
496
|
+
* @returns Object mapping server names to their tool collections
|
|
497
|
+
* @throws {MastraError} If retrieving toolsets fails
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```typescript
|
|
501
|
+
* const agent = new Agent({
|
|
502
|
+
* name: 'Dynamic Agent',
|
|
503
|
+
* instructions: 'You can use tools dynamically.',
|
|
504
|
+
* model: openai('gpt-4'),
|
|
505
|
+
* });
|
|
506
|
+
*
|
|
507
|
+
* const response = await agent.stream(prompt, {
|
|
508
|
+
* toolsets: await mcp.getToolsets(), // { weather: {...}, stockPrice: {...} }
|
|
509
|
+
* });
|
|
510
|
+
* ```
|
|
511
|
+
*/
|
|
152
512
|
getToolsets(): Promise<Record<string, Record<string, any>>>;
|
|
153
513
|
/**
|
|
154
514
|
* @deprecated all resource actions have been moved to the this.resources object. Use this.resources.list() instead.
|
|
@@ -165,8 +525,19 @@ export declare class MCPClient extends MastraBase {
|
|
|
165
525
|
mimeType?: string | undefined;
|
|
166
526
|
}[]>>;
|
|
167
527
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
528
|
+
* Gets current session IDs for all connected MCP clients using Streamable HTTP transport.
|
|
529
|
+
*
|
|
530
|
+
* Returns an object mapping server names to their session IDs. Only includes servers
|
|
531
|
+
* that are currently connected via Streamable HTTP transport.
|
|
532
|
+
*
|
|
533
|
+
* @returns Object mapping server names to session IDs
|
|
534
|
+
*
|
|
535
|
+
* @example
|
|
536
|
+
* ```typescript
|
|
537
|
+
* const sessions = mcp.sessionIds;
|
|
538
|
+
* console.log(sessions);
|
|
539
|
+
* // { weatherServer: 'abc-123', stockServer: 'def-456' }
|
|
540
|
+
* ```
|
|
170
541
|
*/
|
|
171
542
|
get sessionIds(): Record<string, string>;
|
|
172
543
|
private getConnectedClient;
|
|
@@ -174,17 +545,41 @@ export declare class MCPClient extends MastraBase {
|
|
|
174
545
|
private eachClientTools;
|
|
175
546
|
}
|
|
176
547
|
/**
|
|
177
|
-
* @deprecated MCPConfigurationOptions is deprecated and will be removed in a future release. Use MCPClientOptions instead.
|
|
548
|
+
* @deprecated MCPConfigurationOptions is deprecated and will be removed in a future release. Use {@link MCPClientOptions} instead.
|
|
549
|
+
*
|
|
550
|
+
* This interface has been renamed to MCPClientOptions. The API is identical.
|
|
178
551
|
*/
|
|
179
552
|
export interface MCPConfigurationOptions {
|
|
553
|
+
/** @deprecated Use MCPClientOptions.id instead */
|
|
180
554
|
id?: string;
|
|
555
|
+
/** @deprecated Use MCPClientOptions.servers instead */
|
|
181
556
|
servers: Record<string, MastraMCPServerDefinition>;
|
|
557
|
+
/** @deprecated Use MCPClientOptions.timeout instead */
|
|
182
558
|
timeout?: number;
|
|
183
559
|
}
|
|
184
560
|
/**
|
|
185
|
-
* @deprecated MCPConfiguration is deprecated and will be removed in a future release. Use MCPClient instead.
|
|
561
|
+
* @deprecated MCPConfiguration is deprecated and will be removed in a future release. Use {@link MCPClient} instead.
|
|
562
|
+
*
|
|
563
|
+
* This class has been renamed to MCPClient. The API is identical but the class name changed
|
|
564
|
+
* for clarity and consistency.
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```typescript
|
|
568
|
+
* // Old way (deprecated)
|
|
569
|
+
* const config = new MCPConfiguration({
|
|
570
|
+
* servers: { myServer: { command: 'npx', args: ['tsx', 'server.ts'] } }
|
|
571
|
+
* });
|
|
572
|
+
*
|
|
573
|
+
* // New way (recommended)
|
|
574
|
+
* const client = new MCPClient({
|
|
575
|
+
* servers: { myServer: { command: 'npx', args: ['tsx', 'server.ts'] } }
|
|
576
|
+
* });
|
|
577
|
+
* ```
|
|
186
578
|
*/
|
|
187
579
|
export declare class MCPConfiguration extends MCPClient {
|
|
580
|
+
/**
|
|
581
|
+
* @deprecated Use MCPClient constructor instead
|
|
582
|
+
*/
|
|
188
583
|
constructor(args: MCPClientOptions);
|
|
189
584
|
}
|
|
190
585
|
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configuration.d.ts","sourceRoot":"","sources":["../../src/client/configuration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG/C,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,gBAAgB,EACjB,MAAM,oCAAoC,CAAC;AAI5C,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAI1D,MAAM,WAAW,gBAAgB;IAC/B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;IACnD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,SAAU,SAAQ,UAAU;IACvC,OAAO,CAAC,aAAa,CAAiD;IACtE,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,cAAc,CAA8C;IACpE,OAAO,CAAC,iBAAiB,CAA8B;
|
|
1
|
+
{"version":3,"file":"configuration.d.ts","sourceRoot":"","sources":["../../src/client/configuration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG/C,OAAO,KAAK,EACV,a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E,CAAC,EAAE,MAAM,CAAC;IACZ,uDAAuD;IACvD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;IACnD,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;IAC7C;;OAEG;gBACS,IAAI,EAAE,gBAAgB;CAMnC"}
|
|
@@ -5,13 +5,57 @@ interface ElicitationClientActionsConfig {
|
|
|
5
5
|
client: InternalMastraMCPClient;
|
|
6
6
|
logger: IMastraLogger;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Client-side elicitation actions for handling interactive user input requests.
|
|
10
|
+
*
|
|
11
|
+
* Elicitation allows MCP servers to request structured information from users during
|
|
12
|
+
* tool execution. The client provides a handler that collects user input and returns
|
|
13
|
+
* it to the server.
|
|
14
|
+
*/
|
|
8
15
|
export declare class ElicitationClientActions {
|
|
9
16
|
private readonly client;
|
|
10
17
|
private readonly logger;
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
11
21
|
constructor({ client, logger }: ElicitationClientActionsConfig);
|
|
12
22
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
23
|
+
* Sets a handler function for processing elicitation requests from the server.
|
|
24
|
+
*
|
|
25
|
+
* The handler is called when the server needs to collect user input during tool execution.
|
|
26
|
+
* The handler must return a response with action ('accept', 'decline', or 'cancel') and
|
|
27
|
+
* optional content matching the requested schema.
|
|
28
|
+
*
|
|
29
|
+
* @param handler - Callback function to handle elicitation requests
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* client.elicitation.onRequest(async (request) => {
|
|
34
|
+
* console.log('Server message:', request.message);
|
|
35
|
+
* console.log('Requested schema:', request.requestedSchema);
|
|
36
|
+
*
|
|
37
|
+
* // Collect user input (e.g., via CLI prompt or UI form)
|
|
38
|
+
* const userInput = await collectUserInput(request.requestedSchema);
|
|
39
|
+
*
|
|
40
|
+
* return {
|
|
41
|
+
* action: 'accept',
|
|
42
|
+
* content: userInput
|
|
43
|
+
* };
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* // Declining an elicitation request
|
|
50
|
+
* client.elicitation.onRequest(async (request) => {
|
|
51
|
+
* if (!shouldAcceptRequest(request)) {
|
|
52
|
+
* return { action: 'decline' };
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* const input = await getInput();
|
|
56
|
+
* return { action: 'accept', content: input };
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
15
59
|
*/
|
|
16
60
|
onRequest(handler: (request: ElicitRequest['params']) => Promise<ElicitResult>): void;
|
|
17
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elicitationActions.d.ts","sourceRoot":"","sources":["../../src/client/elicitationActions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AACtF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAExD,UAAU,8BAA8B;IACtC,MAAM,EAAE,uBAAuB,CAAC;IAChC,MAAM,EAAE,aAAa,CAAC;CACvB;AAED,qBAAa,wBAAwB;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0B;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;
|
|
1
|
+
{"version":3,"file":"elicitationActions.d.ts","sourceRoot":"","sources":["../../src/client/elicitationActions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AACtF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAExD,UAAU,8BAA8B;IACtC,MAAM,EAAE,uBAAuB,CAAC;IAChC,MAAM,EAAE,aAAa,CAAC;CACvB;AAED;;;;;;GAMG;AACH,qBAAa,wBAAwB;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0B;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IAEvC;;OAEG;gBACS,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,8BAA8B;IAK9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACI,SAAS,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,OAAO,CAAC,YAAY,CAAC,GAAG,IAAI;CAG7F"}
|
|
@@ -6,23 +6,62 @@ interface PromptClientActionsConfig {
|
|
|
6
6
|
logger: IMastraLogger;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
* Client-side prompt actions for
|
|
9
|
+
* Client-side prompt actions for interacting with MCP server prompts.
|
|
10
|
+
*
|
|
11
|
+
* Provides methods to list, retrieve, and subscribe to prompt templates exposed by an MCP server.
|
|
12
|
+
* Prompts are reusable message templates that can be parameterized and used for AI interactions.
|
|
10
13
|
*/
|
|
11
14
|
export declare class PromptClientActions {
|
|
12
15
|
private readonly client;
|
|
13
16
|
private readonly logger;
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
14
20
|
constructor({ client, logger }: PromptClientActionsConfig);
|
|
15
21
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
22
|
+
* Retrieves all available prompts from the connected MCP server.
|
|
23
|
+
*
|
|
24
|
+
* Returns an empty array if the server doesn't support prompts (MethodNotFound error).
|
|
25
|
+
*
|
|
26
|
+
* @returns Promise resolving to array of prompts with their metadata
|
|
27
|
+
* @throws {Error} If fetching prompts fails (excluding MethodNotFound)
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const prompts = await client.prompts.list();
|
|
32
|
+
* prompts.forEach(prompt => {
|
|
33
|
+
* console.log(`${prompt.name} (v${prompt.version}): ${prompt.description}`);
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
18
36
|
*/
|
|
19
37
|
list(): Promise<Prompt[]>;
|
|
20
38
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
39
|
+
* Retrieves a specific prompt with its messages from the MCP server.
|
|
40
|
+
*
|
|
41
|
+
* Prompts can accept arguments to parameterize the template. The returned messages
|
|
42
|
+
* can be used directly in AI chat completions.
|
|
43
|
+
*
|
|
44
|
+
* @param params - Parameters for the prompt request
|
|
45
|
+
* @param params.name - Name of the prompt to retrieve
|
|
46
|
+
* @param params.args - Optional arguments to populate the prompt template
|
|
47
|
+
* @param params.version - Optional specific version of the prompt to retrieve
|
|
48
|
+
* @returns Promise resolving to the prompt result with messages
|
|
49
|
+
* @throws {Error} If fetching the prompt fails or prompt not found
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const prompt = await client.prompts.get({
|
|
54
|
+
* name: 'code-review',
|
|
55
|
+
* args: {
|
|
56
|
+
* language: 'typescript',
|
|
57
|
+
* code: 'const x = 1;'
|
|
58
|
+
* },
|
|
59
|
+
* version: '1.0'
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* // Use prompt messages in AI completion
|
|
63
|
+
* console.log(prompt.messages);
|
|
64
|
+
* ```
|
|
26
65
|
*/
|
|
27
66
|
get({ name, args, version, }: {
|
|
28
67
|
name: string;
|
|
@@ -30,8 +69,20 @@ export declare class PromptClientActions {
|
|
|
30
69
|
version?: string;
|
|
31
70
|
}): Promise<GetPromptResult>;
|
|
32
71
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
72
|
+
* Sets a notification handler for when the list of available prompts changes.
|
|
73
|
+
*
|
|
74
|
+
* The handler is called when prompts are added, removed, or modified on the server.
|
|
75
|
+
*
|
|
76
|
+
* @param handler - Callback function invoked when the prompt list changes
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* await client.prompts.onListChanged(async () => {
|
|
81
|
+
* console.log('Prompt list changed, re-fetching...');
|
|
82
|
+
* const prompts = await client.prompts.list();
|
|
83
|
+
* console.log('Available prompts:', prompts.map(p => p.name));
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
35
86
|
*/
|
|
36
87
|
onListChanged(handler: () => void): Promise<void>;
|
|
37
88
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promptActions.d.ts","sourceRoot":"","sources":["../../src/client/promptActions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,oCAAoC,CAAC;AAClF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAExD,UAAU,yBAAyB;IACjC,MAAM,EAAE,uBAAuB,CAAC;IAChC,MAAM,EAAE,aAAa,CAAC;CACvB;AAED
|
|
1
|
+
{"version":3,"file":"promptActions.d.ts","sourceRoot":"","sources":["../../src/client/promptActions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,oCAAoC,CAAC;AAClF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAExD,UAAU,yBAAyB;IACjC,MAAM,EAAE,uBAAuB,CAAC;IAChC,MAAM,EAAE,aAAa,CAAC;CACvB;AAED;;;;;GAKG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,M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