mcp-use 1.10.6 → 1.11.0-canary.11

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.
Files changed (140) hide show
  1. package/README.md +1 -1
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/{chunk-KIWNNI6F.js → chunk-2NE5H4KG.js} +16 -2
  4. package/dist/{chunk-D5WOXLJ2.js → chunk-4QWS5ME6.js} +171 -11
  5. package/dist/chunk-7P2EMREO.js +101 -0
  6. package/dist/chunk-7PSUUT4A.js +1055 -0
  7. package/dist/{chunk-44DFBJUL.js → chunk-BOCIQYWG.js} +196 -496
  8. package/dist/chunk-BPZJIV4V.js +1873 -0
  9. package/dist/{chunk-EEUJZMOP.js → chunk-CVKKDXI3.js} +1 -1
  10. package/dist/chunk-DPK5NHDR.js +12 -0
  11. package/dist/{chunk-FDKY2O5P.js → chunk-EHCLF3JO.js} +443 -969
  12. package/dist/{chunk-34R6SIER.js → chunk-FRUZDWXH.js} +1 -1
  13. package/dist/chunk-GXNAXUDI.js +0 -0
  14. package/dist/{chunk-JH3ZOGLI.js → chunk-J3WTIYVV.js} +2 -2
  15. package/dist/{chunk-CPG2WZUL.js → chunk-JRGQRPTN.js} +1 -1
  16. package/dist/chunk-MFSO5PUW.js +1049 -0
  17. package/dist/chunk-SQSJ5NKY.js +1055 -0
  18. package/dist/chunk-T4EDAWDS.js +2638 -0
  19. package/dist/chunk-UWWLWLS2.js +62 -0
  20. package/dist/{chunk-BLWPCOUZ.js → chunk-V4YUDB7N.js} +3 -8
  21. package/dist/index.cjs +5065 -4608
  22. package/dist/index.d.ts +2 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +46 -1061
  25. package/dist/{langfuse-N5Y5BSXK.js → langfuse-74RGPTAH.js} +2 -2
  26. package/dist/notifications-FLGIFS56.js +9 -0
  27. package/dist/src/adapters/base.d.ts +44 -0
  28. package/dist/src/adapters/base.d.ts.map +1 -1
  29. package/dist/src/adapters/index.cjs +1346 -0
  30. package/dist/src/adapters/index.js +11 -0
  31. package/dist/src/adapters/langchain_adapter.d.ts +12 -1
  32. package/dist/src/adapters/langchain_adapter.d.ts.map +1 -1
  33. package/dist/src/agents/index.cjs +3141 -159
  34. package/dist/src/agents/index.d.ts +2 -0
  35. package/dist/src/agents/index.d.ts.map +1 -1
  36. package/dist/src/agents/index.js +12 -8
  37. package/dist/src/agents/mcp_agent.d.ts +59 -37
  38. package/dist/src/agents/mcp_agent.d.ts.map +1 -1
  39. package/dist/src/agents/remote.d.ts +25 -0
  40. package/dist/src/agents/remote.d.ts.map +1 -1
  41. package/dist/src/agents/types.d.ts +76 -0
  42. package/dist/src/agents/types.d.ts.map +1 -1
  43. package/dist/src/agents/utils/index.d.ts +1 -0
  44. package/dist/src/agents/utils/index.d.ts.map +1 -1
  45. package/dist/src/agents/utils/llm_provider.d.ts +53 -0
  46. package/dist/src/agents/utils/llm_provider.d.ts.map +1 -0
  47. package/dist/src/browser.cjs +1856 -423
  48. package/dist/src/browser.d.ts +1 -2
  49. package/dist/src/browser.d.ts.map +1 -1
  50. package/dist/src/browser.js +30 -19
  51. package/dist/src/client/base.d.ts +1 -0
  52. package/dist/src/client/base.d.ts.map +1 -1
  53. package/dist/src/client/browser.d.ts +2 -2
  54. package/dist/src/client/browser.d.ts.map +1 -1
  55. package/dist/src/client/prompts.cjs +1 -1
  56. package/dist/src/client/prompts.js +5 -4
  57. package/dist/src/client.cjs +3788 -0
  58. package/dist/src/client.d.ts +2 -0
  59. package/dist/src/client.d.ts.map +1 -1
  60. package/dist/src/client.js +23 -0
  61. package/dist/src/config.d.ts.map +1 -1
  62. package/dist/src/connectors/base.d.ts +8 -0
  63. package/dist/src/connectors/base.d.ts.map +1 -1
  64. package/dist/src/connectors/index.d.ts +0 -1
  65. package/dist/src/connectors/index.d.ts.map +1 -1
  66. package/dist/src/managers/server_manager.d.ts.map +1 -1
  67. package/dist/src/managers/tools/connect_mcp_server.d.ts.map +1 -1
  68. package/dist/src/react/index.cjs +259 -298
  69. package/dist/src/react/index.js +9 -8
  70. package/dist/src/react/types.d.ts +42 -4
  71. package/dist/src/react/types.d.ts.map +1 -1
  72. package/dist/src/react/useMcp.d.ts.map +1 -1
  73. package/dist/src/react/useWidget.d.ts +11 -7
  74. package/dist/src/react/useWidget.d.ts.map +1 -1
  75. package/dist/src/react/widget-types.d.ts +6 -2
  76. package/dist/src/react/widget-types.d.ts.map +1 -1
  77. package/dist/src/server/endpoints/mount-mcp.d.ts.map +1 -1
  78. package/dist/src/server/index.cjs +1288 -140
  79. package/dist/src/server/index.d.ts +2 -0
  80. package/dist/src/server/index.d.ts.map +1 -1
  81. package/dist/src/server/index.js +1183 -102
  82. package/dist/src/server/mcp-server.d.ts +5 -1
  83. package/dist/src/server/mcp-server.d.ts.map +1 -1
  84. package/dist/src/server/notifications/index.d.ts +1 -1
  85. package/dist/src/server/notifications/index.d.ts.map +1 -1
  86. package/dist/src/server/notifications/notification-registration.d.ts +51 -0
  87. package/dist/src/server/notifications/notification-registration.d.ts.map +1 -1
  88. package/dist/src/server/sessions/index.d.ts +3 -1
  89. package/dist/src/server/sessions/index.d.ts.map +1 -1
  90. package/dist/src/server/sessions/session-manager.d.ts +36 -19
  91. package/dist/src/server/sessions/session-manager.d.ts.map +1 -1
  92. package/dist/src/server/sessions/stores/filesystem.d.ts +121 -0
  93. package/dist/src/server/sessions/stores/filesystem.d.ts.map +1 -0
  94. package/dist/src/server/sessions/stores/index.d.ts +94 -0
  95. package/dist/src/server/sessions/stores/index.d.ts.map +1 -0
  96. package/dist/src/server/sessions/stores/memory.d.ts +82 -0
  97. package/dist/src/server/sessions/stores/memory.d.ts.map +1 -0
  98. package/dist/src/server/sessions/stores/redis.d.ts +164 -0
  99. package/dist/src/server/sessions/stores/redis.d.ts.map +1 -0
  100. package/dist/src/server/sessions/streams/index.d.ts +77 -0
  101. package/dist/src/server/sessions/streams/index.d.ts.map +1 -0
  102. package/dist/src/server/sessions/streams/memory.d.ts +76 -0
  103. package/dist/src/server/sessions/streams/memory.d.ts.map +1 -0
  104. package/dist/src/server/sessions/streams/redis.d.ts +146 -0
  105. package/dist/src/server/sessions/streams/redis.d.ts.map +1 -0
  106. package/dist/src/server/types/common.d.ts +105 -28
  107. package/dist/src/server/types/common.d.ts.map +1 -1
  108. package/dist/src/server/types/resource.d.ts +16 -0
  109. package/dist/src/server/types/resource.d.ts.map +1 -1
  110. package/dist/src/server/types/widget.d.ts +21 -2
  111. package/dist/src/server/types/widget.d.ts.map +1 -1
  112. package/dist/src/server/utils/response-helpers.d.ts +12 -6
  113. package/dist/src/server/utils/response-helpers.d.ts.map +1 -1
  114. package/dist/src/server/widgets/index.d.ts +1 -1
  115. package/dist/src/server/widgets/index.d.ts.map +1 -1
  116. package/dist/src/server/widgets/mount-widgets-dev.d.ts.map +1 -1
  117. package/dist/src/server/widgets/setup-widget-routes.d.ts.map +1 -1
  118. package/dist/src/server/widgets/ui-resource-registration.d.ts.map +1 -1
  119. package/dist/src/server/widgets/widget-helpers.d.ts +22 -0
  120. package/dist/src/server/widgets/widget-helpers.d.ts.map +1 -1
  121. package/dist/src/server/widgets/widget-types.d.ts +2 -0
  122. package/dist/src/server/widgets/widget-types.d.ts.map +1 -1
  123. package/dist/src/session.d.ts +16 -2
  124. package/dist/src/session.d.ts.map +1 -1
  125. package/dist/src/task_managers/index.d.ts +10 -1
  126. package/dist/src/task_managers/index.d.ts.map +1 -1
  127. package/dist/src/task_managers/sse.d.ts +34 -1
  128. package/dist/src/task_managers/sse.d.ts.map +1 -1
  129. package/dist/src/task_managers/streamable_http.d.ts +8 -2
  130. package/dist/src/task_managers/streamable_http.d.ts.map +1 -1
  131. package/dist/src/version.d.ts +1 -1
  132. package/dist/src/version.d.ts.map +1 -1
  133. package/dist/{tool-execution-helpers-4X6A63AS.js → tool-execution-helpers-3BYYGIA5.js} +3 -3
  134. package/dist/tsup.config.d.ts.map +1 -1
  135. package/package.json +47 -14
  136. package/dist/src/connectors/websocket.d.ts +0 -38
  137. package/dist/src/connectors/websocket.d.ts.map +0 -1
  138. package/dist/src/task_managers/websocket.d.ts +0 -18
  139. package/dist/src/task_managers/websocket.d.ts.map +0 -1
  140. /package/dist/{chunk-EW4MJSHA.js → chunk-LGDFGYRL.js} +0 -0
@@ -0,0 +1,1049 @@
1
+ import {
2
+ logger
3
+ } from "./chunk-FRUZDWXH.js";
4
+ import {
5
+ __name
6
+ } from "./chunk-3GQAWCBQ.js";
7
+
8
+ // src/adapters/base.ts
9
+ var BaseAdapter = class {
10
+ static {
11
+ __name(this, "BaseAdapter");
12
+ }
13
+ /**
14
+ * List of tool names that should not be available.
15
+ */
16
+ disallowedTools;
17
+ /**
18
+ * Internal cache that maps a connector instance to the list of tools
19
+ * generated for it.
20
+ */
21
+ connectorToolMap = /* @__PURE__ */ new Map();
22
+ constructor(disallowedTools) {
23
+ this.disallowedTools = disallowedTools ?? [];
24
+ }
25
+ /**
26
+ * Create tools from an MCPClient instance.
27
+ *
28
+ * This is the recommended way to create tools from an MCPClient, as it handles
29
+ * session creation and connector extraction automatically.
30
+ *
31
+ * @param client The MCPClient to extract tools from.
32
+ * @param disallowedTools Optional list of tool names to exclude.
33
+ * @returns A promise that resolves with a list of converted tools.
34
+ */
35
+ static async createTools(client, disallowedTools) {
36
+ const adapter = new this(disallowedTools);
37
+ if (!client.activeSessions || Object.keys(client.activeSessions).length === 0) {
38
+ logger.info("No active sessions found, creating new ones...");
39
+ await client.createAllSessions();
40
+ }
41
+ const sessions = client.getAllActiveSessions();
42
+ const connectors = Object.values(sessions).map(
43
+ (session) => session.connector
44
+ );
45
+ return adapter.createToolsFromConnectors(connectors);
46
+ }
47
+ /**
48
+ * Dynamically load tools for a specific connector.
49
+ *
50
+ * @param connector The connector to load tools for.
51
+ * @returns The list of tools that were loaded in the target framework's format.
52
+ */
53
+ async loadToolsForConnector(connector) {
54
+ if (this.connectorToolMap.has(connector)) {
55
+ const cached = this.connectorToolMap.get(connector);
56
+ logger.debug(`Returning ${cached.length} existing tools for connector`);
57
+ return cached;
58
+ }
59
+ const connectorTools = [];
60
+ const success = await this.ensureConnectorInitialized(connector);
61
+ if (!success) {
62
+ return [];
63
+ }
64
+ for (const tool of connector.tools) {
65
+ const converted = this.convertTool(tool, connector);
66
+ if (converted) {
67
+ connectorTools.push(converted);
68
+ }
69
+ }
70
+ this.connectorToolMap.set(connector, connectorTools);
71
+ logger.debug(
72
+ `Loaded ${connectorTools.length} new tools for connector: ${connectorTools.map((t) => t?.name ?? String(t)).join(", ")}`
73
+ );
74
+ return connectorTools;
75
+ }
76
+ /**
77
+ * Create tools from MCP tools in all provided connectors.
78
+ *
79
+ * @param connectors List of MCP connectors to create tools from.
80
+ * @returns A promise that resolves with all converted tools.
81
+ */
82
+ async createToolsFromConnectors(connectors) {
83
+ const tools = [];
84
+ for (const connector of connectors) {
85
+ const connectorTools = await this.loadToolsForConnector(connector);
86
+ tools.push(...connectorTools);
87
+ }
88
+ logger.debug(`Available tools: ${tools.length}`);
89
+ return tools;
90
+ }
91
+ /**
92
+ * Dynamically load resources for a specific connector.
93
+ *
94
+ * @param connector The connector to load resources for.
95
+ * @returns The list of resources that were loaded in the target framework's format.
96
+ */
97
+ async loadResourcesForConnector(connector) {
98
+ const connectorResources = [];
99
+ const success = await this.ensureConnectorInitialized(connector);
100
+ if (!success) {
101
+ return [];
102
+ }
103
+ try {
104
+ const resourcesResult = await connector.listAllResources();
105
+ const resources = resourcesResult?.resources || [];
106
+ if (this.convertResource) {
107
+ for (const resource of resources) {
108
+ const converted = this.convertResource(resource, connector);
109
+ if (converted) {
110
+ connectorResources.push(converted);
111
+ }
112
+ }
113
+ }
114
+ logger.debug(
115
+ `Loaded ${connectorResources.length} new resources for connector: ${connectorResources.map((r) => r?.name ?? String(r)).join(", ")}`
116
+ );
117
+ } catch (err) {
118
+ logger.warn(`Error loading resources for connector: ${err}`);
119
+ }
120
+ return connectorResources;
121
+ }
122
+ /**
123
+ * Dynamically load prompts for a specific connector.
124
+ *
125
+ * @param connector The connector to load prompts for.
126
+ * @returns The list of prompts that were loaded in the target framework's format.
127
+ */
128
+ async loadPromptsForConnector(connector) {
129
+ const connectorPrompts = [];
130
+ const success = await this.ensureConnectorInitialized(connector);
131
+ if (!success) {
132
+ return [];
133
+ }
134
+ try {
135
+ const promptsResult = await connector.listPrompts();
136
+ const prompts = promptsResult?.prompts || [];
137
+ if (this.convertPrompt) {
138
+ for (const prompt of prompts) {
139
+ const converted = this.convertPrompt(prompt, connector);
140
+ if (converted) {
141
+ connectorPrompts.push(converted);
142
+ }
143
+ }
144
+ }
145
+ logger.debug(
146
+ `Loaded ${connectorPrompts.length} new prompts for connector: ${connectorPrompts.map((p) => p?.name ?? String(p)).join(", ")}`
147
+ );
148
+ } catch (err) {
149
+ logger.warn(`Error loading prompts for connector: ${err}`);
150
+ }
151
+ return connectorPrompts;
152
+ }
153
+ /**
154
+ * Create resources from MCP resources in all provided connectors.
155
+ *
156
+ * @param connectors List of MCP connectors to create resources from.
157
+ * @returns A promise that resolves with all converted resources.
158
+ */
159
+ async createResourcesFromConnectors(connectors) {
160
+ const resources = [];
161
+ for (const connector of connectors) {
162
+ const connectorResources = await this.loadResourcesForConnector(connector);
163
+ resources.push(...connectorResources);
164
+ }
165
+ logger.debug(`Available resources: ${resources.length}`);
166
+ return resources;
167
+ }
168
+ /**
169
+ * Create prompts from MCP prompts in all provided connectors.
170
+ *
171
+ * @param connectors List of MCP connectors to create prompts from.
172
+ * @returns A promise that resolves with all converted prompts.
173
+ */
174
+ async createPromptsFromConnectors(connectors) {
175
+ const prompts = [];
176
+ for (const connector of connectors) {
177
+ const connectorPrompts = await this.loadPromptsForConnector(connector);
178
+ prompts.push(...connectorPrompts);
179
+ }
180
+ logger.debug(`Available prompts: ${prompts.length}`);
181
+ return prompts;
182
+ }
183
+ /**
184
+ * Check if a connector is initialized and has tools.
185
+ *
186
+ * @param connector The connector to check.
187
+ * @returns True if the connector is initialized and has tools, false otherwise.
188
+ */
189
+ checkConnectorInitialized(connector) {
190
+ return Boolean(connector.tools && connector.tools.length);
191
+ }
192
+ /**
193
+ * Ensure a connector is initialized.
194
+ *
195
+ * @param connector The connector to initialize.
196
+ * @returns True if initialization succeeded, false otherwise.
197
+ */
198
+ async ensureConnectorInitialized(connector) {
199
+ if (!this.checkConnectorInitialized(connector)) {
200
+ logger.debug("Connector doesn't have tools, initializing it");
201
+ try {
202
+ await connector.initialize();
203
+ return true;
204
+ } catch (err) {
205
+ logger.error(`Error initializing connector: ${err}`);
206
+ return false;
207
+ }
208
+ }
209
+ return true;
210
+ }
211
+ };
212
+
213
+ // src/utils/json-schema-to-zod/JSONSchemaToZod.ts
214
+ import { z } from "zod";
215
+ var JSONSchemaToZod = class {
216
+ static {
217
+ __name(this, "JSONSchemaToZod");
218
+ }
219
+ /**
220
+ * Converts a JSON schema to a Zod schema.
221
+ *
222
+ * @param {JSONSchema} schema - The JSON schema.
223
+ * @returns {ZodSchema} - The Zod schema.
224
+ */
225
+ static convert(schema) {
226
+ return this.parseSchema(schema);
227
+ }
228
+ /**
229
+ * Checks if data matches a condition schema.
230
+ *
231
+ * @param {JSONValue} data - The data to check.
232
+ * @param {JSONSchema} condition - The condition schema.
233
+ * @returns {boolean} - Whether the data matches the condition.
234
+ */
235
+ static matchesCondition(data, condition) {
236
+ if (!condition.properties) {
237
+ return true;
238
+ }
239
+ if (typeof data !== "object" || data === null || Array.isArray(data)) {
240
+ return false;
241
+ }
242
+ const objectData = data;
243
+ for (const [key, propCondition] of Object.entries(condition.properties)) {
244
+ if (!(key in objectData)) {
245
+ if ("const" in propCondition) {
246
+ return false;
247
+ }
248
+ continue;
249
+ }
250
+ const value = objectData[key];
251
+ if ("const" in propCondition && value !== propCondition["const"]) {
252
+ return false;
253
+ }
254
+ if ("minimum" in propCondition && typeof value === "number" && value < propCondition["minimum"]) {
255
+ return false;
256
+ }
257
+ if ("maximum" in propCondition && typeof value === "number" && value > propCondition["maximum"]) {
258
+ return false;
259
+ }
260
+ }
261
+ return true;
262
+ }
263
+ /**
264
+ * Validates data against a conditional schema and adds issues to context if validation fails.
265
+ *
266
+ * @param {JSONValue} data - The data to validate.
267
+ * @param {JSONSchema} schema - The conditional schema.
268
+ * @param {z.RefinementCtx} ctx - The Zod refinement context.
269
+ */
270
+ static validateConditionalSchema(data, schema, ctx) {
271
+ this.validateRequiredProperties(data, schema, ctx);
272
+ this.validatePropertyPatterns(data, schema, ctx);
273
+ this.validateNestedConditions(data, schema, ctx);
274
+ }
275
+ /**
276
+ * Validates that all required properties are present in the data.
277
+ *
278
+ * @param {JSONValue} data - The data to validate.
279
+ * @param {JSONSchema} schema - The schema containing required properties.
280
+ * @param {z.RefinementCtx} ctx - The Zod refinement context.
281
+ */
282
+ static validateRequiredProperties(data, schema, ctx) {
283
+ if (!schema.required) {
284
+ return;
285
+ }
286
+ if (typeof data !== "object" || data === null) {
287
+ for (const requiredProp of schema.required) {
288
+ ctx.addIssue({
289
+ code: z.ZodIssueCode.custom,
290
+ message: `Required property '${requiredProp}' is missing`,
291
+ path: [requiredProp]
292
+ });
293
+ }
294
+ return;
295
+ }
296
+ for (const requiredProp of schema.required) {
297
+ if (!(requiredProp in data)) {
298
+ ctx.addIssue({
299
+ code: z.ZodIssueCode.custom,
300
+ message: `Required property '${requiredProp}' is missing`,
301
+ path: [requiredProp]
302
+ });
303
+ }
304
+ }
305
+ }
306
+ /**
307
+ * Validates property patterns for string properties.
308
+ *
309
+ * @param {JSONValue} data - The data to validate.
310
+ * @param {JSONSchema} schema - The schema containing property patterns.
311
+ * @param {z.RefinementCtx} ctx - The Zod refinement context.
312
+ */
313
+ static validatePropertyPatterns(data, schema, ctx) {
314
+ if (!schema.properties) {
315
+ return;
316
+ }
317
+ if (typeof data !== "object" || data === null) {
318
+ return;
319
+ }
320
+ if (Array.isArray(data)) {
321
+ return;
322
+ }
323
+ const objectData = data;
324
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
325
+ if (!(key in objectData)) {
326
+ continue;
327
+ }
328
+ const value = objectData[key];
329
+ if (propSchema["pattern"] && typeof value === "string") {
330
+ const regex = new RegExp(propSchema["pattern"]);
331
+ if (!regex.test(value)) {
332
+ ctx.addIssue({
333
+ code: z.ZodIssueCode.custom,
334
+ message: `String '${value}' does not match pattern '${propSchema["pattern"]}'`,
335
+ path: [key]
336
+ });
337
+ }
338
+ }
339
+ }
340
+ }
341
+ /**
342
+ * Validates nested if-then-else conditions.
343
+ *
344
+ * @param {JSONValue} data - The data to validate.
345
+ * @param {JSONSchema} schema - The schema containing if-then-else conditions.
346
+ * @param {z.RefinementCtx} ctx - The Zod refinement context.
347
+ */
348
+ static validateNestedConditions(data, schema, ctx) {
349
+ if (!schema["if"] || !schema["then"]) {
350
+ return;
351
+ }
352
+ const matchesIf = this.matchesCondition(data, schema["if"]);
353
+ if (matchesIf) {
354
+ this.validateConditionalSchema(data, schema["then"], ctx);
355
+ } else if (schema["else"]) {
356
+ this.validateConditionalSchema(data, schema["else"], ctx);
357
+ }
358
+ }
359
+ /**
360
+ * Parses a JSON schema and returns the corresponding Zod schema.
361
+ * This is the main entry point for schema conversion.
362
+ *
363
+ * @param {JSONSchema} schema - The JSON schema.
364
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
365
+ */
366
+ static parseSchema(schema) {
367
+ if (Array.isArray(schema.type)) {
368
+ return this.handleTypeArray(schema);
369
+ }
370
+ if (schema.oneOf || schema.anyOf || schema.allOf) {
371
+ return this.parseCombinator(schema);
372
+ }
373
+ if (schema["if"] && schema["then"]) {
374
+ return this.parseObject(schema);
375
+ }
376
+ if (schema.properties && (!schema.type || schema.type === "object")) {
377
+ return this.parseObject(schema);
378
+ }
379
+ return this.handleSingleType(schema);
380
+ }
381
+ /**
382
+ * Handles schemas with an array of types.
383
+ *
384
+ * @param {JSONSchema} schema - The JSON schema with type array.
385
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
386
+ */
387
+ static handleTypeArray(schema) {
388
+ if (!Array.isArray(schema.type)) {
389
+ throw new Error("Expected schema.type to be an array");
390
+ }
391
+ if (schema.type.includes("null")) {
392
+ return this.handleNullableType(schema);
393
+ }
394
+ return this.createUnionFromTypes(schema.type, schema);
395
+ }
396
+ /**
397
+ * Handles nullable types by creating a nullable schema.
398
+ *
399
+ * @param {JSONSchema} schema - The JSON schema with nullable type.
400
+ * @returns {ZodTypeAny} - The nullable Zod schema.
401
+ */
402
+ static handleNullableType(schema) {
403
+ if (!Array.isArray(schema.type)) {
404
+ throw new Error("Expected schema.type to be an array");
405
+ }
406
+ const nonNullSchema = { ...schema };
407
+ nonNullSchema.type = schema.type.filter((t) => t !== "null");
408
+ if (nonNullSchema.type.length === 1) {
409
+ const singleTypeSchema = this.handleSingleType({
410
+ ...schema,
411
+ type: nonNullSchema.type[0]
412
+ });
413
+ return singleTypeSchema.nullable();
414
+ }
415
+ const unionSchema = this.parseSchema(nonNullSchema);
416
+ return unionSchema.nullable();
417
+ }
418
+ /**
419
+ * Creates a union type from an array of types.
420
+ *
421
+ * @param {string[]} types - Array of type strings.
422
+ * @param {JSONSchema} baseSchema - The base schema to apply to each type.
423
+ * @returns {ZodTypeAny} - The union Zod schema.
424
+ */
425
+ static createUnionFromTypes(types, baseSchema) {
426
+ const schemas = types.map((type) => {
427
+ const singleTypeSchema = { ...baseSchema, type };
428
+ return this.parseSchema(singleTypeSchema);
429
+ });
430
+ return z.union(schemas);
431
+ }
432
+ /**
433
+ * Handles schemas with a single type.
434
+ *
435
+ * @param {JSONSchema} schema - The JSON schema with single type.
436
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
437
+ */
438
+ static handleSingleType(schema) {
439
+ if (schema.type === void 0) {
440
+ if (schema.oneOf || schema.anyOf || schema.allOf) {
441
+ return this.parseCombinator(schema);
442
+ }
443
+ if (schema.properties) {
444
+ return this.parseObject(schema);
445
+ }
446
+ return z.any();
447
+ }
448
+ switch (schema.type) {
449
+ case "string":
450
+ return this.parseString(schema);
451
+ case "number":
452
+ case "integer":
453
+ return this.parseNumberSchema(schema);
454
+ case "boolean":
455
+ return z.boolean();
456
+ case "array":
457
+ return this.parseArray(schema);
458
+ case "object":
459
+ return this.parseObject(schema);
460
+ default:
461
+ throw new Error("Unsupported schema type");
462
+ }
463
+ }
464
+ /**
465
+ * Parses a number schema.
466
+ *
467
+ * @param {JSONSchema} schema - The JSON schema for a number.
468
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
469
+ */
470
+ static parseNumberSchema(schema) {
471
+ const numberSchema = z.number();
472
+ let result = numberSchema;
473
+ result = this.applyNumberBounds(numberSchema, schema);
474
+ result = this.applyNumberMultipleOf(numberSchema, schema);
475
+ result = this.applyNumberEnum(numberSchema, schema);
476
+ result = this.applyIntegerConstraint(numberSchema, schema);
477
+ return result;
478
+ }
479
+ /**
480
+ * Applies bounds validation to a number schema.
481
+ *
482
+ * @param {z.ZodNumber} numberSchema - The base number schema.
483
+ * @param {JSONSchema} schema - The JSON schema with bounds.
484
+ * @returns {z.ZodNumber} - The updated schema with bounds validation.
485
+ */
486
+ static applyNumberBounds(numberSchema, schema) {
487
+ let result = numberSchema;
488
+ if (schema["minimum"] !== void 0) {
489
+ result = schema["exclusiveMinimum"] ? result.gt(schema["minimum"]) : result.gte(schema["minimum"]);
490
+ }
491
+ if (schema["maximum"] !== void 0) {
492
+ result = schema["exclusiveMaximum"] ? result.lt(schema["maximum"]) : result.lte(schema["maximum"]);
493
+ }
494
+ return result;
495
+ }
496
+ /**
497
+ * Applies multipleOf validation to a number schema.
498
+ *
499
+ * @param {z.ZodNumber} numberSchema - The base number schema.
500
+ * @param {JSONSchema} schema - The JSON schema with multipleOf.
501
+ * @returns {z.ZodNumber} - The updated schema with multipleOf validation.
502
+ */
503
+ static applyNumberMultipleOf(numberSchema, schema) {
504
+ if (schema["multipleOf"] === void 0) {
505
+ return numberSchema;
506
+ }
507
+ return numberSchema.refine((val) => val % schema["multipleOf"] === 0, {
508
+ message: `Number must be a multiple of ${schema["multipleOf"]}`
509
+ });
510
+ }
511
+ /**
512
+ * Applies enum validation to a number schema.
513
+ *
514
+ * @param {z.ZodNumber} numberSchema - The base number schema.
515
+ * @param {JSONSchema} schema - The JSON schema with enum.
516
+ * @returns {z.ZodNumber} - The updated schema with enum validation.
517
+ */
518
+ static applyNumberEnum(numberSchema, schema) {
519
+ if (!schema.enum) {
520
+ return numberSchema;
521
+ }
522
+ const numberEnums = schema.enum.filter(
523
+ (val) => typeof val === "number"
524
+ );
525
+ if (numberEnums.length === 0) {
526
+ return numberSchema;
527
+ }
528
+ return numberSchema.refine((val) => numberEnums.includes(val), {
529
+ message: `Number must be one of: ${numberEnums.join(", ")}`
530
+ });
531
+ }
532
+ /**
533
+ * Applies integer constraint to a number schema if needed.
534
+ *
535
+ * @param {z.ZodNumber} numberSchema - The base number schema.
536
+ * @param {JSONSchema} schema - The JSON schema.
537
+ * @returns {z.ZodNumber} - The updated schema with integer validation if needed.
538
+ */
539
+ static applyIntegerConstraint(numberSchema, schema) {
540
+ if (schema.type !== "integer") {
541
+ return numberSchema;
542
+ }
543
+ return numberSchema.refine((val) => Number.isInteger(val), {
544
+ message: "Number must be an integer"
545
+ });
546
+ }
547
+ /**
548
+ * Parses a string schema.
549
+ *
550
+ * @param {JSONSchema} schema - The JSON schema for a string.
551
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
552
+ */
553
+ static parseString(schema) {
554
+ const stringSchema = z.string();
555
+ let result = stringSchema;
556
+ if (schema.format) {
557
+ return this.applyStringFormat(stringSchema, schema);
558
+ } else {
559
+ result = this.applyStringPattern(stringSchema, schema);
560
+ result = this.applyStringLength(stringSchema, schema);
561
+ result = this.applyStringEnum(stringSchema, schema);
562
+ }
563
+ return result;
564
+ }
565
+ /**
566
+ * Applies format validation to a string schema.
567
+ *
568
+ * @param {z.ZodString} stringSchema - The base string schema.
569
+ * @param {JSONSchema} schema - The JSON schema with format.
570
+ * @returns {ZodTypeAny} - The updated schema with format validation.
571
+ */
572
+ static applyStringFormat(stringSchema, schema) {
573
+ if (!schema.format) {
574
+ return stringSchema;
575
+ }
576
+ switch (schema.format) {
577
+ case "email":
578
+ return stringSchema.email();
579
+ case "date-time":
580
+ return stringSchema.datetime();
581
+ case "uri":
582
+ return stringSchema.url();
583
+ case "uuid":
584
+ return stringSchema.uuid();
585
+ case "date":
586
+ return stringSchema.date();
587
+ default:
588
+ return stringSchema;
589
+ }
590
+ }
591
+ /**
592
+ * Applies pattern validation to a string schema.
593
+ *
594
+ * @param {z.ZodString} stringSchema - The base string schema.
595
+ * @param {JSONSchema} schema - The JSON schema with pattern.
596
+ * @returns {z.ZodString} - The updated schema with pattern validation.
597
+ */
598
+ static applyStringPattern(stringSchema, schema) {
599
+ if (!schema["pattern"]) {
600
+ return stringSchema;
601
+ }
602
+ const regex = new RegExp(schema["pattern"]);
603
+ return stringSchema.regex(regex, {
604
+ message: `String must match pattern: ${schema["pattern"]}`
605
+ });
606
+ }
607
+ /**
608
+ * Applies length constraints to a string schema.
609
+ *
610
+ * @param {z.ZodString} stringSchema - The base string schema.
611
+ * @param {JSONSchema} schema - The JSON schema with length constraints.
612
+ * @returns {z.ZodString} - The updated schema with length validation.
613
+ */
614
+ static applyStringLength(stringSchema, schema) {
615
+ const result = stringSchema;
616
+ if (schema["minLength"] !== void 0) {
617
+ stringSchema = stringSchema.min(schema["minLength"]);
618
+ }
619
+ if (schema["maxLength"] !== void 0) {
620
+ stringSchema = stringSchema.max(schema["maxLength"]);
621
+ }
622
+ return result;
623
+ }
624
+ /**
625
+ * Applies enum validation to a string schema.
626
+ *
627
+ * @param {z.ZodString} stringSchema - The base string schema.
628
+ * @param {JSONSchema} schema - The JSON schema with enum.
629
+ * @returns {ZodTypeAny} - The updated schema with enum validation.
630
+ */
631
+ static applyStringEnum(stringSchema, schema) {
632
+ if (!schema.enum) {
633
+ return stringSchema;
634
+ }
635
+ return stringSchema.refine((val) => schema.enum?.includes(val), {
636
+ message: `Value must be one of: ${schema.enum?.join(", ")}`
637
+ });
638
+ }
639
+ /**
640
+ * Parses a JSON schema of type array and returns the corresponding Zod schema.
641
+ *
642
+ * @param {JSONSchema} schema - The JSON schema.
643
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
644
+ */
645
+ static parseArray(schema) {
646
+ if (Array.isArray(schema.items)) {
647
+ const tupleSchemas = schema.items.map((item) => this.parseSchema(item));
648
+ return z.union(tupleSchemas);
649
+ }
650
+ const itemSchema = schema.items ? this.parseSchema(schema.items) : z.any();
651
+ const arraySchema = z.array(itemSchema);
652
+ let result = arraySchema;
653
+ result = this.applyArrayConstraints(arraySchema, schema);
654
+ return result;
655
+ }
656
+ /**
657
+ * Applies constraints to an array schema.
658
+ *
659
+ * @param {z.ZodArray<any>} arraySchema - The base array schema.
660
+ * @param {JSONSchema} schema - The JSON schema with array constraints.
661
+ * @returns {z.ZodTypeAny} - The updated array schema with constraints.
662
+ */
663
+ static applyArrayConstraints(arraySchema, schema) {
664
+ if (schema["minItems"] !== void 0) {
665
+ arraySchema = arraySchema.min(schema["minItems"]);
666
+ }
667
+ if (schema["maxItems"] !== void 0) {
668
+ arraySchema = arraySchema.max(schema["maxItems"]);
669
+ }
670
+ if (schema["uniqueItems"]) {
671
+ return arraySchema.refine(
672
+ (items) => new Set(items).size === items.length,
673
+ { message: "Array items must be unique" }
674
+ );
675
+ }
676
+ return arraySchema;
677
+ }
678
+ /**
679
+ * Parses an object schema.
680
+ *
681
+ * @param {JSONSchema} schema - The JSON schema for an object.
682
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
683
+ */
684
+ static parseObject(schema) {
685
+ if (schema["if"] && schema["then"]) {
686
+ return this.parseConditional(schema);
687
+ }
688
+ const shape = {};
689
+ this.processObjectProperties(schema, shape);
690
+ return this.processAdditionalProperties(schema, z.object(shape));
691
+ }
692
+ /**
693
+ * Processes object properties and builds the shape object.
694
+ *
695
+ * @param {JSONSchema} schema - The JSON schema for an object.
696
+ * @param {Record<string, ZodTypeAny>} shape - The shape object to populate.
697
+ */
698
+ static processObjectProperties(schema, shape) {
699
+ const required = new Set(schema.required || []);
700
+ if (!schema.properties) {
701
+ return;
702
+ }
703
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
704
+ const zodSchema = this.parseSchema(propSchema);
705
+ shape[key] = required.has(key) ? zodSchema : zodSchema.optional();
706
+ }
707
+ }
708
+ /**
709
+ * Processes additionalProperties configuration.
710
+ *
711
+ * @param {JSONSchema} schema - The JSON schema for an object.
712
+ * @param {z.ZodObject<any, any>} objectSchema - The Zod object schema.
713
+ * @returns {z.ZodObject<any, any>} - The updated Zod object schema.
714
+ */
715
+ static processAdditionalProperties(schema, objectSchema) {
716
+ if (schema.additionalProperties === true) {
717
+ return objectSchema.passthrough();
718
+ } else if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
719
+ const additionalPropSchema = this.parseSchema(
720
+ schema.additionalProperties
721
+ );
722
+ return objectSchema.catchall(additionalPropSchema);
723
+ } else {
724
+ return objectSchema.strict();
725
+ }
726
+ }
727
+ /**
728
+ * Parses a conditional schema with if-then-else.
729
+ *
730
+ * @param {JSONSchema} schema - The JSON schema with conditional validation.
731
+ * @returns {ZodTypeAny} - The conditional Zod schema.
732
+ */
733
+ static parseConditional(schema) {
734
+ const zodObject = this.createBaseObjectSchema(schema);
735
+ const ifCondition = schema["if"];
736
+ const thenSchema = schema["then"];
737
+ const elseSchema = schema["else"];
738
+ return zodObject.superRefine((data, ctx) => {
739
+ const dataWithDefaults = this.applyDefaultValues(
740
+ data,
741
+ schema
742
+ );
743
+ if (this.matchesCondition(dataWithDefaults, ifCondition)) {
744
+ this.validateConditionalSchema(dataWithDefaults, thenSchema, ctx);
745
+ } else if (elseSchema) {
746
+ this.validateConditionalSchema(dataWithDefaults, elseSchema, ctx);
747
+ }
748
+ });
749
+ }
750
+ /**
751
+ * Creates a base object schema from the given JSON schema.
752
+ *
753
+ * @param {JSONSchema} schema - The JSON schema.
754
+ * @returns {z.ZodObject<any, any>} - The base Zod object schema.
755
+ */
756
+ static createBaseObjectSchema(schema) {
757
+ const shape = {};
758
+ const required = new Set(schema.required || []);
759
+ for (const [key, value] of Object.entries(schema.properties || {})) {
760
+ const zodSchema = this.parseSchema(value);
761
+ shape[key] = required.has(key) ? zodSchema : zodSchema.optional();
762
+ }
763
+ const zodObject = z.object(shape);
764
+ return this.processAdditionalProperties(schema, zodObject);
765
+ }
766
+ /**
767
+ * Applies default values from schema properties to data object.
768
+ *
769
+ * @param {JSONValue} data - The original data object.
770
+ * @param {JSONSchema} schema - The schema with default values.
771
+ * @returns {JSONValue} - The data object with defaults applied.
772
+ */
773
+ static applyDefaultValues(data, schema) {
774
+ if (typeof data !== "object" || data === null) {
775
+ return data;
776
+ }
777
+ if (Array.isArray(data)) {
778
+ return data;
779
+ }
780
+ const objectData = data;
781
+ const dataWithDefaults = { ...objectData };
782
+ if (!schema.properties) {
783
+ return dataWithDefaults;
784
+ }
785
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
786
+ if (!(key in dataWithDefaults) && "default" in propSchema) {
787
+ dataWithDefaults[key] = propSchema["default"];
788
+ }
789
+ }
790
+ return dataWithDefaults;
791
+ }
792
+ /**
793
+ * Parses a schema with combinators (oneOf, anyOf, allOf).
794
+ * Delegates to the appropriate combinator parser based on which combinator is present.
795
+ *
796
+ * @param {JSONSchema} schema - The JSON schema with combinators.
797
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
798
+ */
799
+ static parseCombinator(schema) {
800
+ if (schema.oneOf) {
801
+ return this.parseOneOf(schema.oneOf);
802
+ }
803
+ if (schema.anyOf) {
804
+ return this.parseAnyOf(schema.anyOf);
805
+ }
806
+ if (schema.allOf) {
807
+ return this.parseAllOf(schema.allOf);
808
+ }
809
+ throw new Error("Unsupported schema type");
810
+ }
811
+ /**
812
+ * Parses a oneOf combinator schema.
813
+ *
814
+ * @param {JSONSchema[]} schemas - Array of JSON schemas in the oneOf.
815
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
816
+ */
817
+ static parseOneOf(schemas) {
818
+ return this.createUnionFromSchemas(schemas);
819
+ }
820
+ /**
821
+ * Parses an anyOf combinator schema.
822
+ *
823
+ * @param {JSONSchema[]} schemas - Array of JSON schemas in the anyOf.
824
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
825
+ */
826
+ static parseAnyOf(schemas) {
827
+ return this.createUnionFromSchemas(schemas);
828
+ }
829
+ /**
830
+ * Creates a union from an array of schemas, handling special cases.
831
+ *
832
+ * @param {JSONSchema[]} schemas - Array of JSON schemas to create a union from.
833
+ * @returns {ZodTypeAny} - The union Zod schema.
834
+ */
835
+ static createUnionFromSchemas(schemas) {
836
+ if (schemas.length === 0) {
837
+ return z.any();
838
+ }
839
+ if (schemas.length === 1) {
840
+ return this.parseSchema(schemas[0]);
841
+ }
842
+ const zodSchemas = [];
843
+ for (const subSchema of schemas) {
844
+ if (subSchema.type === "null") {
845
+ zodSchemas.push(z.null());
846
+ } else {
847
+ zodSchemas.push(this.parseSchema(subSchema));
848
+ }
849
+ }
850
+ if (zodSchemas.length >= 2) {
851
+ return z.union(zodSchemas);
852
+ } else if (zodSchemas.length === 1) {
853
+ return zodSchemas[0];
854
+ }
855
+ return z.any();
856
+ }
857
+ /**
858
+ * Parses an allOf combinator schema by merging all schemas.
859
+ *
860
+ * @param {JSONSchema[]} schemas - Array of JSON schemas in the allOf.
861
+ * @returns {ZodTypeAny} - The ZodTypeAny schema.
862
+ */
863
+ static parseAllOf(schemas) {
864
+ if (schemas.length === 0) {
865
+ return z.any();
866
+ }
867
+ if (schemas.length === 1) {
868
+ return this.parseSchema(schemas[0]);
869
+ }
870
+ const mergedSchema = schemas.reduce(
871
+ (acc, currentSchema) => this.mergeSchemas(acc, currentSchema)
872
+ );
873
+ return this.parseSchema(mergedSchema);
874
+ }
875
+ /**
876
+ * Merges two JSON schemas together.
877
+ *
878
+ * @param {JSONSchema} baseSchema - The base JSON schema.
879
+ * @param {JSONSchema} addSchema - The JSON schema to add.
880
+ * @returns {JSONSchema} - The merged JSON schema
881
+ */
882
+ static mergeSchemas(baseSchema, addSchema) {
883
+ const merged = { ...baseSchema, ...addSchema };
884
+ if (baseSchema.properties && addSchema.properties) {
885
+ const mergedProperties = {
886
+ ...baseSchema.properties,
887
+ ...addSchema.properties
888
+ };
889
+ merged.properties = mergedProperties;
890
+ }
891
+ if (baseSchema.required && addSchema.required) {
892
+ const mergedRequired = [
893
+ .../* @__PURE__ */ new Set([...baseSchema.required, ...addSchema.required])
894
+ ];
895
+ merged.required = mergedRequired;
896
+ }
897
+ return merged;
898
+ }
899
+ };
900
+
901
+ // src/adapters/langchain_adapter.ts
902
+ import { DynamicStructuredTool } from "@langchain/core/tools";
903
+ import { z as z2 } from "zod";
904
+ function schemaToZod(schema) {
905
+ try {
906
+ return JSONSchemaToZod.convert(schema);
907
+ } catch (err) {
908
+ logger.warn(`Failed to convert JSON schema to Zod: ${err}`);
909
+ return z2.any();
910
+ }
911
+ }
912
+ __name(schemaToZod, "schemaToZod");
913
+ var LangChainAdapter = class extends BaseAdapter {
914
+ static {
915
+ __name(this, "LangChainAdapter");
916
+ }
917
+ constructor(disallowedTools = []) {
918
+ super(disallowedTools);
919
+ }
920
+ /**
921
+ * Convert a single MCP tool specification into a LangChainJS structured tool.
922
+ */
923
+ convertTool(mcpTool, connector) {
924
+ if (this.disallowedTools.includes(mcpTool.name)) {
925
+ return null;
926
+ }
927
+ const argsSchema = mcpTool.inputSchema ? schemaToZod(mcpTool.inputSchema) : z2.object({}).optional();
928
+ const tool = new DynamicStructuredTool({
929
+ name: mcpTool.name ?? "NO NAME",
930
+ description: mcpTool.description ?? "",
931
+ // Blank is acceptable but discouraged.
932
+ schema: argsSchema,
933
+ func: /* @__PURE__ */ __name(async (input) => {
934
+ logger.debug(
935
+ `MCP tool "${mcpTool.name}" received input: ${JSON.stringify(input)}`
936
+ );
937
+ try {
938
+ const result = await connector.callTool(
939
+ mcpTool.name,
940
+ input
941
+ );
942
+ return JSON.stringify(result);
943
+ } catch (err) {
944
+ logger.error(`Error executing MCP tool: ${err.message}`);
945
+ return `Error executing MCP tool: ${String(err)}`;
946
+ }
947
+ }, "func")
948
+ });
949
+ return tool;
950
+ }
951
+ /**
952
+ * Convert a single MCP resource into a LangChainJS structured tool.
953
+ * Each resource becomes an async tool that returns its content when called.
954
+ */
955
+ convertResource(mcpResource, connector) {
956
+ const sanitizeName = /* @__PURE__ */ __name((name) => {
957
+ return name.replace(/[^A-Za-z0-9_]+/g, "_").toLowerCase().replace(/^_+|_+$/g, "");
958
+ }, "sanitizeName");
959
+ const resourceName = sanitizeName(
960
+ mcpResource.name || `resource_${mcpResource.uri}`
961
+ );
962
+ const resourceUri = mcpResource.uri;
963
+ const tool = new DynamicStructuredTool({
964
+ name: resourceName,
965
+ description: mcpResource.description || `Return the content of the resource located at URI ${resourceUri}.`,
966
+ schema: z2.object({}).optional(),
967
+ // Resources take no arguments
968
+ func: /* @__PURE__ */ __name(async () => {
969
+ logger.debug(`Resource tool: "${resourceName}" called`);
970
+ try {
971
+ const result = await connector.readResource(resourceUri);
972
+ if (result.contents && result.contents.length > 0) {
973
+ return result.contents.map((content) => {
974
+ if (typeof content === "string") {
975
+ return content;
976
+ }
977
+ if (content.text) {
978
+ return content.text;
979
+ }
980
+ if (content.uri) {
981
+ return content.uri;
982
+ }
983
+ return JSON.stringify(content);
984
+ }).join("\n");
985
+ }
986
+ return "Resource is empty or unavailable";
987
+ } catch (err) {
988
+ logger.error(`Error reading resource: ${err.message}`);
989
+ return `Error reading resource: ${String(err)}`;
990
+ }
991
+ }, "func")
992
+ });
993
+ return tool;
994
+ }
995
+ /**
996
+ * Convert a single MCP prompt into a LangChainJS structured tool.
997
+ * The resulting tool executes getPrompt on the connector with the prompt's name
998
+ * and the user-provided arguments (if any).
999
+ */
1000
+ convertPrompt(mcpPrompt, connector) {
1001
+ let argsSchema = z2.object({}).optional();
1002
+ if (mcpPrompt.arguments && mcpPrompt.arguments.length > 0) {
1003
+ const schemaFields = {};
1004
+ for (const arg of mcpPrompt.arguments) {
1005
+ const zodType = z2.string();
1006
+ if (arg.required !== false) {
1007
+ schemaFields[arg.name] = zodType;
1008
+ } else {
1009
+ schemaFields[arg.name] = zodType.optional();
1010
+ }
1011
+ }
1012
+ argsSchema = Object.keys(schemaFields).length > 0 ? z2.object(schemaFields) : z2.object({}).optional();
1013
+ }
1014
+ const tool = new DynamicStructuredTool({
1015
+ name: mcpPrompt.name,
1016
+ description: mcpPrompt.description || "",
1017
+ schema: argsSchema,
1018
+ func: /* @__PURE__ */ __name(async (input) => {
1019
+ logger.debug(
1020
+ `Prompt tool: "${mcpPrompt.name}" called with args: ${JSON.stringify(input)}`
1021
+ );
1022
+ try {
1023
+ const result = await connector.getPrompt(mcpPrompt.name, input);
1024
+ if (result.messages && result.messages.length > 0) {
1025
+ return result.messages.map((msg) => {
1026
+ if (typeof msg === "string") {
1027
+ return msg;
1028
+ }
1029
+ if (msg.content) {
1030
+ return typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
1031
+ }
1032
+ return JSON.stringify(msg);
1033
+ }).join("\n");
1034
+ }
1035
+ return "Prompt returned no messages";
1036
+ } catch (err) {
1037
+ logger.error(`Error getting prompt: ${err.message}`);
1038
+ return `Error getting prompt: ${String(err)}`;
1039
+ }
1040
+ }, "func")
1041
+ });
1042
+ return tool;
1043
+ }
1044
+ };
1045
+
1046
+ export {
1047
+ BaseAdapter,
1048
+ LangChainAdapter
1049
+ };