@mastra/mcp 0.10.2 → 0.10.3
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 +8 -8
- package/CHANGELOG.md +31 -0
- package/README.md +32 -0
- package/dist/_tsup-dts-rollup.d.cts +175 -1
- package/dist/_tsup-dts-rollup.d.ts +175 -1
- package/dist/index.cjs +251 -2
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +253 -4
- package/integration-tests/node_modules/.bin/vitest +2 -2
- package/integration-tests/package.json +3 -3
- package/package.json +9 -9
- package/src/__fixtures__/weather.ts +62 -2
- package/src/client/client.test.ts +46 -0
- package/src/client/client.ts +43 -1
- package/src/client/configuration.test.ts +280 -168
- package/src/client/configuration.ts +27 -1
- package/src/client/index.ts +3 -0
- package/src/client/promptActions.ts +70 -0
- package/src/client/resourceActions.ts +0 -2
- package/src/index.ts +2 -4
- package/src/server/index.ts +2 -0
- package/src/server/promptActions.ts +37 -0
- package/src/server/server.test.ts +224 -1
- package/src/server/server.ts +139 -14
- package/src/server/types.ts +30 -0
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Agent } from '@mastra/core/agent';
|
|
2
2
|
import type { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
|
|
3
3
|
import type { ConvertedTool } from '@mastra/core/mcp';
|
|
4
|
+
import type { GetPromptResult } from '@modelcontextprotocol/sdk/types.js';
|
|
4
5
|
import type * as http from 'node:http';
|
|
5
6
|
import type { IMastraLogger } from '@mastra/core/logger';
|
|
7
|
+
import type { ListPromptsResult } from '@modelcontextprotocol/sdk/types.js';
|
|
6
8
|
import { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
|
|
7
9
|
import { MastraBase } from '@mastra/core/base';
|
|
8
10
|
import { MCPServerBase } from '@mastra/core/mcp';
|
|
@@ -13,6 +15,8 @@ import type { MCPToolType } from '@mastra/core/mcp';
|
|
|
13
15
|
import { objectInputType } from 'zod';
|
|
14
16
|
import { objectOutputType } from 'zod';
|
|
15
17
|
import { objectUtil } from 'zod';
|
|
18
|
+
import type { Prompt } from '@modelcontextprotocol/sdk/types.js';
|
|
19
|
+
import type { PromptMessage } from '@modelcontextprotocol/sdk/types.js';
|
|
16
20
|
import type { Resource } from '@modelcontextprotocol/sdk/types.js';
|
|
17
21
|
import type { ResourceTemplate } from '@modelcontextprotocol/sdk/types.js';
|
|
18
22
|
import { ResourceUpdatedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
@@ -70,6 +74,7 @@ export declare class InternalMastraMCPClient extends MastraBase {
|
|
|
70
74
|
private transport?;
|
|
71
75
|
private currentOperationContext;
|
|
72
76
|
readonly resources: ResourceClientActions;
|
|
77
|
+
readonly prompts: PromptClientActions;
|
|
73
78
|
constructor({ name, version, server, capabilities, timeout, }: InternalMastraMCPClientOptions);
|
|
74
79
|
/**
|
|
75
80
|
* Log a message at the specified level
|
|
@@ -170,6 +175,26 @@ export declare class InternalMastraMCPClient extends MastraBase {
|
|
|
170
175
|
mimeType: z.ZodOptional<z.ZodString>;
|
|
171
176
|
}, z.ZodTypeAny, "passthrough">>, "many">;
|
|
172
177
|
}, z.ZodTypeAny, "passthrough">>;
|
|
178
|
+
/**
|
|
179
|
+
* Fetch the list of available prompts from the MCP server.
|
|
180
|
+
*/
|
|
181
|
+
listPrompts(): Promise<ListPromptsResult>;
|
|
182
|
+
/**
|
|
183
|
+
* Get a prompt and its dynamic messages from the server.
|
|
184
|
+
* @param name The prompt name
|
|
185
|
+
* @param args Arguments for the prompt
|
|
186
|
+
* @param version (optional) The prompt version to retrieve
|
|
187
|
+
*/
|
|
188
|
+
getPrompt({ name, args, version }: {
|
|
189
|
+
name: string;
|
|
190
|
+
args?: Record<string, any>;
|
|
191
|
+
version?: string;
|
|
192
|
+
}): Promise<GetPromptResult>;
|
|
193
|
+
/**
|
|
194
|
+
* Register a handler to be called when the prompt list changes on the server.
|
|
195
|
+
* Use this to refresh cached prompt lists in the client/UI if needed.
|
|
196
|
+
*/
|
|
197
|
+
setPromptListChangedNotificationHandler(handler: () => void): void;
|
|
173
198
|
setResourceUpdatedNotificationHandler(handler: (params: z.infer<typeof ResourceUpdatedNotificationSchema>['params']) => void): void;
|
|
174
199
|
setResourceListChangedNotificationHandler(handler: () => void): void;
|
|
175
200
|
private convertInputSchema;
|
|
@@ -186,10 +211,12 @@ export declare type InternalMastraMCPClientOptions = {
|
|
|
186
211
|
|
|
187
212
|
export { LoggingLevel }
|
|
188
213
|
export { LoggingLevel as LoggingLevel_alias_1 }
|
|
214
|
+
export { LoggingLevel as LoggingLevel_alias_2 }
|
|
189
215
|
|
|
190
216
|
declare type LogHandler = (logMessage: LogMessage) => void;
|
|
191
217
|
export { LogHandler }
|
|
192
218
|
export { LogHandler as LogHandler_alias_1 }
|
|
219
|
+
export { LogHandler as LogHandler_alias_2 }
|
|
193
220
|
|
|
194
221
|
declare interface LogMessage {
|
|
195
222
|
level: LoggingLevel;
|
|
@@ -201,6 +228,7 @@ declare interface LogMessage {
|
|
|
201
228
|
}
|
|
202
229
|
export { LogMessage }
|
|
203
230
|
export { LogMessage as LogMessage_alias_1 }
|
|
231
|
+
export { LogMessage as LogMessage_alias_2 }
|
|
204
232
|
|
|
205
233
|
/**
|
|
206
234
|
* @deprecated MastraMCPClient is deprecated and will be removed in a future release. Please use MCPClient instead.
|
|
@@ -210,10 +238,12 @@ declare class MastraMCPClient extends InternalMastraMCPClient {
|
|
|
210
238
|
}
|
|
211
239
|
export { MastraMCPClient }
|
|
212
240
|
export { MastraMCPClient as MastraMCPClient_alias_1 }
|
|
241
|
+
export { MastraMCPClient as MastraMCPClient_alias_2 }
|
|
213
242
|
|
|
214
243
|
declare type MastraMCPServerDefinition = StdioServerDefinition | HttpServerDefinition;
|
|
215
244
|
export { MastraMCPServerDefinition }
|
|
216
245
|
export { MastraMCPServerDefinition as MastraMCPServerDefinition_alias_1 }
|
|
246
|
+
export { MastraMCPServerDefinition as MastraMCPServerDefinition_alias_2 }
|
|
217
247
|
|
|
218
248
|
declare class MCPClient extends MastraBase {
|
|
219
249
|
private serverConfigs;
|
|
@@ -267,6 +297,55 @@ declare class MCPClient extends MastraBase {
|
|
|
267
297
|
}) => void) => Promise<void>;
|
|
268
298
|
onListChanged: (serverName: string, handler: () => void) => Promise<void>;
|
|
269
299
|
};
|
|
300
|
+
get prompts(): {
|
|
301
|
+
list: () => Promise<Record<string, Prompt[]>>;
|
|
302
|
+
get: ({ serverName, name, args, version }: {
|
|
303
|
+
serverName: string;
|
|
304
|
+
name: string;
|
|
305
|
+
args?: Record<string, any>;
|
|
306
|
+
version?: string;
|
|
307
|
+
}) => Promise<{
|
|
308
|
+
[x: string]: unknown;
|
|
309
|
+
messages: {
|
|
310
|
+
[x: string]: unknown;
|
|
311
|
+
role: "user" | "assistant";
|
|
312
|
+
content: {
|
|
313
|
+
[x: string]: unknown;
|
|
314
|
+
type: "text";
|
|
315
|
+
text: string;
|
|
316
|
+
} | {
|
|
317
|
+
[x: string]: unknown;
|
|
318
|
+
type: "image";
|
|
319
|
+
data: string;
|
|
320
|
+
mimeType: string;
|
|
321
|
+
} | {
|
|
322
|
+
[x: string]: unknown;
|
|
323
|
+
type: "audio";
|
|
324
|
+
data: string;
|
|
325
|
+
mimeType: string;
|
|
326
|
+
} | {
|
|
327
|
+
[x: string]: unknown;
|
|
328
|
+
type: "resource";
|
|
329
|
+
resource: {
|
|
330
|
+
[x: string]: unknown;
|
|
331
|
+
text: string;
|
|
332
|
+
uri: string;
|
|
333
|
+
mimeType?: string | undefined;
|
|
334
|
+
} | {
|
|
335
|
+
[x: string]: unknown;
|
|
336
|
+
uri: string;
|
|
337
|
+
blob: string;
|
|
338
|
+
mimeType?: string | undefined;
|
|
339
|
+
};
|
|
340
|
+
};
|
|
341
|
+
}[];
|
|
342
|
+
description?: string | undefined;
|
|
343
|
+
_meta?: {
|
|
344
|
+
[x: string]: unknown;
|
|
345
|
+
} | undefined;
|
|
346
|
+
}>;
|
|
347
|
+
onListChanged: (serverName: string, handler: () => void) => Promise<void>;
|
|
348
|
+
};
|
|
270
349
|
private addToInstanceCache;
|
|
271
350
|
private makeId;
|
|
272
351
|
disconnect(): Promise<void>;
|
|
@@ -277,8 +356,8 @@ declare class MCPClient extends MastraBase {
|
|
|
277
356
|
*/
|
|
278
357
|
getResources(): Promise<Record<string, {
|
|
279
358
|
[x: string]: unknown;
|
|
280
|
-
uri: string;
|
|
281
359
|
name: string;
|
|
360
|
+
uri: string;
|
|
282
361
|
description?: string | undefined;
|
|
283
362
|
mimeType?: string | undefined;
|
|
284
363
|
}[]>>;
|
|
@@ -293,6 +372,7 @@ declare class MCPClient extends MastraBase {
|
|
|
293
372
|
}
|
|
294
373
|
export { MCPClient }
|
|
295
374
|
export { MCPClient as MCPClient_alias_1 }
|
|
375
|
+
export { MCPClient as MCPClient_alias_2 }
|
|
296
376
|
|
|
297
377
|
declare interface MCPClientOptions {
|
|
298
378
|
id?: string;
|
|
@@ -301,6 +381,7 @@ declare interface MCPClientOptions {
|
|
|
301
381
|
}
|
|
302
382
|
export { MCPClientOptions }
|
|
303
383
|
export { MCPClientOptions as MCPClientOptions_alias_1 }
|
|
384
|
+
export { MCPClientOptions as MCPClientOptions_alias_2 }
|
|
304
385
|
|
|
305
386
|
/**
|
|
306
387
|
* @deprecated MCPConfiguration is deprecated and will be removed in a future release. Use MCPClient instead.
|
|
@@ -310,6 +391,7 @@ declare class MCPConfiguration extends MCPClient {
|
|
|
310
391
|
}
|
|
311
392
|
export { MCPConfiguration }
|
|
312
393
|
export { MCPConfiguration as MCPConfiguration_alias_1 }
|
|
394
|
+
export { MCPConfiguration as MCPConfiguration_alias_2 }
|
|
313
395
|
|
|
314
396
|
/**
|
|
315
397
|
* @deprecated MCPConfigurationOptions is deprecated and will be removed in a future release. Use MCPClientOptions instead.
|
|
@@ -321,6 +403,7 @@ declare interface MCPConfigurationOptions {
|
|
|
321
403
|
}
|
|
322
404
|
export { MCPConfigurationOptions }
|
|
323
405
|
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
|
|
406
|
+
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_2 }
|
|
324
407
|
|
|
325
408
|
declare class MCPServer extends MCPServerBase {
|
|
326
409
|
private server;
|
|
@@ -335,11 +418,16 @@ declare class MCPServer extends MCPServerBase {
|
|
|
335
418
|
private listResourceTemplatesHandlerIsRegistered;
|
|
336
419
|
private subscribeResourceHandlerIsRegistered;
|
|
337
420
|
private unsubscribeResourceHandlerIsRegistered;
|
|
421
|
+
private listPromptsHandlerIsRegistered;
|
|
422
|
+
private getPromptHandlerIsRegistered;
|
|
338
423
|
private definedResources?;
|
|
339
424
|
private definedResourceTemplates?;
|
|
340
425
|
private resourceOptions?;
|
|
426
|
+
private definedPrompts?;
|
|
427
|
+
private promptOptions?;
|
|
341
428
|
private subscriptions;
|
|
342
429
|
readonly resources: ServerResourceActions;
|
|
430
|
+
readonly prompts: ServerPromptActions;
|
|
343
431
|
/**
|
|
344
432
|
* Get the current stdio transport.
|
|
345
433
|
*/
|
|
@@ -366,6 +454,7 @@ declare class MCPServer extends MCPServerBase {
|
|
|
366
454
|
*/
|
|
367
455
|
constructor(opts: MCPServerConfig & {
|
|
368
456
|
resources?: MCPServerResources;
|
|
457
|
+
prompts?: MCPServerPrompts;
|
|
369
458
|
});
|
|
370
459
|
private convertAgentsToTools;
|
|
371
460
|
private convertWorkflowsToTools;
|
|
@@ -406,6 +495,14 @@ declare class MCPServer extends MCPServerBase {
|
|
|
406
495
|
* Register the UnsubscribeResource handler.
|
|
407
496
|
*/
|
|
408
497
|
private registerUnsubscribeResourceHandler;
|
|
498
|
+
/**
|
|
499
|
+
* Register the ListPrompts handler.
|
|
500
|
+
*/
|
|
501
|
+
private registerListPromptsHandler;
|
|
502
|
+
/**
|
|
503
|
+
* Register the GetPrompt handler.
|
|
504
|
+
*/
|
|
505
|
+
private registerGetPromptHandler;
|
|
409
506
|
/**
|
|
410
507
|
* Start the MCP server using stdio transport (for Windsurf integration).
|
|
411
508
|
*/
|
|
@@ -509,9 +606,27 @@ declare class MCPServer extends MCPServerBase {
|
|
|
509
606
|
}
|
|
510
607
|
export { MCPServer }
|
|
511
608
|
export { MCPServer as MCPServer_alias_1 }
|
|
609
|
+
export { MCPServer as MCPServer_alias_2 }
|
|
512
610
|
|
|
513
611
|
export declare const mcpServerName = "firecrawl-mcp-fixture";
|
|
514
612
|
|
|
613
|
+
declare type MCPServerPromptMessagesCallback = ({ name, version, args, }: {
|
|
614
|
+
name: string;
|
|
615
|
+
version?: string;
|
|
616
|
+
args?: any;
|
|
617
|
+
}) => Promise<PromptMessage[]>;
|
|
618
|
+
export { MCPServerPromptMessagesCallback }
|
|
619
|
+
export { MCPServerPromptMessagesCallback as MCPServerPromptMessagesCallback_alias_1 }
|
|
620
|
+
export { MCPServerPromptMessagesCallback as MCPServerPromptMessagesCallback_alias_2 }
|
|
621
|
+
|
|
622
|
+
declare type MCPServerPrompts = {
|
|
623
|
+
listPrompts: () => Promise<Prompt[]>;
|
|
624
|
+
getPromptMessages?: MCPServerPromptMessagesCallback;
|
|
625
|
+
};
|
|
626
|
+
export { MCPServerPrompts }
|
|
627
|
+
export { MCPServerPrompts as MCPServerPrompts_alias_1 }
|
|
628
|
+
export { MCPServerPrompts as MCPServerPrompts_alias_2 }
|
|
629
|
+
|
|
515
630
|
declare type MCPServerResourceContent = {
|
|
516
631
|
text?: string;
|
|
517
632
|
} | {
|
|
@@ -519,12 +634,14 @@ declare type MCPServerResourceContent = {
|
|
|
519
634
|
};
|
|
520
635
|
export { MCPServerResourceContent }
|
|
521
636
|
export { MCPServerResourceContent as MCPServerResourceContent_alias_1 }
|
|
637
|
+
export { MCPServerResourceContent as MCPServerResourceContent_alias_2 }
|
|
522
638
|
|
|
523
639
|
declare type MCPServerResourceContentCallback = ({ uri, }: {
|
|
524
640
|
uri: string;
|
|
525
641
|
}) => Promise<MCPServerResourceContent | MCPServerResourceContent[]>;
|
|
526
642
|
export { MCPServerResourceContentCallback }
|
|
527
643
|
export { MCPServerResourceContentCallback as MCPServerResourceContentCallback_alias_1 }
|
|
644
|
+
export { MCPServerResourceContentCallback as MCPServerResourceContentCallback_alias_2 }
|
|
528
645
|
|
|
529
646
|
declare type MCPServerResources = {
|
|
530
647
|
listResources: () => Promise<Resource[]>;
|
|
@@ -533,9 +650,47 @@ declare type MCPServerResources = {
|
|
|
533
650
|
};
|
|
534
651
|
export { MCPServerResources }
|
|
535
652
|
export { MCPServerResources as MCPServerResources_alias_1 }
|
|
653
|
+
export { MCPServerResources as MCPServerResources_alias_2 }
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Client-side prompt actions for listing, getting, and subscribing to prompt changes.
|
|
657
|
+
*/
|
|
658
|
+
export declare class PromptClientActions {
|
|
659
|
+
private readonly client;
|
|
660
|
+
private readonly logger;
|
|
661
|
+
constructor({ client, logger }: PromptClientActionsConfig);
|
|
662
|
+
/**
|
|
663
|
+
* Get all prompts from the connected MCP server.
|
|
664
|
+
* @returns A list of prompts with their versions.
|
|
665
|
+
*/
|
|
666
|
+
list(): Promise<Prompt[]>;
|
|
667
|
+
/**
|
|
668
|
+
* Get a specific prompt.
|
|
669
|
+
* @param name The name of the prompt to get.
|
|
670
|
+
* @param args Optional arguments for the prompt.
|
|
671
|
+
* @param version Optional version of the prompt to get.
|
|
672
|
+
* @returns The prompt content.
|
|
673
|
+
*/
|
|
674
|
+
get({ name, args, version }: {
|
|
675
|
+
name: string;
|
|
676
|
+
args?: Record<string, any>;
|
|
677
|
+
version?: string;
|
|
678
|
+
}): Promise<GetPromptResult>;
|
|
679
|
+
/**
|
|
680
|
+
* Set a notification handler for when the list of available prompts changes.
|
|
681
|
+
* @param handler The callback function to handle the notification.
|
|
682
|
+
*/
|
|
683
|
+
onListChanged(handler: () => void): Promise<void>;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
declare interface PromptClientActionsConfig {
|
|
687
|
+
client: InternalMastraMCPClient;
|
|
688
|
+
logger: IMastraLogger;
|
|
689
|
+
}
|
|
536
690
|
|
|
537
691
|
export { Resource }
|
|
538
692
|
export { Resource as Resource_alias_1 }
|
|
693
|
+
export { Resource as Resource_alias_2 }
|
|
539
694
|
|
|
540
695
|
export declare class ResourceClientActions {
|
|
541
696
|
private readonly client;
|
|
@@ -622,6 +777,7 @@ declare interface ResourceClientActionsConfig {
|
|
|
622
777
|
|
|
623
778
|
export { ResourceTemplate }
|
|
624
779
|
export { ResourceTemplate as ResourceTemplate_alias_1 }
|
|
780
|
+
export { ResourceTemplate as ResourceTemplate_alias_2 }
|
|
625
781
|
|
|
626
782
|
export declare const server: Server<{
|
|
627
783
|
method: string;
|
|
@@ -649,6 +805,24 @@ export declare const server: Server<{
|
|
|
649
805
|
|
|
650
806
|
export declare const server_alias_1: MCPServer;
|
|
651
807
|
|
|
808
|
+
export declare class ServerPromptActions {
|
|
809
|
+
private readonly getLogger;
|
|
810
|
+
private readonly getSdkServer;
|
|
811
|
+
private readonly clearDefinedPrompts;
|
|
812
|
+
constructor(dependencies: ServerPromptActionsDependencies);
|
|
813
|
+
/**
|
|
814
|
+
* Notifies the server that the overall list of available prompts has changed.
|
|
815
|
+
* This will clear the internal cache of defined prompts and send a list_changed notification to clients.
|
|
816
|
+
*/
|
|
817
|
+
notifyListChanged(): Promise<void>;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
declare interface ServerPromptActionsDependencies {
|
|
821
|
+
getLogger: () => IMastraLogger;
|
|
822
|
+
getSdkServer: () => Server;
|
|
823
|
+
clearDefinedPrompts: () => void;
|
|
824
|
+
}
|
|
825
|
+
|
|
652
826
|
export declare class ServerResourceActions {
|
|
653
827
|
private readonly getSubscriptions;
|
|
654
828
|
private readonly getLogger;
|
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,58 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
30
30
|
var equal__default = /*#__PURE__*/_interopDefault(equal);
|
|
31
31
|
|
|
32
32
|
// src/client/client.ts
|
|
33
|
+
var PromptClientActions = class {
|
|
34
|
+
client;
|
|
35
|
+
logger;
|
|
36
|
+
constructor({ client, logger }) {
|
|
37
|
+
this.client = client;
|
|
38
|
+
this.logger = logger;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get all prompts from the connected MCP server.
|
|
42
|
+
* @returns A list of prompts with their versions.
|
|
43
|
+
*/
|
|
44
|
+
async list() {
|
|
45
|
+
try {
|
|
46
|
+
const response = await this.client.listPrompts();
|
|
47
|
+
if (response && response.prompts && Array.isArray(response.prompts)) {
|
|
48
|
+
return response.prompts.map((prompt) => ({ ...prompt, version: prompt.version || "" }));
|
|
49
|
+
} else {
|
|
50
|
+
this.logger.warn(`Prompts response from server ${this.client.name} did not have expected structure.`, {
|
|
51
|
+
response
|
|
52
|
+
});
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
} catch (e) {
|
|
56
|
+
if (e.code === types_js.ErrorCode.MethodNotFound) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
this.logger.error(`Error getting prompts from server ${this.client.name}`, {
|
|
60
|
+
error: e instanceof Error ? e.message : String(e)
|
|
61
|
+
});
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Failed to fetch prompts from server ${this.client.name}: ${e instanceof Error ? e.stack || e.message : String(e)}`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get a specific prompt.
|
|
69
|
+
* @param name The name of the prompt to get.
|
|
70
|
+
* @param args Optional arguments for the prompt.
|
|
71
|
+
* @param version Optional version of the prompt to get.
|
|
72
|
+
* @returns The prompt content.
|
|
73
|
+
*/
|
|
74
|
+
async get({ name, args, version }) {
|
|
75
|
+
return this.client.getPrompt({ name, args, version });
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Set a notification handler for when the list of available prompts changes.
|
|
79
|
+
* @param handler The callback function to handle the notification.
|
|
80
|
+
*/
|
|
81
|
+
async onListChanged(handler) {
|
|
82
|
+
this.client.setPromptListChangedNotificationHandler(handler);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
33
85
|
var ResourceClientActions = class {
|
|
34
86
|
client;
|
|
35
87
|
logger;
|
|
@@ -59,7 +111,6 @@ var ResourceClientActions = class {
|
|
|
59
111
|
this.logger.error(`Error getting resources from server ${this.client.name}`, {
|
|
60
112
|
error: e instanceof Error ? e.message : String(e)
|
|
61
113
|
});
|
|
62
|
-
console.log("errorheere", e);
|
|
63
114
|
throw new Error(
|
|
64
115
|
`Failed to fetch resources from server ${this.client.name}: ${e instanceof Error ? e.stack || e.message : String(e)}`
|
|
65
116
|
);
|
|
@@ -82,7 +133,6 @@ var ResourceClientActions = class {
|
|
|
82
133
|
return [];
|
|
83
134
|
}
|
|
84
135
|
} catch (e) {
|
|
85
|
-
console.log({ errorcooode: e.code });
|
|
86
136
|
if (e.code === types_js.ErrorCode.MethodNotFound) {
|
|
87
137
|
return [];
|
|
88
138
|
}
|
|
@@ -161,6 +211,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
161
211
|
transport;
|
|
162
212
|
currentOperationContext = null;
|
|
163
213
|
resources;
|
|
214
|
+
prompts;
|
|
164
215
|
constructor({
|
|
165
216
|
name,
|
|
166
217
|
version = "1.0.0",
|
|
@@ -185,6 +236,7 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
185
236
|
);
|
|
186
237
|
this.setupLogging();
|
|
187
238
|
this.resources = new ResourceClientActions({ client: this, logger: this.logger });
|
|
239
|
+
this.prompts = new PromptClientActions({ client: this, logger: this.logger });
|
|
188
240
|
}
|
|
189
241
|
/**
|
|
190
242
|
* Log a message at the specified level
|
|
@@ -368,6 +420,39 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
368
420
|
timeout: this.timeout
|
|
369
421
|
});
|
|
370
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Fetch the list of available prompts from the MCP server.
|
|
425
|
+
*/
|
|
426
|
+
async listPrompts() {
|
|
427
|
+
this.log("debug", `Requesting prompts from MCP server`);
|
|
428
|
+
return await this.client.request({ method: "prompts/list" }, types_js.ListPromptsResultSchema, {
|
|
429
|
+
timeout: this.timeout
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Get a prompt and its dynamic messages from the server.
|
|
434
|
+
* @param name The prompt name
|
|
435
|
+
* @param args Arguments for the prompt
|
|
436
|
+
* @param version (optional) The prompt version to retrieve
|
|
437
|
+
*/
|
|
438
|
+
async getPrompt({ name, args, version }) {
|
|
439
|
+
this.log("debug", `Requesting prompt from MCP server: ${name}`);
|
|
440
|
+
return await this.client.request(
|
|
441
|
+
{ method: "prompts/get", params: { name, arguments: args, version } },
|
|
442
|
+
types_js.GetPromptResultSchema,
|
|
443
|
+
{ timeout: this.timeout }
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Register a handler to be called when the prompt list changes on the server.
|
|
448
|
+
* Use this to refresh cached prompt lists in the client/UI if needed.
|
|
449
|
+
*/
|
|
450
|
+
setPromptListChangedNotificationHandler(handler) {
|
|
451
|
+
this.log("debug", "Setting prompt list changed notification handler");
|
|
452
|
+
this.client.setNotificationHandler(types_js.PromptListChangedNotificationSchema, () => {
|
|
453
|
+
handler();
|
|
454
|
+
});
|
|
455
|
+
}
|
|
371
456
|
setResourceUpdatedNotificationHandler(handler) {
|
|
372
457
|
this.log("debug", "Setting resource updated notification handler");
|
|
373
458
|
this.client.setNotificationHandler(types_js.ResourceUpdatedNotificationSchema, (notification) => {
|
|
@@ -557,6 +642,31 @@ To fix this you have three different options:
|
|
|
557
642
|
}
|
|
558
643
|
};
|
|
559
644
|
}
|
|
645
|
+
get prompts() {
|
|
646
|
+
this.addToInstanceCache();
|
|
647
|
+
return {
|
|
648
|
+
list: async () => {
|
|
649
|
+
const allPrompts = {};
|
|
650
|
+
for (const serverName of Object.keys(this.serverConfigs)) {
|
|
651
|
+
try {
|
|
652
|
+
const internalClient = await this.getConnectedClientForServer(serverName);
|
|
653
|
+
allPrompts[serverName] = await internalClient.prompts.list();
|
|
654
|
+
} catch (error) {
|
|
655
|
+
this.logger.error(`Failed to list prompts from server ${serverName}`, { error });
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return allPrompts;
|
|
659
|
+
},
|
|
660
|
+
get: async ({ serverName, name, args, version }) => {
|
|
661
|
+
const internalClient = await this.getConnectedClientForServer(serverName);
|
|
662
|
+
return internalClient.prompts.get({ name, args, version });
|
|
663
|
+
},
|
|
664
|
+
onListChanged: async (serverName, handler) => {
|
|
665
|
+
const internalClient = await this.getConnectedClientForServer(serverName);
|
|
666
|
+
return internalClient.prompts.onListChanged(handler);
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
}
|
|
560
670
|
addToInstanceCache() {
|
|
561
671
|
if (!mcpClientInstances.has(this.id)) {
|
|
562
672
|
mcpClientInstances.set(this.id, this);
|
|
@@ -765,6 +875,34 @@ var SSETransport = class {
|
|
|
765
875
|
}
|
|
766
876
|
};
|
|
767
877
|
|
|
878
|
+
// src/server/promptActions.ts
|
|
879
|
+
var ServerPromptActions = class {
|
|
880
|
+
getLogger;
|
|
881
|
+
getSdkServer;
|
|
882
|
+
clearDefinedPrompts;
|
|
883
|
+
constructor(dependencies) {
|
|
884
|
+
this.getLogger = dependencies.getLogger;
|
|
885
|
+
this.getSdkServer = dependencies.getSdkServer;
|
|
886
|
+
this.clearDefinedPrompts = dependencies.clearDefinedPrompts;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Notifies the server that the overall list of available prompts has changed.
|
|
890
|
+
* This will clear the internal cache of defined prompts and send a list_changed notification to clients.
|
|
891
|
+
*/
|
|
892
|
+
async notifyListChanged() {
|
|
893
|
+
this.getLogger().info("Prompt list change externally notified. Clearing definedPrompts and sending notification.");
|
|
894
|
+
this.clearDefinedPrompts();
|
|
895
|
+
try {
|
|
896
|
+
await this.getSdkServer().sendPromptListChanged();
|
|
897
|
+
} catch (error) {
|
|
898
|
+
this.getLogger().error("Failed to send prompt list changed notification:", {
|
|
899
|
+
error: error instanceof Error ? error.message : String(error)
|
|
900
|
+
});
|
|
901
|
+
throw error;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
|
|
768
906
|
// src/server/resourceActions.ts
|
|
769
907
|
var ServerResourceActions = class {
|
|
770
908
|
getSubscriptions;
|
|
@@ -829,11 +967,16 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
829
967
|
listResourceTemplatesHandlerIsRegistered = false;
|
|
830
968
|
subscribeResourceHandlerIsRegistered = false;
|
|
831
969
|
unsubscribeResourceHandlerIsRegistered = false;
|
|
970
|
+
listPromptsHandlerIsRegistered = false;
|
|
971
|
+
getPromptHandlerIsRegistered = false;
|
|
832
972
|
definedResources;
|
|
833
973
|
definedResourceTemplates;
|
|
834
974
|
resourceOptions;
|
|
975
|
+
definedPrompts;
|
|
976
|
+
promptOptions;
|
|
835
977
|
subscriptions = /* @__PURE__ */ new Set();
|
|
836
978
|
resources;
|
|
979
|
+
prompts;
|
|
837
980
|
/**
|
|
838
981
|
* Get the current stdio transport.
|
|
839
982
|
*/
|
|
@@ -871,6 +1014,7 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
871
1014
|
constructor(opts) {
|
|
872
1015
|
super(opts);
|
|
873
1016
|
this.resourceOptions = opts.resources;
|
|
1017
|
+
this.promptOptions = opts.prompts;
|
|
874
1018
|
const capabilities = {
|
|
875
1019
|
tools: {},
|
|
876
1020
|
logging: { enabled: true }
|
|
@@ -878,6 +1022,9 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
878
1022
|
if (opts.resources) {
|
|
879
1023
|
capabilities.resources = { subscribe: true, listChanged: true };
|
|
880
1024
|
}
|
|
1025
|
+
if (opts.prompts) {
|
|
1026
|
+
capabilities.prompts = { listChanged: true };
|
|
1027
|
+
}
|
|
881
1028
|
this.server = new index_js.Server({ name: this.name, version: this.version }, { capabilities });
|
|
882
1029
|
this.logger.info(
|
|
883
1030
|
`Initialized MCPServer '${this.name}' v${this.version} (ID: ${this.id}) with tools: ${Object.keys(this.convertedTools).join(", ")} and resources. Capabilities: ${JSON.stringify(capabilities)}`
|
|
@@ -894,6 +1041,12 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
894
1041
|
this.registerListResourceTemplatesHandler();
|
|
895
1042
|
}
|
|
896
1043
|
}
|
|
1044
|
+
if (opts.prompts) {
|
|
1045
|
+
this.registerListPromptsHandler();
|
|
1046
|
+
this.registerGetPromptHandler({
|
|
1047
|
+
getPromptMessagesCallback: opts.prompts.getPromptMessages
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
897
1050
|
this.resources = new ServerResourceActions({
|
|
898
1051
|
getSubscriptions: () => this.subscriptions,
|
|
899
1052
|
getLogger: () => this.logger,
|
|
@@ -905,6 +1058,13 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
905
1058
|
this.definedResourceTemplates = void 0;
|
|
906
1059
|
}
|
|
907
1060
|
});
|
|
1061
|
+
this.prompts = new ServerPromptActions({
|
|
1062
|
+
getLogger: () => this.logger,
|
|
1063
|
+
getSdkServer: () => this.server,
|
|
1064
|
+
clearDefinedPrompts: () => {
|
|
1065
|
+
this.definedPrompts = void 0;
|
|
1066
|
+
}
|
|
1067
|
+
});
|
|
908
1068
|
}
|
|
909
1069
|
convertAgentsToTools(agentsConfig, definedConvertedTools) {
|
|
910
1070
|
const agentTools = {};
|
|
@@ -1331,6 +1491,95 @@ var MCPServer = class extends mcp.MCPServerBase {
|
|
|
1331
1491
|
return {};
|
|
1332
1492
|
});
|
|
1333
1493
|
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Register the ListPrompts handler.
|
|
1496
|
+
*/
|
|
1497
|
+
registerListPromptsHandler() {
|
|
1498
|
+
if (this.listPromptsHandlerIsRegistered) {
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
this.listPromptsHandlerIsRegistered = true;
|
|
1502
|
+
const capturedPromptOptions = this.promptOptions;
|
|
1503
|
+
if (!capturedPromptOptions?.listPrompts) {
|
|
1504
|
+
this.logger.warn("ListPrompts capability not supported by server configuration.");
|
|
1505
|
+
return;
|
|
1506
|
+
}
|
|
1507
|
+
this.server.setRequestHandler(types_js.ListPromptsRequestSchema, async () => {
|
|
1508
|
+
this.logger.debug("Handling ListPrompts request");
|
|
1509
|
+
if (this.definedPrompts) {
|
|
1510
|
+
return {
|
|
1511
|
+
prompts: this.definedPrompts?.map((p) => ({ ...p, version: p.version ?? void 0 }))
|
|
1512
|
+
};
|
|
1513
|
+
} else {
|
|
1514
|
+
try {
|
|
1515
|
+
const prompts = await capturedPromptOptions.listPrompts();
|
|
1516
|
+
for (const prompt of prompts) {
|
|
1517
|
+
types_js.PromptSchema.parse(prompt);
|
|
1518
|
+
}
|
|
1519
|
+
this.definedPrompts = prompts;
|
|
1520
|
+
this.logger.debug(`Fetched and cached ${this.definedPrompts.length} prompts.`);
|
|
1521
|
+
return {
|
|
1522
|
+
prompts: this.definedPrompts?.map((p) => ({ ...p, version: p.version ?? void 0 }))
|
|
1523
|
+
};
|
|
1524
|
+
} catch (error) {
|
|
1525
|
+
this.logger.error("Error fetching prompts via listPrompts():", {
|
|
1526
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1527
|
+
});
|
|
1528
|
+
throw error;
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* Register the GetPrompt handler.
|
|
1535
|
+
*/
|
|
1536
|
+
registerGetPromptHandler({
|
|
1537
|
+
getPromptMessagesCallback
|
|
1538
|
+
}) {
|
|
1539
|
+
if (this.getPromptHandlerIsRegistered) return;
|
|
1540
|
+
this.getPromptHandlerIsRegistered = true;
|
|
1541
|
+
this.server.setRequestHandler(
|
|
1542
|
+
types_js.GetPromptRequestSchema,
|
|
1543
|
+
async (request) => {
|
|
1544
|
+
const startTime = Date.now();
|
|
1545
|
+
const { name, version, arguments: args } = request.params;
|
|
1546
|
+
if (!this.definedPrompts) {
|
|
1547
|
+
const prompts = await this.promptOptions?.listPrompts?.();
|
|
1548
|
+
if (!prompts) throw new Error("Failed to load prompts");
|
|
1549
|
+
this.definedPrompts = prompts;
|
|
1550
|
+
}
|
|
1551
|
+
let prompt;
|
|
1552
|
+
if (version) {
|
|
1553
|
+
prompt = this.definedPrompts?.find((p) => p.name === name && p.version === version);
|
|
1554
|
+
} else {
|
|
1555
|
+
prompt = this.definedPrompts?.find((p) => p.name === name);
|
|
1556
|
+
}
|
|
1557
|
+
if (!prompt) throw new Error(`Prompt "${name}"${version ? ` (version ${version})` : ""} not found`);
|
|
1558
|
+
if (prompt.arguments) {
|
|
1559
|
+
for (const arg of prompt.arguments) {
|
|
1560
|
+
if (arg.required && (args?.[arg.name] === void 0 || args?.[arg.name] === null)) {
|
|
1561
|
+
throw new Error(`Missing required argument: ${arg.name}`);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
try {
|
|
1566
|
+
let messages = [];
|
|
1567
|
+
if (getPromptMessagesCallback) {
|
|
1568
|
+
messages = await getPromptMessagesCallback({ name, version, args });
|
|
1569
|
+
}
|
|
1570
|
+
const duration = Date.now() - startTime;
|
|
1571
|
+
this.logger.info(
|
|
1572
|
+
`Prompt '${name}'${version ? ` (version ${version})` : ""} retrieved successfully in ${duration}ms.`
|
|
1573
|
+
);
|
|
1574
|
+
return { prompt, messages };
|
|
1575
|
+
} catch (error) {
|
|
1576
|
+
const duration = Date.now() - startTime;
|
|
1577
|
+
this.logger.error(`Failed to get content for prompt '${name}' in ${duration}ms`, { error });
|
|
1578
|
+
throw error;
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
);
|
|
1582
|
+
}
|
|
1334
1583
|
/**
|
|
1335
1584
|
* Start the MCP server using stdio transport (for Windsurf integration).
|
|
1336
1585
|
*/
|