@elementor/editor-mcp 3.33.0-271 → 3.33.0-272

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/dist/index.d.mts CHANGED
@@ -1,10 +1,11 @@
1
1
  import * as _elementor_external_angie_sdk from '@elementor-external/angie-sdk';
2
2
  import { AngieMcpSdk } from '@elementor-external/angie-sdk';
3
3
  import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4
- export { McpServer, ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
5
- import { ZodRawShape, z, ZodTypeAny } from '@elementor/schema';
6
- import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
4
+ export { McpServer, RegisteredResource, ResourceTemplate, ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
7
5
  import { ServerRequest, ServerNotification } from '@modelcontextprotocol/sdk/types.js';
6
+ export { SamplingMessageSchema } from '@modelcontextprotocol/sdk/types.js';
7
+ import { z } from '@elementor/schema';
8
+ import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
8
9
 
9
10
  declare function init(): Promise<void>;
10
11
 
@@ -12,22 +13,78 @@ declare const registerMcp: (mcp: McpServer, name: string) => void;
12
13
  declare function activateMcpRegistration(sdk: AngieMcpSdk): Promise<void>;
13
14
  /**
14
15
  *
15
- * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.
16
+ * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.
17
+ * @param options
18
+ * @param options.instructions
16
19
  */
17
- declare const getMCPByDomain: (namespace: string) => MCPRegistryEntry;
20
+ declare const getMCPByDomain: (namespace: string, options?: {
21
+ instructions?: string;
22
+ }) => MCPRegistryEntry;
18
23
  interface MCPRegistryEntry {
19
- addTool: <T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined>(opts: ToolRegistrationOptions<T, O>) => void;
24
+ addTool: <T extends undefined | z.ZodRawShape = undefined, O extends undefined | z.ZodRawShape = undefined>(opts: ToolRegistrationOptions<T, O>) => void;
20
25
  setMCPDescription: (description: string) => void;
26
+ getActiveChatInfo: () => {
27
+ sessionId: string;
28
+ expiresAt: number;
29
+ };
30
+ mcpServer: McpServer;
21
31
  }
22
- type ToolRegistrationOptions<InputArgs extends undefined | ZodRawShape = undefined, OutputSchema extends undefined | ZodRawShape = undefined, ExpectedOutput = OutputSchema extends ZodRawShape ? z.objectOutputType<OutputSchema, ZodTypeAny> : string> = {
32
+ type ResourceList = {
33
+ type: 'resource_link';
34
+ uri: string;
35
+ name: string;
36
+ description: string;
37
+ _meta: Record<string, string>;
38
+ mimeType?: string;
39
+ annotations?: Record<string, unknown>;
40
+ }[];
41
+ type ToolRegistrationOptions<InputArgs extends undefined | z.ZodRawShape = undefined, OutputSchema extends undefined | z.ZodRawShape = undefined, ExpectedOutput = OutputSchema extends z.ZodRawShape ? z.objectOutputType<OutputSchema, z.ZodTypeAny> : string> = {
23
42
  name: string;
24
43
  description: string;
25
44
  schema?: InputArgs;
26
45
  outputSchema?: OutputSchema;
27
- handler: InputArgs extends ZodRawShape ? (args: z.objectOutputType<InputArgs, ZodTypeAny>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput> : (args: unknown, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput>;
46
+ handler: InputArgs extends z.ZodRawShape ? (args: z.objectOutputType<InputArgs, z.ZodTypeAny>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput> : (args: unknown, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput>;
28
47
  isDestrcutive?: boolean;
48
+ resourceList?: ResourceList;
49
+ };
50
+
51
+ type Server = RequestHandlerExtra<ServerRequest, ServerNotification>;
52
+ type Opts = {
53
+ maxTokens?: number;
54
+ modelPreferences?: string;
55
+ model?: string;
56
+ };
57
+ type SamplingOpts = {
58
+ systemPrompt?: string;
59
+ structuredOutput?: z.ZodTypeAny;
60
+ messages: {
61
+ role: 'user' | 'assistant';
62
+ content: {
63
+ type: 'text';
64
+ text: string;
65
+ };
66
+ }[];
29
67
  };
68
+ declare const createSampler: (server: Server, opts?: Opts) => (payload: SamplingOpts) => Promise<any>;
69
+
70
+ declare class ToolPrompts {
71
+ name: string;
72
+ _description: string;
73
+ _parameters: Record<string, string>;
74
+ _examples: string[];
75
+ _furtherInstructions: string[];
76
+ constructor(name: string);
77
+ description(): string;
78
+ description(desc: string): this;
79
+ parameter(key: string): string;
80
+ parameter(key: string, description: string): this;
81
+ instruction(instruction: string): this;
82
+ example(example: string): this;
83
+ get examples(): string;
84
+ prompt(): string;
85
+ }
86
+ declare const toolPrompts: (name: string) => ToolPrompts;
30
87
 
31
88
  declare const getAngieSdk: () => _elementor_external_angie_sdk.AngieMcpSdk;
32
89
 
33
- export { type MCPRegistryEntry, activateMcpRegistration, getAngieSdk, getMCPByDomain, init, registerMcp };
90
+ export { type MCPRegistryEntry, activateMcpRegistration, createSampler, getAngieSdk, getMCPByDomain, init, registerMcp, toolPrompts };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import * as _elementor_external_angie_sdk from '@elementor-external/angie-sdk';
2
2
  import { AngieMcpSdk } from '@elementor-external/angie-sdk';
3
3
  import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4
- export { McpServer, ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
5
- import { ZodRawShape, z, ZodTypeAny } from '@elementor/schema';
6
- import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
4
+ export { McpServer, RegisteredResource, ResourceTemplate, ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
7
5
  import { ServerRequest, ServerNotification } from '@modelcontextprotocol/sdk/types.js';
6
+ export { SamplingMessageSchema } from '@modelcontextprotocol/sdk/types.js';
7
+ import { z } from '@elementor/schema';
8
+ import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
8
9
 
9
10
  declare function init(): Promise<void>;
10
11
 
@@ -12,22 +13,78 @@ declare const registerMcp: (mcp: McpServer, name: string) => void;
12
13
  declare function activateMcpRegistration(sdk: AngieMcpSdk): Promise<void>;
13
14
  /**
14
15
  *
15
- * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.
16
+ * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.
17
+ * @param options
18
+ * @param options.instructions
16
19
  */
17
- declare const getMCPByDomain: (namespace: string) => MCPRegistryEntry;
20
+ declare const getMCPByDomain: (namespace: string, options?: {
21
+ instructions?: string;
22
+ }) => MCPRegistryEntry;
18
23
  interface MCPRegistryEntry {
19
- addTool: <T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined>(opts: ToolRegistrationOptions<T, O>) => void;
24
+ addTool: <T extends undefined | z.ZodRawShape = undefined, O extends undefined | z.ZodRawShape = undefined>(opts: ToolRegistrationOptions<T, O>) => void;
20
25
  setMCPDescription: (description: string) => void;
26
+ getActiveChatInfo: () => {
27
+ sessionId: string;
28
+ expiresAt: number;
29
+ };
30
+ mcpServer: McpServer;
21
31
  }
22
- type ToolRegistrationOptions<InputArgs extends undefined | ZodRawShape = undefined, OutputSchema extends undefined | ZodRawShape = undefined, ExpectedOutput = OutputSchema extends ZodRawShape ? z.objectOutputType<OutputSchema, ZodTypeAny> : string> = {
32
+ type ResourceList = {
33
+ type: 'resource_link';
34
+ uri: string;
35
+ name: string;
36
+ description: string;
37
+ _meta: Record<string, string>;
38
+ mimeType?: string;
39
+ annotations?: Record<string, unknown>;
40
+ }[];
41
+ type ToolRegistrationOptions<InputArgs extends undefined | z.ZodRawShape = undefined, OutputSchema extends undefined | z.ZodRawShape = undefined, ExpectedOutput = OutputSchema extends z.ZodRawShape ? z.objectOutputType<OutputSchema, z.ZodTypeAny> : string> = {
23
42
  name: string;
24
43
  description: string;
25
44
  schema?: InputArgs;
26
45
  outputSchema?: OutputSchema;
27
- handler: InputArgs extends ZodRawShape ? (args: z.objectOutputType<InputArgs, ZodTypeAny>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput> : (args: unknown, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput>;
46
+ handler: InputArgs extends z.ZodRawShape ? (args: z.objectOutputType<InputArgs, z.ZodTypeAny>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput> : (args: unknown, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => ExpectedOutput | Promise<ExpectedOutput>;
28
47
  isDestrcutive?: boolean;
48
+ resourceList?: ResourceList;
49
+ };
50
+
51
+ type Server = RequestHandlerExtra<ServerRequest, ServerNotification>;
52
+ type Opts = {
53
+ maxTokens?: number;
54
+ modelPreferences?: string;
55
+ model?: string;
56
+ };
57
+ type SamplingOpts = {
58
+ systemPrompt?: string;
59
+ structuredOutput?: z.ZodTypeAny;
60
+ messages: {
61
+ role: 'user' | 'assistant';
62
+ content: {
63
+ type: 'text';
64
+ text: string;
65
+ };
66
+ }[];
29
67
  };
68
+ declare const createSampler: (server: Server, opts?: Opts) => (payload: SamplingOpts) => Promise<any>;
69
+
70
+ declare class ToolPrompts {
71
+ name: string;
72
+ _description: string;
73
+ _parameters: Record<string, string>;
74
+ _examples: string[];
75
+ _furtherInstructions: string[];
76
+ constructor(name: string);
77
+ description(): string;
78
+ description(desc: string): this;
79
+ parameter(key: string): string;
80
+ parameter(key: string, description: string): this;
81
+ instruction(instruction: string): this;
82
+ example(example: string): this;
83
+ get examples(): string;
84
+ prompt(): string;
85
+ }
86
+ declare const toolPrompts: (name: string) => ToolPrompts;
30
87
 
31
88
  declare const getAngieSdk: () => _elementor_external_angie_sdk.AngieMcpSdk;
32
89
 
33
- export { type MCPRegistryEntry, activateMcpRegistration, getAngieSdk, getMCPByDomain, init, registerMcp };
90
+ export { type MCPRegistryEntry, activateMcpRegistration, createSampler, getAngieSdk, getMCPByDomain, init, registerMcp, toolPrompts };
package/dist/index.js CHANGED
@@ -21,11 +21,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  McpServer: () => import_mcp2.McpServer,
24
+ ResourceTemplate: () => import_mcp2.ResourceTemplate,
25
+ SamplingMessageSchema: () => import_types2.SamplingMessageSchema,
24
26
  activateMcpRegistration: () => activateMcpRegistration,
27
+ createSampler: () => createSampler,
25
28
  getAngieSdk: () => getAngieSdk,
26
29
  getMCPByDomain: () => getMCPByDomain,
27
30
  init: () => init,
28
- registerMcp: () => registerMcp
31
+ registerMcp: () => registerMcp,
32
+ toolPrompts: () => toolPrompts
29
33
  });
30
34
  module.exports = __toCommonJS(index_exports);
31
35
 
@@ -37,12 +41,26 @@ var import_angie_sdk = require("@elementor-external/angie-sdk");
37
41
  var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
38
42
 
39
43
  // src/test-utils/mock-mcp-registry.ts
44
+ var mock = new Proxy(
45
+ {},
46
+ {
47
+ get: () => {
48
+ function mockedFn(..._) {
49
+ }
50
+ return mockedFn;
51
+ }
52
+ }
53
+ );
40
54
  var mockMcpRegistry = () => {
41
55
  return {
42
56
  addTool: () => {
43
57
  },
44
58
  setMCPDescription: () => {
45
- }
59
+ },
60
+ getActiveChatInfo() {
61
+ return { sessionId: "mock-session-id", expiresAt: Date.now() + 36e5 };
62
+ },
63
+ mcpServer: mock
46
64
  };
47
65
  };
48
66
 
@@ -80,28 +98,49 @@ var isAlphabet = (str) => {
80
98
  }
81
99
  return str;
82
100
  };
83
- var getMCPByDomain = (namespace) => {
101
+ var getMCPByDomain = (namespace, options) => {
84
102
  const mcpName = `editor-${isAlphabet(namespace)}`;
85
103
  if (typeof globalThis.jest !== "undefined") {
86
104
  return mockMcpRegistry();
87
105
  }
88
106
  if (!mcpRegistry[namespace]) {
89
- mcpRegistry[namespace] = new import_mcp.McpServer({
90
- name: mcpName,
91
- version: "1.0.0"
92
- });
107
+ mcpRegistry[namespace] = new import_mcp.McpServer(
108
+ {
109
+ name: mcpName,
110
+ version: "1.0.0"
111
+ },
112
+ {
113
+ instructions: options?.instructions
114
+ }
115
+ );
93
116
  }
94
117
  const mcpServer = mcpRegistry[namespace];
95
118
  const { addTool } = createToolRegistrator(mcpServer);
96
119
  return {
120
+ mcpServer,
97
121
  addTool,
98
122
  setMCPDescription: (description) => {
99
123
  mcpDescriptions[namespace] = description;
124
+ },
125
+ getActiveChatInfo: () => {
126
+ const info = localStorage.getItem("angie_active_chat_id");
127
+ if (!info) {
128
+ return {
129
+ expiresAt: 0,
130
+ sessionId: ""
131
+ };
132
+ }
133
+ const rawData = JSON.parse(info);
134
+ return {
135
+ expiresAt: rawData.expiresAt,
136
+ sessionId: rawData.sessionId
137
+ };
100
138
  }
101
139
  };
102
140
  };
103
141
  function createToolRegistrator(server) {
104
142
  function addTool(opts) {
143
+ const outputSchema = opts.outputSchema;
105
144
  const inputSchema = opts.schema ? opts.schema : {};
106
145
  if (isMcpRegistrationActivated) {
107
146
  throw new Error("MCP Registration is already activated. Cannot add new tools.");
@@ -115,7 +154,8 @@ function createToolRegistrator(server) {
115
154
  {
116
155
  type: "text",
117
156
  text: typeof invocationResult === "string" ? invocationResult : JSON.stringify(invocationResult)
118
- }
157
+ },
158
+ ...opts.resourceList || []
119
159
  ]
120
160
  };
121
161
  } catch (error) {
@@ -135,7 +175,7 @@ function createToolRegistrator(server) {
135
175
  {
136
176
  description: opts.description,
137
177
  inputSchema,
138
- outputSchema: opts.outputSchema,
178
+ outputSchema,
139
179
  title: opts.name,
140
180
  annotations: {
141
181
  destructiveHint: opts.isDestrcutive,
@@ -154,7 +194,8 @@ function createToolRegistrator(server) {
154
194
  // src/init.ts
155
195
  var sdk;
156
196
  var getSDK = () => {
157
- if (typeof globalThis.jest !== "undefined") {
197
+ const isMCPDisabled = !!globalThis.__ELEMENTOR_MCP_DISABLED__;
198
+ if (isMCPDisabled) {
158
199
  return {};
159
200
  }
160
201
  if (!sdk) {
@@ -186,14 +227,126 @@ document.addEventListener(
186
227
 
187
228
  // src/index.ts
188
229
  var import_mcp2 = require("@modelcontextprotocol/sdk/server/mcp.js");
230
+ var import_types2 = require("@modelcontextprotocol/sdk/types.js");
231
+
232
+ // src/sampler.ts
233
+ var import_types = require("@modelcontextprotocol/sdk/types.js");
234
+ var DEFAULT_OPTS = {
235
+ maxTokens: 1e4,
236
+ modelPreferences: "openai",
237
+ model: "gpt-4o"
238
+ };
239
+ var DEFAULT_STRUCTURED_OUTPUT = {
240
+ type: "object",
241
+ properties: {
242
+ content: {
243
+ type: "string",
244
+ description: "Result"
245
+ }
246
+ },
247
+ required: ["content"],
248
+ additionalProperties: false
249
+ };
250
+ var createSampler = (server, opts = DEFAULT_OPTS) => {
251
+ const { maxTokens = 1e3, modelPreferences = "openai", model = "gpt-4o" } = opts;
252
+ const exec = async (payload) => {
253
+ const systemPromptObject = { ...payload.systemPrompt ? { systemPrompt: payload.systemPrompt } : {} };
254
+ const result = await server.sendRequest(
255
+ {
256
+ method: "sampling/createMessage",
257
+ params: {
258
+ maxTokens,
259
+ modelPreferences: {
260
+ hints: [{ name: modelPreferences }]
261
+ },
262
+ metadata: {
263
+ model,
264
+ ...systemPromptObject,
265
+ ...{ structured_output: payload.structuredOutput || DEFAULT_STRUCTURED_OUTPUT }
266
+ },
267
+ messages: payload.messages
268
+ }
269
+ // ...systemPromptObject,
270
+ },
271
+ import_types.SamplingMessageSchema
272
+ );
273
+ try {
274
+ return JSON.parse(result.content.text);
275
+ } catch {
276
+ return result.content;
277
+ }
278
+ };
279
+ return exec;
280
+ };
281
+
282
+ // src/utils/prompt-builder.ts
283
+ var ToolPrompts = class {
284
+ constructor(name) {
285
+ this.name = name;
286
+ }
287
+ _description = "";
288
+ _parameters = {};
289
+ _examples = [];
290
+ _furtherInstructions = [];
291
+ description(desc) {
292
+ if (typeof desc === "undefined") {
293
+ return this._description;
294
+ }
295
+ this._description = desc;
296
+ return this;
297
+ }
298
+ parameter(key, description) {
299
+ if (typeof description === "undefined") {
300
+ return this._parameters[key];
301
+ }
302
+ this._parameters[key] = `**${key}**:
303
+ ${description}`;
304
+ return this;
305
+ }
306
+ instruction(instruction) {
307
+ this._furtherInstructions.push(instruction);
308
+ return this;
309
+ }
310
+ example(example) {
311
+ this._examples.push(example);
312
+ return this;
313
+ }
314
+ get examples() {
315
+ return this._examples.join("\n\n");
316
+ }
317
+ prompt() {
318
+ return `# ${this.name}
319
+ # Description
320
+ ${this._description}
321
+
322
+ ${this._parameters.length ? "# Parameters" : ""}
323
+ ${Object.values(this._parameters).join("\n\n")}
324
+
325
+ ${this._examples.length ? "# Examples" : ""}
326
+ ${this.examples}
327
+
328
+ ${this._furtherInstructions.length ? "# Further Instructions" : ""}
329
+ ${this._furtherInstructions.join("\n\n")}
330
+ `.trim();
331
+ }
332
+ };
333
+ var toolPrompts = (name) => {
334
+ return new ToolPrompts(name);
335
+ };
336
+
337
+ // src/index.ts
189
338
  var getAngieSdk = () => getSDK();
190
339
  // Annotate the CommonJS export names for ESM import in node:
191
340
  0 && (module.exports = {
192
341
  McpServer,
342
+ ResourceTemplate,
343
+ SamplingMessageSchema,
193
344
  activateMcpRegistration,
345
+ createSampler,
194
346
  getAngieSdk,
195
347
  getMCPByDomain,
196
348
  init,
197
- registerMcp
349
+ registerMcp,
350
+ toolPrompts
198
351
  });
199
352
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/init.ts","../src/mcp-registry.ts","../src/test-utils/mock-mcp-registry.ts"],"sourcesContent":["import { getSDK } from './init';\n\nexport { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';\nexport { init } from './init';\nexport * from './mcp-registry';\nexport const getAngieSdk = () => getSDK();\n","import { isExperimentActive } from '@elementor/editor-v1-adapters';\nimport { AngieMcpSdk } from '@elementor-external/angie-sdk';\n\nimport { activateMcpRegistration } from './mcp-registry';\n\nlet sdk: AngieMcpSdk;\n\nexport const getSDK = () => {\n\t// @ts-ignore - QUnit fails this\n\tif ( typeof globalThis.jest !== 'undefined' ) {\n\t\treturn {} as unknown as AngieMcpSdk;\n\t}\n\tif ( ! sdk ) {\n\t\tsdk = new AngieMcpSdk();\n\t}\n\treturn sdk;\n};\n\nexport function init() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK().waitForReady();\n\t}\n\treturn Promise.resolve();\n}\n\nexport function startMCPServer() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK()\n\t\t\t.waitForReady()\n\t\t\t.then( () => activateMcpRegistration( sdk ) );\n\t}\n\treturn Promise.resolve();\n}\n\ndocument.addEventListener(\n\t'DOMContentLoaded',\n\t() => {\n\t\tstartMCPServer();\n\t},\n\t{\n\t\tonce: true,\n\t}\n);\n","import { type z, type ZodRawShape, type ZodTypeAny } from '@elementor/schema';\nimport { type AngieMcpSdk } from '@elementor-external/angie-sdk';\nimport { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';\nimport { type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';\n\nimport { mockMcpRegistry } from './test-utils/mock-mcp-registry';\n\nconst mcpRegistry: { [ namespace: string ]: McpServer } = {};\nconst mcpDescriptions: { [ namespace: string ]: string } = {};\n// @ts-ignore - QUnit fails this\nlet isMcpRegistrationActivated = false || typeof globalThis.jest !== 'undefined';\n\nexport const registerMcp = ( mcp: McpServer, name: string ) => {\n\tif ( isMcpRegistrationActivated ) {\n\t\tthrow new Error( 'MCP Registration is already activated. Cannot register new MCP servers.' );\n\t}\n\tconst mcpName = isAlphabet( name );\n\tmcpRegistry[ mcpName ] = mcp;\n};\n\nexport async function activateMcpRegistration( sdk: AngieMcpSdk ) {\n\tif ( isMcpRegistrationActivated ) {\n\t\treturn;\n\t}\n\tisMcpRegistrationActivated = true;\n\tconst mcpServerList = Object.entries( mcpRegistry );\n\tfor await ( const entry of mcpServerList ) {\n\t\tconst [ key, mcpServer ] = entry;\n\t\tawait sdk.registerServer( {\n\t\t\tname: `editor-${ key }`,\n\t\t\tserver: mcpServer,\n\t\t\tversion: '1.0.0',\n\t\t\tdescription: mcpDescriptions[ key ] || key,\n\t\t} );\n\t}\n}\n\nconst isAlphabet = ( str: string ): string | never => {\n\tconst passes = !! str && /^[a-z_]+$/.test( str );\n\tif ( ! passes ) {\n\t\tthrow new Error( 'Not alphabet' );\n\t}\n\treturn str;\n};\n\n/**\n *\n * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.\n */\nexport const getMCPByDomain = ( namespace: string ): MCPRegistryEntry => {\n\tconst mcpName = `editor-${ isAlphabet( namespace ) }`;\n\t// @ts-ignore - QUnit fails this\n\tif ( typeof globalThis.jest !== 'undefined' ) {\n\t\treturn mockMcpRegistry();\n\t}\n\tif ( ! mcpRegistry[ namespace ] ) {\n\t\tmcpRegistry[ namespace ] = new McpServer( {\n\t\t\tname: mcpName,\n\t\t\tversion: '1.0.0',\n\t\t} );\n\t}\n\tconst mcpServer = mcpRegistry[ namespace ];\n\tconst { addTool } = createToolRegistrator( mcpServer );\n\treturn {\n\t\taddTool,\n\t\tsetMCPDescription: ( description: string ) => {\n\t\t\tmcpDescriptions[ namespace ] = description;\n\t\t},\n\t};\n};\n\nexport interface MCPRegistryEntry {\n\taddTool: < T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined >(\n\t\topts: ToolRegistrationOptions< T, O >\n\t) => void;\n\tsetMCPDescription: ( description: string ) => void;\n}\n\ntype ToolRegistrationOptions<\n\tInputArgs extends undefined | ZodRawShape = undefined,\n\tOutputSchema extends undefined | ZodRawShape = undefined,\n\tExpectedOutput = OutputSchema extends ZodRawShape ? z.objectOutputType< OutputSchema, ZodTypeAny > : string,\n> = {\n\tname: string;\n\tdescription: string;\n\tschema?: InputArgs;\n\toutputSchema?: OutputSchema;\n\thandler: InputArgs extends ZodRawShape\n\t\t? (\n\t\t\t\targs: z.objectOutputType< InputArgs, ZodTypeAny >,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >\n\t\t: (\n\t\t\t\targs: unknown,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >;\n\tisDestrcutive?: boolean;\n};\n\nfunction createToolRegistrator( server: McpServer ) {\n\tfunction addTool< T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined >(\n\t\topts: ToolRegistrationOptions< T, O >\n\t) {\n\t\tconst inputSchema: ZodRawShape = opts.schema ? opts.schema : {};\n\t\tif ( isMcpRegistrationActivated ) {\n\t\t\tthrow new Error( 'MCP Registration is already activated. Cannot add new tools.' );\n\t\t}\n\t\tconst toolCallback: ToolCallback< typeof inputSchema > = async function ( args, extra ) {\n\t\t\ttry {\n\t\t\t\tconst invocationResult = await opts.handler( opts.schema ? args : {}, extra );\n\t\t\t\treturn {\n\t\t\t\t\tstructuredContent: typeof invocationResult === 'string' ? undefined : invocationResult,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext:\n\t\t\t\t\t\t\t\ttypeof invocationResult === 'string'\n\t\t\t\t\t\t\t\t\t? invocationResult\n\t\t\t\t\t\t\t\t\t: JSON.stringify( invocationResult ),\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t} catch ( error ) {\n\t\t\t\treturn {\n\t\t\t\t\tisError: true,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext: ( error as Error ).message || 'Unknown error',\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\t\tserver.registerTool(\n\t\t\topts.name,\n\t\t\t{\n\t\t\t\tdescription: opts.description,\n\t\t\t\tinputSchema,\n\t\t\t\toutputSchema: opts.outputSchema,\n\t\t\t\ttitle: opts.name,\n\t\t\t\tannotations: {\n\t\t\t\t\tdestructiveHint: opts.isDestrcutive,\n\t\t\t\t\treadOnlyHint: opts.isDestrcutive ? false : undefined,\n\t\t\t\t\ttitle: opts.name,\n\t\t\t\t},\n\t\t\t},\n\t\t\ttoolCallback\n\t\t);\n\t}\n\treturn {\n\t\taddTool,\n\t};\n}\n","import { type MCPRegistryEntry } from '../mcp-registry';\n\nexport const mockMcpRegistry = (): MCPRegistryEntry => {\n\treturn {\n\t\taddTool: () => {},\n\t\tsetMCPDescription: () => {},\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,gCAAmC;AACnC,uBAA4B;;;ACC5B,iBAA6C;;;ACAtC,IAAM,kBAAkB,MAAwB;AACtD,SAAO;AAAA,IACN,SAAS,MAAM;AAAA,IAAC;AAAA,IAChB,mBAAmB,MAAM;AAAA,IAAC;AAAA,EAC3B;AACD;;;ADCA,IAAM,cAAoD,CAAC;AAC3D,IAAM,kBAAqD,CAAC;AAE5D,IAAI,6BAAsC,OAAO,WAAW,SAAS;AAE9D,IAAM,cAAc,CAAE,KAAgB,SAAkB;AAC9D,MAAK,4BAA6B;AACjC,UAAM,IAAI,MAAO,yEAA0E;AAAA,EAC5F;AACA,QAAM,UAAU,WAAY,IAAK;AACjC,cAAa,OAAQ,IAAI;AAC1B;AAEA,eAAsB,wBAAyBA,MAAmB;AACjE,MAAK,4BAA6B;AACjC;AAAA,EACD;AACA,+BAA6B;AAC7B,QAAM,gBAAgB,OAAO,QAAS,WAAY;AAClD,mBAAkB,SAAS,eAAg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,OAAO;","names":["sdk","import_mcp"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/init.ts","../src/mcp-registry.ts","../src/test-utils/mock-mcp-registry.ts","../src/sampler.ts","../src/utils/prompt-builder.ts"],"sourcesContent":["import { getSDK } from './init';\n\nexport {\n\tMcpServer,\n\tResourceTemplate,\n\ttype RegisteredResource,\n\ttype ToolCallback,\n} from '@modelcontextprotocol/sdk/server/mcp.js';\nexport { SamplingMessageSchema } from '@modelcontextprotocol/sdk/types.js';\nexport { init } from './init';\nexport * from './mcp-registry';\nexport { createSampler } from './sampler';\nexport { toolPrompts } from './utils/prompt-builder';\nexport const getAngieSdk = () => getSDK();\n","import { isExperimentActive } from '@elementor/editor-v1-adapters';\nimport { AngieMcpSdk } from '@elementor-external/angie-sdk';\n\nimport { activateMcpRegistration } from './mcp-registry';\n\nlet sdk: AngieMcpSdk;\n\nexport const getSDK = () => {\n\t// @ts-ignore - QUnit fails this\n\tconst isMCPDisabled = !! ( globalThis as Record< string, unknown > ).__ELEMENTOR_MCP_DISABLED__;\n\tif ( isMCPDisabled ) {\n\t\treturn {} as unknown as AngieMcpSdk;\n\t}\n\tif ( ! sdk ) {\n\t\tsdk = new AngieMcpSdk();\n\t}\n\treturn sdk;\n};\n\nexport function init() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK().waitForReady();\n\t}\n\treturn Promise.resolve();\n}\n\nexport function startMCPServer() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK()\n\t\t\t.waitForReady()\n\t\t\t.then( () => activateMcpRegistration( sdk ) );\n\t}\n\treturn Promise.resolve();\n}\n\ndocument.addEventListener(\n\t'DOMContentLoaded',\n\t() => {\n\t\tstartMCPServer();\n\t},\n\t{\n\t\tonce: true,\n\t}\n);\n","import { type z, type z3 } from '@elementor/schema';\nimport { type AngieMcpSdk } from '@elementor-external/angie-sdk';\nimport { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';\nimport { type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';\n\nimport { mockMcpRegistry } from './test-utils/mock-mcp-registry';\n\ntype ZodRawShape = z3.ZodRawShape;\n\nconst mcpRegistry: { [ namespace: string ]: McpServer } = {};\nconst mcpDescriptions: { [ namespace: string ]: string } = {};\n// @ts-ignore - QUnit fails this\nlet isMcpRegistrationActivated = false || typeof globalThis.jest !== 'undefined';\n\nexport const registerMcp = ( mcp: McpServer, name: string ) => {\n\tif ( isMcpRegistrationActivated ) {\n\t\tthrow new Error( 'MCP Registration is already activated. Cannot register new MCP servers.' );\n\t}\n\tconst mcpName = isAlphabet( name );\n\tmcpRegistry[ mcpName ] = mcp;\n};\n\nexport async function activateMcpRegistration( sdk: AngieMcpSdk ) {\n\tif ( isMcpRegistrationActivated ) {\n\t\treturn;\n\t}\n\tisMcpRegistrationActivated = true;\n\tconst mcpServerList = Object.entries( mcpRegistry );\n\tfor await ( const entry of mcpServerList ) {\n\t\tconst [ key, mcpServer ] = entry;\n\t\tawait sdk.registerServer( {\n\t\t\tname: `editor-${ key }`,\n\t\t\tserver: mcpServer,\n\t\t\tversion: '1.0.0',\n\t\t\tdescription: mcpDescriptions[ key ] || key,\n\t\t} );\n\t}\n}\n\nconst isAlphabet = ( str: string ): string | never => {\n\tconst passes = !! str && /^[a-z_]+$/.test( str );\n\tif ( ! passes ) {\n\t\tthrow new Error( 'Not alphabet' );\n\t}\n\treturn str;\n};\n\n/**\n *\n * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.\n * @param options\n * @param options.instructions\n */\nexport const getMCPByDomain = ( namespace: string, options?: { instructions?: string } ): MCPRegistryEntry => {\n\tconst mcpName = `editor-${ isAlphabet( namespace ) }`;\n\t// @ts-ignore - QUnit fails this\n\tif ( typeof globalThis.jest !== 'undefined' ) {\n\t\treturn mockMcpRegistry();\n\t}\n\tif ( ! mcpRegistry[ namespace ] ) {\n\t\tmcpRegistry[ namespace ] = new McpServer(\n\t\t\t{\n\t\t\t\tname: mcpName,\n\t\t\t\tversion: '1.0.0',\n\t\t\t},\n\t\t\t{\n\t\t\t\tinstructions: options?.instructions,\n\t\t\t}\n\t\t);\n\t}\n\tconst mcpServer = mcpRegistry[ namespace ];\n\tconst { addTool } = createToolRegistrator( mcpServer );\n\treturn {\n\t\tmcpServer,\n\t\taddTool,\n\t\tsetMCPDescription: ( description: string ) => {\n\t\t\tmcpDescriptions[ namespace ] = description;\n\t\t},\n\t\tgetActiveChatInfo: () => {\n\t\t\tconst info = localStorage.getItem( 'angie_active_chat_id' );\n\t\t\tif ( ! info ) {\n\t\t\t\treturn {\n\t\t\t\t\texpiresAt: 0,\n\t\t\t\t\tsessionId: '',\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst rawData = JSON.parse( info );\n\t\t\treturn {\n\t\t\t\texpiresAt: rawData.expiresAt as number,\n\t\t\t\tsessionId: rawData.sessionId as string,\n\t\t\t};\n\t\t},\n\t};\n};\n\nexport interface MCPRegistryEntry {\n\taddTool: < T extends undefined | z.ZodRawShape = undefined, O extends undefined | z.ZodRawShape = undefined >(\n\t\topts: ToolRegistrationOptions< T, O >\n\t) => void;\n\tsetMCPDescription: ( description: string ) => void;\n\tgetActiveChatInfo: () => { sessionId: string; expiresAt: number };\n\tmcpServer: McpServer;\n}\n\ntype ResourceList = {\n\ttype: 'resource_link';\n\turi: string;\n\tname: string;\n\tdescription: string;\n\t_meta: Record< string, string >;\n\tmimeType?: string;\n\tannotations?: Record< string, unknown >;\n}[];\n\ntype ToolRegistrationOptions<\n\tInputArgs extends undefined | z.ZodRawShape = undefined,\n\tOutputSchema extends undefined | z.ZodRawShape = undefined,\n\tExpectedOutput = OutputSchema extends z.ZodRawShape ? z.objectOutputType< OutputSchema, z.ZodTypeAny > : string,\n> = {\n\tname: string;\n\tdescription: string;\n\tschema?: InputArgs;\n\toutputSchema?: OutputSchema;\n\thandler: InputArgs extends z.ZodRawShape\n\t\t? (\n\t\t\t\targs: z.objectOutputType< InputArgs, z.ZodTypeAny >,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >\n\t\t: (\n\t\t\t\targs: unknown,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >;\n\tisDestrcutive?: boolean;\n\tresourceList?: ResourceList;\n};\n\nfunction createToolRegistrator( server: McpServer ) {\n\tfunction addTool<\n\t\tT extends undefined | z.ZodRawShape = undefined,\n\t\tO extends undefined | z.ZodRawShape = undefined,\n\t>( opts: ToolRegistrationOptions< T, O > ) {\n\t\tconst outputSchema = opts.outputSchema as ZodRawShape | undefined;\n\t\t// @ts-ignore: TS is unable to infer the type here\n\t\tconst inputSchema: ZodRawShape = opts.schema ? opts.schema : {};\n\t\tif ( isMcpRegistrationActivated ) {\n\t\t\tthrow new Error( 'MCP Registration is already activated. Cannot add new tools.' );\n\t\t}\n\t\tconst toolCallback: ToolCallback< ZodRawShape > = async function ( args, extra ) {\n\t\t\ttry {\n\t\t\t\tconst invocationResult = await opts.handler( opts.schema ? args : {}, extra );\n\t\t\t\treturn {\n\t\t\t\t\tstructuredContent: typeof invocationResult === 'string' ? undefined : invocationResult,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext:\n\t\t\t\t\t\t\t\ttypeof invocationResult === 'string'\n\t\t\t\t\t\t\t\t\t? invocationResult\n\t\t\t\t\t\t\t\t\t: JSON.stringify( invocationResult ),\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...( opts.resourceList || [] ),\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t} catch ( error ) {\n\t\t\t\treturn {\n\t\t\t\t\tisError: true,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext: ( error as Error ).message || 'Unknown error',\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\t\tserver.registerTool(\n\t\t\topts.name,\n\t\t\t{\n\t\t\t\tdescription: opts.description,\n\t\t\t\tinputSchema,\n\t\t\t\toutputSchema,\n\t\t\t\ttitle: opts.name,\n\t\t\t\tannotations: {\n\t\t\t\t\tdestructiveHint: opts.isDestrcutive,\n\t\t\t\t\treadOnlyHint: opts.isDestrcutive ? false : undefined,\n\t\t\t\t\ttitle: opts.name,\n\t\t\t\t},\n\t\t\t},\n\t\t\ttoolCallback\n\t\t);\n\t}\n\treturn {\n\t\taddTool,\n\t};\n}\n","import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\n\nimport { type MCPRegistryEntry } from '../mcp-registry';\n\nconst mock = new Proxy(\n\t{},\n\t{\n\t\tget: () => {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\t\t\tfunction mockedFn( ..._: unknown[] ) {}\n\t\t\treturn mockedFn;\n\t\t},\n\t}\n);\n\nexport const mockMcpRegistry = (): MCPRegistryEntry => {\n\treturn {\n\t\taddTool: () => {},\n\t\tsetMCPDescription: () => {},\n\t\tgetActiveChatInfo() {\n\t\t\treturn { sessionId: 'mock-session-id', expiresAt: Date.now() + 3600000 };\n\t\t},\n\t\tmcpServer: mock as McpServer,\n\t};\n};\n","import { type z } from '@elementor/schema';\nimport { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';\nimport { SamplingMessageSchema, type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';\n\ntype Server = RequestHandlerExtra< ServerRequest, ServerNotification >;\ntype Opts = {\n\tmaxTokens?: number;\n\tmodelPreferences?: string;\n\tmodel?: string;\n};\n\nconst DEFAULT_OPTS: Opts = {\n\tmaxTokens: 10000,\n\tmodelPreferences: 'openai',\n\tmodel: 'gpt-4o',\n};\n\ntype SamplingOpts = {\n\tsystemPrompt?: string;\n\tstructuredOutput?: z.ZodTypeAny;\n\tmessages: { role: 'user' | 'assistant'; content: { type: 'text'; text: string } }[];\n};\n\nconst DEFAULT_STRUCTURED_OUTPUT = {\n\ttype: 'object',\n\tproperties: {\n\t\tcontent: {\n\t\t\ttype: 'string',\n\t\t\tdescription: 'Result',\n\t\t},\n\t},\n\trequired: [ 'content' ],\n\tadditionalProperties: false,\n};\n\nexport const createSampler = ( server: Server, opts: Opts = DEFAULT_OPTS ) => {\n\tconst { maxTokens = 1000, modelPreferences = 'openai', model = 'gpt-4o' } = opts;\n\tconst exec = async ( payload: SamplingOpts ) => {\n\t\tconst systemPromptObject = { ...( payload.systemPrompt ? { systemPrompt: payload.systemPrompt } : {} ) };\n\t\tconst result = await server.sendRequest(\n\t\t\t{\n\t\t\t\tmethod: 'sampling/createMessage',\n\t\t\t\tparams: {\n\t\t\t\t\tmaxTokens,\n\t\t\t\t\tmodelPreferences: {\n\t\t\t\t\t\thints: [ { name: modelPreferences } ],\n\t\t\t\t\t},\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t...systemPromptObject,\n\t\t\t\t\t\t...{ structured_output: payload.structuredOutput || DEFAULT_STRUCTURED_OUTPUT },\n\t\t\t\t\t},\n\t\t\t\t\tmessages: payload.messages,\n\t\t\t\t},\n\t\t\t\t// ...systemPromptObject,\n\t\t\t},\n\t\t\tSamplingMessageSchema\n\t\t);\n\t\ttry {\n\t\t\treturn JSON.parse( result.content.text as string );\n\t\t} catch {\n\t\t\treturn result.content;\n\t\t}\n\t};\n\treturn exec;\n};\n","class ToolPrompts {\n\tpublic _description = '';\n\tpublic _parameters: Record< string, string > = {};\n\tpublic _examples: string[] = [];\n\tpublic _furtherInstructions: string[] = [];\n\n\tconstructor( public name: string ) {}\n\n\tdescription(): string;\n\tdescription( desc: string ): this;\n\tdescription( desc?: string | undefined ) {\n\t\tif ( typeof desc === 'undefined' ) {\n\t\t\treturn this._description;\n\t\t}\n\t\tthis._description = desc;\n\t\treturn this;\n\t}\n\n\tparameter( key: string ): string;\n\tparameter( key: string, description: string ): this;\n\tparameter( key: string, description?: string ) {\n\t\tif ( typeof description === 'undefined' ) {\n\t\t\treturn this._parameters[ key ];\n\t\t}\n\t\tthis._parameters[ key ] = `**${ key }**:\\n${ description }`;\n\t\treturn this;\n\t}\n\n\tinstruction( instruction: string ): this {\n\t\tthis._furtherInstructions.push( instruction );\n\t\treturn this;\n\t}\n\n\texample( example: string ): this {\n\t\tthis._examples.push( example );\n\t\treturn this;\n\t}\n\n\tpublic get examples() {\n\t\treturn this._examples.join( '\\n\\n' );\n\t}\n\n\tprompt(): string {\n\t\treturn `# ${ this.name }\n# Description\n${ this._description }\n\n${ this._parameters.length ? '# Parameters' : '' }\n${ Object.values( this._parameters ).join( '\\n\\n' ) }\n\n${ this._examples.length ? '# Examples' : '' }\n${ this.examples }\n\n${ this._furtherInstructions.length ? '# Further Instructions' : '' }\n${ this._furtherInstructions.join( '\\n\\n' ) }\n`.trim();\n\t}\n}\n\nexport const toolPrompts = ( name: string ) => {\n\treturn new ToolPrompts( name );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,gCAAmC;AACnC,uBAA4B;;;ACC5B,iBAA6C;;;ACE7C,IAAM,OAAO,IAAI;AAAA,EAChB,CAAC;AAAA,EACD;AAAA,IACC,KAAK,MAAM;AAEV,eAAS,YAAa,GAAe;AAAA,MAAC;AACtC,aAAO;AAAA,IACR;AAAA,EACD;AACD;AAEO,IAAM,kBAAkB,MAAwB;AACtD,SAAO;AAAA,IACN,SAAS,MAAM;AAAA,IAAC;AAAA,IAChB,mBAAmB,MAAM;AAAA,IAAC;AAAA,IAC1B,oBAAoB;AACnB,aAAO,EAAE,WAAW,mBAAmB,WAAW,KAAK,IAAI,IAAI,KAAQ;AAA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SAAwB;AAChC,SAAK,UAAU,KAAM,OAAQ;AAC7B,WAAO;AAAA,EACR;AAAA,EAEA,IAAW,WAAW;AACrB,WAAO,KAAK,UAAU,KAAM,MAAO;AAAA,EACpC;AAAA,EAEA,SAAiB;AAChB,WAAO,KAAM,KAAK,IAAK;AAAA;AAAA,EAEtB,KAAK,YAAa;AAAA;AAAA,EAElB,KAAK,YAAY,SAAS,iBAAiB,EAAG;AAAA,EAC9C,OAAO,OAAQ,KAAK,WAAY,EAAE,KAAM,MAAO,CAAE;AAAA;AAAA,EAEjD,KAAK,UAAU,SAAS,eAAe,EAAG;AAAA,EAC1C,KAAK,QAAS;AAAA;AAAA,EAEd,KAAK,qBAAqB,SAAS,2BAA2B,EAAG;AAAA,EACjE,KAAK,qBAAqB,KAAM,MAAO,CAAE;AAAA,EAC1C,KAAK;AAAA,EACN;AACD;AAEO,IAAM,cAAc,CAAE,SAAkB;AAC9C,SAAO,IAAI,YAAa,IAAK;AAC9B;;;ALhDO,IAAM,cAAc,MAAM,OAAO;","names":["sdk","import_mcp","import_types"]}
package/dist/index.mjs CHANGED
@@ -6,12 +6,26 @@ import { AngieMcpSdk } from "@elementor-external/angie-sdk";
6
6
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
7
7
 
8
8
  // src/test-utils/mock-mcp-registry.ts
9
+ var mock = new Proxy(
10
+ {},
11
+ {
12
+ get: () => {
13
+ function mockedFn(..._) {
14
+ }
15
+ return mockedFn;
16
+ }
17
+ }
18
+ );
9
19
  var mockMcpRegistry = () => {
10
20
  return {
11
21
  addTool: () => {
12
22
  },
13
23
  setMCPDescription: () => {
14
- }
24
+ },
25
+ getActiveChatInfo() {
26
+ return { sessionId: "mock-session-id", expiresAt: Date.now() + 36e5 };
27
+ },
28
+ mcpServer: mock
15
29
  };
16
30
  };
17
31
 
@@ -49,28 +63,49 @@ var isAlphabet = (str) => {
49
63
  }
50
64
  return str;
51
65
  };
52
- var getMCPByDomain = (namespace) => {
66
+ var getMCPByDomain = (namespace, options) => {
53
67
  const mcpName = `editor-${isAlphabet(namespace)}`;
54
68
  if (typeof globalThis.jest !== "undefined") {
55
69
  return mockMcpRegistry();
56
70
  }
57
71
  if (!mcpRegistry[namespace]) {
58
- mcpRegistry[namespace] = new McpServer({
59
- name: mcpName,
60
- version: "1.0.0"
61
- });
72
+ mcpRegistry[namespace] = new McpServer(
73
+ {
74
+ name: mcpName,
75
+ version: "1.0.0"
76
+ },
77
+ {
78
+ instructions: options?.instructions
79
+ }
80
+ );
62
81
  }
63
82
  const mcpServer = mcpRegistry[namespace];
64
83
  const { addTool } = createToolRegistrator(mcpServer);
65
84
  return {
85
+ mcpServer,
66
86
  addTool,
67
87
  setMCPDescription: (description) => {
68
88
  mcpDescriptions[namespace] = description;
89
+ },
90
+ getActiveChatInfo: () => {
91
+ const info = localStorage.getItem("angie_active_chat_id");
92
+ if (!info) {
93
+ return {
94
+ expiresAt: 0,
95
+ sessionId: ""
96
+ };
97
+ }
98
+ const rawData = JSON.parse(info);
99
+ return {
100
+ expiresAt: rawData.expiresAt,
101
+ sessionId: rawData.sessionId
102
+ };
69
103
  }
70
104
  };
71
105
  };
72
106
  function createToolRegistrator(server) {
73
107
  function addTool(opts) {
108
+ const outputSchema = opts.outputSchema;
74
109
  const inputSchema = opts.schema ? opts.schema : {};
75
110
  if (isMcpRegistrationActivated) {
76
111
  throw new Error("MCP Registration is already activated. Cannot add new tools.");
@@ -84,7 +119,8 @@ function createToolRegistrator(server) {
84
119
  {
85
120
  type: "text",
86
121
  text: typeof invocationResult === "string" ? invocationResult : JSON.stringify(invocationResult)
87
- }
122
+ },
123
+ ...opts.resourceList || []
88
124
  ]
89
125
  };
90
126
  } catch (error) {
@@ -104,7 +140,7 @@ function createToolRegistrator(server) {
104
140
  {
105
141
  description: opts.description,
106
142
  inputSchema,
107
- outputSchema: opts.outputSchema,
143
+ outputSchema,
108
144
  title: opts.name,
109
145
  annotations: {
110
146
  destructiveHint: opts.isDestrcutive,
@@ -123,7 +159,8 @@ function createToolRegistrator(server) {
123
159
  // src/init.ts
124
160
  var sdk;
125
161
  var getSDK = () => {
126
- if (typeof globalThis.jest !== "undefined") {
162
+ const isMCPDisabled = !!globalThis.__ELEMENTOR_MCP_DISABLED__;
163
+ if (isMCPDisabled) {
127
164
  return {};
128
165
  }
129
166
  if (!sdk) {
@@ -154,14 +191,129 @@ document.addEventListener(
154
191
  );
155
192
 
156
193
  // src/index.ts
157
- import { McpServer as McpServer2 } from "@modelcontextprotocol/sdk/server/mcp.js";
194
+ import {
195
+ McpServer as McpServer2,
196
+ ResourceTemplate
197
+ } from "@modelcontextprotocol/sdk/server/mcp.js";
198
+ import { SamplingMessageSchema as SamplingMessageSchema2 } from "@modelcontextprotocol/sdk/types.js";
199
+
200
+ // src/sampler.ts
201
+ import { SamplingMessageSchema } from "@modelcontextprotocol/sdk/types.js";
202
+ var DEFAULT_OPTS = {
203
+ maxTokens: 1e4,
204
+ modelPreferences: "openai",
205
+ model: "gpt-4o"
206
+ };
207
+ var DEFAULT_STRUCTURED_OUTPUT = {
208
+ type: "object",
209
+ properties: {
210
+ content: {
211
+ type: "string",
212
+ description: "Result"
213
+ }
214
+ },
215
+ required: ["content"],
216
+ additionalProperties: false
217
+ };
218
+ var createSampler = (server, opts = DEFAULT_OPTS) => {
219
+ const { maxTokens = 1e3, modelPreferences = "openai", model = "gpt-4o" } = opts;
220
+ const exec = async (payload) => {
221
+ const systemPromptObject = { ...payload.systemPrompt ? { systemPrompt: payload.systemPrompt } : {} };
222
+ const result = await server.sendRequest(
223
+ {
224
+ method: "sampling/createMessage",
225
+ params: {
226
+ maxTokens,
227
+ modelPreferences: {
228
+ hints: [{ name: modelPreferences }]
229
+ },
230
+ metadata: {
231
+ model,
232
+ ...systemPromptObject,
233
+ ...{ structured_output: payload.structuredOutput || DEFAULT_STRUCTURED_OUTPUT }
234
+ },
235
+ messages: payload.messages
236
+ }
237
+ // ...systemPromptObject,
238
+ },
239
+ SamplingMessageSchema
240
+ );
241
+ try {
242
+ return JSON.parse(result.content.text);
243
+ } catch {
244
+ return result.content;
245
+ }
246
+ };
247
+ return exec;
248
+ };
249
+
250
+ // src/utils/prompt-builder.ts
251
+ var ToolPrompts = class {
252
+ constructor(name) {
253
+ this.name = name;
254
+ }
255
+ _description = "";
256
+ _parameters = {};
257
+ _examples = [];
258
+ _furtherInstructions = [];
259
+ description(desc) {
260
+ if (typeof desc === "undefined") {
261
+ return this._description;
262
+ }
263
+ this._description = desc;
264
+ return this;
265
+ }
266
+ parameter(key, description) {
267
+ if (typeof description === "undefined") {
268
+ return this._parameters[key];
269
+ }
270
+ this._parameters[key] = `**${key}**:
271
+ ${description}`;
272
+ return this;
273
+ }
274
+ instruction(instruction) {
275
+ this._furtherInstructions.push(instruction);
276
+ return this;
277
+ }
278
+ example(example) {
279
+ this._examples.push(example);
280
+ return this;
281
+ }
282
+ get examples() {
283
+ return this._examples.join("\n\n");
284
+ }
285
+ prompt() {
286
+ return `# ${this.name}
287
+ # Description
288
+ ${this._description}
289
+
290
+ ${this._parameters.length ? "# Parameters" : ""}
291
+ ${Object.values(this._parameters).join("\n\n")}
292
+
293
+ ${this._examples.length ? "# Examples" : ""}
294
+ ${this.examples}
295
+
296
+ ${this._furtherInstructions.length ? "# Further Instructions" : ""}
297
+ ${this._furtherInstructions.join("\n\n")}
298
+ `.trim();
299
+ }
300
+ };
301
+ var toolPrompts = (name) => {
302
+ return new ToolPrompts(name);
303
+ };
304
+
305
+ // src/index.ts
158
306
  var getAngieSdk = () => getSDK();
159
307
  export {
160
308
  McpServer2 as McpServer,
309
+ ResourceTemplate,
310
+ SamplingMessageSchema2 as SamplingMessageSchema,
161
311
  activateMcpRegistration,
312
+ createSampler,
162
313
  getAngieSdk,
163
314
  getMCPByDomain,
164
315
  init,
165
- registerMcp
316
+ registerMcp,
317
+ toolPrompts
166
318
  };
167
319
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/init.ts","../src/mcp-registry.ts","../src/test-utils/mock-mcp-registry.ts","../src/index.ts"],"sourcesContent":["import { isExperimentActive } from '@elementor/editor-v1-adapters';\nimport { AngieMcpSdk } from '@elementor-external/angie-sdk';\n\nimport { activateMcpRegistration } from './mcp-registry';\n\nlet sdk: AngieMcpSdk;\n\nexport const getSDK = () => {\n\t// @ts-ignore - QUnit fails this\n\tif ( typeof globalThis.jest !== 'undefined' ) {\n\t\treturn {} as unknown as AngieMcpSdk;\n\t}\n\tif ( ! sdk ) {\n\t\tsdk = new AngieMcpSdk();\n\t}\n\treturn sdk;\n};\n\nexport function init() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK().waitForReady();\n\t}\n\treturn Promise.resolve();\n}\n\nexport function startMCPServer() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK()\n\t\t\t.waitForReady()\n\t\t\t.then( () => activateMcpRegistration( sdk ) );\n\t}\n\treturn Promise.resolve();\n}\n\ndocument.addEventListener(\n\t'DOMContentLoaded',\n\t() => {\n\t\tstartMCPServer();\n\t},\n\t{\n\t\tonce: true,\n\t}\n);\n","import { type z, type ZodRawShape, type ZodTypeAny } from '@elementor/schema';\nimport { type AngieMcpSdk } from '@elementor-external/angie-sdk';\nimport { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';\nimport { type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';\n\nimport { mockMcpRegistry } from './test-utils/mock-mcp-registry';\n\nconst mcpRegistry: { [ namespace: string ]: McpServer } = {};\nconst mcpDescriptions: { [ namespace: string ]: string } = {};\n// @ts-ignore - QUnit fails this\nlet isMcpRegistrationActivated = false || typeof globalThis.jest !== 'undefined';\n\nexport const registerMcp = ( mcp: McpServer, name: string ) => {\n\tif ( isMcpRegistrationActivated ) {\n\t\tthrow new Error( 'MCP Registration is already activated. Cannot register new MCP servers.' );\n\t}\n\tconst mcpName = isAlphabet( name );\n\tmcpRegistry[ mcpName ] = mcp;\n};\n\nexport async function activateMcpRegistration( sdk: AngieMcpSdk ) {\n\tif ( isMcpRegistrationActivated ) {\n\t\treturn;\n\t}\n\tisMcpRegistrationActivated = true;\n\tconst mcpServerList = Object.entries( mcpRegistry );\n\tfor await ( const entry of mcpServerList ) {\n\t\tconst [ key, mcpServer ] = entry;\n\t\tawait sdk.registerServer( {\n\t\t\tname: `editor-${ key }`,\n\t\t\tserver: mcpServer,\n\t\t\tversion: '1.0.0',\n\t\t\tdescription: mcpDescriptions[ key ] || key,\n\t\t} );\n\t}\n}\n\nconst isAlphabet = ( str: string ): string | never => {\n\tconst passes = !! str && /^[a-z_]+$/.test( str );\n\tif ( ! passes ) {\n\t\tthrow new Error( 'Not alphabet' );\n\t}\n\treturn str;\n};\n\n/**\n *\n * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.\n */\nexport const getMCPByDomain = ( namespace: string ): MCPRegistryEntry => {\n\tconst mcpName = `editor-${ isAlphabet( namespace ) }`;\n\t// @ts-ignore - QUnit fails this\n\tif ( typeof globalThis.jest !== 'undefined' ) {\n\t\treturn mockMcpRegistry();\n\t}\n\tif ( ! mcpRegistry[ namespace ] ) {\n\t\tmcpRegistry[ namespace ] = new McpServer( {\n\t\t\tname: mcpName,\n\t\t\tversion: '1.0.0',\n\t\t} );\n\t}\n\tconst mcpServer = mcpRegistry[ namespace ];\n\tconst { addTool } = createToolRegistrator( mcpServer );\n\treturn {\n\t\taddTool,\n\t\tsetMCPDescription: ( description: string ) => {\n\t\t\tmcpDescriptions[ namespace ] = description;\n\t\t},\n\t};\n};\n\nexport interface MCPRegistryEntry {\n\taddTool: < T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined >(\n\t\topts: ToolRegistrationOptions< T, O >\n\t) => void;\n\tsetMCPDescription: ( description: string ) => void;\n}\n\ntype ToolRegistrationOptions<\n\tInputArgs extends undefined | ZodRawShape = undefined,\n\tOutputSchema extends undefined | ZodRawShape = undefined,\n\tExpectedOutput = OutputSchema extends ZodRawShape ? z.objectOutputType< OutputSchema, ZodTypeAny > : string,\n> = {\n\tname: string;\n\tdescription: string;\n\tschema?: InputArgs;\n\toutputSchema?: OutputSchema;\n\thandler: InputArgs extends ZodRawShape\n\t\t? (\n\t\t\t\targs: z.objectOutputType< InputArgs, ZodTypeAny >,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >\n\t\t: (\n\t\t\t\targs: unknown,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >;\n\tisDestrcutive?: boolean;\n};\n\nfunction createToolRegistrator( server: McpServer ) {\n\tfunction addTool< T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined >(\n\t\topts: ToolRegistrationOptions< T, O >\n\t) {\n\t\tconst inputSchema: ZodRawShape = opts.schema ? opts.schema : {};\n\t\tif ( isMcpRegistrationActivated ) {\n\t\t\tthrow new Error( 'MCP Registration is already activated. Cannot add new tools.' );\n\t\t}\n\t\tconst toolCallback: ToolCallback< typeof inputSchema > = async function ( args, extra ) {\n\t\t\ttry {\n\t\t\t\tconst invocationResult = await opts.handler( opts.schema ? args : {}, extra );\n\t\t\t\treturn {\n\t\t\t\t\tstructuredContent: typeof invocationResult === 'string' ? undefined : invocationResult,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext:\n\t\t\t\t\t\t\t\ttypeof invocationResult === 'string'\n\t\t\t\t\t\t\t\t\t? invocationResult\n\t\t\t\t\t\t\t\t\t: JSON.stringify( invocationResult ),\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t} catch ( error ) {\n\t\t\t\treturn {\n\t\t\t\t\tisError: true,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext: ( error as Error ).message || 'Unknown error',\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\t\tserver.registerTool(\n\t\t\topts.name,\n\t\t\t{\n\t\t\t\tdescription: opts.description,\n\t\t\t\tinputSchema,\n\t\t\t\toutputSchema: opts.outputSchema,\n\t\t\t\ttitle: opts.name,\n\t\t\t\tannotations: {\n\t\t\t\t\tdestructiveHint: opts.isDestrcutive,\n\t\t\t\t\treadOnlyHint: opts.isDestrcutive ? false : undefined,\n\t\t\t\t\ttitle: opts.name,\n\t\t\t\t},\n\t\t\t},\n\t\t\ttoolCallback\n\t\t);\n\t}\n\treturn {\n\t\taddTool,\n\t};\n}\n","import { type MCPRegistryEntry } from '../mcp-registry';\n\nexport const mockMcpRegistry = (): MCPRegistryEntry => {\n\treturn {\n\t\taddTool: () => {},\n\t\tsetMCPDescription: () => {},\n\t};\n};\n","import { getSDK } from './init';\n\nexport { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';\nexport { init } from './init';\nexport * from './mcp-registry';\nexport const getAngieSdk = () => getSDK();\n"],"mappings":";AAAA,SAAS,0BAA0B;AACnC,SAAS,mBAAmB;;;ACC5B,SAAS,iBAAoC;;;ACAtC,IAAM,kBAAkB,MAAwB;AACtD,SAAO;AAAA,IACN,SAAS,MAAM;AAAA,IAAC;AAAA,IAChB,mBAAmB,MAAM;AAAA,IAAC;AAAA,EAC3B;AACD;;;ADCA,IAAM,cAAoD,CAAC;AAC3D,IAAM,kBAAqD,CAAC;AAE5D,IAAI,6BAAsC,OAAO,WAAW,SAAS;AAE9D,IAAM,cAAc,CAAE,KAAgB,SAAkB;AAC9D,MAAK,4BAA6B;AACjC,UAAM,IAAI,MAAO,yEAA0E;AAAA,EAC5F;AACA,QAAM,UAAU,WAAY,IAAK;AACjC,cAAa,OAAQ,IAAI;AAC1B;AAEA,e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ACR;AAAA,EACA,MAAM;AACL,mBAAe;AAAA,EAChB;AAAA,EACA;AAAA,IACC,MAAM;AAAA,EACP;AACD;;;AGxCA,SAAS,aAAAC,kBAAoC;AAGtC,IAAM,cAAc,MAAM,OAAO;","names":["sdk","McpServer"]}
1
+ {"version":3,"sources":["../src/init.ts","../src/mcp-registry.ts","../src/test-utils/mock-mcp-registry.ts","../src/index.ts","../src/sampler.ts","../src/utils/prompt-builder.ts"],"sourcesContent":["import { isExperimentActive } from '@elementor/editor-v1-adapters';\nimport { AngieMcpSdk } from '@elementor-external/angie-sdk';\n\nimport { activateMcpRegistration } from './mcp-registry';\n\nlet sdk: AngieMcpSdk;\n\nexport const getSDK = () => {\n\t// @ts-ignore - QUnit fails this\n\tconst isMCPDisabled = !! ( globalThis as Record< string, unknown > ).__ELEMENTOR_MCP_DISABLED__;\n\tif ( isMCPDisabled ) {\n\t\treturn {} as unknown as AngieMcpSdk;\n\t}\n\tif ( ! sdk ) {\n\t\tsdk = new AngieMcpSdk();\n\t}\n\treturn sdk;\n};\n\nexport function init() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK().waitForReady();\n\t}\n\treturn Promise.resolve();\n}\n\nexport function startMCPServer() {\n\tif ( isExperimentActive( 'editor_mcp' ) ) {\n\t\treturn getSDK()\n\t\t\t.waitForReady()\n\t\t\t.then( () => activateMcpRegistration( sdk ) );\n\t}\n\treturn Promise.resolve();\n}\n\ndocument.addEventListener(\n\t'DOMContentLoaded',\n\t() => {\n\t\tstartMCPServer();\n\t},\n\t{\n\t\tonce: true,\n\t}\n);\n","import { type z, type z3 } from '@elementor/schema';\nimport { type AngieMcpSdk } from '@elementor-external/angie-sdk';\nimport { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';\nimport { type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';\n\nimport { mockMcpRegistry } from './test-utils/mock-mcp-registry';\n\ntype ZodRawShape = z3.ZodRawShape;\n\nconst mcpRegistry: { [ namespace: string ]: McpServer } = {};\nconst mcpDescriptions: { [ namespace: string ]: string } = {};\n// @ts-ignore - QUnit fails this\nlet isMcpRegistrationActivated = false || typeof globalThis.jest !== 'undefined';\n\nexport const registerMcp = ( mcp: McpServer, name: string ) => {\n\tif ( isMcpRegistrationActivated ) {\n\t\tthrow new Error( 'MCP Registration is already activated. Cannot register new MCP servers.' );\n\t}\n\tconst mcpName = isAlphabet( name );\n\tmcpRegistry[ mcpName ] = mcp;\n};\n\nexport async function activateMcpRegistration( sdk: AngieMcpSdk ) {\n\tif ( isMcpRegistrationActivated ) {\n\t\treturn;\n\t}\n\tisMcpRegistrationActivated = true;\n\tconst mcpServerList = Object.entries( mcpRegistry );\n\tfor await ( const entry of mcpServerList ) {\n\t\tconst [ key, mcpServer ] = entry;\n\t\tawait sdk.registerServer( {\n\t\t\tname: `editor-${ key }`,\n\t\t\tserver: mcpServer,\n\t\t\tversion: '1.0.0',\n\t\t\tdescription: mcpDescriptions[ key ] || key,\n\t\t} );\n\t}\n}\n\nconst isAlphabet = ( str: string ): string | never => {\n\tconst passes = !! str && /^[a-z_]+$/.test( str );\n\tif ( ! passes ) {\n\t\tthrow new Error( 'Not alphabet' );\n\t}\n\treturn str;\n};\n\n/**\n *\n * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.\n * @param options\n * @param options.instructions\n */\nexport const getMCPByDomain = ( namespace: string, options?: { instructions?: string } ): MCPRegistryEntry => {\n\tconst mcpName = `editor-${ isAlphabet( namespace ) }`;\n\t// @ts-ignore - QUnit fails this\n\tif ( typeof globalThis.jest !== 'undefined' ) {\n\t\treturn mockMcpRegistry();\n\t}\n\tif ( ! mcpRegistry[ namespace ] ) {\n\t\tmcpRegistry[ namespace ] = new McpServer(\n\t\t\t{\n\t\t\t\tname: mcpName,\n\t\t\t\tversion: '1.0.0',\n\t\t\t},\n\t\t\t{\n\t\t\t\tinstructions: options?.instructions,\n\t\t\t}\n\t\t);\n\t}\n\tconst mcpServer = mcpRegistry[ namespace ];\n\tconst { addTool } = createToolRegistrator( mcpServer );\n\treturn {\n\t\tmcpServer,\n\t\taddTool,\n\t\tsetMCPDescription: ( description: string ) => {\n\t\t\tmcpDescriptions[ namespace ] = description;\n\t\t},\n\t\tgetActiveChatInfo: () => {\n\t\t\tconst info = localStorage.getItem( 'angie_active_chat_id' );\n\t\t\tif ( ! info ) {\n\t\t\t\treturn {\n\t\t\t\t\texpiresAt: 0,\n\t\t\t\t\tsessionId: '',\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst rawData = JSON.parse( info );\n\t\t\treturn {\n\t\t\t\texpiresAt: rawData.expiresAt as number,\n\t\t\t\tsessionId: rawData.sessionId as string,\n\t\t\t};\n\t\t},\n\t};\n};\n\nexport interface MCPRegistryEntry {\n\taddTool: < T extends undefined | z.ZodRawShape = undefined, O extends undefined | z.ZodRawShape = undefined >(\n\t\topts: ToolRegistrationOptions< T, O >\n\t) => void;\n\tsetMCPDescription: ( description: string ) => void;\n\tgetActiveChatInfo: () => { sessionId: string; expiresAt: number };\n\tmcpServer: McpServer;\n}\n\ntype ResourceList = {\n\ttype: 'resource_link';\n\turi: string;\n\tname: string;\n\tdescription: string;\n\t_meta: Record< string, string >;\n\tmimeType?: string;\n\tannotations?: Record< string, unknown >;\n}[];\n\ntype ToolRegistrationOptions<\n\tInputArgs extends undefined | z.ZodRawShape = undefined,\n\tOutputSchema extends undefined | z.ZodRawShape = undefined,\n\tExpectedOutput = OutputSchema extends z.ZodRawShape ? z.objectOutputType< OutputSchema, z.ZodTypeAny > : string,\n> = {\n\tname: string;\n\tdescription: string;\n\tschema?: InputArgs;\n\toutputSchema?: OutputSchema;\n\thandler: InputArgs extends z.ZodRawShape\n\t\t? (\n\t\t\t\targs: z.objectOutputType< InputArgs, z.ZodTypeAny >,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >\n\t\t: (\n\t\t\t\targs: unknown,\n\t\t\t\textra: RequestHandlerExtra< ServerRequest, ServerNotification >\n\t\t ) => ExpectedOutput | Promise< ExpectedOutput >;\n\tisDestrcutive?: boolean;\n\tresourceList?: ResourceList;\n};\n\nfunction createToolRegistrator( server: McpServer ) {\n\tfunction addTool<\n\t\tT extends undefined | z.ZodRawShape = undefined,\n\t\tO extends undefined | z.ZodRawShape = undefined,\n\t>( opts: ToolRegistrationOptions< T, O > ) {\n\t\tconst outputSchema = opts.outputSchema as ZodRawShape | undefined;\n\t\t// @ts-ignore: TS is unable to infer the type here\n\t\tconst inputSchema: ZodRawShape = opts.schema ? opts.schema : {};\n\t\tif ( isMcpRegistrationActivated ) {\n\t\t\tthrow new Error( 'MCP Registration is already activated. Cannot add new tools.' );\n\t\t}\n\t\tconst toolCallback: ToolCallback< ZodRawShape > = async function ( args, extra ) {\n\t\t\ttry {\n\t\t\t\tconst invocationResult = await opts.handler( opts.schema ? args : {}, extra );\n\t\t\t\treturn {\n\t\t\t\t\tstructuredContent: typeof invocationResult === 'string' ? undefined : invocationResult,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext:\n\t\t\t\t\t\t\t\ttypeof invocationResult === 'string'\n\t\t\t\t\t\t\t\t\t? invocationResult\n\t\t\t\t\t\t\t\t\t: JSON.stringify( invocationResult ),\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...( opts.resourceList || [] ),\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t} catch ( error ) {\n\t\t\t\treturn {\n\t\t\t\t\tisError: true,\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\t\ttext: ( error as Error ).message || 'Unknown error',\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\t\tserver.registerTool(\n\t\t\topts.name,\n\t\t\t{\n\t\t\t\tdescription: opts.description,\n\t\t\t\tinputSchema,\n\t\t\t\toutputSchema,\n\t\t\t\ttitle: opts.name,\n\t\t\t\tannotations: {\n\t\t\t\t\tdestructiveHint: opts.isDestrcutive,\n\t\t\t\t\treadOnlyHint: opts.isDestrcutive ? false : undefined,\n\t\t\t\t\ttitle: opts.name,\n\t\t\t\t},\n\t\t\t},\n\t\t\ttoolCallback\n\t\t);\n\t}\n\treturn {\n\t\taddTool,\n\t};\n}\n","import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\n\nimport { type MCPRegistryEntry } from '../mcp-registry';\n\nconst mock = new Proxy(\n\t{},\n\t{\n\t\tget: () => {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\t\t\tfunction mockedFn( ..._: unknown[] ) {}\n\t\t\treturn mockedFn;\n\t\t},\n\t}\n);\n\nexport const mockMcpRegistry = (): MCPRegistryEntry => {\n\treturn {\n\t\taddTool: () => {},\n\t\tsetMCPDescription: () => {},\n\t\tgetActiveChatInfo() {\n\t\t\treturn { sessionId: 'mock-session-id', expiresAt: Date.now() + 3600000 };\n\t\t},\n\t\tmcpServer: mock as McpServer,\n\t};\n};\n","import { getSDK } from './init';\n\nexport {\n\tMcpServer,\n\tResourceTemplate,\n\ttype RegisteredResource,\n\ttype ToolCallback,\n} from '@modelcontextprotocol/sdk/server/mcp.js';\nexport { SamplingMessageSchema } from '@modelcontextprotocol/sdk/types.js';\nexport { init } from './init';\nexport * from './mcp-registry';\nexport { createSampler } from './sampler';\nexport { toolPrompts } from './utils/prompt-builder';\nexport const getAngieSdk = () => getSDK();\n","import { type z } from '@elementor/schema';\nimport { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';\nimport { SamplingMessageSchema, type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';\n\ntype Server = RequestHandlerExtra< ServerRequest, ServerNotification >;\ntype Opts = {\n\tmaxTokens?: number;\n\tmodelPreferences?: string;\n\tmodel?: string;\n};\n\nconst DEFAULT_OPTS: Opts = {\n\tmaxTokens: 10000,\n\tmodelPreferences: 'openai',\n\tmodel: 'gpt-4o',\n};\n\ntype SamplingOpts = {\n\tsystemPrompt?: string;\n\tstructuredOutput?: z.ZodTypeAny;\n\tmessages: { role: 'user' | 'assistant'; content: { type: 'text'; text: string } }[];\n};\n\nconst DEFAULT_STRUCTURED_OUTPUT = {\n\ttype: 'object',\n\tproperties: {\n\t\tcontent: {\n\t\t\ttype: 'string',\n\t\t\tdescription: 'Result',\n\t\t},\n\t},\n\trequired: [ 'content' ],\n\tadditionalProperties: false,\n};\n\nexport const createSampler = ( server: Server, opts: Opts = DEFAULT_OPTS ) => {\n\tconst { maxTokens = 1000, modelPreferences = 'openai', model = 'gpt-4o' } = opts;\n\tconst exec = async ( payload: SamplingOpts ) => {\n\t\tconst systemPromptObject = { ...( payload.systemPrompt ? { systemPrompt: payload.systemPrompt } : {} ) };\n\t\tconst result = await server.sendRequest(\n\t\t\t{\n\t\t\t\tmethod: 'sampling/createMessage',\n\t\t\t\tparams: {\n\t\t\t\t\tmaxTokens,\n\t\t\t\t\tmodelPreferences: {\n\t\t\t\t\t\thints: [ { name: modelPreferences } ],\n\t\t\t\t\t},\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t...systemPromptObject,\n\t\t\t\t\t\t...{ structured_output: payload.structuredOutput || DEFAULT_STRUCTURED_OUTPUT },\n\t\t\t\t\t},\n\t\t\t\t\tmessages: payload.messages,\n\t\t\t\t},\n\t\t\t\t// ...systemPromptObject,\n\t\t\t},\n\t\t\tSamplingMessageSchema\n\t\t);\n\t\ttry {\n\t\t\treturn JSON.parse( result.content.text as string );\n\t\t} catch {\n\t\t\treturn result.content;\n\t\t}\n\t};\n\treturn exec;\n};\n","class ToolPrompts {\n\tpublic _description = '';\n\tpublic _parameters: Record< string, string > = {};\n\tpublic _examples: string[] = [];\n\tpublic _furtherInstructions: string[] = [];\n\n\tconstructor( public name: string ) {}\n\n\tdescription(): string;\n\tdescription( desc: string ): this;\n\tdescription( desc?: string | undefined ) {\n\t\tif ( typeof desc === 'undefined' ) {\n\t\t\treturn this._description;\n\t\t}\n\t\tthis._description = desc;\n\t\treturn this;\n\t}\n\n\tparameter( key: string ): string;\n\tparameter( key: string, description: string ): this;\n\tparameter( key: string, description?: string ) {\n\t\tif ( typeof description === 'undefined' ) {\n\t\t\treturn this._parameters[ key ];\n\t\t}\n\t\tthis._parameters[ key ] = `**${ key }**:\\n${ description }`;\n\t\treturn this;\n\t}\n\n\tinstruction( instruction: string ): this {\n\t\tthis._furtherInstructions.push( instruction );\n\t\treturn this;\n\t}\n\n\texample( example: string ): this {\n\t\tthis._examples.push( example );\n\t\treturn this;\n\t}\n\n\tpublic get examples() {\n\t\treturn this._examples.join( '\\n\\n' );\n\t}\n\n\tprompt(): string {\n\t\treturn `# ${ this.name }\n# Description\n${ this._description }\n\n${ this._parameters.length ? '# Parameters' : '' }\n${ Object.values( this._parameters ).join( '\\n\\n' ) }\n\n${ this._examples.length ? '# Examples' : '' }\n${ this.examples }\n\n${ this._furtherInstructions.length ? '# Further Instructions' : '' }\n${ this._furtherInstructions.join( '\\n\\n' ) }\n`.trim();\n\t}\n}\n\nexport const toolPrompts = ( name: string ) => {\n\treturn new ToolPrompts( name );\n};\n"],"mappings":";AAAA,SAAS,0BAA0B;AACnC,SAAS,mBAAmB;;;ACC5B,SAAS,iBAAoC;;;ACE7C,IAAM,OAAO,IAAI;AAAA,EAChB,CAAC;AAAA,EACD;AAAA,IACC,KAAK,MAAM;AAEV,eAAS,YAAa,GAAe;AAAA,MAAC;AACtC,aAAO;AAAA,IACR;AAAA,EACD;AACD;AAEO,IAAM,kBAAkB,MAAwB;AACtD,SAAO;AAAA,IACN,SAAS,MAAM;AAAA,IAAC;AAAA,IAChB,mBAAmB,MAAM;AAAA,IAAC;AAAA,IAC1B,oBAAoB;AACnB,aAAO,EAAE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EA,YAAa,aAA4B;AACxC,SAAK,qBAAqB,KAAM,WAAY;AAC5C,WAAO;AAAA,EACR;AAAA,EAEA,QAAS,SAAwB;AAChC,SAAK,UAAU,KAAM,OAAQ;AAC7B,WAAO;AAAA,EACR;AAAA,EAEA,IAAW,WAAW;AACrB,WAAO,KAAK,UAAU,KAAM,MAAO;AAAA,EACpC;AAAA,EAEA,SAAiB;AAChB,WAAO,KAAM,KAAK,IAAK;AAAA;AAAA,EAEtB,KAAK,YAAa;AAAA;AAAA,EAElB,KAAK,YAAY,SAAS,iBAAiB,EAAG;AAAA,EAC9C,OAAO,OAAQ,KAAK,WAAY,EAAE,KAAM,MAAO,CAAE;AAAA;AAAA,EAEjD,KAAK,UAAU,SAAS,eAAe,EAAG;AAAA,EAC1C,KAAK,QAAS;AAAA;AAAA,EAEd,KAAK,qBAAqB,SAAS,2BAA2B,EAAG;AAAA,EACjE,KAAK,qBAAqB,KAAM,MAAO,CAAE;AAAA,EAC1C,KAAK;AAAA,EACN;AACD;AAEO,IAAM,cAAc,CAAE,SAAkB;AAC9C,SAAO,IAAI,YAAa,IAAK;AAC9B;;;AFhDO,IAAM,cAAc,MAAM,OAAO;","names":["sdk","McpServer","SamplingMessageSchema"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elementor/editor-mcp",
3
- "version": "3.33.0-271",
3
+ "version": "3.33.0-272",
4
4
  "private": false,
5
5
  "author": "Elementor Team",
6
6
  "homepage": "https://elementor.com/",
@@ -42,10 +42,10 @@
42
42
  "tsup": "^8.3.5"
43
43
  },
44
44
  "dependencies": {
45
- "@elementor-external/angie-sdk": "npm:@elementor/angie-sdk@1.0.1",
46
- "@elementor/schema": "3.33.0-271",
47
- "@modelcontextprotocol/sdk": "1.17.3",
48
- "@elementor/store": "3.33.0-271",
49
- "@elementor/editor-v1-adapters": "3.33.0-271"
45
+ "@elementor-external/angie-sdk": "npm:@elementor/angie-sdk@1.0.3",
46
+ "@elementor/schema": "3.33.0-272",
47
+ "@modelcontextprotocol/sdk": "1.17.4",
48
+ "@elementor/store": "3.33.0-272",
49
+ "@elementor/editor-v1-adapters": "3.33.0-272"
50
50
  }
51
51
  }
package/src/index.ts CHANGED
@@ -1,6 +1,14 @@
1
1
  import { getSDK } from './init';
2
2
 
3
- export { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ export {
4
+ McpServer,
5
+ ResourceTemplate,
6
+ type RegisteredResource,
7
+ type ToolCallback,
8
+ } from '@modelcontextprotocol/sdk/server/mcp.js';
9
+ export { SamplingMessageSchema } from '@modelcontextprotocol/sdk/types.js';
4
10
  export { init } from './init';
5
11
  export * from './mcp-registry';
12
+ export { createSampler } from './sampler';
13
+ export { toolPrompts } from './utils/prompt-builder';
6
14
  export const getAngieSdk = () => getSDK();
package/src/init.ts CHANGED
@@ -7,7 +7,8 @@ let sdk: AngieMcpSdk;
7
7
 
8
8
  export const getSDK = () => {
9
9
  // @ts-ignore - QUnit fails this
10
- if ( typeof globalThis.jest !== 'undefined' ) {
10
+ const isMCPDisabled = !! ( globalThis as Record< string, unknown > ).__ELEMENTOR_MCP_DISABLED__;
11
+ if ( isMCPDisabled ) {
11
12
  return {} as unknown as AngieMcpSdk;
12
13
  }
13
14
  if ( ! sdk ) {
@@ -1,4 +1,4 @@
1
- import { type z, type ZodRawShape, type ZodTypeAny } from '@elementor/schema';
1
+ import { type z, type z3 } from '@elementor/schema';
2
2
  import { type AngieMcpSdk } from '@elementor-external/angie-sdk';
3
3
  import { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
4
4
  import { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
@@ -6,6 +6,8 @@ import { type ServerNotification, type ServerRequest } from '@modelcontextprotoc
6
6
 
7
7
  import { mockMcpRegistry } from './test-utils/mock-mcp-registry';
8
8
 
9
+ type ZodRawShape = z3.ZodRawShape;
10
+
9
11
  const mcpRegistry: { [ namespace: string ]: McpServer } = {};
10
12
  const mcpDescriptions: { [ namespace: string ]: string } = {};
11
13
  // @ts-ignore - QUnit fails this
@@ -46,49 +48,83 @@ const isAlphabet = ( str: string ): string | never => {
46
48
 
47
49
  /**
48
50
  *
49
- * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.
51
+ * @param namespace The namespace of the MCP server. It should contain only lowercase alphabetic characters.
52
+ * @param options
53
+ * @param options.instructions
50
54
  */
51
- export const getMCPByDomain = ( namespace: string ): MCPRegistryEntry => {
55
+ export const getMCPByDomain = ( namespace: string, options?: { instructions?: string } ): MCPRegistryEntry => {
52
56
  const mcpName = `editor-${ isAlphabet( namespace ) }`;
53
57
  // @ts-ignore - QUnit fails this
54
58
  if ( typeof globalThis.jest !== 'undefined' ) {
55
59
  return mockMcpRegistry();
56
60
  }
57
61
  if ( ! mcpRegistry[ namespace ] ) {
58
- mcpRegistry[ namespace ] = new McpServer( {
59
- name: mcpName,
60
- version: '1.0.0',
61
- } );
62
+ mcpRegistry[ namespace ] = new McpServer(
63
+ {
64
+ name: mcpName,
65
+ version: '1.0.0',
66
+ },
67
+ {
68
+ instructions: options?.instructions,
69
+ }
70
+ );
62
71
  }
63
72
  const mcpServer = mcpRegistry[ namespace ];
64
73
  const { addTool } = createToolRegistrator( mcpServer );
65
74
  return {
75
+ mcpServer,
66
76
  addTool,
67
77
  setMCPDescription: ( description: string ) => {
68
78
  mcpDescriptions[ namespace ] = description;
69
79
  },
80
+ getActiveChatInfo: () => {
81
+ const info = localStorage.getItem( 'angie_active_chat_id' );
82
+ if ( ! info ) {
83
+ return {
84
+ expiresAt: 0,
85
+ sessionId: '',
86
+ };
87
+ }
88
+ const rawData = JSON.parse( info );
89
+ return {
90
+ expiresAt: rawData.expiresAt as number,
91
+ sessionId: rawData.sessionId as string,
92
+ };
93
+ },
70
94
  };
71
95
  };
72
96
 
73
97
  export interface MCPRegistryEntry {
74
- addTool: < T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined >(
98
+ addTool: < T extends undefined | z.ZodRawShape = undefined, O extends undefined | z.ZodRawShape = undefined >(
75
99
  opts: ToolRegistrationOptions< T, O >
76
100
  ) => void;
77
101
  setMCPDescription: ( description: string ) => void;
102
+ getActiveChatInfo: () => { sessionId: string; expiresAt: number };
103
+ mcpServer: McpServer;
78
104
  }
79
105
 
106
+ type ResourceList = {
107
+ type: 'resource_link';
108
+ uri: string;
109
+ name: string;
110
+ description: string;
111
+ _meta: Record< string, string >;
112
+ mimeType?: string;
113
+ annotations?: Record< string, unknown >;
114
+ }[];
115
+
80
116
  type ToolRegistrationOptions<
81
- InputArgs extends undefined | ZodRawShape = undefined,
82
- OutputSchema extends undefined | ZodRawShape = undefined,
83
- ExpectedOutput = OutputSchema extends ZodRawShape ? z.objectOutputType< OutputSchema, ZodTypeAny > : string,
117
+ InputArgs extends undefined | z.ZodRawShape = undefined,
118
+ OutputSchema extends undefined | z.ZodRawShape = undefined,
119
+ ExpectedOutput = OutputSchema extends z.ZodRawShape ? z.objectOutputType< OutputSchema, z.ZodTypeAny > : string,
84
120
  > = {
85
121
  name: string;
86
122
  description: string;
87
123
  schema?: InputArgs;
88
124
  outputSchema?: OutputSchema;
89
- handler: InputArgs extends ZodRawShape
125
+ handler: InputArgs extends z.ZodRawShape
90
126
  ? (
91
- args: z.objectOutputType< InputArgs, ZodTypeAny >,
127
+ args: z.objectOutputType< InputArgs, z.ZodTypeAny >,
92
128
  extra: RequestHandlerExtra< ServerRequest, ServerNotification >
93
129
  ) => ExpectedOutput | Promise< ExpectedOutput >
94
130
  : (
@@ -96,17 +132,21 @@ type ToolRegistrationOptions<
96
132
  extra: RequestHandlerExtra< ServerRequest, ServerNotification >
97
133
  ) => ExpectedOutput | Promise< ExpectedOutput >;
98
134
  isDestrcutive?: boolean;
135
+ resourceList?: ResourceList;
99
136
  };
100
137
 
101
138
  function createToolRegistrator( server: McpServer ) {
102
- function addTool< T extends undefined | ZodRawShape = undefined, O extends undefined | ZodRawShape = undefined >(
103
- opts: ToolRegistrationOptions< T, O >
104
- ) {
139
+ function addTool<
140
+ T extends undefined | z.ZodRawShape = undefined,
141
+ O extends undefined | z.ZodRawShape = undefined,
142
+ >( opts: ToolRegistrationOptions< T, O > ) {
143
+ const outputSchema = opts.outputSchema as ZodRawShape | undefined;
144
+ // @ts-ignore: TS is unable to infer the type here
105
145
  const inputSchema: ZodRawShape = opts.schema ? opts.schema : {};
106
146
  if ( isMcpRegistrationActivated ) {
107
147
  throw new Error( 'MCP Registration is already activated. Cannot add new tools.' );
108
148
  }
109
- const toolCallback: ToolCallback< typeof inputSchema > = async function ( args, extra ) {
149
+ const toolCallback: ToolCallback< ZodRawShape > = async function ( args, extra ) {
110
150
  try {
111
151
  const invocationResult = await opts.handler( opts.schema ? args : {}, extra );
112
152
  return {
@@ -119,6 +159,7 @@ function createToolRegistrator( server: McpServer ) {
119
159
  ? invocationResult
120
160
  : JSON.stringify( invocationResult ),
121
161
  },
162
+ ...( opts.resourceList || [] ),
122
163
  ],
123
164
  };
124
165
  } catch ( error ) {
@@ -138,7 +179,7 @@ function createToolRegistrator( server: McpServer ) {
138
179
  {
139
180
  description: opts.description,
140
181
  inputSchema,
141
- outputSchema: opts.outputSchema,
182
+ outputSchema,
142
183
  title: opts.name,
143
184
  annotations: {
144
185
  destructiveHint: opts.isDestrcutive,
package/src/sampler.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { type z } from '@elementor/schema';
2
+ import { type RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
3
+ import { SamplingMessageSchema, type ServerNotification, type ServerRequest } from '@modelcontextprotocol/sdk/types.js';
4
+
5
+ type Server = RequestHandlerExtra< ServerRequest, ServerNotification >;
6
+ type Opts = {
7
+ maxTokens?: number;
8
+ modelPreferences?: string;
9
+ model?: string;
10
+ };
11
+
12
+ const DEFAULT_OPTS: Opts = {
13
+ maxTokens: 10000,
14
+ modelPreferences: 'openai',
15
+ model: 'gpt-4o',
16
+ };
17
+
18
+ type SamplingOpts = {
19
+ systemPrompt?: string;
20
+ structuredOutput?: z.ZodTypeAny;
21
+ messages: { role: 'user' | 'assistant'; content: { type: 'text'; text: string } }[];
22
+ };
23
+
24
+ const DEFAULT_STRUCTURED_OUTPUT = {
25
+ type: 'object',
26
+ properties: {
27
+ content: {
28
+ type: 'string',
29
+ description: 'Result',
30
+ },
31
+ },
32
+ required: [ 'content' ],
33
+ additionalProperties: false,
34
+ };
35
+
36
+ export const createSampler = ( server: Server, opts: Opts = DEFAULT_OPTS ) => {
37
+ const { maxTokens = 1000, modelPreferences = 'openai', model = 'gpt-4o' } = opts;
38
+ const exec = async ( payload: SamplingOpts ) => {
39
+ const systemPromptObject = { ...( payload.systemPrompt ? { systemPrompt: payload.systemPrompt } : {} ) };
40
+ const result = await server.sendRequest(
41
+ {
42
+ method: 'sampling/createMessage',
43
+ params: {
44
+ maxTokens,
45
+ modelPreferences: {
46
+ hints: [ { name: modelPreferences } ],
47
+ },
48
+ metadata: {
49
+ model,
50
+ ...systemPromptObject,
51
+ ...{ structured_output: payload.structuredOutput || DEFAULT_STRUCTURED_OUTPUT },
52
+ },
53
+ messages: payload.messages,
54
+ },
55
+ // ...systemPromptObject,
56
+ },
57
+ SamplingMessageSchema
58
+ );
59
+ try {
60
+ return JSON.parse( result.content.text as string );
61
+ } catch {
62
+ return result.content;
63
+ }
64
+ };
65
+ return exec;
66
+ };
@@ -1,8 +1,25 @@
1
+ import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+
1
3
  import { type MCPRegistryEntry } from '../mcp-registry';
2
4
 
5
+ const mock = new Proxy(
6
+ {},
7
+ {
8
+ get: () => {
9
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
+ function mockedFn( ..._: unknown[] ) {}
11
+ return mockedFn;
12
+ },
13
+ }
14
+ );
15
+
3
16
  export const mockMcpRegistry = (): MCPRegistryEntry => {
4
17
  return {
5
18
  addTool: () => {},
6
19
  setMCPDescription: () => {},
20
+ getActiveChatInfo() {
21
+ return { sessionId: 'mock-session-id', expiresAt: Date.now() + 3600000 };
22
+ },
23
+ mcpServer: mock as McpServer,
7
24
  };
8
25
  };
@@ -0,0 +1,62 @@
1
+ class ToolPrompts {
2
+ public _description = '';
3
+ public _parameters: Record< string, string > = {};
4
+ public _examples: string[] = [];
5
+ public _furtherInstructions: string[] = [];
6
+
7
+ constructor( public name: string ) {}
8
+
9
+ description(): string;
10
+ description( desc: string ): this;
11
+ description( desc?: string | undefined ) {
12
+ if ( typeof desc === 'undefined' ) {
13
+ return this._description;
14
+ }
15
+ this._description = desc;
16
+ return this;
17
+ }
18
+
19
+ parameter( key: string ): string;
20
+ parameter( key: string, description: string ): this;
21
+ parameter( key: string, description?: string ) {
22
+ if ( typeof description === 'undefined' ) {
23
+ return this._parameters[ key ];
24
+ }
25
+ this._parameters[ key ] = `**${ key }**:\n${ description }`;
26
+ return this;
27
+ }
28
+
29
+ instruction( instruction: string ): this {
30
+ this._furtherInstructions.push( instruction );
31
+ return this;
32
+ }
33
+
34
+ example( example: string ): this {
35
+ this._examples.push( example );
36
+ return this;
37
+ }
38
+
39
+ public get examples() {
40
+ return this._examples.join( '\n\n' );
41
+ }
42
+
43
+ prompt(): string {
44
+ return `# ${ this.name }
45
+ # Description
46
+ ${ this._description }
47
+
48
+ ${ this._parameters.length ? '# Parameters' : '' }
49
+ ${ Object.values( this._parameters ).join( '\n\n' ) }
50
+
51
+ ${ this._examples.length ? '# Examples' : '' }
52
+ ${ this.examples }
53
+
54
+ ${ this._furtherInstructions.length ? '# Further Instructions' : '' }
55
+ ${ this._furtherInstructions.join( '\n\n' ) }
56
+ `.trim();
57
+ }
58
+ }
59
+
60
+ export const toolPrompts = ( name: string ) => {
61
+ return new ToolPrompts( name );
62
+ };