mcp-use 1.11.0-canary.7 → 1.11.0-canary.8
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/.tsbuildinfo +1 -1
- package/dist/{chunk-ZN3MKSKM.js → chunk-4LZSXUFM.js} +1 -1
- package/dist/{chunk-OD6B7KGQ.js → chunk-5RTMAOZ6.js} +20 -1057
- package/dist/{chunk-REYY7LSD.js → chunk-EBSNALCB.js} +2 -2
- package/dist/{chunk-M7CHBY4S.js → chunk-GVU7C2ZD.js} +1 -1
- package/dist/chunk-GXNAXUDI.js +0 -0
- package/dist/{chunk-3QVRNWW7.js → chunk-JPKFN73V.js} +1 -1
- package/dist/{chunk-REX2YTWF.js → chunk-JZNXOM7C.js} +1 -1
- package/dist/chunk-MFSO5PUW.js +1049 -0
- package/dist/{chunk-QP7MQ2UJ.js → chunk-TAEHPLGV.js} +2 -2
- package/dist/{chunk-WTGUJLTR.js → chunk-X7JKFBPN.js} +18 -4
- package/dist/{chunk-CHHWJQVC.js → chunk-XKTBHYNM.js} +1 -1
- package/dist/index.cjs +17 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -17
- package/dist/src/adapters/index.cjs +1346 -0
- package/dist/src/adapters/index.js +11 -0
- package/dist/src/agents/index.cjs +1 -1
- package/dist/src/agents/index.js +7 -6
- package/dist/src/browser.cjs +1 -2
- package/dist/src/browser.d.ts +1 -1
- package/dist/src/browser.d.ts.map +1 -1
- package/dist/src/browser.js +11 -10
- package/dist/src/client/prompts.js +4 -4
- package/dist/src/react/index.cjs +17 -3
- package/dist/src/react/index.js +5 -5
- package/dist/src/react/useWidget.d.ts +11 -7
- package/dist/src/react/useWidget.d.ts.map +1 -1
- package/dist/src/react/widget-types.d.ts +6 -2
- package/dist/src/react/widget-types.d.ts.map +1 -1
- package/dist/src/server/index.cjs +131 -24
- package/dist/src/server/index.js +135 -28
- package/dist/src/server/mcp-server.d.ts +1 -0
- package/dist/src/server/mcp-server.d.ts.map +1 -1
- package/dist/src/server/types/common.d.ts +23 -0
- package/dist/src/server/types/common.d.ts.map +1 -1
- package/dist/src/server/types/resource.d.ts +16 -0
- package/dist/src/server/types/resource.d.ts.map +1 -1
- package/dist/src/server/types/widget.d.ts +21 -2
- package/dist/src/server/types/widget.d.ts.map +1 -1
- package/dist/src/server/utils/response-helpers.d.ts +10 -6
- package/dist/src/server/utils/response-helpers.d.ts.map +1 -1
- package/dist/src/server/widgets/index.d.ts +1 -1
- package/dist/src/server/widgets/index.d.ts.map +1 -1
- package/dist/src/server/widgets/mount-widgets-dev.d.ts.map +1 -1
- package/dist/src/server/widgets/setup-widget-routes.d.ts.map +1 -1
- package/dist/src/server/widgets/ui-resource-registration.d.ts.map +1 -1
- package/dist/src/server/widgets/widget-helpers.d.ts +22 -0
- package/dist/src/server/widgets/widget-helpers.d.ts.map +1 -1
- package/dist/src/server/widgets/widget-types.d.ts +2 -0
- package/dist/src/server/widgets/widget-types.d.ts.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/{tool-execution-helpers-PAFGGAGL.js → tool-execution-helpers-EYAIJERC.js} +2 -2
- package/dist/tsup.config.d.ts.map +1 -1
- package/package.json +16 -5
- package/dist/chunk-5LBXMCKC.js +0 -4711
- /package/dist/{chunk-H4BZVTGK.js → chunk-LGDFGYRL.js} +0 -0
package/dist/chunk-5LBXMCKC.js
DELETED
|
@@ -1,4711 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BaseMCPClient,
|
|
3
|
-
ConnectionManager,
|
|
4
|
-
HttpConnector,
|
|
5
|
-
MCPSession
|
|
6
|
-
} from "./chunk-QP7MQ2UJ.js";
|
|
7
|
-
import {
|
|
8
|
-
CodeModeConnector
|
|
9
|
-
} from "./chunk-REX2YTWF.js";
|
|
10
|
-
import {
|
|
11
|
-
BaseConnector
|
|
12
|
-
} from "./chunk-CHHWJQVC.js";
|
|
13
|
-
import {
|
|
14
|
-
Tel,
|
|
15
|
-
Telemetry,
|
|
16
|
-
extractModelInfo,
|
|
17
|
-
getPackageVersion
|
|
18
|
-
} from "./chunk-3QVRNWW7.js";
|
|
19
|
-
import {
|
|
20
|
-
logger
|
|
21
|
-
} from "./chunk-FRUZDWXH.js";
|
|
22
|
-
import {
|
|
23
|
-
__name,
|
|
24
|
-
__require
|
|
25
|
-
} from "./chunk-3GQAWCBQ.js";
|
|
26
|
-
|
|
27
|
-
// src/adapters/base.ts
|
|
28
|
-
var BaseAdapter = class {
|
|
29
|
-
static {
|
|
30
|
-
__name(this, "BaseAdapter");
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* List of tool names that should not be available.
|
|
34
|
-
*/
|
|
35
|
-
disallowedTools;
|
|
36
|
-
/**
|
|
37
|
-
* Internal cache that maps a connector instance to the list of tools
|
|
38
|
-
* generated for it.
|
|
39
|
-
*/
|
|
40
|
-
connectorToolMap = /* @__PURE__ */ new Map();
|
|
41
|
-
constructor(disallowedTools) {
|
|
42
|
-
this.disallowedTools = disallowedTools ?? [];
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Create tools from an MCPClient instance.
|
|
46
|
-
*
|
|
47
|
-
* This is the recommended way to create tools from an MCPClient, as it handles
|
|
48
|
-
* session creation and connector extraction automatically.
|
|
49
|
-
*
|
|
50
|
-
* @param client The MCPClient to extract tools from.
|
|
51
|
-
* @param disallowedTools Optional list of tool names to exclude.
|
|
52
|
-
* @returns A promise that resolves with a list of converted tools.
|
|
53
|
-
*/
|
|
54
|
-
static async createTools(client, disallowedTools) {
|
|
55
|
-
const adapter = new this(disallowedTools);
|
|
56
|
-
if (!client.activeSessions || Object.keys(client.activeSessions).length === 0) {
|
|
57
|
-
logger.info("No active sessions found, creating new ones...");
|
|
58
|
-
await client.createAllSessions();
|
|
59
|
-
}
|
|
60
|
-
const sessions = client.getAllActiveSessions();
|
|
61
|
-
const connectors = Object.values(sessions).map(
|
|
62
|
-
(session) => session.connector
|
|
63
|
-
);
|
|
64
|
-
return adapter.createToolsFromConnectors(connectors);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Dynamically load tools for a specific connector.
|
|
68
|
-
*
|
|
69
|
-
* @param connector The connector to load tools for.
|
|
70
|
-
* @returns The list of tools that were loaded in the target framework's format.
|
|
71
|
-
*/
|
|
72
|
-
async loadToolsForConnector(connector) {
|
|
73
|
-
if (this.connectorToolMap.has(connector)) {
|
|
74
|
-
const cached = this.connectorToolMap.get(connector);
|
|
75
|
-
logger.debug(`Returning ${cached.length} existing tools for connector`);
|
|
76
|
-
return cached;
|
|
77
|
-
}
|
|
78
|
-
const connectorTools = [];
|
|
79
|
-
const success = await this.ensureConnectorInitialized(connector);
|
|
80
|
-
if (!success) {
|
|
81
|
-
return [];
|
|
82
|
-
}
|
|
83
|
-
for (const tool of connector.tools) {
|
|
84
|
-
const converted = this.convertTool(tool, connector);
|
|
85
|
-
if (converted) {
|
|
86
|
-
connectorTools.push(converted);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
this.connectorToolMap.set(connector, connectorTools);
|
|
90
|
-
logger.debug(
|
|
91
|
-
`Loaded ${connectorTools.length} new tools for connector: ${connectorTools.map((t) => t?.name ?? String(t)).join(", ")}`
|
|
92
|
-
);
|
|
93
|
-
return connectorTools;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Create tools from MCP tools in all provided connectors.
|
|
97
|
-
*
|
|
98
|
-
* @param connectors List of MCP connectors to create tools from.
|
|
99
|
-
* @returns A promise that resolves with all converted tools.
|
|
100
|
-
*/
|
|
101
|
-
async createToolsFromConnectors(connectors) {
|
|
102
|
-
const tools = [];
|
|
103
|
-
for (const connector of connectors) {
|
|
104
|
-
const connectorTools = await this.loadToolsForConnector(connector);
|
|
105
|
-
tools.push(...connectorTools);
|
|
106
|
-
}
|
|
107
|
-
logger.debug(`Available tools: ${tools.length}`);
|
|
108
|
-
return tools;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Dynamically load resources for a specific connector.
|
|
112
|
-
*
|
|
113
|
-
* @param connector The connector to load resources for.
|
|
114
|
-
* @returns The list of resources that were loaded in the target framework's format.
|
|
115
|
-
*/
|
|
116
|
-
async loadResourcesForConnector(connector) {
|
|
117
|
-
const connectorResources = [];
|
|
118
|
-
const success = await this.ensureConnectorInitialized(connector);
|
|
119
|
-
if (!success) {
|
|
120
|
-
return [];
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
const resourcesResult = await connector.listAllResources();
|
|
124
|
-
const resources = resourcesResult?.resources || [];
|
|
125
|
-
if (this.convertResource) {
|
|
126
|
-
for (const resource of resources) {
|
|
127
|
-
const converted = this.convertResource(resource, connector);
|
|
128
|
-
if (converted) {
|
|
129
|
-
connectorResources.push(converted);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
logger.debug(
|
|
134
|
-
`Loaded ${connectorResources.length} new resources for connector: ${connectorResources.map((r) => r?.name ?? String(r)).join(", ")}`
|
|
135
|
-
);
|
|
136
|
-
} catch (err) {
|
|
137
|
-
logger.warn(`Error loading resources for connector: ${err}`);
|
|
138
|
-
}
|
|
139
|
-
return connectorResources;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Dynamically load prompts for a specific connector.
|
|
143
|
-
*
|
|
144
|
-
* @param connector The connector to load prompts for.
|
|
145
|
-
* @returns The list of prompts that were loaded in the target framework's format.
|
|
146
|
-
*/
|
|
147
|
-
async loadPromptsForConnector(connector) {
|
|
148
|
-
const connectorPrompts = [];
|
|
149
|
-
const success = await this.ensureConnectorInitialized(connector);
|
|
150
|
-
if (!success) {
|
|
151
|
-
return [];
|
|
152
|
-
}
|
|
153
|
-
try {
|
|
154
|
-
const promptsResult = await connector.listPrompts();
|
|
155
|
-
const prompts = promptsResult?.prompts || [];
|
|
156
|
-
if (this.convertPrompt) {
|
|
157
|
-
for (const prompt of prompts) {
|
|
158
|
-
const converted = this.convertPrompt(prompt, connector);
|
|
159
|
-
if (converted) {
|
|
160
|
-
connectorPrompts.push(converted);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
logger.debug(
|
|
165
|
-
`Loaded ${connectorPrompts.length} new prompts for connector: ${connectorPrompts.map((p) => p?.name ?? String(p)).join(", ")}`
|
|
166
|
-
);
|
|
167
|
-
} catch (err) {
|
|
168
|
-
logger.warn(`Error loading prompts for connector: ${err}`);
|
|
169
|
-
}
|
|
170
|
-
return connectorPrompts;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Create resources from MCP resources in all provided connectors.
|
|
174
|
-
*
|
|
175
|
-
* @param connectors List of MCP connectors to create resources from.
|
|
176
|
-
* @returns A promise that resolves with all converted resources.
|
|
177
|
-
*/
|
|
178
|
-
async createResourcesFromConnectors(connectors) {
|
|
179
|
-
const resources = [];
|
|
180
|
-
for (const connector of connectors) {
|
|
181
|
-
const connectorResources = await this.loadResourcesForConnector(connector);
|
|
182
|
-
resources.push(...connectorResources);
|
|
183
|
-
}
|
|
184
|
-
logger.debug(`Available resources: ${resources.length}`);
|
|
185
|
-
return resources;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Create prompts from MCP prompts in all provided connectors.
|
|
189
|
-
*
|
|
190
|
-
* @param connectors List of MCP connectors to create prompts from.
|
|
191
|
-
* @returns A promise that resolves with all converted prompts.
|
|
192
|
-
*/
|
|
193
|
-
async createPromptsFromConnectors(connectors) {
|
|
194
|
-
const prompts = [];
|
|
195
|
-
for (const connector of connectors) {
|
|
196
|
-
const connectorPrompts = await this.loadPromptsForConnector(connector);
|
|
197
|
-
prompts.push(...connectorPrompts);
|
|
198
|
-
}
|
|
199
|
-
logger.debug(`Available prompts: ${prompts.length}`);
|
|
200
|
-
return prompts;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Check if a connector is initialized and has tools.
|
|
204
|
-
*
|
|
205
|
-
* @param connector The connector to check.
|
|
206
|
-
* @returns True if the connector is initialized and has tools, false otherwise.
|
|
207
|
-
*/
|
|
208
|
-
checkConnectorInitialized(connector) {
|
|
209
|
-
return Boolean(connector.tools && connector.tools.length);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Ensure a connector is initialized.
|
|
213
|
-
*
|
|
214
|
-
* @param connector The connector to initialize.
|
|
215
|
-
* @returns True if initialization succeeded, false otherwise.
|
|
216
|
-
*/
|
|
217
|
-
async ensureConnectorInitialized(connector) {
|
|
218
|
-
if (!this.checkConnectorInitialized(connector)) {
|
|
219
|
-
logger.debug("Connector doesn't have tools, initializing it");
|
|
220
|
-
try {
|
|
221
|
-
await connector.initialize();
|
|
222
|
-
return true;
|
|
223
|
-
} catch (err) {
|
|
224
|
-
logger.error(`Error initializing connector: ${err}`);
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return true;
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
// src/utils/json-schema-to-zod/JSONSchemaToZod.ts
|
|
233
|
-
import { z } from "zod";
|
|
234
|
-
var JSONSchemaToZod = class {
|
|
235
|
-
static {
|
|
236
|
-
__name(this, "JSONSchemaToZod");
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Converts a JSON schema to a Zod schema.
|
|
240
|
-
*
|
|
241
|
-
* @param {JSONSchema} schema - The JSON schema.
|
|
242
|
-
* @returns {ZodSchema} - The Zod schema.
|
|
243
|
-
*/
|
|
244
|
-
static convert(schema) {
|
|
245
|
-
return this.parseSchema(schema);
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Checks if data matches a condition schema.
|
|
249
|
-
*
|
|
250
|
-
* @param {JSONValue} data - The data to check.
|
|
251
|
-
* @param {JSONSchema} condition - The condition schema.
|
|
252
|
-
* @returns {boolean} - Whether the data matches the condition.
|
|
253
|
-
*/
|
|
254
|
-
static matchesCondition(data, condition) {
|
|
255
|
-
if (!condition.properties) {
|
|
256
|
-
return true;
|
|
257
|
-
}
|
|
258
|
-
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
const objectData = data;
|
|
262
|
-
for (const [key, propCondition] of Object.entries(condition.properties)) {
|
|
263
|
-
if (!(key in objectData)) {
|
|
264
|
-
if ("const" in propCondition) {
|
|
265
|
-
return false;
|
|
266
|
-
}
|
|
267
|
-
continue;
|
|
268
|
-
}
|
|
269
|
-
const value = objectData[key];
|
|
270
|
-
if ("const" in propCondition && value !== propCondition["const"]) {
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
if ("minimum" in propCondition && typeof value === "number" && value < propCondition["minimum"]) {
|
|
274
|
-
return false;
|
|
275
|
-
}
|
|
276
|
-
if ("maximum" in propCondition && typeof value === "number" && value > propCondition["maximum"]) {
|
|
277
|
-
return false;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Validates data against a conditional schema and adds issues to context if validation fails.
|
|
284
|
-
*
|
|
285
|
-
* @param {JSONValue} data - The data to validate.
|
|
286
|
-
* @param {JSONSchema} schema - The conditional schema.
|
|
287
|
-
* @param {z.RefinementCtx} ctx - The Zod refinement context.
|
|
288
|
-
*/
|
|
289
|
-
static validateConditionalSchema(data, schema, ctx) {
|
|
290
|
-
this.validateRequiredProperties(data, schema, ctx);
|
|
291
|
-
this.validatePropertyPatterns(data, schema, ctx);
|
|
292
|
-
this.validateNestedConditions(data, schema, ctx);
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Validates that all required properties are present in the data.
|
|
296
|
-
*
|
|
297
|
-
* @param {JSONValue} data - The data to validate.
|
|
298
|
-
* @param {JSONSchema} schema - The schema containing required properties.
|
|
299
|
-
* @param {z.RefinementCtx} ctx - The Zod refinement context.
|
|
300
|
-
*/
|
|
301
|
-
static validateRequiredProperties(data, schema, ctx) {
|
|
302
|
-
if (!schema.required) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
if (typeof data !== "object" || data === null) {
|
|
306
|
-
for (const requiredProp of schema.required) {
|
|
307
|
-
ctx.addIssue({
|
|
308
|
-
code: z.ZodIssueCode.custom,
|
|
309
|
-
message: `Required property '${requiredProp}' is missing`,
|
|
310
|
-
path: [requiredProp]
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
for (const requiredProp of schema.required) {
|
|
316
|
-
if (!(requiredProp in data)) {
|
|
317
|
-
ctx.addIssue({
|
|
318
|
-
code: z.ZodIssueCode.custom,
|
|
319
|
-
message: `Required property '${requiredProp}' is missing`,
|
|
320
|
-
path: [requiredProp]
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Validates property patterns for string properties.
|
|
327
|
-
*
|
|
328
|
-
* @param {JSONValue} data - The data to validate.
|
|
329
|
-
* @param {JSONSchema} schema - The schema containing property patterns.
|
|
330
|
-
* @param {z.RefinementCtx} ctx - The Zod refinement context.
|
|
331
|
-
*/
|
|
332
|
-
static validatePropertyPatterns(data, schema, ctx) {
|
|
333
|
-
if (!schema.properties) {
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
if (typeof data !== "object" || data === null) {
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
if (Array.isArray(data)) {
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
const objectData = data;
|
|
343
|
-
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
344
|
-
if (!(key in objectData)) {
|
|
345
|
-
continue;
|
|
346
|
-
}
|
|
347
|
-
const value = objectData[key];
|
|
348
|
-
if (propSchema["pattern"] && typeof value === "string") {
|
|
349
|
-
const regex = new RegExp(propSchema["pattern"]);
|
|
350
|
-
if (!regex.test(value)) {
|
|
351
|
-
ctx.addIssue({
|
|
352
|
-
code: z.ZodIssueCode.custom,
|
|
353
|
-
message: `String '${value}' does not match pattern '${propSchema["pattern"]}'`,
|
|
354
|
-
path: [key]
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Validates nested if-then-else conditions.
|
|
362
|
-
*
|
|
363
|
-
* @param {JSONValue} data - The data to validate.
|
|
364
|
-
* @param {JSONSchema} schema - The schema containing if-then-else conditions.
|
|
365
|
-
* @param {z.RefinementCtx} ctx - The Zod refinement context.
|
|
366
|
-
*/
|
|
367
|
-
static validateNestedConditions(data, schema, ctx) {
|
|
368
|
-
if (!schema["if"] || !schema["then"]) {
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
const matchesIf = this.matchesCondition(data, schema["if"]);
|
|
372
|
-
if (matchesIf) {
|
|
373
|
-
this.validateConditionalSchema(data, schema["then"], ctx);
|
|
374
|
-
} else if (schema["else"]) {
|
|
375
|
-
this.validateConditionalSchema(data, schema["else"], ctx);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Parses a JSON schema and returns the corresponding Zod schema.
|
|
380
|
-
* This is the main entry point for schema conversion.
|
|
381
|
-
*
|
|
382
|
-
* @param {JSONSchema} schema - The JSON schema.
|
|
383
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
384
|
-
*/
|
|
385
|
-
static parseSchema(schema) {
|
|
386
|
-
if (Array.isArray(schema.type)) {
|
|
387
|
-
return this.handleTypeArray(schema);
|
|
388
|
-
}
|
|
389
|
-
if (schema.oneOf || schema.anyOf || schema.allOf) {
|
|
390
|
-
return this.parseCombinator(schema);
|
|
391
|
-
}
|
|
392
|
-
if (schema["if"] && schema["then"]) {
|
|
393
|
-
return this.parseObject(schema);
|
|
394
|
-
}
|
|
395
|
-
if (schema.properties && (!schema.type || schema.type === "object")) {
|
|
396
|
-
return this.parseObject(schema);
|
|
397
|
-
}
|
|
398
|
-
return this.handleSingleType(schema);
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Handles schemas with an array of types.
|
|
402
|
-
*
|
|
403
|
-
* @param {JSONSchema} schema - The JSON schema with type array.
|
|
404
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
405
|
-
*/
|
|
406
|
-
static handleTypeArray(schema) {
|
|
407
|
-
if (!Array.isArray(schema.type)) {
|
|
408
|
-
throw new Error("Expected schema.type to be an array");
|
|
409
|
-
}
|
|
410
|
-
if (schema.type.includes("null")) {
|
|
411
|
-
return this.handleNullableType(schema);
|
|
412
|
-
}
|
|
413
|
-
return this.createUnionFromTypes(schema.type, schema);
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Handles nullable types by creating a nullable schema.
|
|
417
|
-
*
|
|
418
|
-
* @param {JSONSchema} schema - The JSON schema with nullable type.
|
|
419
|
-
* @returns {ZodTypeAny} - The nullable Zod schema.
|
|
420
|
-
*/
|
|
421
|
-
static handleNullableType(schema) {
|
|
422
|
-
if (!Array.isArray(schema.type)) {
|
|
423
|
-
throw new Error("Expected schema.type to be an array");
|
|
424
|
-
}
|
|
425
|
-
const nonNullSchema = { ...schema };
|
|
426
|
-
nonNullSchema.type = schema.type.filter((t) => t !== "null");
|
|
427
|
-
if (nonNullSchema.type.length === 1) {
|
|
428
|
-
const singleTypeSchema = this.handleSingleType({
|
|
429
|
-
...schema,
|
|
430
|
-
type: nonNullSchema.type[0]
|
|
431
|
-
});
|
|
432
|
-
return singleTypeSchema.nullable();
|
|
433
|
-
}
|
|
434
|
-
const unionSchema = this.parseSchema(nonNullSchema);
|
|
435
|
-
return unionSchema.nullable();
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Creates a union type from an array of types.
|
|
439
|
-
*
|
|
440
|
-
* @param {string[]} types - Array of type strings.
|
|
441
|
-
* @param {JSONSchema} baseSchema - The base schema to apply to each type.
|
|
442
|
-
* @returns {ZodTypeAny} - The union Zod schema.
|
|
443
|
-
*/
|
|
444
|
-
static createUnionFromTypes(types, baseSchema) {
|
|
445
|
-
const schemas = types.map((type) => {
|
|
446
|
-
const singleTypeSchema = { ...baseSchema, type };
|
|
447
|
-
return this.parseSchema(singleTypeSchema);
|
|
448
|
-
});
|
|
449
|
-
return z.union(schemas);
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Handles schemas with a single type.
|
|
453
|
-
*
|
|
454
|
-
* @param {JSONSchema} schema - The JSON schema with single type.
|
|
455
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
456
|
-
*/
|
|
457
|
-
static handleSingleType(schema) {
|
|
458
|
-
if (schema.type === void 0) {
|
|
459
|
-
if (schema.oneOf || schema.anyOf || schema.allOf) {
|
|
460
|
-
return this.parseCombinator(schema);
|
|
461
|
-
}
|
|
462
|
-
if (schema.properties) {
|
|
463
|
-
return this.parseObject(schema);
|
|
464
|
-
}
|
|
465
|
-
return z.any();
|
|
466
|
-
}
|
|
467
|
-
switch (schema.type) {
|
|
468
|
-
case "string":
|
|
469
|
-
return this.parseString(schema);
|
|
470
|
-
case "number":
|
|
471
|
-
case "integer":
|
|
472
|
-
return this.parseNumberSchema(schema);
|
|
473
|
-
case "boolean":
|
|
474
|
-
return z.boolean();
|
|
475
|
-
case "array":
|
|
476
|
-
return this.parseArray(schema);
|
|
477
|
-
case "object":
|
|
478
|
-
return this.parseObject(schema);
|
|
479
|
-
default:
|
|
480
|
-
throw new Error("Unsupported schema type");
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
/**
|
|
484
|
-
* Parses a number schema.
|
|
485
|
-
*
|
|
486
|
-
* @param {JSONSchema} schema - The JSON schema for a number.
|
|
487
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
488
|
-
*/
|
|
489
|
-
static parseNumberSchema(schema) {
|
|
490
|
-
const numberSchema = z.number();
|
|
491
|
-
let result = numberSchema;
|
|
492
|
-
result = this.applyNumberBounds(numberSchema, schema);
|
|
493
|
-
result = this.applyNumberMultipleOf(numberSchema, schema);
|
|
494
|
-
result = this.applyNumberEnum(numberSchema, schema);
|
|
495
|
-
result = this.applyIntegerConstraint(numberSchema, schema);
|
|
496
|
-
return result;
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Applies bounds validation to a number schema.
|
|
500
|
-
*
|
|
501
|
-
* @param {z.ZodNumber} numberSchema - The base number schema.
|
|
502
|
-
* @param {JSONSchema} schema - The JSON schema with bounds.
|
|
503
|
-
* @returns {z.ZodNumber} - The updated schema with bounds validation.
|
|
504
|
-
*/
|
|
505
|
-
static applyNumberBounds(numberSchema, schema) {
|
|
506
|
-
let result = numberSchema;
|
|
507
|
-
if (schema["minimum"] !== void 0) {
|
|
508
|
-
result = schema["exclusiveMinimum"] ? result.gt(schema["minimum"]) : result.gte(schema["minimum"]);
|
|
509
|
-
}
|
|
510
|
-
if (schema["maximum"] !== void 0) {
|
|
511
|
-
result = schema["exclusiveMaximum"] ? result.lt(schema["maximum"]) : result.lte(schema["maximum"]);
|
|
512
|
-
}
|
|
513
|
-
return result;
|
|
514
|
-
}
|
|
515
|
-
/**
|
|
516
|
-
* Applies multipleOf validation to a number schema.
|
|
517
|
-
*
|
|
518
|
-
* @param {z.ZodNumber} numberSchema - The base number schema.
|
|
519
|
-
* @param {JSONSchema} schema - The JSON schema with multipleOf.
|
|
520
|
-
* @returns {z.ZodNumber} - The updated schema with multipleOf validation.
|
|
521
|
-
*/
|
|
522
|
-
static applyNumberMultipleOf(numberSchema, schema) {
|
|
523
|
-
if (schema["multipleOf"] === void 0) {
|
|
524
|
-
return numberSchema;
|
|
525
|
-
}
|
|
526
|
-
return numberSchema.refine((val) => val % schema["multipleOf"] === 0, {
|
|
527
|
-
message: `Number must be a multiple of ${schema["multipleOf"]}`
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* Applies enum validation to a number schema.
|
|
532
|
-
*
|
|
533
|
-
* @param {z.ZodNumber} numberSchema - The base number schema.
|
|
534
|
-
* @param {JSONSchema} schema - The JSON schema with enum.
|
|
535
|
-
* @returns {z.ZodNumber} - The updated schema with enum validation.
|
|
536
|
-
*/
|
|
537
|
-
static applyNumberEnum(numberSchema, schema) {
|
|
538
|
-
if (!schema.enum) {
|
|
539
|
-
return numberSchema;
|
|
540
|
-
}
|
|
541
|
-
const numberEnums = schema.enum.filter(
|
|
542
|
-
(val) => typeof val === "number"
|
|
543
|
-
);
|
|
544
|
-
if (numberEnums.length === 0) {
|
|
545
|
-
return numberSchema;
|
|
546
|
-
}
|
|
547
|
-
return numberSchema.refine((val) => numberEnums.includes(val), {
|
|
548
|
-
message: `Number must be one of: ${numberEnums.join(", ")}`
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Applies integer constraint to a number schema if needed.
|
|
553
|
-
*
|
|
554
|
-
* @param {z.ZodNumber} numberSchema - The base number schema.
|
|
555
|
-
* @param {JSONSchema} schema - The JSON schema.
|
|
556
|
-
* @returns {z.ZodNumber} - The updated schema with integer validation if needed.
|
|
557
|
-
*/
|
|
558
|
-
static applyIntegerConstraint(numberSchema, schema) {
|
|
559
|
-
if (schema.type !== "integer") {
|
|
560
|
-
return numberSchema;
|
|
561
|
-
}
|
|
562
|
-
return numberSchema.refine((val) => Number.isInteger(val), {
|
|
563
|
-
message: "Number must be an integer"
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Parses a string schema.
|
|
568
|
-
*
|
|
569
|
-
* @param {JSONSchema} schema - The JSON schema for a string.
|
|
570
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
571
|
-
*/
|
|
572
|
-
static parseString(schema) {
|
|
573
|
-
const stringSchema = z.string();
|
|
574
|
-
let result = stringSchema;
|
|
575
|
-
if (schema.format) {
|
|
576
|
-
return this.applyStringFormat(stringSchema, schema);
|
|
577
|
-
} else {
|
|
578
|
-
result = this.applyStringPattern(stringSchema, schema);
|
|
579
|
-
result = this.applyStringLength(stringSchema, schema);
|
|
580
|
-
result = this.applyStringEnum(stringSchema, schema);
|
|
581
|
-
}
|
|
582
|
-
return result;
|
|
583
|
-
}
|
|
584
|
-
/**
|
|
585
|
-
* Applies format validation to a string schema.
|
|
586
|
-
*
|
|
587
|
-
* @param {z.ZodString} stringSchema - The base string schema.
|
|
588
|
-
* @param {JSONSchema} schema - The JSON schema with format.
|
|
589
|
-
* @returns {ZodTypeAny} - The updated schema with format validation.
|
|
590
|
-
*/
|
|
591
|
-
static applyStringFormat(stringSchema, schema) {
|
|
592
|
-
if (!schema.format) {
|
|
593
|
-
return stringSchema;
|
|
594
|
-
}
|
|
595
|
-
switch (schema.format) {
|
|
596
|
-
case "email":
|
|
597
|
-
return stringSchema.email();
|
|
598
|
-
case "date-time":
|
|
599
|
-
return stringSchema.datetime();
|
|
600
|
-
case "uri":
|
|
601
|
-
return stringSchema.url();
|
|
602
|
-
case "uuid":
|
|
603
|
-
return stringSchema.uuid();
|
|
604
|
-
case "date":
|
|
605
|
-
return stringSchema.date();
|
|
606
|
-
default:
|
|
607
|
-
return stringSchema;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Applies pattern validation to a string schema.
|
|
612
|
-
*
|
|
613
|
-
* @param {z.ZodString} stringSchema - The base string schema.
|
|
614
|
-
* @param {JSONSchema} schema - The JSON schema with pattern.
|
|
615
|
-
* @returns {z.ZodString} - The updated schema with pattern validation.
|
|
616
|
-
*/
|
|
617
|
-
static applyStringPattern(stringSchema, schema) {
|
|
618
|
-
if (!schema["pattern"]) {
|
|
619
|
-
return stringSchema;
|
|
620
|
-
}
|
|
621
|
-
const regex = new RegExp(schema["pattern"]);
|
|
622
|
-
return stringSchema.regex(regex, {
|
|
623
|
-
message: `String must match pattern: ${schema["pattern"]}`
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
/**
|
|
627
|
-
* Applies length constraints to a string schema.
|
|
628
|
-
*
|
|
629
|
-
* @param {z.ZodString} stringSchema - The base string schema.
|
|
630
|
-
* @param {JSONSchema} schema - The JSON schema with length constraints.
|
|
631
|
-
* @returns {z.ZodString} - The updated schema with length validation.
|
|
632
|
-
*/
|
|
633
|
-
static applyStringLength(stringSchema, schema) {
|
|
634
|
-
const result = stringSchema;
|
|
635
|
-
if (schema["minLength"] !== void 0) {
|
|
636
|
-
stringSchema = stringSchema.min(schema["minLength"]);
|
|
637
|
-
}
|
|
638
|
-
if (schema["maxLength"] !== void 0) {
|
|
639
|
-
stringSchema = stringSchema.max(schema["maxLength"]);
|
|
640
|
-
}
|
|
641
|
-
return result;
|
|
642
|
-
}
|
|
643
|
-
/**
|
|
644
|
-
* Applies enum validation to a string schema.
|
|
645
|
-
*
|
|
646
|
-
* @param {z.ZodString} stringSchema - The base string schema.
|
|
647
|
-
* @param {JSONSchema} schema - The JSON schema with enum.
|
|
648
|
-
* @returns {ZodTypeAny} - The updated schema with enum validation.
|
|
649
|
-
*/
|
|
650
|
-
static applyStringEnum(stringSchema, schema) {
|
|
651
|
-
if (!schema.enum) {
|
|
652
|
-
return stringSchema;
|
|
653
|
-
}
|
|
654
|
-
return stringSchema.refine((val) => schema.enum?.includes(val), {
|
|
655
|
-
message: `Value must be one of: ${schema.enum?.join(", ")}`
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
/**
|
|
659
|
-
* Parses a JSON schema of type array and returns the corresponding Zod schema.
|
|
660
|
-
*
|
|
661
|
-
* @param {JSONSchema} schema - The JSON schema.
|
|
662
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
663
|
-
*/
|
|
664
|
-
static parseArray(schema) {
|
|
665
|
-
if (Array.isArray(schema.items)) {
|
|
666
|
-
const tupleSchemas = schema.items.map((item) => this.parseSchema(item));
|
|
667
|
-
return z.union(tupleSchemas);
|
|
668
|
-
}
|
|
669
|
-
const itemSchema = schema.items ? this.parseSchema(schema.items) : z.any();
|
|
670
|
-
const arraySchema = z.array(itemSchema);
|
|
671
|
-
let result = arraySchema;
|
|
672
|
-
result = this.applyArrayConstraints(arraySchema, schema);
|
|
673
|
-
return result;
|
|
674
|
-
}
|
|
675
|
-
/**
|
|
676
|
-
* Applies constraints to an array schema.
|
|
677
|
-
*
|
|
678
|
-
* @param {z.ZodArray<any>} arraySchema - The base array schema.
|
|
679
|
-
* @param {JSONSchema} schema - The JSON schema with array constraints.
|
|
680
|
-
* @returns {z.ZodTypeAny} - The updated array schema with constraints.
|
|
681
|
-
*/
|
|
682
|
-
static applyArrayConstraints(arraySchema, schema) {
|
|
683
|
-
if (schema["minItems"] !== void 0) {
|
|
684
|
-
arraySchema = arraySchema.min(schema["minItems"]);
|
|
685
|
-
}
|
|
686
|
-
if (schema["maxItems"] !== void 0) {
|
|
687
|
-
arraySchema = arraySchema.max(schema["maxItems"]);
|
|
688
|
-
}
|
|
689
|
-
if (schema["uniqueItems"]) {
|
|
690
|
-
return arraySchema.refine(
|
|
691
|
-
(items) => new Set(items).size === items.length,
|
|
692
|
-
{ message: "Array items must be unique" }
|
|
693
|
-
);
|
|
694
|
-
}
|
|
695
|
-
return arraySchema;
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Parses an object schema.
|
|
699
|
-
*
|
|
700
|
-
* @param {JSONSchema} schema - The JSON schema for an object.
|
|
701
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
702
|
-
*/
|
|
703
|
-
static parseObject(schema) {
|
|
704
|
-
if (schema["if"] && schema["then"]) {
|
|
705
|
-
return this.parseConditional(schema);
|
|
706
|
-
}
|
|
707
|
-
const shape = {};
|
|
708
|
-
this.processObjectProperties(schema, shape);
|
|
709
|
-
return this.processAdditionalProperties(schema, z.object(shape));
|
|
710
|
-
}
|
|
711
|
-
/**
|
|
712
|
-
* Processes object properties and builds the shape object.
|
|
713
|
-
*
|
|
714
|
-
* @param {JSONSchema} schema - The JSON schema for an object.
|
|
715
|
-
* @param {Record<string, ZodTypeAny>} shape - The shape object to populate.
|
|
716
|
-
*/
|
|
717
|
-
static processObjectProperties(schema, shape) {
|
|
718
|
-
const required = new Set(schema.required || []);
|
|
719
|
-
if (!schema.properties) {
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
723
|
-
const zodSchema = this.parseSchema(propSchema);
|
|
724
|
-
shape[key] = required.has(key) ? zodSchema : zodSchema.optional();
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
/**
|
|
728
|
-
* Processes additionalProperties configuration.
|
|
729
|
-
*
|
|
730
|
-
* @param {JSONSchema} schema - The JSON schema for an object.
|
|
731
|
-
* @param {z.ZodObject<any, any>} objectSchema - The Zod object schema.
|
|
732
|
-
* @returns {z.ZodObject<any, any>} - The updated Zod object schema.
|
|
733
|
-
*/
|
|
734
|
-
static processAdditionalProperties(schema, objectSchema) {
|
|
735
|
-
if (schema.additionalProperties === true) {
|
|
736
|
-
return objectSchema.passthrough();
|
|
737
|
-
} else if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
738
|
-
const additionalPropSchema = this.parseSchema(
|
|
739
|
-
schema.additionalProperties
|
|
740
|
-
);
|
|
741
|
-
return objectSchema.catchall(additionalPropSchema);
|
|
742
|
-
} else {
|
|
743
|
-
return objectSchema.strict();
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
|
-
* Parses a conditional schema with if-then-else.
|
|
748
|
-
*
|
|
749
|
-
* @param {JSONSchema} schema - The JSON schema with conditional validation.
|
|
750
|
-
* @returns {ZodTypeAny} - The conditional Zod schema.
|
|
751
|
-
*/
|
|
752
|
-
static parseConditional(schema) {
|
|
753
|
-
const zodObject = this.createBaseObjectSchema(schema);
|
|
754
|
-
const ifCondition = schema["if"];
|
|
755
|
-
const thenSchema = schema["then"];
|
|
756
|
-
const elseSchema = schema["else"];
|
|
757
|
-
return zodObject.superRefine((data, ctx) => {
|
|
758
|
-
const dataWithDefaults = this.applyDefaultValues(
|
|
759
|
-
data,
|
|
760
|
-
schema
|
|
761
|
-
);
|
|
762
|
-
if (this.matchesCondition(dataWithDefaults, ifCondition)) {
|
|
763
|
-
this.validateConditionalSchema(dataWithDefaults, thenSchema, ctx);
|
|
764
|
-
} else if (elseSchema) {
|
|
765
|
-
this.validateConditionalSchema(dataWithDefaults, elseSchema, ctx);
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
/**
|
|
770
|
-
* Creates a base object schema from the given JSON schema.
|
|
771
|
-
*
|
|
772
|
-
* @param {JSONSchema} schema - The JSON schema.
|
|
773
|
-
* @returns {z.ZodObject<any, any>} - The base Zod object schema.
|
|
774
|
-
*/
|
|
775
|
-
static createBaseObjectSchema(schema) {
|
|
776
|
-
const shape = {};
|
|
777
|
-
const required = new Set(schema.required || []);
|
|
778
|
-
for (const [key, value] of Object.entries(schema.properties || {})) {
|
|
779
|
-
const zodSchema = this.parseSchema(value);
|
|
780
|
-
shape[key] = required.has(key) ? zodSchema : zodSchema.optional();
|
|
781
|
-
}
|
|
782
|
-
const zodObject = z.object(shape);
|
|
783
|
-
return this.processAdditionalProperties(schema, zodObject);
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Applies default values from schema properties to data object.
|
|
787
|
-
*
|
|
788
|
-
* @param {JSONValue} data - The original data object.
|
|
789
|
-
* @param {JSONSchema} schema - The schema with default values.
|
|
790
|
-
* @returns {JSONValue} - The data object with defaults applied.
|
|
791
|
-
*/
|
|
792
|
-
static applyDefaultValues(data, schema) {
|
|
793
|
-
if (typeof data !== "object" || data === null) {
|
|
794
|
-
return data;
|
|
795
|
-
}
|
|
796
|
-
if (Array.isArray(data)) {
|
|
797
|
-
return data;
|
|
798
|
-
}
|
|
799
|
-
const objectData = data;
|
|
800
|
-
const dataWithDefaults = { ...objectData };
|
|
801
|
-
if (!schema.properties) {
|
|
802
|
-
return dataWithDefaults;
|
|
803
|
-
}
|
|
804
|
-
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
805
|
-
if (!(key in dataWithDefaults) && "default" in propSchema) {
|
|
806
|
-
dataWithDefaults[key] = propSchema["default"];
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
return dataWithDefaults;
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* Parses a schema with combinators (oneOf, anyOf, allOf).
|
|
813
|
-
* Delegates to the appropriate combinator parser based on which combinator is present.
|
|
814
|
-
*
|
|
815
|
-
* @param {JSONSchema} schema - The JSON schema with combinators.
|
|
816
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
817
|
-
*/
|
|
818
|
-
static parseCombinator(schema) {
|
|
819
|
-
if (schema.oneOf) {
|
|
820
|
-
return this.parseOneOf(schema.oneOf);
|
|
821
|
-
}
|
|
822
|
-
if (schema.anyOf) {
|
|
823
|
-
return this.parseAnyOf(schema.anyOf);
|
|
824
|
-
}
|
|
825
|
-
if (schema.allOf) {
|
|
826
|
-
return this.parseAllOf(schema.allOf);
|
|
827
|
-
}
|
|
828
|
-
throw new Error("Unsupported schema type");
|
|
829
|
-
}
|
|
830
|
-
/**
|
|
831
|
-
* Parses a oneOf combinator schema.
|
|
832
|
-
*
|
|
833
|
-
* @param {JSONSchema[]} schemas - Array of JSON schemas in the oneOf.
|
|
834
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
835
|
-
*/
|
|
836
|
-
static parseOneOf(schemas) {
|
|
837
|
-
return this.createUnionFromSchemas(schemas);
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* Parses an anyOf combinator schema.
|
|
841
|
-
*
|
|
842
|
-
* @param {JSONSchema[]} schemas - Array of JSON schemas in the anyOf.
|
|
843
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
844
|
-
*/
|
|
845
|
-
static parseAnyOf(schemas) {
|
|
846
|
-
return this.createUnionFromSchemas(schemas);
|
|
847
|
-
}
|
|
848
|
-
/**
|
|
849
|
-
* Creates a union from an array of schemas, handling special cases.
|
|
850
|
-
*
|
|
851
|
-
* @param {JSONSchema[]} schemas - Array of JSON schemas to create a union from.
|
|
852
|
-
* @returns {ZodTypeAny} - The union Zod schema.
|
|
853
|
-
*/
|
|
854
|
-
static createUnionFromSchemas(schemas) {
|
|
855
|
-
if (schemas.length === 0) {
|
|
856
|
-
return z.any();
|
|
857
|
-
}
|
|
858
|
-
if (schemas.length === 1) {
|
|
859
|
-
return this.parseSchema(schemas[0]);
|
|
860
|
-
}
|
|
861
|
-
const zodSchemas = [];
|
|
862
|
-
for (const subSchema of schemas) {
|
|
863
|
-
if (subSchema.type === "null") {
|
|
864
|
-
zodSchemas.push(z.null());
|
|
865
|
-
} else {
|
|
866
|
-
zodSchemas.push(this.parseSchema(subSchema));
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
if (zodSchemas.length >= 2) {
|
|
870
|
-
return z.union(zodSchemas);
|
|
871
|
-
} else if (zodSchemas.length === 1) {
|
|
872
|
-
return zodSchemas[0];
|
|
873
|
-
}
|
|
874
|
-
return z.any();
|
|
875
|
-
}
|
|
876
|
-
/**
|
|
877
|
-
* Parses an allOf combinator schema by merging all schemas.
|
|
878
|
-
*
|
|
879
|
-
* @param {JSONSchema[]} schemas - Array of JSON schemas in the allOf.
|
|
880
|
-
* @returns {ZodTypeAny} - The ZodTypeAny schema.
|
|
881
|
-
*/
|
|
882
|
-
static parseAllOf(schemas) {
|
|
883
|
-
if (schemas.length === 0) {
|
|
884
|
-
return z.any();
|
|
885
|
-
}
|
|
886
|
-
if (schemas.length === 1) {
|
|
887
|
-
return this.parseSchema(schemas[0]);
|
|
888
|
-
}
|
|
889
|
-
const mergedSchema = schemas.reduce(
|
|
890
|
-
(acc, currentSchema) => this.mergeSchemas(acc, currentSchema)
|
|
891
|
-
);
|
|
892
|
-
return this.parseSchema(mergedSchema);
|
|
893
|
-
}
|
|
894
|
-
/**
|
|
895
|
-
* Merges two JSON schemas together.
|
|
896
|
-
*
|
|
897
|
-
* @param {JSONSchema} baseSchema - The base JSON schema.
|
|
898
|
-
* @param {JSONSchema} addSchema - The JSON schema to add.
|
|
899
|
-
* @returns {JSONSchema} - The merged JSON schema
|
|
900
|
-
*/
|
|
901
|
-
static mergeSchemas(baseSchema, addSchema) {
|
|
902
|
-
const merged = { ...baseSchema, ...addSchema };
|
|
903
|
-
if (baseSchema.properties && addSchema.properties) {
|
|
904
|
-
const mergedProperties = {
|
|
905
|
-
...baseSchema.properties,
|
|
906
|
-
...addSchema.properties
|
|
907
|
-
};
|
|
908
|
-
merged.properties = mergedProperties;
|
|
909
|
-
}
|
|
910
|
-
if (baseSchema.required && addSchema.required) {
|
|
911
|
-
const mergedRequired = [
|
|
912
|
-
.../* @__PURE__ */ new Set([...baseSchema.required, ...addSchema.required])
|
|
913
|
-
];
|
|
914
|
-
merged.required = mergedRequired;
|
|
915
|
-
}
|
|
916
|
-
return merged;
|
|
917
|
-
}
|
|
918
|
-
};
|
|
919
|
-
|
|
920
|
-
// src/adapters/langchain_adapter.ts
|
|
921
|
-
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
922
|
-
import { z as z2 } from "zod";
|
|
923
|
-
function schemaToZod(schema) {
|
|
924
|
-
try {
|
|
925
|
-
return JSONSchemaToZod.convert(schema);
|
|
926
|
-
} catch (err) {
|
|
927
|
-
logger.warn(`Failed to convert JSON schema to Zod: ${err}`);
|
|
928
|
-
return z2.any();
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
__name(schemaToZod, "schemaToZod");
|
|
932
|
-
var LangChainAdapter = class extends BaseAdapter {
|
|
933
|
-
static {
|
|
934
|
-
__name(this, "LangChainAdapter");
|
|
935
|
-
}
|
|
936
|
-
constructor(disallowedTools = []) {
|
|
937
|
-
super(disallowedTools);
|
|
938
|
-
}
|
|
939
|
-
/**
|
|
940
|
-
* Convert a single MCP tool specification into a LangChainJS structured tool.
|
|
941
|
-
*/
|
|
942
|
-
convertTool(mcpTool, connector) {
|
|
943
|
-
if (this.disallowedTools.includes(mcpTool.name)) {
|
|
944
|
-
return null;
|
|
945
|
-
}
|
|
946
|
-
const argsSchema = mcpTool.inputSchema ? schemaToZod(mcpTool.inputSchema) : z2.object({}).optional();
|
|
947
|
-
const tool = new DynamicStructuredTool({
|
|
948
|
-
name: mcpTool.name ?? "NO NAME",
|
|
949
|
-
description: mcpTool.description ?? "",
|
|
950
|
-
// Blank is acceptable but discouraged.
|
|
951
|
-
schema: argsSchema,
|
|
952
|
-
func: /* @__PURE__ */ __name(async (input) => {
|
|
953
|
-
logger.debug(
|
|
954
|
-
`MCP tool "${mcpTool.name}" received input: ${JSON.stringify(input)}`
|
|
955
|
-
);
|
|
956
|
-
try {
|
|
957
|
-
const result = await connector.callTool(
|
|
958
|
-
mcpTool.name,
|
|
959
|
-
input
|
|
960
|
-
);
|
|
961
|
-
return JSON.stringify(result);
|
|
962
|
-
} catch (err) {
|
|
963
|
-
logger.error(`Error executing MCP tool: ${err.message}`);
|
|
964
|
-
return `Error executing MCP tool: ${String(err)}`;
|
|
965
|
-
}
|
|
966
|
-
}, "func")
|
|
967
|
-
});
|
|
968
|
-
return tool;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* Convert a single MCP resource into a LangChainJS structured tool.
|
|
972
|
-
* Each resource becomes an async tool that returns its content when called.
|
|
973
|
-
*/
|
|
974
|
-
convertResource(mcpResource, connector) {
|
|
975
|
-
const sanitizeName = /* @__PURE__ */ __name((name) => {
|
|
976
|
-
return name.replace(/[^A-Za-z0-9_]+/g, "_").toLowerCase().replace(/^_+|_+$/g, "");
|
|
977
|
-
}, "sanitizeName");
|
|
978
|
-
const resourceName = sanitizeName(
|
|
979
|
-
mcpResource.name || `resource_${mcpResource.uri}`
|
|
980
|
-
);
|
|
981
|
-
const resourceUri = mcpResource.uri;
|
|
982
|
-
const tool = new DynamicStructuredTool({
|
|
983
|
-
name: resourceName,
|
|
984
|
-
description: mcpResource.description || `Return the content of the resource located at URI ${resourceUri}.`,
|
|
985
|
-
schema: z2.object({}).optional(),
|
|
986
|
-
// Resources take no arguments
|
|
987
|
-
func: /* @__PURE__ */ __name(async () => {
|
|
988
|
-
logger.debug(`Resource tool: "${resourceName}" called`);
|
|
989
|
-
try {
|
|
990
|
-
const result = await connector.readResource(resourceUri);
|
|
991
|
-
if (result.contents && result.contents.length > 0) {
|
|
992
|
-
return result.contents.map((content) => {
|
|
993
|
-
if (typeof content === "string") {
|
|
994
|
-
return content;
|
|
995
|
-
}
|
|
996
|
-
if (content.text) {
|
|
997
|
-
return content.text;
|
|
998
|
-
}
|
|
999
|
-
if (content.uri) {
|
|
1000
|
-
return content.uri;
|
|
1001
|
-
}
|
|
1002
|
-
return JSON.stringify(content);
|
|
1003
|
-
}).join("\n");
|
|
1004
|
-
}
|
|
1005
|
-
return "Resource is empty or unavailable";
|
|
1006
|
-
} catch (err) {
|
|
1007
|
-
logger.error(`Error reading resource: ${err.message}`);
|
|
1008
|
-
return `Error reading resource: ${String(err)}`;
|
|
1009
|
-
}
|
|
1010
|
-
}, "func")
|
|
1011
|
-
});
|
|
1012
|
-
return tool;
|
|
1013
|
-
}
|
|
1014
|
-
/**
|
|
1015
|
-
* Convert a single MCP prompt into a LangChainJS structured tool.
|
|
1016
|
-
* The resulting tool executes getPrompt on the connector with the prompt's name
|
|
1017
|
-
* and the user-provided arguments (if any).
|
|
1018
|
-
*/
|
|
1019
|
-
convertPrompt(mcpPrompt, connector) {
|
|
1020
|
-
let argsSchema = z2.object({}).optional();
|
|
1021
|
-
if (mcpPrompt.arguments && mcpPrompt.arguments.length > 0) {
|
|
1022
|
-
const schemaFields = {};
|
|
1023
|
-
for (const arg of mcpPrompt.arguments) {
|
|
1024
|
-
const zodType = z2.string();
|
|
1025
|
-
if (arg.required !== false) {
|
|
1026
|
-
schemaFields[arg.name] = zodType;
|
|
1027
|
-
} else {
|
|
1028
|
-
schemaFields[arg.name] = zodType.optional();
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
argsSchema = Object.keys(schemaFields).length > 0 ? z2.object(schemaFields) : z2.object({}).optional();
|
|
1032
|
-
}
|
|
1033
|
-
const tool = new DynamicStructuredTool({
|
|
1034
|
-
name: mcpPrompt.name,
|
|
1035
|
-
description: mcpPrompt.description || "",
|
|
1036
|
-
schema: argsSchema,
|
|
1037
|
-
func: /* @__PURE__ */ __name(async (input) => {
|
|
1038
|
-
logger.debug(
|
|
1039
|
-
`Prompt tool: "${mcpPrompt.name}" called with args: ${JSON.stringify(input)}`
|
|
1040
|
-
);
|
|
1041
|
-
try {
|
|
1042
|
-
const result = await connector.getPrompt(mcpPrompt.name, input);
|
|
1043
|
-
if (result.messages && result.messages.length > 0) {
|
|
1044
|
-
return result.messages.map((msg) => {
|
|
1045
|
-
if (typeof msg === "string") {
|
|
1046
|
-
return msg;
|
|
1047
|
-
}
|
|
1048
|
-
if (msg.content) {
|
|
1049
|
-
return typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
|
1050
|
-
}
|
|
1051
|
-
return JSON.stringify(msg);
|
|
1052
|
-
}).join("\n");
|
|
1053
|
-
}
|
|
1054
|
-
return "Prompt returned no messages";
|
|
1055
|
-
} catch (err) {
|
|
1056
|
-
logger.error(`Error getting prompt: ${err.message}`);
|
|
1057
|
-
return `Error getting prompt: ${String(err)}`;
|
|
1058
|
-
}
|
|
1059
|
-
}, "func")
|
|
1060
|
-
});
|
|
1061
|
-
return tool;
|
|
1062
|
-
}
|
|
1063
|
-
};
|
|
1064
|
-
|
|
1065
|
-
// src/client/executors/base.ts
|
|
1066
|
-
var BaseCodeExecutor = class {
|
|
1067
|
-
static {
|
|
1068
|
-
__name(this, "BaseCodeExecutor");
|
|
1069
|
-
}
|
|
1070
|
-
client;
|
|
1071
|
-
_connecting = false;
|
|
1072
|
-
constructor(client) {
|
|
1073
|
-
this.client = client;
|
|
1074
|
-
}
|
|
1075
|
-
/**
|
|
1076
|
-
* Ensure all configured MCP servers are connected before execution.
|
|
1077
|
-
* Prevents race conditions with a connection lock.
|
|
1078
|
-
*/
|
|
1079
|
-
async ensureServersConnected() {
|
|
1080
|
-
const configuredServers = this.client.getServerNames();
|
|
1081
|
-
const activeSessions = Object.keys(this.client.getAllActiveSessions());
|
|
1082
|
-
const missingServers = configuredServers.filter(
|
|
1083
|
-
(s) => !activeSessions.includes(s)
|
|
1084
|
-
);
|
|
1085
|
-
if (missingServers.length > 0 && !this._connecting) {
|
|
1086
|
-
this._connecting = true;
|
|
1087
|
-
try {
|
|
1088
|
-
logger.debug(
|
|
1089
|
-
`Connecting to configured servers for code execution: ${missingServers.join(", ")}`
|
|
1090
|
-
);
|
|
1091
|
-
await this.client.createAllSessions();
|
|
1092
|
-
} finally {
|
|
1093
|
-
this._connecting = false;
|
|
1094
|
-
}
|
|
1095
|
-
} else if (missingServers.length > 0 && this._connecting) {
|
|
1096
|
-
logger.debug("Waiting for ongoing server connection...");
|
|
1097
|
-
const startWait = Date.now();
|
|
1098
|
-
while (this._connecting && Date.now() - startWait < 5e3) {
|
|
1099
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
/**
|
|
1104
|
-
* Get tool namespace information from all active MCP sessions.
|
|
1105
|
-
* Filters out the internal code_mode server.
|
|
1106
|
-
*/
|
|
1107
|
-
getToolNamespaces() {
|
|
1108
|
-
const namespaces = [];
|
|
1109
|
-
const activeSessions = this.client.getAllActiveSessions();
|
|
1110
|
-
for (const [serverName, session] of Object.entries(activeSessions)) {
|
|
1111
|
-
if (serverName === "code_mode") continue;
|
|
1112
|
-
try {
|
|
1113
|
-
const connector = session.connector;
|
|
1114
|
-
let tools;
|
|
1115
|
-
try {
|
|
1116
|
-
tools = connector.tools;
|
|
1117
|
-
} catch (e) {
|
|
1118
|
-
logger.warn(`Tools not available for server ${serverName}: ${e}`);
|
|
1119
|
-
continue;
|
|
1120
|
-
}
|
|
1121
|
-
if (!tools || tools.length === 0) continue;
|
|
1122
|
-
namespaces.push({ serverName, tools, session });
|
|
1123
|
-
} catch (e) {
|
|
1124
|
-
logger.warn(`Failed to load tools for server ${serverName}: ${e}`);
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
return namespaces;
|
|
1128
|
-
}
|
|
1129
|
-
/**
|
|
1130
|
-
* Create a search function for discovering available MCP tools.
|
|
1131
|
-
* Used by code execution environments to find tools at runtime.
|
|
1132
|
-
*/
|
|
1133
|
-
createSearchToolsFunction() {
|
|
1134
|
-
return async (query = "", detailLevel = "full") => {
|
|
1135
|
-
const allTools = [];
|
|
1136
|
-
const allNamespaces = /* @__PURE__ */ new Set();
|
|
1137
|
-
const queryLower = query.toLowerCase();
|
|
1138
|
-
const activeSessions = this.client.getAllActiveSessions();
|
|
1139
|
-
for (const [serverName, session] of Object.entries(activeSessions)) {
|
|
1140
|
-
if (serverName === "code_mode") continue;
|
|
1141
|
-
try {
|
|
1142
|
-
const tools = session.connector.tools;
|
|
1143
|
-
if (tools && tools.length > 0) {
|
|
1144
|
-
allNamespaces.add(serverName);
|
|
1145
|
-
}
|
|
1146
|
-
for (const tool of tools) {
|
|
1147
|
-
if (detailLevel === "names") {
|
|
1148
|
-
allTools.push({ name: tool.name, server: serverName });
|
|
1149
|
-
} else if (detailLevel === "descriptions") {
|
|
1150
|
-
allTools.push({
|
|
1151
|
-
name: tool.name,
|
|
1152
|
-
server: serverName,
|
|
1153
|
-
description: tool.description
|
|
1154
|
-
});
|
|
1155
|
-
} else {
|
|
1156
|
-
allTools.push({
|
|
1157
|
-
name: tool.name,
|
|
1158
|
-
server: serverName,
|
|
1159
|
-
description: tool.description,
|
|
1160
|
-
input_schema: tool.inputSchema
|
|
1161
|
-
});
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
} catch (e) {
|
|
1165
|
-
logger.warn(`Failed to search tools in server ${serverName}: ${e}`);
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1168
|
-
let filteredTools = allTools;
|
|
1169
|
-
if (query) {
|
|
1170
|
-
filteredTools = allTools.filter((tool) => {
|
|
1171
|
-
const nameMatch = tool.name.toLowerCase().includes(queryLower);
|
|
1172
|
-
const descMatch = tool.description?.toLowerCase().includes(queryLower);
|
|
1173
|
-
const serverMatch = tool.server.toLowerCase().includes(queryLower);
|
|
1174
|
-
return nameMatch || descMatch || serverMatch;
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
return {
|
|
1178
|
-
meta: {
|
|
1179
|
-
total_tools: allTools.length,
|
|
1180
|
-
namespaces: Array.from(allNamespaces).sort(),
|
|
1181
|
-
result_count: filteredTools.length
|
|
1182
|
-
},
|
|
1183
|
-
results: filteredTools
|
|
1184
|
-
};
|
|
1185
|
-
};
|
|
1186
|
-
}
|
|
1187
|
-
};
|
|
1188
|
-
|
|
1189
|
-
// src/client/executors/e2b.ts
|
|
1190
|
-
var E2BCodeExecutor = class extends BaseCodeExecutor {
|
|
1191
|
-
static {
|
|
1192
|
-
__name(this, "E2BCodeExecutor");
|
|
1193
|
-
}
|
|
1194
|
-
e2bApiKey;
|
|
1195
|
-
codeExecSandbox = null;
|
|
1196
|
-
SandboxClass = null;
|
|
1197
|
-
timeoutMs;
|
|
1198
|
-
constructor(client, options) {
|
|
1199
|
-
super(client);
|
|
1200
|
-
this.e2bApiKey = options.apiKey;
|
|
1201
|
-
this.timeoutMs = options.timeoutMs ?? 3e5;
|
|
1202
|
-
}
|
|
1203
|
-
/**
|
|
1204
|
-
* Lazy load E2B Sandbox class.
|
|
1205
|
-
* This allows the library to work without E2B installed.
|
|
1206
|
-
*/
|
|
1207
|
-
async ensureSandboxClass() {
|
|
1208
|
-
if (this.SandboxClass) return;
|
|
1209
|
-
try {
|
|
1210
|
-
const e2b = await import("@e2b/code-interpreter");
|
|
1211
|
-
this.SandboxClass = e2b.Sandbox;
|
|
1212
|
-
} catch (error) {
|
|
1213
|
-
throw new Error(
|
|
1214
|
-
"@e2b/code-interpreter is not installed. The E2B code executor requires this optional dependency. Install it with: yarn add @e2b/code-interpreter"
|
|
1215
|
-
);
|
|
1216
|
-
}
|
|
1217
|
-
}
|
|
1218
|
-
/**
|
|
1219
|
-
* Get or create a dedicated sandbox for code execution.
|
|
1220
|
-
*/
|
|
1221
|
-
async getOrCreateCodeExecSandbox() {
|
|
1222
|
-
if (this.codeExecSandbox) return this.codeExecSandbox;
|
|
1223
|
-
await this.ensureSandboxClass();
|
|
1224
|
-
logger.debug("Starting E2B sandbox for code execution...");
|
|
1225
|
-
this.codeExecSandbox = await this.SandboxClass.create("base", {
|
|
1226
|
-
apiKey: this.e2bApiKey,
|
|
1227
|
-
timeoutMs: this.timeoutMs
|
|
1228
|
-
});
|
|
1229
|
-
return this.codeExecSandbox;
|
|
1230
|
-
}
|
|
1231
|
-
/**
|
|
1232
|
-
* Generate the shim code that exposes tools to the sandbox environment.
|
|
1233
|
-
* Creates a bridge that intercepts tool calls and sends them back to host.
|
|
1234
|
-
*/
|
|
1235
|
-
generateShim(tools) {
|
|
1236
|
-
let shim = `
|
|
1237
|
-
// MCP Bridge Shim
|
|
1238
|
-
global.__callMcpTool = async (server, tool, args) => {
|
|
1239
|
-
const id = Math.random().toString(36).substring(7);
|
|
1240
|
-
console.log(JSON.stringify({
|
|
1241
|
-
type: '__MCP_TOOL_CALL__',
|
|
1242
|
-
id,
|
|
1243
|
-
server,
|
|
1244
|
-
tool,
|
|
1245
|
-
args
|
|
1246
|
-
}));
|
|
1247
|
-
|
|
1248
|
-
const resultPath = \`/tmp/mcp_result_\${id}.json\`;
|
|
1249
|
-
const fs = require('fs');
|
|
1250
|
-
|
|
1251
|
-
// Poll for result file
|
|
1252
|
-
let attempts = 0;
|
|
1253
|
-
while (attempts < 300) { // 30 seconds timeout
|
|
1254
|
-
if (fs.existsSync(resultPath)) {
|
|
1255
|
-
const content = fs.readFileSync(resultPath, 'utf8');
|
|
1256
|
-
const result = JSON.parse(content);
|
|
1257
|
-
fs.unlinkSync(resultPath); // Clean up
|
|
1258
|
-
|
|
1259
|
-
if (result.error) {
|
|
1260
|
-
throw new Error(result.error);
|
|
1261
|
-
}
|
|
1262
|
-
return result.data;
|
|
1263
|
-
}
|
|
1264
|
-
await new Promise(resolve => setTimeout(resolve, 100));
|
|
1265
|
-
attempts++;
|
|
1266
|
-
}
|
|
1267
|
-
throw new Error('Tool execution timed out');
|
|
1268
|
-
};
|
|
1269
|
-
|
|
1270
|
-
// Global search_tools helper
|
|
1271
|
-
global.search_tools = async (query, detailLevel = 'full') => {
|
|
1272
|
-
const allTools = ${JSON.stringify(
|
|
1273
|
-
Object.entries(tools).flatMap(
|
|
1274
|
-
([server, serverTools]) => serverTools.map((tool) => ({
|
|
1275
|
-
name: tool.name,
|
|
1276
|
-
description: tool.description,
|
|
1277
|
-
server,
|
|
1278
|
-
input_schema: tool.inputSchema
|
|
1279
|
-
}))
|
|
1280
|
-
)
|
|
1281
|
-
)};
|
|
1282
|
-
|
|
1283
|
-
const filtered = allTools.filter(tool => {
|
|
1284
|
-
if (!query) return true;
|
|
1285
|
-
const q = query.toLowerCase();
|
|
1286
|
-
return tool.name.toLowerCase().includes(q) ||
|
|
1287
|
-
(tool.description && tool.description.toLowerCase().includes(q));
|
|
1288
|
-
});
|
|
1289
|
-
|
|
1290
|
-
if (detailLevel === 'names') {
|
|
1291
|
-
return filtered.map(t => ({ name: t.name, server: t.server }));
|
|
1292
|
-
} else if (detailLevel === 'descriptions') {
|
|
1293
|
-
return filtered.map(t => ({ name: t.name, server: t.server, description: t.description }));
|
|
1294
|
-
}
|
|
1295
|
-
return filtered;
|
|
1296
|
-
};
|
|
1297
|
-
`;
|
|
1298
|
-
for (const [serverName, serverTools] of Object.entries(tools)) {
|
|
1299
|
-
if (!serverTools || serverTools.length === 0) continue;
|
|
1300
|
-
const safeServerName = serverName.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
1301
|
-
shim += `
|
|
1302
|
-
global['${serverName}'] = {`;
|
|
1303
|
-
for (const tool of serverTools) {
|
|
1304
|
-
shim += `
|
|
1305
|
-
'${tool.name}': async (args) => await global.__callMcpTool('${serverName}', '${tool.name}', args),`;
|
|
1306
|
-
}
|
|
1307
|
-
shim += `
|
|
1308
|
-
};
|
|
1309
|
-
|
|
1310
|
-
// Also expose as safe name if different
|
|
1311
|
-
if ('${safeServerName}' !== '${serverName}') {
|
|
1312
|
-
global['${safeServerName}'] = global['${serverName}'];
|
|
1313
|
-
}
|
|
1314
|
-
`;
|
|
1315
|
-
}
|
|
1316
|
-
return shim;
|
|
1317
|
-
}
|
|
1318
|
-
/**
|
|
1319
|
-
* Build the tool catalog for the shim.
|
|
1320
|
-
* Returns a map of server names to their available tools.
|
|
1321
|
-
*/
|
|
1322
|
-
buildToolCatalog() {
|
|
1323
|
-
const catalog = {};
|
|
1324
|
-
const namespaces = this.getToolNamespaces();
|
|
1325
|
-
for (const { serverName, tools } of namespaces) {
|
|
1326
|
-
catalog[serverName] = tools;
|
|
1327
|
-
}
|
|
1328
|
-
return catalog;
|
|
1329
|
-
}
|
|
1330
|
-
/**
|
|
1331
|
-
* Execute JavaScript/TypeScript code in an E2B sandbox with MCP tool access.
|
|
1332
|
-
* Tool calls are proxied back to the host via the bridge pattern.
|
|
1333
|
-
*
|
|
1334
|
-
* @param code - Code to execute
|
|
1335
|
-
* @param timeout - Execution timeout in milliseconds (default: 30000)
|
|
1336
|
-
*/
|
|
1337
|
-
async execute(code, timeout = 3e4) {
|
|
1338
|
-
const startTime = Date.now();
|
|
1339
|
-
let result = null;
|
|
1340
|
-
let error = null;
|
|
1341
|
-
let logs = [];
|
|
1342
|
-
try {
|
|
1343
|
-
await this.ensureServersConnected();
|
|
1344
|
-
const sandbox = await this.getOrCreateCodeExecSandbox();
|
|
1345
|
-
const toolCatalog = this.buildToolCatalog();
|
|
1346
|
-
const shim = this.generateShim(toolCatalog);
|
|
1347
|
-
const wrappedCode = `
|
|
1348
|
-
${shim}
|
|
1349
|
-
|
|
1350
|
-
(async () => {
|
|
1351
|
-
try {
|
|
1352
|
-
const func = async () => {
|
|
1353
|
-
${code}
|
|
1354
|
-
};
|
|
1355
|
-
const result = await func();
|
|
1356
|
-
console.log('__MCP_RESULT_START__');
|
|
1357
|
-
console.log(JSON.stringify(result));
|
|
1358
|
-
console.log('__MCP_RESULT_END__');
|
|
1359
|
-
} catch (e) {
|
|
1360
|
-
console.error(e);
|
|
1361
|
-
process.exit(1);
|
|
1362
|
-
}
|
|
1363
|
-
})();
|
|
1364
|
-
`;
|
|
1365
|
-
const filename = `exec_${Date.now()}.js`;
|
|
1366
|
-
await sandbox.files.write(filename, wrappedCode);
|
|
1367
|
-
const execution = await sandbox.commands.run(`node ${filename}`, {
|
|
1368
|
-
timeoutMs: timeout,
|
|
1369
|
-
onStdout: /* @__PURE__ */ __name(async (data) => {
|
|
1370
|
-
try {
|
|
1371
|
-
const lines = data.split("\n");
|
|
1372
|
-
for (const line of lines) {
|
|
1373
|
-
if (line.trim().startsWith('{"type":"__MCP_TOOL_CALL__"')) {
|
|
1374
|
-
const call = JSON.parse(line);
|
|
1375
|
-
if (call.type === "__MCP_TOOL_CALL__") {
|
|
1376
|
-
try {
|
|
1377
|
-
logger.debug(
|
|
1378
|
-
`[E2B Bridge] Calling tool ${call.server}.${call.tool}`
|
|
1379
|
-
);
|
|
1380
|
-
const activeSessions = this.client.getAllActiveSessions();
|
|
1381
|
-
const session = activeSessions[call.server];
|
|
1382
|
-
if (!session) {
|
|
1383
|
-
throw new Error(`Server ${call.server} not found`);
|
|
1384
|
-
}
|
|
1385
|
-
const toolResult = await session.connector.callTool(
|
|
1386
|
-
call.tool,
|
|
1387
|
-
call.args
|
|
1388
|
-
);
|
|
1389
|
-
let extractedResult = toolResult;
|
|
1390
|
-
if (toolResult.content && toolResult.content.length > 0) {
|
|
1391
|
-
const item = toolResult.content[0];
|
|
1392
|
-
if (item.type === "text") {
|
|
1393
|
-
try {
|
|
1394
|
-
extractedResult = JSON.parse(item.text);
|
|
1395
|
-
} catch {
|
|
1396
|
-
extractedResult = item.text;
|
|
1397
|
-
}
|
|
1398
|
-
} else {
|
|
1399
|
-
extractedResult = item;
|
|
1400
|
-
}
|
|
1401
|
-
}
|
|
1402
|
-
const resultPath = `/tmp/mcp_result_${call.id}.json`;
|
|
1403
|
-
await sandbox.files.write(
|
|
1404
|
-
resultPath,
|
|
1405
|
-
JSON.stringify({ data: extractedResult })
|
|
1406
|
-
);
|
|
1407
|
-
} catch (err) {
|
|
1408
|
-
logger.error(
|
|
1409
|
-
`[E2B Bridge] Tool execution failed: ${err.message}`
|
|
1410
|
-
);
|
|
1411
|
-
const resultPath = `/tmp/mcp_result_${call.id}.json`;
|
|
1412
|
-
await sandbox.files.write(
|
|
1413
|
-
resultPath,
|
|
1414
|
-
JSON.stringify({
|
|
1415
|
-
error: err.message || String(err)
|
|
1416
|
-
})
|
|
1417
|
-
);
|
|
1418
|
-
}
|
|
1419
|
-
}
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
} catch (e) {
|
|
1423
|
-
}
|
|
1424
|
-
}, "onStdout")
|
|
1425
|
-
});
|
|
1426
|
-
logs = [execution.stdout, execution.stderr].filter(Boolean);
|
|
1427
|
-
if (execution.exitCode !== 0) {
|
|
1428
|
-
error = execution.stderr || "Execution failed";
|
|
1429
|
-
} else {
|
|
1430
|
-
const stdout = execution.stdout;
|
|
1431
|
-
const startMarker = "__MCP_RESULT_START__";
|
|
1432
|
-
const endMarker = "__MCP_RESULT_END__";
|
|
1433
|
-
const startIndex = stdout.indexOf(startMarker);
|
|
1434
|
-
const endIndex = stdout.indexOf(endMarker);
|
|
1435
|
-
if (startIndex !== -1 && endIndex !== -1) {
|
|
1436
|
-
const jsonStr = stdout.substring(startIndex + startMarker.length, endIndex).trim();
|
|
1437
|
-
try {
|
|
1438
|
-
result = JSON.parse(jsonStr);
|
|
1439
|
-
} catch (e) {
|
|
1440
|
-
result = jsonStr;
|
|
1441
|
-
}
|
|
1442
|
-
logs = logs.map((log) => {
|
|
1443
|
-
let cleaned = log.replace(
|
|
1444
|
-
new RegExp(startMarker + "[\\s\\S]*?" + endMarker),
|
|
1445
|
-
"[Result captured]"
|
|
1446
|
-
);
|
|
1447
|
-
cleaned = cleaned.split("\n").filter((l) => !l.includes("__MCP_TOOL_CALL__")).join("\n");
|
|
1448
|
-
return cleaned;
|
|
1449
|
-
});
|
|
1450
|
-
}
|
|
1451
|
-
}
|
|
1452
|
-
} catch (e) {
|
|
1453
|
-
error = e.message || String(e);
|
|
1454
|
-
if (error && (error.includes("timeout") || error.includes("timed out"))) {
|
|
1455
|
-
error = "Script execution timed out";
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
1458
|
-
return {
|
|
1459
|
-
result,
|
|
1460
|
-
logs,
|
|
1461
|
-
error,
|
|
1462
|
-
execution_time: (Date.now() - startTime) / 1e3
|
|
1463
|
-
};
|
|
1464
|
-
}
|
|
1465
|
-
/**
|
|
1466
|
-
* Clean up the E2B sandbox.
|
|
1467
|
-
* Should be called when the executor is no longer needed.
|
|
1468
|
-
*/
|
|
1469
|
-
async cleanup() {
|
|
1470
|
-
if (this.codeExecSandbox) {
|
|
1471
|
-
try {
|
|
1472
|
-
await this.codeExecSandbox.kill();
|
|
1473
|
-
this.codeExecSandbox = null;
|
|
1474
|
-
logger.debug("E2B code execution sandbox stopped");
|
|
1475
|
-
} catch (error) {
|
|
1476
|
-
logger.error("Failed to stop E2B code execution sandbox:", error);
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
|
-
}
|
|
1480
|
-
};
|
|
1481
|
-
|
|
1482
|
-
// src/client/executors/vm.ts
|
|
1483
|
-
var vm = null;
|
|
1484
|
-
var vmCheckAttempted = false;
|
|
1485
|
-
function getVMModuleName() {
|
|
1486
|
-
return ["node", "vm"].join(":");
|
|
1487
|
-
}
|
|
1488
|
-
__name(getVMModuleName, "getVMModuleName");
|
|
1489
|
-
function tryLoadVM() {
|
|
1490
|
-
if (vmCheckAttempted) {
|
|
1491
|
-
return vm !== null;
|
|
1492
|
-
}
|
|
1493
|
-
vmCheckAttempted = true;
|
|
1494
|
-
try {
|
|
1495
|
-
const nodeRequire = typeof __require !== "undefined" ? __require : null;
|
|
1496
|
-
if (nodeRequire) {
|
|
1497
|
-
vm = nodeRequire(getVMModuleName());
|
|
1498
|
-
return true;
|
|
1499
|
-
}
|
|
1500
|
-
} catch (error) {
|
|
1501
|
-
logger.debug("node:vm module not available via require");
|
|
1502
|
-
}
|
|
1503
|
-
return false;
|
|
1504
|
-
}
|
|
1505
|
-
__name(tryLoadVM, "tryLoadVM");
|
|
1506
|
-
async function tryLoadVMAsync() {
|
|
1507
|
-
if (vm !== null) {
|
|
1508
|
-
return true;
|
|
1509
|
-
}
|
|
1510
|
-
if (!vmCheckAttempted) {
|
|
1511
|
-
if (tryLoadVM()) {
|
|
1512
|
-
return true;
|
|
1513
|
-
}
|
|
1514
|
-
}
|
|
1515
|
-
try {
|
|
1516
|
-
vm = await import(
|
|
1517
|
-
/* @vite-ignore */
|
|
1518
|
-
getVMModuleName()
|
|
1519
|
-
);
|
|
1520
|
-
return true;
|
|
1521
|
-
} catch (error) {
|
|
1522
|
-
logger.debug(
|
|
1523
|
-
"node:vm module not available in this environment (e.g., Deno)"
|
|
1524
|
-
);
|
|
1525
|
-
return false;
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
__name(tryLoadVMAsync, "tryLoadVMAsync");
|
|
1529
|
-
function isVMAvailable() {
|
|
1530
|
-
tryLoadVM();
|
|
1531
|
-
return vm !== null;
|
|
1532
|
-
}
|
|
1533
|
-
__name(isVMAvailable, "isVMAvailable");
|
|
1534
|
-
var VMCodeExecutor = class extends BaseCodeExecutor {
|
|
1535
|
-
static {
|
|
1536
|
-
__name(this, "VMCodeExecutor");
|
|
1537
|
-
}
|
|
1538
|
-
defaultTimeout;
|
|
1539
|
-
memoryLimitMb;
|
|
1540
|
-
constructor(client, options) {
|
|
1541
|
-
super(client);
|
|
1542
|
-
this.defaultTimeout = options?.timeoutMs ?? 3e4;
|
|
1543
|
-
this.memoryLimitMb = options?.memoryLimitMb;
|
|
1544
|
-
tryLoadVM();
|
|
1545
|
-
}
|
|
1546
|
-
/**
|
|
1547
|
-
* Ensure VM module is loaded before execution
|
|
1548
|
-
*/
|
|
1549
|
-
async ensureVMLoaded() {
|
|
1550
|
-
if (vm !== null) {
|
|
1551
|
-
return;
|
|
1552
|
-
}
|
|
1553
|
-
const loaded = await tryLoadVMAsync();
|
|
1554
|
-
if (!loaded) {
|
|
1555
|
-
throw new Error(
|
|
1556
|
-
"node:vm module is not available in this environment. Please use E2B executor instead or run in a Node.js environment."
|
|
1557
|
-
);
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
/**
|
|
1561
|
-
* Execute JavaScript/TypeScript code with access to MCP tools.
|
|
1562
|
-
*
|
|
1563
|
-
* @param code - Code to execute
|
|
1564
|
-
* @param timeout - Execution timeout in milliseconds (default: configured timeout or 30000)
|
|
1565
|
-
*/
|
|
1566
|
-
async execute(code, timeout) {
|
|
1567
|
-
const effectiveTimeout = timeout ?? this.defaultTimeout;
|
|
1568
|
-
await this.ensureVMLoaded();
|
|
1569
|
-
await this.ensureServersConnected();
|
|
1570
|
-
const logs = [];
|
|
1571
|
-
const startTime = Date.now();
|
|
1572
|
-
let result = null;
|
|
1573
|
-
let error = null;
|
|
1574
|
-
try {
|
|
1575
|
-
const context = await this._buildContext(logs);
|
|
1576
|
-
const wrappedCode = `
|
|
1577
|
-
(async () => {
|
|
1578
|
-
try {
|
|
1579
|
-
${code}
|
|
1580
|
-
} catch (e) {
|
|
1581
|
-
throw e;
|
|
1582
|
-
}
|
|
1583
|
-
})()
|
|
1584
|
-
`;
|
|
1585
|
-
const script = new vm.Script(wrappedCode, {
|
|
1586
|
-
filename: "agent_code.js"
|
|
1587
|
-
});
|
|
1588
|
-
const promise = script.runInNewContext(context, {
|
|
1589
|
-
timeout: effectiveTimeout,
|
|
1590
|
-
displayErrors: true
|
|
1591
|
-
});
|
|
1592
|
-
result = await promise;
|
|
1593
|
-
} catch (e) {
|
|
1594
|
-
error = e.message || String(e);
|
|
1595
|
-
if (e.code === "ERR_SCRIPT_EXECUTION_TIMEOUT" || e.message === "Script execution timed out." || typeof error === "string" && (error.includes("timed out") || error.includes("timeout"))) {
|
|
1596
|
-
error = "Script execution timed out";
|
|
1597
|
-
}
|
|
1598
|
-
if (e.stack) {
|
|
1599
|
-
logger.debug(`Code execution error stack: ${e.stack}`);
|
|
1600
|
-
}
|
|
1601
|
-
}
|
|
1602
|
-
const executionTime = (Date.now() - startTime) / 1e3;
|
|
1603
|
-
return {
|
|
1604
|
-
result,
|
|
1605
|
-
logs,
|
|
1606
|
-
error,
|
|
1607
|
-
execution_time: executionTime
|
|
1608
|
-
};
|
|
1609
|
-
}
|
|
1610
|
-
/**
|
|
1611
|
-
* Build the VM execution context with MCP tools and standard globals.
|
|
1612
|
-
*
|
|
1613
|
-
* @param logs - Array to capture console output
|
|
1614
|
-
*/
|
|
1615
|
-
async _buildContext(logs) {
|
|
1616
|
-
const logHandler = /* @__PURE__ */ __name((...args) => {
|
|
1617
|
-
logs.push(
|
|
1618
|
-
args.map(
|
|
1619
|
-
(arg) => typeof arg === "object" ? JSON.stringify(arg, null, 2) : String(arg)
|
|
1620
|
-
).join(" ")
|
|
1621
|
-
);
|
|
1622
|
-
}, "logHandler");
|
|
1623
|
-
const sandbox = {
|
|
1624
|
-
console: {
|
|
1625
|
-
log: logHandler,
|
|
1626
|
-
error: /* @__PURE__ */ __name((...args) => {
|
|
1627
|
-
logHandler("[ERROR]", ...args);
|
|
1628
|
-
}, "error"),
|
|
1629
|
-
warn: /* @__PURE__ */ __name((...args) => {
|
|
1630
|
-
logHandler("[WARN]", ...args);
|
|
1631
|
-
}, "warn"),
|
|
1632
|
-
info: logHandler,
|
|
1633
|
-
debug: logHandler
|
|
1634
|
-
},
|
|
1635
|
-
// Standard globals
|
|
1636
|
-
Object,
|
|
1637
|
-
Array,
|
|
1638
|
-
String,
|
|
1639
|
-
Number,
|
|
1640
|
-
Boolean,
|
|
1641
|
-
Date,
|
|
1642
|
-
Math,
|
|
1643
|
-
JSON,
|
|
1644
|
-
RegExp,
|
|
1645
|
-
Map,
|
|
1646
|
-
Set,
|
|
1647
|
-
Promise,
|
|
1648
|
-
parseInt,
|
|
1649
|
-
parseFloat,
|
|
1650
|
-
isNaN,
|
|
1651
|
-
isFinite,
|
|
1652
|
-
encodeURI,
|
|
1653
|
-
decodeURI,
|
|
1654
|
-
encodeURIComponent,
|
|
1655
|
-
decodeURIComponent,
|
|
1656
|
-
setTimeout,
|
|
1657
|
-
clearTimeout,
|
|
1658
|
-
// Helper for tools
|
|
1659
|
-
search_tools: this.createSearchToolsFunction(),
|
|
1660
|
-
__tool_namespaces: []
|
|
1661
|
-
};
|
|
1662
|
-
const toolNamespaces = {};
|
|
1663
|
-
const namespaceInfos = this.getToolNamespaces();
|
|
1664
|
-
for (const { serverName, tools, session } of namespaceInfos) {
|
|
1665
|
-
const serverNamespace = {};
|
|
1666
|
-
for (const tool of tools) {
|
|
1667
|
-
const toolName = tool.name;
|
|
1668
|
-
serverNamespace[toolName] = async (args) => {
|
|
1669
|
-
const result = await session.connector.callTool(toolName, args || {});
|
|
1670
|
-
if (result.content && result.content.length > 0) {
|
|
1671
|
-
const item = result.content[0];
|
|
1672
|
-
if (item.type === "text") {
|
|
1673
|
-
try {
|
|
1674
|
-
return JSON.parse(item.text);
|
|
1675
|
-
} catch {
|
|
1676
|
-
return item.text;
|
|
1677
|
-
}
|
|
1678
|
-
}
|
|
1679
|
-
return item;
|
|
1680
|
-
}
|
|
1681
|
-
return result;
|
|
1682
|
-
};
|
|
1683
|
-
}
|
|
1684
|
-
sandbox[serverName] = serverNamespace;
|
|
1685
|
-
toolNamespaces[serverName] = true;
|
|
1686
|
-
}
|
|
1687
|
-
sandbox.__tool_namespaces = Object.keys(toolNamespaces);
|
|
1688
|
-
return vm.createContext(sandbox);
|
|
1689
|
-
}
|
|
1690
|
-
/**
|
|
1691
|
-
* Clean up resources.
|
|
1692
|
-
* VM executor doesn't need cleanup, but method kept for interface consistency.
|
|
1693
|
-
*/
|
|
1694
|
-
async cleanup() {
|
|
1695
|
-
}
|
|
1696
|
-
};
|
|
1697
|
-
|
|
1698
|
-
// src/connectors/stdio.ts
|
|
1699
|
-
import process2 from "process";
|
|
1700
|
-
import { Client } from "@mcp-use/modelcontextprotocol-sdk/client/index.js";
|
|
1701
|
-
|
|
1702
|
-
// src/task_managers/stdio.ts
|
|
1703
|
-
import { StdioClientTransport } from "@mcp-use/modelcontextprotocol-sdk/client/stdio.js";
|
|
1704
|
-
var StdioConnectionManager = class extends ConnectionManager {
|
|
1705
|
-
static {
|
|
1706
|
-
__name(this, "StdioConnectionManager");
|
|
1707
|
-
}
|
|
1708
|
-
serverParams;
|
|
1709
|
-
errlog;
|
|
1710
|
-
_transport = null;
|
|
1711
|
-
/**
|
|
1712
|
-
* Create a new stdio connection manager.
|
|
1713
|
-
*
|
|
1714
|
-
* @param serverParams Parameters for the stdio server process.
|
|
1715
|
-
* @param errlog Stream to which the server's stderr should be piped.
|
|
1716
|
-
* Defaults to `process.stderr`.
|
|
1717
|
-
*/
|
|
1718
|
-
constructor(serverParams, errlog = process.stderr) {
|
|
1719
|
-
super();
|
|
1720
|
-
this.serverParams = serverParams;
|
|
1721
|
-
this.errlog = errlog;
|
|
1722
|
-
}
|
|
1723
|
-
/**
|
|
1724
|
-
* Establish the stdio connection by spawning the server process and starting
|
|
1725
|
-
* the SDK's transport. Returns the live `StdioClientTransport` instance.
|
|
1726
|
-
*/
|
|
1727
|
-
async establishConnection() {
|
|
1728
|
-
this._transport = new StdioClientTransport(this.serverParams);
|
|
1729
|
-
if (this._transport.stderr && typeof this._transport.stderr.pipe === "function") {
|
|
1730
|
-
this._transport.stderr.pipe(
|
|
1731
|
-
this.errlog
|
|
1732
|
-
);
|
|
1733
|
-
}
|
|
1734
|
-
logger.debug(`${this.constructor.name} connected successfully`);
|
|
1735
|
-
return this._transport;
|
|
1736
|
-
}
|
|
1737
|
-
/**
|
|
1738
|
-
* Close the stdio connection, making sure the transport cleans up the child
|
|
1739
|
-
* process and associated resources.
|
|
1740
|
-
*/
|
|
1741
|
-
async closeConnection(_connection) {
|
|
1742
|
-
if (this._transport) {
|
|
1743
|
-
try {
|
|
1744
|
-
await this._transport.close();
|
|
1745
|
-
} catch (e) {
|
|
1746
|
-
logger.warn(`Error closing stdio transport: ${e}`);
|
|
1747
|
-
} finally {
|
|
1748
|
-
this._transport = null;
|
|
1749
|
-
}
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
};
|
|
1753
|
-
|
|
1754
|
-
// src/connectors/stdio.ts
|
|
1755
|
-
var StdioConnector = class extends BaseConnector {
|
|
1756
|
-
static {
|
|
1757
|
-
__name(this, "StdioConnector");
|
|
1758
|
-
}
|
|
1759
|
-
command;
|
|
1760
|
-
args;
|
|
1761
|
-
env;
|
|
1762
|
-
errlog;
|
|
1763
|
-
clientInfo;
|
|
1764
|
-
constructor({
|
|
1765
|
-
command = "npx",
|
|
1766
|
-
args = [],
|
|
1767
|
-
env,
|
|
1768
|
-
errlog = process2.stderr,
|
|
1769
|
-
...rest
|
|
1770
|
-
} = {}) {
|
|
1771
|
-
super(rest);
|
|
1772
|
-
this.command = command;
|
|
1773
|
-
this.args = args;
|
|
1774
|
-
this.env = env;
|
|
1775
|
-
this.errlog = errlog;
|
|
1776
|
-
this.clientInfo = rest.clientInfo ?? {
|
|
1777
|
-
name: "stdio-connector",
|
|
1778
|
-
version: "1.0.0"
|
|
1779
|
-
};
|
|
1780
|
-
}
|
|
1781
|
-
/** Establish connection to the MCP implementation. */
|
|
1782
|
-
async connect() {
|
|
1783
|
-
if (this.connected) {
|
|
1784
|
-
logger.debug("Already connected to MCP implementation");
|
|
1785
|
-
return;
|
|
1786
|
-
}
|
|
1787
|
-
logger.debug(`Connecting to MCP implementation via stdio: ${this.command}`);
|
|
1788
|
-
try {
|
|
1789
|
-
let mergedEnv;
|
|
1790
|
-
if (this.env) {
|
|
1791
|
-
mergedEnv = {};
|
|
1792
|
-
for (const [key, value] of Object.entries(process2.env)) {
|
|
1793
|
-
if (value !== void 0) {
|
|
1794
|
-
mergedEnv[key] = value;
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
Object.assign(mergedEnv, this.env);
|
|
1798
|
-
}
|
|
1799
|
-
const serverParams = {
|
|
1800
|
-
command: this.command,
|
|
1801
|
-
args: this.args,
|
|
1802
|
-
env: mergedEnv
|
|
1803
|
-
};
|
|
1804
|
-
this.connectionManager = new StdioConnectionManager(
|
|
1805
|
-
serverParams,
|
|
1806
|
-
this.errlog
|
|
1807
|
-
);
|
|
1808
|
-
const transport = await this.connectionManager.start();
|
|
1809
|
-
const clientOptions = {
|
|
1810
|
-
...this.opts.clientOptions || {},
|
|
1811
|
-
capabilities: {
|
|
1812
|
-
...this.opts.clientOptions?.capabilities || {},
|
|
1813
|
-
roots: { listChanged: true },
|
|
1814
|
-
// Always advertise roots capability
|
|
1815
|
-
// Add sampling capability if callback is provided
|
|
1816
|
-
...this.opts.samplingCallback ? { sampling: {} } : {},
|
|
1817
|
-
// Add elicitation capability if callback is provided
|
|
1818
|
-
...this.opts.elicitationCallback ? { elicitation: { form: {}, url: {} } } : {}
|
|
1819
|
-
}
|
|
1820
|
-
};
|
|
1821
|
-
this.client = new Client(this.clientInfo, clientOptions);
|
|
1822
|
-
await this.client.connect(transport);
|
|
1823
|
-
this.connected = true;
|
|
1824
|
-
this.setupNotificationHandler();
|
|
1825
|
-
this.setupRootsHandler();
|
|
1826
|
-
this.setupSamplingHandler();
|
|
1827
|
-
this.setupElicitationHandler();
|
|
1828
|
-
logger.debug(
|
|
1829
|
-
`Successfully connected to MCP implementation: ${this.command}`
|
|
1830
|
-
);
|
|
1831
|
-
this.trackConnectorInit({
|
|
1832
|
-
serverCommand: this.command,
|
|
1833
|
-
serverArgs: this.args,
|
|
1834
|
-
publicIdentifier: `${this.command} ${this.args.join(" ")}`
|
|
1835
|
-
});
|
|
1836
|
-
} catch (err) {
|
|
1837
|
-
logger.error(`Failed to connect to MCP implementation: ${err}`);
|
|
1838
|
-
await this.cleanupResources();
|
|
1839
|
-
throw err;
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
get publicIdentifier() {
|
|
1843
|
-
return {
|
|
1844
|
-
type: "stdio",
|
|
1845
|
-
"command&args": `${this.command} ${this.args.join(" ")}`
|
|
1846
|
-
};
|
|
1847
|
-
}
|
|
1848
|
-
};
|
|
1849
|
-
|
|
1850
|
-
// src/config.ts
|
|
1851
|
-
import { readFileSync } from "fs";
|
|
1852
|
-
function loadConfigFile(filepath) {
|
|
1853
|
-
const raw = readFileSync(filepath, "utf-8");
|
|
1854
|
-
return JSON.parse(raw);
|
|
1855
|
-
}
|
|
1856
|
-
__name(loadConfigFile, "loadConfigFile");
|
|
1857
|
-
function createConnectorFromConfig(serverConfig, connectorOptions) {
|
|
1858
|
-
if ("command" in serverConfig && "args" in serverConfig) {
|
|
1859
|
-
return new StdioConnector({
|
|
1860
|
-
command: serverConfig.command,
|
|
1861
|
-
args: serverConfig.args,
|
|
1862
|
-
env: serverConfig.env,
|
|
1863
|
-
...connectorOptions
|
|
1864
|
-
});
|
|
1865
|
-
}
|
|
1866
|
-
if ("url" in serverConfig) {
|
|
1867
|
-
const transport = serverConfig.transport || "http";
|
|
1868
|
-
return new HttpConnector(serverConfig.url, {
|
|
1869
|
-
headers: serverConfig.headers,
|
|
1870
|
-
authToken: serverConfig.auth_token || serverConfig.authToken,
|
|
1871
|
-
// Only force SSE if explicitly requested
|
|
1872
|
-
preferSse: serverConfig.preferSse || transport === "sse",
|
|
1873
|
-
...connectorOptions
|
|
1874
|
-
});
|
|
1875
|
-
}
|
|
1876
|
-
throw new Error("Cannot determine connector type from config");
|
|
1877
|
-
}
|
|
1878
|
-
__name(createConnectorFromConfig, "createConnectorFromConfig");
|
|
1879
|
-
|
|
1880
|
-
// src/client.ts
|
|
1881
|
-
import fs from "fs";
|
|
1882
|
-
import path from "path";
|
|
1883
|
-
var MCPClient = class _MCPClient extends BaseMCPClient {
|
|
1884
|
-
static {
|
|
1885
|
-
__name(this, "MCPClient");
|
|
1886
|
-
}
|
|
1887
|
-
/**
|
|
1888
|
-
* Get the mcp-use package version.
|
|
1889
|
-
* Works in all environments (Node.js, browser, Cloudflare Workers, Deno, etc.)
|
|
1890
|
-
*/
|
|
1891
|
-
static getPackageVersion() {
|
|
1892
|
-
return getPackageVersion();
|
|
1893
|
-
}
|
|
1894
|
-
codeMode = false;
|
|
1895
|
-
_codeExecutor = null;
|
|
1896
|
-
_customCodeExecutor = null;
|
|
1897
|
-
_codeExecutorConfig = "vm";
|
|
1898
|
-
_executorOptions;
|
|
1899
|
-
_samplingCallback;
|
|
1900
|
-
_elicitationCallback;
|
|
1901
|
-
constructor(config, options) {
|
|
1902
|
-
if (config) {
|
|
1903
|
-
if (typeof config === "string") {
|
|
1904
|
-
super(loadConfigFile(config));
|
|
1905
|
-
} else {
|
|
1906
|
-
super(config);
|
|
1907
|
-
}
|
|
1908
|
-
} else {
|
|
1909
|
-
super();
|
|
1910
|
-
}
|
|
1911
|
-
let codeModeEnabled = false;
|
|
1912
|
-
let executorConfig = "vm";
|
|
1913
|
-
let executorOptions;
|
|
1914
|
-
if (options?.codeMode) {
|
|
1915
|
-
if (typeof options.codeMode === "boolean") {
|
|
1916
|
-
codeModeEnabled = options.codeMode;
|
|
1917
|
-
} else {
|
|
1918
|
-
codeModeEnabled = options.codeMode.enabled;
|
|
1919
|
-
executorConfig = options.codeMode.executor ?? "vm";
|
|
1920
|
-
executorOptions = options.codeMode.executorOptions;
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
this.codeMode = codeModeEnabled;
|
|
1924
|
-
this._codeExecutorConfig = executorConfig;
|
|
1925
|
-
this._executorOptions = executorOptions;
|
|
1926
|
-
this._samplingCallback = options?.samplingCallback;
|
|
1927
|
-
this._elicitationCallback = options?.elicitationCallback;
|
|
1928
|
-
if (this.codeMode) {
|
|
1929
|
-
this._setupCodeModeConnector();
|
|
1930
|
-
}
|
|
1931
|
-
this._trackClientInit();
|
|
1932
|
-
}
|
|
1933
|
-
_trackClientInit() {
|
|
1934
|
-
const servers = Object.keys(this.config.mcpServers ?? {});
|
|
1935
|
-
const hasSamplingCallback = !!this._samplingCallback;
|
|
1936
|
-
const hasElicitationCallback = !!this._elicitationCallback;
|
|
1937
|
-
Tel.getInstance().trackMCPClientInit({
|
|
1938
|
-
codeMode: this.codeMode,
|
|
1939
|
-
sandbox: false,
|
|
1940
|
-
// Sandbox not supported in TS yet
|
|
1941
|
-
allCallbacks: hasSamplingCallback && hasElicitationCallback,
|
|
1942
|
-
verify: false,
|
|
1943
|
-
// No verify option in TS client
|
|
1944
|
-
servers,
|
|
1945
|
-
numServers: servers.length,
|
|
1946
|
-
isBrowser: false
|
|
1947
|
-
// Node.js MCPClient
|
|
1948
|
-
}).catch((e) => logger.debug(`Failed to track MCPClient init: ${e}`));
|
|
1949
|
-
}
|
|
1950
|
-
static fromDict(cfg, options) {
|
|
1951
|
-
return new _MCPClient(cfg, options);
|
|
1952
|
-
}
|
|
1953
|
-
static fromConfigFile(path2, options) {
|
|
1954
|
-
return new _MCPClient(loadConfigFile(path2), options);
|
|
1955
|
-
}
|
|
1956
|
-
/**
|
|
1957
|
-
* Save configuration to a file (Node.js only)
|
|
1958
|
-
*/
|
|
1959
|
-
saveConfig(filepath) {
|
|
1960
|
-
const dir = path.dirname(filepath);
|
|
1961
|
-
if (!fs.existsSync(dir)) {
|
|
1962
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
1963
|
-
}
|
|
1964
|
-
fs.writeFileSync(filepath, JSON.stringify(this.config, null, 2), "utf-8");
|
|
1965
|
-
}
|
|
1966
|
-
/**
|
|
1967
|
-
* Create a connector from server configuration (Node.js version)
|
|
1968
|
-
* Supports all connector types including StdioConnector
|
|
1969
|
-
*/
|
|
1970
|
-
createConnectorFromConfig(serverConfig) {
|
|
1971
|
-
return createConnectorFromConfig(serverConfig, {
|
|
1972
|
-
samplingCallback: this._samplingCallback,
|
|
1973
|
-
elicitationCallback: this._elicitationCallback
|
|
1974
|
-
});
|
|
1975
|
-
}
|
|
1976
|
-
_setupCodeModeConnector() {
|
|
1977
|
-
logger.debug("Code mode connector initialized as internal meta server");
|
|
1978
|
-
const connector = new CodeModeConnector(this);
|
|
1979
|
-
const session = new MCPSession(connector);
|
|
1980
|
-
this.sessions["code_mode"] = session;
|
|
1981
|
-
this.activeSessions.push("code_mode");
|
|
1982
|
-
}
|
|
1983
|
-
_ensureCodeExecutor() {
|
|
1984
|
-
if (!this._codeExecutor) {
|
|
1985
|
-
const config = this._codeExecutorConfig;
|
|
1986
|
-
if (config instanceof BaseCodeExecutor) {
|
|
1987
|
-
this._codeExecutor = config;
|
|
1988
|
-
} else if (typeof config === "function") {
|
|
1989
|
-
this._customCodeExecutor = config;
|
|
1990
|
-
throw new Error(
|
|
1991
|
-
"Custom executor function should be handled in executeCode"
|
|
1992
|
-
);
|
|
1993
|
-
} else if (config === "e2b") {
|
|
1994
|
-
const opts = this._executorOptions;
|
|
1995
|
-
if (!opts?.apiKey) {
|
|
1996
|
-
logger.warn("E2B executor requires apiKey. Falling back to VM.");
|
|
1997
|
-
try {
|
|
1998
|
-
this._codeExecutor = new VMCodeExecutor(
|
|
1999
|
-
this,
|
|
2000
|
-
this._executorOptions
|
|
2001
|
-
);
|
|
2002
|
-
} catch (error) {
|
|
2003
|
-
throw new Error(
|
|
2004
|
-
"VM executor is not available in this environment and E2B API key is not provided. Please provide an E2B API key or run in a Node.js environment."
|
|
2005
|
-
);
|
|
2006
|
-
}
|
|
2007
|
-
} else {
|
|
2008
|
-
this._codeExecutor = new E2BCodeExecutor(this, opts);
|
|
2009
|
-
}
|
|
2010
|
-
} else {
|
|
2011
|
-
try {
|
|
2012
|
-
this._codeExecutor = new VMCodeExecutor(
|
|
2013
|
-
this,
|
|
2014
|
-
this._executorOptions
|
|
2015
|
-
);
|
|
2016
|
-
} catch (error) {
|
|
2017
|
-
const e2bOpts = this._executorOptions;
|
|
2018
|
-
const e2bApiKey = e2bOpts?.apiKey || process.env.E2B_API_KEY;
|
|
2019
|
-
if (e2bApiKey) {
|
|
2020
|
-
logger.info(
|
|
2021
|
-
"VM executor not available in this environment. Falling back to E2B."
|
|
2022
|
-
);
|
|
2023
|
-
this._codeExecutor = new E2BCodeExecutor(this, {
|
|
2024
|
-
...e2bOpts,
|
|
2025
|
-
apiKey: e2bApiKey
|
|
2026
|
-
});
|
|
2027
|
-
} else {
|
|
2028
|
-
throw new Error(
|
|
2029
|
-
"VM executor is not available in this environment. Please provide an E2B API key via executorOptions or E2B_API_KEY environment variable, or run in a Node.js environment."
|
|
2030
|
-
);
|
|
2031
|
-
}
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
2035
|
-
return this._codeExecutor;
|
|
2036
|
-
}
|
|
2037
|
-
/**
|
|
2038
|
-
* Execute code in code mode
|
|
2039
|
-
*/
|
|
2040
|
-
async executeCode(code, timeout) {
|
|
2041
|
-
if (!this.codeMode) {
|
|
2042
|
-
throw new Error("Code execution mode is not enabled");
|
|
2043
|
-
}
|
|
2044
|
-
if (this._customCodeExecutor) {
|
|
2045
|
-
return this._customCodeExecutor(code, timeout);
|
|
2046
|
-
}
|
|
2047
|
-
return this._ensureCodeExecutor().execute(code, timeout);
|
|
2048
|
-
}
|
|
2049
|
-
/**
|
|
2050
|
-
* Search available tools (used by code mode)
|
|
2051
|
-
*/
|
|
2052
|
-
async searchTools(query = "", detailLevel = "full") {
|
|
2053
|
-
if (!this.codeMode) {
|
|
2054
|
-
throw new Error("Code execution mode is not enabled");
|
|
2055
|
-
}
|
|
2056
|
-
return this._ensureCodeExecutor().createSearchToolsFunction()(
|
|
2057
|
-
query,
|
|
2058
|
-
detailLevel
|
|
2059
|
-
);
|
|
2060
|
-
}
|
|
2061
|
-
/**
|
|
2062
|
-
* Override getServerNames to exclude internal code_mode server
|
|
2063
|
-
*/
|
|
2064
|
-
getServerNames() {
|
|
2065
|
-
const isCodeModeEnabled = this.codeMode;
|
|
2066
|
-
return super.getServerNames().filter((name) => {
|
|
2067
|
-
return !isCodeModeEnabled || name !== "code_mode";
|
|
2068
|
-
});
|
|
2069
|
-
}
|
|
2070
|
-
/**
|
|
2071
|
-
* Close the client and clean up resources including code executors.
|
|
2072
|
-
* This ensures E2B sandboxes and other resources are properly released.
|
|
2073
|
-
*/
|
|
2074
|
-
async close() {
|
|
2075
|
-
if (this._codeExecutor) {
|
|
2076
|
-
await this._codeExecutor.cleanup();
|
|
2077
|
-
this._codeExecutor = null;
|
|
2078
|
-
}
|
|
2079
|
-
await this.closeAllSessions();
|
|
2080
|
-
}
|
|
2081
|
-
};
|
|
2082
|
-
|
|
2083
|
-
// src/managers/tools/acquire_active_mcp_server.ts
|
|
2084
|
-
import { z as z3 } from "zod";
|
|
2085
|
-
|
|
2086
|
-
// src/managers/tools/base.ts
|
|
2087
|
-
import { StructuredTool } from "@langchain/core/tools";
|
|
2088
|
-
var MCPServerTool = class extends StructuredTool {
|
|
2089
|
-
static {
|
|
2090
|
-
__name(this, "MCPServerTool");
|
|
2091
|
-
}
|
|
2092
|
-
name = "mcp_server_tool";
|
|
2093
|
-
description = "Base tool for MCP server operations.";
|
|
2094
|
-
schema;
|
|
2095
|
-
_manager;
|
|
2096
|
-
constructor(manager) {
|
|
2097
|
-
super();
|
|
2098
|
-
this._manager = manager;
|
|
2099
|
-
}
|
|
2100
|
-
async _call(_arg, _runManager, _parentConfig) {
|
|
2101
|
-
throw new Error("Method not implemented.");
|
|
2102
|
-
}
|
|
2103
|
-
get manager() {
|
|
2104
|
-
return this._manager;
|
|
2105
|
-
}
|
|
2106
|
-
};
|
|
2107
|
-
|
|
2108
|
-
// src/managers/tools/acquire_active_mcp_server.ts
|
|
2109
|
-
var PresentActiveServerSchema = z3.object({});
|
|
2110
|
-
var AcquireActiveMCPServerTool = class extends MCPServerTool {
|
|
2111
|
-
static {
|
|
2112
|
-
__name(this, "AcquireActiveMCPServerTool");
|
|
2113
|
-
}
|
|
2114
|
-
name = "get_active_mcp_server";
|
|
2115
|
-
description = "Get the currently active MCP (Model Context Protocol) server";
|
|
2116
|
-
schema = PresentActiveServerSchema;
|
|
2117
|
-
constructor(manager) {
|
|
2118
|
-
super(manager);
|
|
2119
|
-
}
|
|
2120
|
-
async _call() {
|
|
2121
|
-
if (!this.manager.activeServer) {
|
|
2122
|
-
return `No MCP server is currently active. Use connect_to_mcp_server to connect to a server.`;
|
|
2123
|
-
}
|
|
2124
|
-
return `Currently active MCP server: ${this.manager.activeServer}`;
|
|
2125
|
-
}
|
|
2126
|
-
};
|
|
2127
|
-
|
|
2128
|
-
// src/managers/tools/add_server_from_config.ts
|
|
2129
|
-
import { StructuredTool as StructuredTool2 } from "@langchain/core/tools";
|
|
2130
|
-
import { z as z4 } from "zod";
|
|
2131
|
-
var AddMCPServerFromConfigTool = class extends StructuredTool2 {
|
|
2132
|
-
static {
|
|
2133
|
-
__name(this, "AddMCPServerFromConfigTool");
|
|
2134
|
-
}
|
|
2135
|
-
name = "add_mcp_server_from_config";
|
|
2136
|
-
description = "Adds a new MCP server to the client from a configuration object and connects to it, making its tools available.";
|
|
2137
|
-
schema = z4.object({
|
|
2138
|
-
serverName: z4.string().describe("The name for the new MCP server."),
|
|
2139
|
-
serverConfig: z4.any().describe(
|
|
2140
|
-
'The configuration object for the server. This should not include the top-level "mcpServers" key.'
|
|
2141
|
-
)
|
|
2142
|
-
});
|
|
2143
|
-
manager;
|
|
2144
|
-
constructor(manager) {
|
|
2145
|
-
super();
|
|
2146
|
-
this.manager = manager;
|
|
2147
|
-
}
|
|
2148
|
-
async _call({
|
|
2149
|
-
serverName,
|
|
2150
|
-
serverConfig
|
|
2151
|
-
}) {
|
|
2152
|
-
try {
|
|
2153
|
-
this.manager.client.addServer(serverName, serverConfig);
|
|
2154
|
-
let result = `Server '${serverName}' added to the client.`;
|
|
2155
|
-
logger.debug(
|
|
2156
|
-
`Connecting to new server '${serverName}' and discovering tools.`
|
|
2157
|
-
);
|
|
2158
|
-
const session = await this.manager.client.createSession(serverName);
|
|
2159
|
-
const connector = session.connector;
|
|
2160
|
-
const tools = await this.manager.adapter.createToolsFromConnectors([connector]);
|
|
2161
|
-
this.manager.serverTools[serverName] = tools;
|
|
2162
|
-
this.manager.initializedServers[serverName] = true;
|
|
2163
|
-
this.manager.activeServer = serverName;
|
|
2164
|
-
const numTools = tools.length;
|
|
2165
|
-
result += ` Session created and connected. '${serverName}' is now the active server with ${numTools} tools available.`;
|
|
2166
|
-
result += `
|
|
2167
|
-
|
|
2168
|
-
${tools.map((t) => t.name).join("\n")}`;
|
|
2169
|
-
logger.info(result);
|
|
2170
|
-
return result;
|
|
2171
|
-
} catch (e) {
|
|
2172
|
-
logger.error(
|
|
2173
|
-
`Failed to add or connect to server '${serverName}': ${e.message}`
|
|
2174
|
-
);
|
|
2175
|
-
return `Failed to add or connect to server '${serverName}': ${e.message}`;
|
|
2176
|
-
}
|
|
2177
|
-
}
|
|
2178
|
-
};
|
|
2179
|
-
|
|
2180
|
-
// src/managers/tools/connect_mcp_server.ts
|
|
2181
|
-
import { z as z5 } from "zod";
|
|
2182
|
-
var ConnectMCPServerSchema = z5.object({
|
|
2183
|
-
serverName: z5.string().describe("The name of the MCP server.")
|
|
2184
|
-
});
|
|
2185
|
-
var ConnectMCPServerTool = class extends MCPServerTool {
|
|
2186
|
-
static {
|
|
2187
|
-
__name(this, "ConnectMCPServerTool");
|
|
2188
|
-
}
|
|
2189
|
-
name = "connect_to_mcp_server";
|
|
2190
|
-
description = "Connect to a specific MCP (Model Context Protocol) server to use its tools. Use this tool to connect to a specific server and use its tools.";
|
|
2191
|
-
schema = ConnectMCPServerSchema;
|
|
2192
|
-
constructor(manager) {
|
|
2193
|
-
super(manager);
|
|
2194
|
-
}
|
|
2195
|
-
async _call({ serverName }) {
|
|
2196
|
-
const serverNames = this.manager.client.getServerNames();
|
|
2197
|
-
if (!serverNames.includes(serverName)) {
|
|
2198
|
-
const available = serverNames.length > 0 ? serverNames.join(", ") : "none";
|
|
2199
|
-
return `Server '${serverName}' not found. Available servers: ${available}`;
|
|
2200
|
-
}
|
|
2201
|
-
if (this.manager.activeServer === serverName) {
|
|
2202
|
-
return `Already connected to MCP server '${serverName}'`;
|
|
2203
|
-
}
|
|
2204
|
-
try {
|
|
2205
|
-
let session = this.manager.client.getSession(serverName);
|
|
2206
|
-
logger.debug(`Using existing session for server '${serverName}'`);
|
|
2207
|
-
if (!session) {
|
|
2208
|
-
logger.debug(`Creating new session for server '${serverName}'`);
|
|
2209
|
-
session = await this.manager.client.createSession(serverName);
|
|
2210
|
-
}
|
|
2211
|
-
this.manager.activeServer = serverName;
|
|
2212
|
-
if (!this.manager.serverTools[serverName]) {
|
|
2213
|
-
const connector = session.connector;
|
|
2214
|
-
const tools = await this.manager.adapter.createToolsFromConnectors([connector]);
|
|
2215
|
-
const resources = await this.manager.adapter.createResourcesFromConnectors([connector]);
|
|
2216
|
-
const prompts = await this.manager.adapter.createPromptsFromConnectors([connector]);
|
|
2217
|
-
const allItems = [...tools, ...resources, ...prompts];
|
|
2218
|
-
this.manager.serverTools[serverName] = allItems;
|
|
2219
|
-
this.manager.initializedServers[serverName] = true;
|
|
2220
|
-
logger.debug(
|
|
2221
|
-
`Loaded ${allItems.length} items for server '${serverName}': ${tools.length} tools, ${resources.length} resources, ${prompts.length} prompts`
|
|
2222
|
-
);
|
|
2223
|
-
}
|
|
2224
|
-
const serverTools = this.manager.serverTools[serverName] || [];
|
|
2225
|
-
const numTools = serverTools.length;
|
|
2226
|
-
return `Connected to MCP server '${serverName}'. ${numTools} tools, resources, and prompts are now available.`;
|
|
2227
|
-
} catch (error) {
|
|
2228
|
-
logger.error(
|
|
2229
|
-
`Error connecting to server '${serverName}': ${String(error)}`
|
|
2230
|
-
);
|
|
2231
|
-
return `Failed to connect to server '${serverName}': ${String(error)}`;
|
|
2232
|
-
}
|
|
2233
|
-
}
|
|
2234
|
-
};
|
|
2235
|
-
|
|
2236
|
-
// src/managers/tools/list_mcp_servers.ts
|
|
2237
|
-
import { z as z6 } from "zod";
|
|
2238
|
-
var EnumerateServersSchema = z6.object({});
|
|
2239
|
-
var ListMCPServersTool = class extends MCPServerTool {
|
|
2240
|
-
static {
|
|
2241
|
-
__name(this, "ListMCPServersTool");
|
|
2242
|
-
}
|
|
2243
|
-
name = "list_mcp_servers";
|
|
2244
|
-
description = `Lists all available MCP (Model Context Protocol) servers that can be connected to, along with the tools available on each server. Use this tool to discover servers and see what functionalities they offer.`;
|
|
2245
|
-
schema = EnumerateServersSchema;
|
|
2246
|
-
constructor(manager) {
|
|
2247
|
-
super(manager);
|
|
2248
|
-
}
|
|
2249
|
-
async _call() {
|
|
2250
|
-
const serverNames = this.manager.client.getServerNames();
|
|
2251
|
-
if (serverNames.length === 0) {
|
|
2252
|
-
return `No MCP servers are currently defined.`;
|
|
2253
|
-
}
|
|
2254
|
-
const outputLines = ["Available MCP servers:"];
|
|
2255
|
-
for (const serverName of serverNames) {
|
|
2256
|
-
const isActiveServer = serverName === this.manager.activeServer;
|
|
2257
|
-
const activeFlag = isActiveServer ? " (ACTIVE)" : "";
|
|
2258
|
-
outputLines.push(`- ${serverName}${activeFlag}`);
|
|
2259
|
-
try {
|
|
2260
|
-
const serverTools = this.manager.serverTools?.[serverName] ?? [];
|
|
2261
|
-
const numberOfTools = Array.isArray(serverTools) ? serverTools.length : 0;
|
|
2262
|
-
outputLines.push(`${numberOfTools} tools available for this server
|
|
2263
|
-
`);
|
|
2264
|
-
} catch (error) {
|
|
2265
|
-
logger.error(
|
|
2266
|
-
`Unexpected error listing tools for server '${serverName}': ${String(error)}`
|
|
2267
|
-
);
|
|
2268
|
-
}
|
|
2269
|
-
}
|
|
2270
|
-
return outputLines.join("\n");
|
|
2271
|
-
}
|
|
2272
|
-
};
|
|
2273
|
-
|
|
2274
|
-
// src/managers/tools/release_mcp_server_connection.ts
|
|
2275
|
-
import { z as z7 } from "zod";
|
|
2276
|
-
var ReleaseConnectionSchema = z7.object({});
|
|
2277
|
-
var ReleaseMCPServerConnectionTool = class extends MCPServerTool {
|
|
2278
|
-
static {
|
|
2279
|
-
__name(this, "ReleaseMCPServerConnectionTool");
|
|
2280
|
-
}
|
|
2281
|
-
name = "disconnect_from_mcp_server";
|
|
2282
|
-
description = "Disconnect from the currently active MCP (Model Context Protocol) server";
|
|
2283
|
-
schema = ReleaseConnectionSchema;
|
|
2284
|
-
constructor(manager) {
|
|
2285
|
-
super(manager);
|
|
2286
|
-
}
|
|
2287
|
-
async _call() {
|
|
2288
|
-
if (!this.manager.activeServer) {
|
|
2289
|
-
return `No MCP server is currently active, so there's nothing to disconnect from.`;
|
|
2290
|
-
}
|
|
2291
|
-
const serverName = this.manager.activeServer;
|
|
2292
|
-
this.manager.activeServer = null;
|
|
2293
|
-
return `Successfully disconnected from MCP server '${serverName}'.`;
|
|
2294
|
-
}
|
|
2295
|
-
};
|
|
2296
|
-
|
|
2297
|
-
// src/managers/server_manager.ts
|
|
2298
|
-
function isEqual(a, b) {
|
|
2299
|
-
if (a === b) return true;
|
|
2300
|
-
if (a == null || b == null) return false;
|
|
2301
|
-
if (typeof a !== typeof b) return false;
|
|
2302
|
-
if (a instanceof Date && b instanceof Date) {
|
|
2303
|
-
return a.getTime() === b.getTime();
|
|
2304
|
-
}
|
|
2305
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
2306
|
-
if (a.length !== b.length) return false;
|
|
2307
|
-
return a.every((item, index) => isEqual(item, b[index]));
|
|
2308
|
-
}
|
|
2309
|
-
if (typeof a === "object" && typeof b === "object") {
|
|
2310
|
-
const keysA = Object.keys(a);
|
|
2311
|
-
const keysB = Object.keys(b);
|
|
2312
|
-
if (keysA.length !== keysB.length) return false;
|
|
2313
|
-
return keysA.every((key) => {
|
|
2314
|
-
return Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key]);
|
|
2315
|
-
});
|
|
2316
|
-
}
|
|
2317
|
-
return false;
|
|
2318
|
-
}
|
|
2319
|
-
__name(isEqual, "isEqual");
|
|
2320
|
-
var ServerManager = class {
|
|
2321
|
-
static {
|
|
2322
|
-
__name(this, "ServerManager");
|
|
2323
|
-
}
|
|
2324
|
-
initializedServers = {};
|
|
2325
|
-
serverTools = {};
|
|
2326
|
-
client;
|
|
2327
|
-
adapter;
|
|
2328
|
-
activeServer = null;
|
|
2329
|
-
overrideManagementTools;
|
|
2330
|
-
constructor(client, adapter, managementTools) {
|
|
2331
|
-
this.client = client;
|
|
2332
|
-
this.adapter = adapter;
|
|
2333
|
-
this.overrideManagementTools = managementTools;
|
|
2334
|
-
}
|
|
2335
|
-
setManagementTools(tools) {
|
|
2336
|
-
this.overrideManagementTools = tools;
|
|
2337
|
-
logger.info(
|
|
2338
|
-
`Overriding default management tools with a new set of ${tools.length} tools.`
|
|
2339
|
-
);
|
|
2340
|
-
}
|
|
2341
|
-
logState(context) {
|
|
2342
|
-
const allServerNames = this.client.getServerNames();
|
|
2343
|
-
const activeSessionNames = Object.keys(this.client.getAllActiveSessions());
|
|
2344
|
-
if (allServerNames.length === 0) {
|
|
2345
|
-
logger.info("Server Manager State: No servers configured.");
|
|
2346
|
-
return;
|
|
2347
|
-
}
|
|
2348
|
-
const tableData = allServerNames.map((name) => ({
|
|
2349
|
-
"Server Name": name,
|
|
2350
|
-
Connected: activeSessionNames.includes(name) ? "\u2705" : "\u274C",
|
|
2351
|
-
Initialized: this.initializedServers[name] ? "\u2705" : "\u274C",
|
|
2352
|
-
"Tool Count": this.serverTools[name]?.length ?? 0,
|
|
2353
|
-
Active: this.activeServer === name ? "\u2705" : "\u274C"
|
|
2354
|
-
}));
|
|
2355
|
-
logger.info(`Server Manager State: [${context}]`);
|
|
2356
|
-
console.table(tableData);
|
|
2357
|
-
}
|
|
2358
|
-
initialize() {
|
|
2359
|
-
const serverNames = this.client.getServerNames?.();
|
|
2360
|
-
if (serverNames.length === 0) {
|
|
2361
|
-
logger.warn("No MCP servers defined in client configuration");
|
|
2362
|
-
}
|
|
2363
|
-
}
|
|
2364
|
-
async prefetchServerTools() {
|
|
2365
|
-
const servers = this.client.getServerNames();
|
|
2366
|
-
for (const serverName of servers) {
|
|
2367
|
-
try {
|
|
2368
|
-
let session = null;
|
|
2369
|
-
session = this.client.getSession(serverName);
|
|
2370
|
-
logger.debug(
|
|
2371
|
-
`Using existing session for server '${serverName}' to prefetch tools.`
|
|
2372
|
-
);
|
|
2373
|
-
if (!session) {
|
|
2374
|
-
session = await this.client.createSession(serverName).catch((createSessionError) => {
|
|
2375
|
-
logger.warn(
|
|
2376
|
-
`Could not create session for '${serverName}' during prefetch: ${createSessionError}`
|
|
2377
|
-
);
|
|
2378
|
-
return null;
|
|
2379
|
-
});
|
|
2380
|
-
logger.debug(
|
|
2381
|
-
`Temporarily created session for '${serverName}' to prefetch tools.`
|
|
2382
|
-
);
|
|
2383
|
-
}
|
|
2384
|
-
if (session) {
|
|
2385
|
-
const connector = session.connector;
|
|
2386
|
-
let tools = [];
|
|
2387
|
-
let resources = [];
|
|
2388
|
-
let prompts = [];
|
|
2389
|
-
try {
|
|
2390
|
-
tools = await this.adapter.createToolsFromConnectors([connector]);
|
|
2391
|
-
resources = await this.adapter.createResourcesFromConnectors([
|
|
2392
|
-
connector
|
|
2393
|
-
]);
|
|
2394
|
-
prompts = await this.adapter.createPromptsFromConnectors([
|
|
2395
|
-
connector
|
|
2396
|
-
]);
|
|
2397
|
-
} catch (toolFetchError) {
|
|
2398
|
-
logger.error(
|
|
2399
|
-
`Failed to create tools/resources/prompts from connector for server '${serverName}': ${toolFetchError}`
|
|
2400
|
-
);
|
|
2401
|
-
continue;
|
|
2402
|
-
}
|
|
2403
|
-
const allItems = [...tools, ...resources, ...prompts];
|
|
2404
|
-
const cachedTools = this.serverTools[serverName];
|
|
2405
|
-
const toolsChanged = !cachedTools || !isEqual(cachedTools, allItems);
|
|
2406
|
-
if (toolsChanged) {
|
|
2407
|
-
this.serverTools[serverName] = allItems;
|
|
2408
|
-
this.initializedServers[serverName] = true;
|
|
2409
|
-
logger.debug(
|
|
2410
|
-
`Prefetched ${allItems.length} items for server '${serverName}': ${tools.length} tools, ${resources.length} resources, ${prompts.length} prompts.`
|
|
2411
|
-
);
|
|
2412
|
-
} else {
|
|
2413
|
-
logger.debug(
|
|
2414
|
-
`Tools for server '${serverName}' unchanged, using cached version.`
|
|
2415
|
-
);
|
|
2416
|
-
}
|
|
2417
|
-
}
|
|
2418
|
-
} catch (outerError) {
|
|
2419
|
-
logger.error(
|
|
2420
|
-
`Error prefetching tools for server '${serverName}': ${outerError}`
|
|
2421
|
-
);
|
|
2422
|
-
}
|
|
2423
|
-
}
|
|
2424
|
-
}
|
|
2425
|
-
get tools() {
|
|
2426
|
-
if (logger.level === "debug") {
|
|
2427
|
-
this.logState("Providing tools to agent");
|
|
2428
|
-
}
|
|
2429
|
-
const managementTools = this.overrideManagementTools ?? [
|
|
2430
|
-
new AddMCPServerFromConfigTool(this),
|
|
2431
|
-
new ListMCPServersTool(this),
|
|
2432
|
-
new ConnectMCPServerTool(this),
|
|
2433
|
-
new AcquireActiveMCPServerTool(this),
|
|
2434
|
-
new ReleaseMCPServerConnectionTool(this)
|
|
2435
|
-
];
|
|
2436
|
-
if (this.activeServer && this.serverTools[this.activeServer]) {
|
|
2437
|
-
const activeTools = this.serverTools[this.activeServer];
|
|
2438
|
-
logger.debug(
|
|
2439
|
-
`Adding ${activeTools.length} tools from active server '${this.activeServer}'`
|
|
2440
|
-
);
|
|
2441
|
-
return [...managementTools, ...activeTools];
|
|
2442
|
-
}
|
|
2443
|
-
return managementTools;
|
|
2444
|
-
}
|
|
2445
|
-
};
|
|
2446
|
-
|
|
2447
|
-
// src/observability/manager.ts
|
|
2448
|
-
var ObservabilityManager = class {
|
|
2449
|
-
static {
|
|
2450
|
-
__name(this, "ObservabilityManager");
|
|
2451
|
-
}
|
|
2452
|
-
customCallbacks;
|
|
2453
|
-
availableHandlers = [];
|
|
2454
|
-
handlerNames = [];
|
|
2455
|
-
initialized = false;
|
|
2456
|
-
verbose;
|
|
2457
|
-
observe;
|
|
2458
|
-
agentId;
|
|
2459
|
-
metadata;
|
|
2460
|
-
metadataProvider;
|
|
2461
|
-
tagsProvider;
|
|
2462
|
-
constructor(config = {}) {
|
|
2463
|
-
this.customCallbacks = config.customCallbacks;
|
|
2464
|
-
this.verbose = config.verbose ?? false;
|
|
2465
|
-
this.observe = config.observe ?? true;
|
|
2466
|
-
this.agentId = config.agentId;
|
|
2467
|
-
this.metadata = config.metadata;
|
|
2468
|
-
this.metadataProvider = config.metadataProvider;
|
|
2469
|
-
this.tagsProvider = config.tagsProvider;
|
|
2470
|
-
}
|
|
2471
|
-
/**
|
|
2472
|
-
* Collect all available observability handlers from configured platforms.
|
|
2473
|
-
*/
|
|
2474
|
-
async collectAvailableHandlers() {
|
|
2475
|
-
if (this.initialized) {
|
|
2476
|
-
return;
|
|
2477
|
-
}
|
|
2478
|
-
try {
|
|
2479
|
-
const { langfuseHandler: langfuseHandler2, langfuseInitPromise: langfuseInitPromise2 } = await import("./langfuse-74RGPTAH.js");
|
|
2480
|
-
if (this.agentId || this.metadata || this.metadataProvider || this.tagsProvider) {
|
|
2481
|
-
const { initializeLangfuse } = await import("./langfuse-74RGPTAH.js");
|
|
2482
|
-
await initializeLangfuse(
|
|
2483
|
-
this.agentId,
|
|
2484
|
-
this.metadata,
|
|
2485
|
-
this.metadataProvider,
|
|
2486
|
-
this.tagsProvider
|
|
2487
|
-
);
|
|
2488
|
-
logger.debug(
|
|
2489
|
-
`ObservabilityManager: Reinitialized Langfuse with agent ID: ${this.agentId}, metadata: ${JSON.stringify(this.metadata)}`
|
|
2490
|
-
);
|
|
2491
|
-
} else {
|
|
2492
|
-
const initPromise = langfuseInitPromise2();
|
|
2493
|
-
if (initPromise) {
|
|
2494
|
-
await initPromise;
|
|
2495
|
-
}
|
|
2496
|
-
}
|
|
2497
|
-
const handler = langfuseHandler2();
|
|
2498
|
-
if (handler) {
|
|
2499
|
-
this.availableHandlers.push(handler);
|
|
2500
|
-
this.handlerNames.push("Langfuse");
|
|
2501
|
-
logger.debug("ObservabilityManager: Langfuse handler available");
|
|
2502
|
-
}
|
|
2503
|
-
} catch {
|
|
2504
|
-
logger.debug("ObservabilityManager: Langfuse module not available");
|
|
2505
|
-
}
|
|
2506
|
-
this.initialized = true;
|
|
2507
|
-
}
|
|
2508
|
-
/**
|
|
2509
|
-
* Get the list of callbacks to use.
|
|
2510
|
-
* @returns List of callbacks - either custom callbacks if provided, or all available observability handlers.
|
|
2511
|
-
*/
|
|
2512
|
-
async getCallbacks() {
|
|
2513
|
-
if (!this.observe) {
|
|
2514
|
-
logger.debug(
|
|
2515
|
-
"ObservabilityManager: Observability disabled via observe=false"
|
|
2516
|
-
);
|
|
2517
|
-
return [];
|
|
2518
|
-
}
|
|
2519
|
-
if (this.customCallbacks) {
|
|
2520
|
-
logger.debug(
|
|
2521
|
-
`ObservabilityManager: Using ${this.customCallbacks.length} custom callbacks`
|
|
2522
|
-
);
|
|
2523
|
-
return this.customCallbacks;
|
|
2524
|
-
}
|
|
2525
|
-
await this.collectAvailableHandlers();
|
|
2526
|
-
if (this.availableHandlers.length > 0) {
|
|
2527
|
-
logger.debug(
|
|
2528
|
-
`ObservabilityManager: Using ${this.availableHandlers.length} handlers`
|
|
2529
|
-
);
|
|
2530
|
-
} else {
|
|
2531
|
-
logger.debug("ObservabilityManager: No callbacks configured");
|
|
2532
|
-
}
|
|
2533
|
-
return this.availableHandlers;
|
|
2534
|
-
}
|
|
2535
|
-
/**
|
|
2536
|
-
* Get the names of available handlers.
|
|
2537
|
-
* @returns List of handler names (e.g., ["Langfuse", "Laminar"])
|
|
2538
|
-
*/
|
|
2539
|
-
async getHandlerNames() {
|
|
2540
|
-
if (!this.observe) {
|
|
2541
|
-
return [];
|
|
2542
|
-
}
|
|
2543
|
-
if (this.customCallbacks) {
|
|
2544
|
-
return this.customCallbacks.map((cb) => cb.constructor.name);
|
|
2545
|
-
}
|
|
2546
|
-
await this.collectAvailableHandlers();
|
|
2547
|
-
return this.handlerNames;
|
|
2548
|
-
}
|
|
2549
|
-
/**
|
|
2550
|
-
* Check if any callbacks are available.
|
|
2551
|
-
* @returns True if callbacks are available, False otherwise.
|
|
2552
|
-
*/
|
|
2553
|
-
async hasCallbacks() {
|
|
2554
|
-
if (!this.observe) {
|
|
2555
|
-
return false;
|
|
2556
|
-
}
|
|
2557
|
-
const callbacks = await this.getCallbacks();
|
|
2558
|
-
return callbacks.length > 0;
|
|
2559
|
-
}
|
|
2560
|
-
/**
|
|
2561
|
-
* Get the current observability status including metadata and tags.
|
|
2562
|
-
* @returns Object containing enabled status, callback count, handler names, metadata, and tags.
|
|
2563
|
-
*/
|
|
2564
|
-
async getStatus() {
|
|
2565
|
-
const callbacks = await this.getCallbacks();
|
|
2566
|
-
const handlerNames = await this.getHandlerNames();
|
|
2567
|
-
const currentMetadata = this.metadataProvider ? this.metadataProvider() : this.metadata || {};
|
|
2568
|
-
const currentTags = this.tagsProvider ? this.tagsProvider() : [];
|
|
2569
|
-
return {
|
|
2570
|
-
enabled: this.observe && callbacks.length > 0,
|
|
2571
|
-
callbackCount: callbacks.length,
|
|
2572
|
-
handlerNames,
|
|
2573
|
-
metadata: currentMetadata,
|
|
2574
|
-
tags: currentTags
|
|
2575
|
-
};
|
|
2576
|
-
}
|
|
2577
|
-
/**
|
|
2578
|
-
* Add a callback to the custom callbacks list.
|
|
2579
|
-
* @param callback The callback to add.
|
|
2580
|
-
*/
|
|
2581
|
-
addCallback(callback) {
|
|
2582
|
-
if (!this.customCallbacks) {
|
|
2583
|
-
this.customCallbacks = [];
|
|
2584
|
-
}
|
|
2585
|
-
this.customCallbacks.push(callback);
|
|
2586
|
-
logger.debug(
|
|
2587
|
-
`ObservabilityManager: Added custom callback: ${callback.constructor.name}`
|
|
2588
|
-
);
|
|
2589
|
-
}
|
|
2590
|
-
/**
|
|
2591
|
-
* Clear all custom callbacks.
|
|
2592
|
-
*/
|
|
2593
|
-
clearCallbacks() {
|
|
2594
|
-
this.customCallbacks = [];
|
|
2595
|
-
logger.debug("ObservabilityManager: Cleared all custom callbacks");
|
|
2596
|
-
}
|
|
2597
|
-
/**
|
|
2598
|
-
* Flush all pending traces to observability platforms.
|
|
2599
|
-
* Important for serverless environments and short-lived processes.
|
|
2600
|
-
*/
|
|
2601
|
-
async flush() {
|
|
2602
|
-
const callbacks = await this.getCallbacks();
|
|
2603
|
-
for (const callback of callbacks) {
|
|
2604
|
-
if ("flushAsync" in callback && typeof callback.flushAsync === "function") {
|
|
2605
|
-
await callback.flushAsync();
|
|
2606
|
-
}
|
|
2607
|
-
}
|
|
2608
|
-
logger.debug("ObservabilityManager: All traces flushed");
|
|
2609
|
-
}
|
|
2610
|
-
/**
|
|
2611
|
-
* Shutdown all handlers gracefully (for serverless environments).
|
|
2612
|
-
*/
|
|
2613
|
-
async shutdown() {
|
|
2614
|
-
await this.flush();
|
|
2615
|
-
const callbacks = await this.getCallbacks();
|
|
2616
|
-
for (const callback of callbacks) {
|
|
2617
|
-
if ("shutdownAsync" in callback && typeof callback.shutdownAsync === "function") {
|
|
2618
|
-
await callback.shutdownAsync();
|
|
2619
|
-
} else if ("shutdown" in callback && typeof callback.shutdown === "function") {
|
|
2620
|
-
await callback.shutdown();
|
|
2621
|
-
}
|
|
2622
|
-
}
|
|
2623
|
-
logger.debug("ObservabilityManager: All handlers shutdown");
|
|
2624
|
-
}
|
|
2625
|
-
/**
|
|
2626
|
-
* String representation of the ObservabilityManager.
|
|
2627
|
-
*/
|
|
2628
|
-
toString() {
|
|
2629
|
-
const names = this.handlerNames;
|
|
2630
|
-
if (names.length > 0) {
|
|
2631
|
-
return `ObservabilityManager(handlers=${names.join(", ")})`;
|
|
2632
|
-
}
|
|
2633
|
-
return "ObservabilityManager(no handlers)";
|
|
2634
|
-
}
|
|
2635
|
-
};
|
|
2636
|
-
|
|
2637
|
-
// src/agents/remote.ts
|
|
2638
|
-
import { toJSONSchema } from "zod";
|
|
2639
|
-
var API_CHATS_ENDPOINT = "/api/v1/chats";
|
|
2640
|
-
var API_CHAT_EXECUTE_ENDPOINT = "/api/v1/chats/{chat_id}/execute";
|
|
2641
|
-
function normalizeRemoteRunOptions(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema) {
|
|
2642
|
-
if (typeof queryOrOptions === "object" && queryOrOptions !== null) {
|
|
2643
|
-
const options = queryOrOptions;
|
|
2644
|
-
return {
|
|
2645
|
-
query: options.prompt,
|
|
2646
|
-
maxSteps: options.maxSteps,
|
|
2647
|
-
manageConnector: options.manageConnector,
|
|
2648
|
-
externalHistory: options.externalHistory,
|
|
2649
|
-
outputSchema: options.schema
|
|
2650
|
-
};
|
|
2651
|
-
}
|
|
2652
|
-
return {
|
|
2653
|
-
query: queryOrOptions,
|
|
2654
|
-
maxSteps,
|
|
2655
|
-
manageConnector,
|
|
2656
|
-
externalHistory,
|
|
2657
|
-
outputSchema
|
|
2658
|
-
};
|
|
2659
|
-
}
|
|
2660
|
-
__name(normalizeRemoteRunOptions, "normalizeRemoteRunOptions");
|
|
2661
|
-
var RemoteAgent = class {
|
|
2662
|
-
static {
|
|
2663
|
-
__name(this, "RemoteAgent");
|
|
2664
|
-
}
|
|
2665
|
-
agentId;
|
|
2666
|
-
apiKey;
|
|
2667
|
-
baseUrl;
|
|
2668
|
-
chatId = null;
|
|
2669
|
-
constructor(options) {
|
|
2670
|
-
this.agentId = options.agentId;
|
|
2671
|
-
this.baseUrl = options.baseUrl ?? "https://cloud.mcp-use.com";
|
|
2672
|
-
const apiKey = options.apiKey ?? process.env.MCP_USE_API_KEY;
|
|
2673
|
-
if (!apiKey) {
|
|
2674
|
-
throw new Error(
|
|
2675
|
-
"API key is required for remote execution. Please provide it as a parameter or set the MCP_USE_API_KEY environment variable. You can get an API key from https://cloud.mcp-use.com"
|
|
2676
|
-
);
|
|
2677
|
-
}
|
|
2678
|
-
this.apiKey = apiKey;
|
|
2679
|
-
}
|
|
2680
|
-
pydanticToJsonSchema(schema) {
|
|
2681
|
-
return toJSONSchema(schema);
|
|
2682
|
-
}
|
|
2683
|
-
parseStructuredResponse(responseData, outputSchema) {
|
|
2684
|
-
let resultData;
|
|
2685
|
-
if (typeof responseData === "object" && responseData !== null) {
|
|
2686
|
-
if ("result" in responseData) {
|
|
2687
|
-
const outerResult = responseData.result;
|
|
2688
|
-
if (typeof outerResult === "object" && outerResult !== null && "result" in outerResult) {
|
|
2689
|
-
resultData = outerResult.result;
|
|
2690
|
-
} else {
|
|
2691
|
-
resultData = outerResult;
|
|
2692
|
-
}
|
|
2693
|
-
} else {
|
|
2694
|
-
resultData = responseData;
|
|
2695
|
-
}
|
|
2696
|
-
} else if (typeof responseData === "string") {
|
|
2697
|
-
try {
|
|
2698
|
-
resultData = JSON.parse(responseData);
|
|
2699
|
-
} catch {
|
|
2700
|
-
resultData = { content: responseData };
|
|
2701
|
-
}
|
|
2702
|
-
} else {
|
|
2703
|
-
resultData = responseData;
|
|
2704
|
-
}
|
|
2705
|
-
try {
|
|
2706
|
-
return outputSchema.parse(resultData);
|
|
2707
|
-
} catch (e) {
|
|
2708
|
-
logger.warn(`Failed to parse structured output: ${e}`);
|
|
2709
|
-
const schemaShape = outputSchema._def?.shape();
|
|
2710
|
-
if (schemaShape && "content" in schemaShape) {
|
|
2711
|
-
return outputSchema.parse({ content: String(resultData) });
|
|
2712
|
-
}
|
|
2713
|
-
throw e;
|
|
2714
|
-
}
|
|
2715
|
-
}
|
|
2716
|
-
async createChatSession() {
|
|
2717
|
-
const chatPayload = {
|
|
2718
|
-
title: `Remote Agent Session - ${this.agentId}`,
|
|
2719
|
-
agent_id: this.agentId,
|
|
2720
|
-
type: "agent_execution"
|
|
2721
|
-
};
|
|
2722
|
-
const headers = {
|
|
2723
|
-
"Content-Type": "application/json",
|
|
2724
|
-
"x-api-key": this.apiKey
|
|
2725
|
-
};
|
|
2726
|
-
const chatUrl = `${this.baseUrl}${API_CHATS_ENDPOINT}`;
|
|
2727
|
-
logger.info(`\u{1F4DD} Creating chat session for agent ${this.agentId}`);
|
|
2728
|
-
try {
|
|
2729
|
-
const response = await fetch(chatUrl, {
|
|
2730
|
-
method: "POST",
|
|
2731
|
-
headers,
|
|
2732
|
-
body: JSON.stringify(chatPayload)
|
|
2733
|
-
});
|
|
2734
|
-
if (!response.ok) {
|
|
2735
|
-
const responseText = await response.text();
|
|
2736
|
-
const statusCode = response.status;
|
|
2737
|
-
if (statusCode === 404) {
|
|
2738
|
-
throw new Error(
|
|
2739
|
-
`Agent not found: Agent '${this.agentId}' does not exist or you don't have access to it. Please verify the agent ID and ensure it exists in your account.`
|
|
2740
|
-
);
|
|
2741
|
-
}
|
|
2742
|
-
throw new Error(
|
|
2743
|
-
`Failed to create chat session: ${statusCode} - ${responseText}`
|
|
2744
|
-
);
|
|
2745
|
-
}
|
|
2746
|
-
const chatData = await response.json();
|
|
2747
|
-
const chatId = chatData.id;
|
|
2748
|
-
logger.info(`\u2705 Chat session created: ${chatId}`);
|
|
2749
|
-
return chatId;
|
|
2750
|
-
} catch (e) {
|
|
2751
|
-
if (e instanceof Error) {
|
|
2752
|
-
throw new TypeError(`Failed to create chat session: ${e.message}`);
|
|
2753
|
-
}
|
|
2754
|
-
throw new Error(`Failed to create chat session: ${String(e)}`);
|
|
2755
|
-
}
|
|
2756
|
-
}
|
|
2757
|
-
async run(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema) {
|
|
2758
|
-
const {
|
|
2759
|
-
query,
|
|
2760
|
-
maxSteps: steps,
|
|
2761
|
-
externalHistory: history,
|
|
2762
|
-
outputSchema: schema
|
|
2763
|
-
} = normalizeRemoteRunOptions(
|
|
2764
|
-
queryOrOptions,
|
|
2765
|
-
maxSteps,
|
|
2766
|
-
manageConnector,
|
|
2767
|
-
externalHistory,
|
|
2768
|
-
outputSchema
|
|
2769
|
-
);
|
|
2770
|
-
if (history !== void 0) {
|
|
2771
|
-
logger.warn("External history is not yet supported for remote execution");
|
|
2772
|
-
}
|
|
2773
|
-
try {
|
|
2774
|
-
logger.info(`\u{1F310} Executing query on remote agent ${this.agentId}`);
|
|
2775
|
-
if (this.chatId === null) {
|
|
2776
|
-
this.chatId = await this.createChatSession();
|
|
2777
|
-
}
|
|
2778
|
-
const chatId = this.chatId;
|
|
2779
|
-
const executionPayload = {
|
|
2780
|
-
query,
|
|
2781
|
-
max_steps: steps ?? 10
|
|
2782
|
-
};
|
|
2783
|
-
if (schema) {
|
|
2784
|
-
executionPayload.output_schema = this.pydanticToJsonSchema(schema);
|
|
2785
|
-
logger.info(`\u{1F527} Using structured output with schema`);
|
|
2786
|
-
}
|
|
2787
|
-
const headers = {
|
|
2788
|
-
"Content-Type": "application/json",
|
|
2789
|
-
"x-api-key": this.apiKey
|
|
2790
|
-
};
|
|
2791
|
-
const executionUrl = `${this.baseUrl}${API_CHAT_EXECUTE_ENDPOINT.replace("{chat_id}", chatId)}`;
|
|
2792
|
-
logger.info(`\u{1F680} Executing agent in chat ${chatId}`);
|
|
2793
|
-
const response = await fetch(executionUrl, {
|
|
2794
|
-
method: "POST",
|
|
2795
|
-
headers,
|
|
2796
|
-
body: JSON.stringify(executionPayload),
|
|
2797
|
-
signal: AbortSignal.timeout(3e5)
|
|
2798
|
-
// 5 minute timeout
|
|
2799
|
-
});
|
|
2800
|
-
if (!response.ok) {
|
|
2801
|
-
const responseText = await response.text();
|
|
2802
|
-
const statusCode = response.status;
|
|
2803
|
-
if (statusCode === 401) {
|
|
2804
|
-
logger.error(`\u274C Authentication failed: ${responseText}`);
|
|
2805
|
-
throw new Error(
|
|
2806
|
-
"Authentication failed: Invalid or missing API key. Please check your API key and ensure the MCP_USE_API_KEY environment variable is set correctly."
|
|
2807
|
-
);
|
|
2808
|
-
} else if (statusCode === 403) {
|
|
2809
|
-
logger.error(`\u274C Access forbidden: ${responseText}`);
|
|
2810
|
-
throw new Error(
|
|
2811
|
-
`Access denied: You don't have permission to execute agent '${this.agentId}'. Check if the agent exists and you have the necessary permissions.`
|
|
2812
|
-
);
|
|
2813
|
-
} else if (statusCode === 404) {
|
|
2814
|
-
logger.error(`\u274C Agent not found: ${responseText}`);
|
|
2815
|
-
throw new Error(
|
|
2816
|
-
`Agent not found: Agent '${this.agentId}' does not exist or you don't have access to it. Please verify the agent ID and ensure it exists in your account.`
|
|
2817
|
-
);
|
|
2818
|
-
} else if (statusCode === 422) {
|
|
2819
|
-
logger.error(`\u274C Validation error: ${responseText}`);
|
|
2820
|
-
throw new Error(
|
|
2821
|
-
`Request validation failed: ${responseText}. Please check your query parameters and output schema format.`
|
|
2822
|
-
);
|
|
2823
|
-
} else if (statusCode === 500) {
|
|
2824
|
-
logger.error(`\u274C Server error: ${responseText}`);
|
|
2825
|
-
throw new Error(
|
|
2826
|
-
"Internal server error occurred during agent execution. Please try again later or contact support if the issue persists."
|
|
2827
|
-
);
|
|
2828
|
-
} else {
|
|
2829
|
-
logger.error(
|
|
2830
|
-
`\u274C Remote execution failed with status ${statusCode}: ${responseText}`
|
|
2831
|
-
);
|
|
2832
|
-
throw new Error(
|
|
2833
|
-
`Remote agent execution failed: ${statusCode} - ${responseText}`
|
|
2834
|
-
);
|
|
2835
|
-
}
|
|
2836
|
-
}
|
|
2837
|
-
const result = await response.json();
|
|
2838
|
-
logger.info(`\u{1F527} Response: ${JSON.stringify(result)}`);
|
|
2839
|
-
logger.info("\u2705 Remote execution completed successfully");
|
|
2840
|
-
if (typeof result === "object" && result !== null) {
|
|
2841
|
-
if (result.status === "error" || result.error !== null) {
|
|
2842
|
-
const errorMsg = result.error ?? String(result);
|
|
2843
|
-
logger.error(`\u274C Remote agent execution failed: ${errorMsg}`);
|
|
2844
|
-
throw new Error(`Remote agent execution failed: ${errorMsg}`);
|
|
2845
|
-
}
|
|
2846
|
-
if (String(result).includes("failed to initialize")) {
|
|
2847
|
-
logger.error(`\u274C Agent initialization failed: ${result}`);
|
|
2848
|
-
throw new Error(
|
|
2849
|
-
`Agent initialization failed on remote server. This usually indicates:
|
|
2850
|
-
\u2022 Invalid agent configuration (LLM model, system prompt)
|
|
2851
|
-
\u2022 Missing or invalid MCP server configurations
|
|
2852
|
-
\u2022 Network connectivity issues with MCP servers
|
|
2853
|
-
\u2022 Missing environment variables or credentials
|
|
2854
|
-
Raw error: ${result}`
|
|
2855
|
-
);
|
|
2856
|
-
}
|
|
2857
|
-
}
|
|
2858
|
-
if (schema) {
|
|
2859
|
-
return this.parseStructuredResponse(result, schema);
|
|
2860
|
-
}
|
|
2861
|
-
if (typeof result === "object" && result !== null && "result" in result) {
|
|
2862
|
-
return result.result;
|
|
2863
|
-
} else if (typeof result === "string") {
|
|
2864
|
-
return result;
|
|
2865
|
-
} else {
|
|
2866
|
-
return String(result);
|
|
2867
|
-
}
|
|
2868
|
-
} catch (e) {
|
|
2869
|
-
if (e instanceof Error) {
|
|
2870
|
-
if (e.name === "AbortError") {
|
|
2871
|
-
logger.error(`\u274C Remote execution timed out: ${e}`);
|
|
2872
|
-
throw new Error(
|
|
2873
|
-
"Remote agent execution timed out. The server may be overloaded or the query is taking too long to process. Try again or use a simpler query."
|
|
2874
|
-
);
|
|
2875
|
-
}
|
|
2876
|
-
logger.error(`\u274C Remote execution error: ${e}`);
|
|
2877
|
-
throw new Error(`Remote agent execution failed: ${e.message}`);
|
|
2878
|
-
}
|
|
2879
|
-
logger.error(`\u274C Remote execution error: ${e}`);
|
|
2880
|
-
throw new Error(`Remote agent execution failed: ${String(e)}`);
|
|
2881
|
-
}
|
|
2882
|
-
}
|
|
2883
|
-
// eslint-disable-next-line require-yield
|
|
2884
|
-
async *stream(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema) {
|
|
2885
|
-
const result = await this.run(
|
|
2886
|
-
queryOrOptions,
|
|
2887
|
-
maxSteps,
|
|
2888
|
-
manageConnector,
|
|
2889
|
-
externalHistory,
|
|
2890
|
-
outputSchema
|
|
2891
|
-
);
|
|
2892
|
-
return result;
|
|
2893
|
-
}
|
|
2894
|
-
async close() {
|
|
2895
|
-
logger.info("\u{1F50C} Remote agent client closed");
|
|
2896
|
-
}
|
|
2897
|
-
};
|
|
2898
|
-
|
|
2899
|
-
// src/agents/utils/llm_provider.ts
|
|
2900
|
-
var PROVIDER_CONFIG = {
|
|
2901
|
-
openai: {
|
|
2902
|
-
package: "@langchain/openai",
|
|
2903
|
-
className: "ChatOpenAI",
|
|
2904
|
-
envVars: ["OPENAI_API_KEY"],
|
|
2905
|
-
defaultModel: "gpt-4o"
|
|
2906
|
-
},
|
|
2907
|
-
anthropic: {
|
|
2908
|
-
package: "@langchain/anthropic",
|
|
2909
|
-
className: "ChatAnthropic",
|
|
2910
|
-
envVars: ["ANTHROPIC_API_KEY"],
|
|
2911
|
-
defaultModel: "claude-3-5-sonnet-20241022"
|
|
2912
|
-
},
|
|
2913
|
-
google: {
|
|
2914
|
-
package: "@langchain/google-genai",
|
|
2915
|
-
className: "ChatGoogleGenerativeAI",
|
|
2916
|
-
envVars: ["GOOGLE_API_KEY", "GOOGLE_GENERATIVE_AI_API_KEY"],
|
|
2917
|
-
defaultModel: "gemini-pro"
|
|
2918
|
-
},
|
|
2919
|
-
groq: {
|
|
2920
|
-
package: "@langchain/groq",
|
|
2921
|
-
className: "ChatGroq",
|
|
2922
|
-
envVars: ["GROQ_API_KEY"],
|
|
2923
|
-
defaultModel: "llama-3.1-70b-versatile"
|
|
2924
|
-
}
|
|
2925
|
-
};
|
|
2926
|
-
function parseLLMString(llmString) {
|
|
2927
|
-
const parts = llmString.split("/");
|
|
2928
|
-
if (parts.length !== 2) {
|
|
2929
|
-
throw new Error(
|
|
2930
|
-
`Invalid LLM string format. Expected 'provider/model', got '${llmString}'. Examples: 'openai/gpt-4', 'anthropic/claude-3-5-sonnet-20241022', 'google/gemini-pro', 'groq/llama-3.1-70b-versatile'`
|
|
2931
|
-
);
|
|
2932
|
-
}
|
|
2933
|
-
const [provider, model] = parts;
|
|
2934
|
-
if (!provider || !model) {
|
|
2935
|
-
throw new Error(
|
|
2936
|
-
`Invalid LLM string format. Both provider and model must be non-empty. Got '${llmString}'`
|
|
2937
|
-
);
|
|
2938
|
-
}
|
|
2939
|
-
const normalizedProvider = provider.toLowerCase();
|
|
2940
|
-
if (!(normalizedProvider in PROVIDER_CONFIG)) {
|
|
2941
|
-
const supportedProviders = Object.keys(PROVIDER_CONFIG).join(", ");
|
|
2942
|
-
throw new Error(
|
|
2943
|
-
`Unsupported LLM provider '${provider}'. Supported providers: ${supportedProviders}`
|
|
2944
|
-
);
|
|
2945
|
-
}
|
|
2946
|
-
return { provider: normalizedProvider, model };
|
|
2947
|
-
}
|
|
2948
|
-
__name(parseLLMString, "parseLLMString");
|
|
2949
|
-
function getAPIKey(provider, config) {
|
|
2950
|
-
if (config?.apiKey) {
|
|
2951
|
-
return config.apiKey;
|
|
2952
|
-
}
|
|
2953
|
-
const providerConfig = PROVIDER_CONFIG[provider];
|
|
2954
|
-
for (const envVar of providerConfig.envVars) {
|
|
2955
|
-
const apiKey = process.env[envVar];
|
|
2956
|
-
if (apiKey) {
|
|
2957
|
-
logger.debug(
|
|
2958
|
-
`Using API key from environment variable ${envVar} for provider ${provider}`
|
|
2959
|
-
);
|
|
2960
|
-
return apiKey;
|
|
2961
|
-
}
|
|
2962
|
-
}
|
|
2963
|
-
const envVarsStr = providerConfig.envVars.join(" or ");
|
|
2964
|
-
throw new Error(
|
|
2965
|
-
`API key not found for provider '${provider}'. Set ${envVarsStr} environment variable or pass apiKey in llmConfig. Example: new MCPAgent({ llm: '${provider}/model', llmConfig: { apiKey: 'your-key' } })`
|
|
2966
|
-
);
|
|
2967
|
-
}
|
|
2968
|
-
__name(getAPIKey, "getAPIKey");
|
|
2969
|
-
async function createLLMFromString(llmString, config) {
|
|
2970
|
-
logger.info(`Creating LLM from string: ${llmString}`);
|
|
2971
|
-
const { provider, model } = parseLLMString(llmString);
|
|
2972
|
-
const providerConfig = PROVIDER_CONFIG[provider];
|
|
2973
|
-
const apiKey = getAPIKey(provider, config);
|
|
2974
|
-
let providerModule;
|
|
2975
|
-
try {
|
|
2976
|
-
logger.debug(`Importing package ${providerConfig.package}...`);
|
|
2977
|
-
providerModule = await import(providerConfig.package);
|
|
2978
|
-
} catch (error) {
|
|
2979
|
-
if (error?.code === "MODULE_NOT_FOUND" || error?.message?.includes("Cannot find module") || error?.message?.includes("Cannot find package")) {
|
|
2980
|
-
throw new Error(
|
|
2981
|
-
`Package '${providerConfig.package}' is not installed. Install it with: npm install ${providerConfig.package} or yarn add ${providerConfig.package}`
|
|
2982
|
-
);
|
|
2983
|
-
}
|
|
2984
|
-
throw new Error(
|
|
2985
|
-
`Failed to import ${providerConfig.package}: ${error?.message || error}`
|
|
2986
|
-
);
|
|
2987
|
-
}
|
|
2988
|
-
const LLMClass = providerModule[providerConfig.className];
|
|
2989
|
-
if (!LLMClass) {
|
|
2990
|
-
throw new Error(
|
|
2991
|
-
`Could not find ${providerConfig.className} in package ${providerConfig.package}. This might be a version compatibility issue.`
|
|
2992
|
-
);
|
|
2993
|
-
}
|
|
2994
|
-
const llmConfig = {
|
|
2995
|
-
model,
|
|
2996
|
-
apiKey,
|
|
2997
|
-
...config
|
|
2998
|
-
};
|
|
2999
|
-
if (config?.apiKey) {
|
|
3000
|
-
delete llmConfig.apiKey;
|
|
3001
|
-
llmConfig.apiKey = apiKey;
|
|
3002
|
-
}
|
|
3003
|
-
if (provider === "anthropic") {
|
|
3004
|
-
llmConfig.model = model;
|
|
3005
|
-
} else if (provider === "google") {
|
|
3006
|
-
llmConfig.model = model;
|
|
3007
|
-
} else if (provider === "openai") {
|
|
3008
|
-
llmConfig.model = model;
|
|
3009
|
-
} else if (provider === "groq") {
|
|
3010
|
-
llmConfig.model = model;
|
|
3011
|
-
}
|
|
3012
|
-
try {
|
|
3013
|
-
const llmInstance = new LLMClass(llmConfig);
|
|
3014
|
-
logger.info(`Successfully created ${provider} LLM with model ${model}`);
|
|
3015
|
-
return llmInstance;
|
|
3016
|
-
} catch (error) {
|
|
3017
|
-
throw new Error(
|
|
3018
|
-
`Failed to instantiate ${providerConfig.className} with model '${model}': ${error?.message || error}`
|
|
3019
|
-
);
|
|
3020
|
-
}
|
|
3021
|
-
}
|
|
3022
|
-
__name(createLLMFromString, "createLLMFromString");
|
|
3023
|
-
function isValidLLMString(llmString) {
|
|
3024
|
-
try {
|
|
3025
|
-
parseLLMString(llmString);
|
|
3026
|
-
return true;
|
|
3027
|
-
} catch {
|
|
3028
|
-
return false;
|
|
3029
|
-
}
|
|
3030
|
-
}
|
|
3031
|
-
__name(isValidLLMString, "isValidLLMString");
|
|
3032
|
-
function getSupportedProviders() {
|
|
3033
|
-
return Object.keys(PROVIDER_CONFIG);
|
|
3034
|
-
}
|
|
3035
|
-
__name(getSupportedProviders, "getSupportedProviders");
|
|
3036
|
-
|
|
3037
|
-
// src/agents/mcp_agent.ts
|
|
3038
|
-
import {
|
|
3039
|
-
AIMessage,
|
|
3040
|
-
createAgent,
|
|
3041
|
-
HumanMessage,
|
|
3042
|
-
modelCallLimitMiddleware,
|
|
3043
|
-
SystemMessage as SystemMessage2,
|
|
3044
|
-
ToolMessage
|
|
3045
|
-
} from "langchain";
|
|
3046
|
-
import { toJSONSchema as toJSONSchema2 } from "zod";
|
|
3047
|
-
|
|
3048
|
-
// src/agents/prompts/system_prompt_builder.ts
|
|
3049
|
-
import { SystemMessage } from "langchain";
|
|
3050
|
-
function generateToolDescriptions(tools, disallowedTools) {
|
|
3051
|
-
const disallowedSet = new Set(disallowedTools ?? []);
|
|
3052
|
-
const descriptions = [];
|
|
3053
|
-
for (const tool of tools) {
|
|
3054
|
-
if (disallowedSet.has(tool.name)) continue;
|
|
3055
|
-
const escaped = tool.description.replace(/\{/g, "{{").replace(/\}/g, "}}");
|
|
3056
|
-
descriptions.push(`- ${tool.name}: ${escaped}`);
|
|
3057
|
-
}
|
|
3058
|
-
return descriptions;
|
|
3059
|
-
}
|
|
3060
|
-
__name(generateToolDescriptions, "generateToolDescriptions");
|
|
3061
|
-
function buildSystemPromptContent(template, toolDescriptionLines, additionalInstructions) {
|
|
3062
|
-
const block = toolDescriptionLines.join("\n");
|
|
3063
|
-
let content;
|
|
3064
|
-
if (template.includes("{tool_descriptions}")) {
|
|
3065
|
-
content = template.replace("{tool_descriptions}", block);
|
|
3066
|
-
} else {
|
|
3067
|
-
console.warn(
|
|
3068
|
-
"`{tool_descriptions}` placeholder not found; appending at end."
|
|
3069
|
-
);
|
|
3070
|
-
content = `${template}
|
|
3071
|
-
|
|
3072
|
-
Available tools:
|
|
3073
|
-
${block}`;
|
|
3074
|
-
}
|
|
3075
|
-
if (additionalInstructions) {
|
|
3076
|
-
content += `
|
|
3077
|
-
|
|
3078
|
-
${additionalInstructions}`;
|
|
3079
|
-
}
|
|
3080
|
-
return content;
|
|
3081
|
-
}
|
|
3082
|
-
__name(buildSystemPromptContent, "buildSystemPromptContent");
|
|
3083
|
-
function createSystemMessage(tools, systemPromptTemplate, serverManagerTemplate, useServerManager, disallowedTools, userProvidedPrompt, additionalInstructions) {
|
|
3084
|
-
if (userProvidedPrompt) {
|
|
3085
|
-
return new SystemMessage({ content: userProvidedPrompt });
|
|
3086
|
-
}
|
|
3087
|
-
const template = useServerManager ? serverManagerTemplate : systemPromptTemplate;
|
|
3088
|
-
const toolLines = generateToolDescriptions(tools, disallowedTools);
|
|
3089
|
-
const finalContent = buildSystemPromptContent(
|
|
3090
|
-
template,
|
|
3091
|
-
toolLines,
|
|
3092
|
-
additionalInstructions
|
|
3093
|
-
);
|
|
3094
|
-
return new SystemMessage({ content: finalContent });
|
|
3095
|
-
}
|
|
3096
|
-
__name(createSystemMessage, "createSystemMessage");
|
|
3097
|
-
|
|
3098
|
-
// src/agents/prompts/templates.ts
|
|
3099
|
-
var DEFAULT_SYSTEM_PROMPT_TEMPLATE = `You are a helpful AI assistant.
|
|
3100
|
-
You have access to the following tools:
|
|
3101
|
-
|
|
3102
|
-
{tool_descriptions}
|
|
3103
|
-
|
|
3104
|
-
Use the following format:
|
|
3105
|
-
|
|
3106
|
-
Question: the input question you must answer
|
|
3107
|
-
Thought: you should always think about what to do
|
|
3108
|
-
Action: the action to take, should be one of the available tools
|
|
3109
|
-
Action Input: the input to the action
|
|
3110
|
-
Observation: the result of the action
|
|
3111
|
-
... (this Thought/Action/Action Input/Observation can repeat N times)
|
|
3112
|
-
Thought: I now know the final answer
|
|
3113
|
-
Final Answer: the final answer to the original input question`;
|
|
3114
|
-
var SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE = `You are a helpful assistant designed to interact with MCP
|
|
3115
|
-
(Model Context Protocol) servers. You can manage connections to different servers and use the tools
|
|
3116
|
-
provided by the currently active server.
|
|
3117
|
-
|
|
3118
|
-
Important: The available tools change depending on which server is active.
|
|
3119
|
-
If a request requires tools not listed below (e.g., file operations, web browsing,
|
|
3120
|
-
image manipulation), you MUST first connect to the appropriate server using
|
|
3121
|
-
'connect_to_mcp_server'.
|
|
3122
|
-
Use 'list_mcp_servers' to find the relevant server if you are unsure.
|
|
3123
|
-
Only after successfully connecting and seeing the new tools listed in
|
|
3124
|
-
the response should you attempt to use those server-specific tools.
|
|
3125
|
-
Before attempting a task that requires specific tools, you should
|
|
3126
|
-
ensure you are connected to the correct server and aware of its
|
|
3127
|
-
available tools. If unsure, use 'list_mcp_servers' to see options
|
|
3128
|
-
or 'get_active_mcp_server' to check the current connection.
|
|
3129
|
-
|
|
3130
|
-
When you connect to a server using 'connect_to_mcp_server',
|
|
3131
|
-
you will be informed about the new tools that become available.
|
|
3132
|
-
You can then use these server-specific tools in subsequent steps.
|
|
3133
|
-
|
|
3134
|
-
Here are the tools *currently* available to you (this list includes server management tools and will
|
|
3135
|
-
change when you connect to a server):
|
|
3136
|
-
{tool_descriptions}
|
|
3137
|
-
`;
|
|
3138
|
-
|
|
3139
|
-
// src/agents/mcp_agent.ts
|
|
3140
|
-
function normalizeRunOptions(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema) {
|
|
3141
|
-
if (typeof queryOrOptions === "object" && queryOrOptions !== null) {
|
|
3142
|
-
const options = queryOrOptions;
|
|
3143
|
-
return {
|
|
3144
|
-
query: options.prompt,
|
|
3145
|
-
maxSteps: options.maxSteps,
|
|
3146
|
-
manageConnector: options.manageConnector,
|
|
3147
|
-
externalHistory: options.externalHistory,
|
|
3148
|
-
outputSchema: options.schema
|
|
3149
|
-
};
|
|
3150
|
-
}
|
|
3151
|
-
return {
|
|
3152
|
-
query: queryOrOptions,
|
|
3153
|
-
maxSteps,
|
|
3154
|
-
manageConnector,
|
|
3155
|
-
externalHistory,
|
|
3156
|
-
outputSchema
|
|
3157
|
-
};
|
|
3158
|
-
}
|
|
3159
|
-
__name(normalizeRunOptions, "normalizeRunOptions");
|
|
3160
|
-
var MCPAgent = class {
|
|
3161
|
-
static {
|
|
3162
|
-
__name(this, "MCPAgent");
|
|
3163
|
-
}
|
|
3164
|
-
/**
|
|
3165
|
-
* Get the mcp-use package version.
|
|
3166
|
-
* Works in all environments (Node.js, browser, Cloudflare Workers, Deno, etc.)
|
|
3167
|
-
*/
|
|
3168
|
-
static getPackageVersion() {
|
|
3169
|
-
return getPackageVersion();
|
|
3170
|
-
}
|
|
3171
|
-
llm;
|
|
3172
|
-
client;
|
|
3173
|
-
connectors;
|
|
3174
|
-
maxSteps;
|
|
3175
|
-
autoInitialize;
|
|
3176
|
-
memoryEnabled;
|
|
3177
|
-
disallowedTools;
|
|
3178
|
-
additionalTools;
|
|
3179
|
-
toolsUsedNames = [];
|
|
3180
|
-
useServerManager;
|
|
3181
|
-
verbose;
|
|
3182
|
-
observe;
|
|
3183
|
-
systemPrompt;
|
|
3184
|
-
systemPromptTemplateOverride;
|
|
3185
|
-
additionalInstructions;
|
|
3186
|
-
_initialized = false;
|
|
3187
|
-
conversationHistory = [];
|
|
3188
|
-
_agentExecutor = null;
|
|
3189
|
-
sessions = {};
|
|
3190
|
-
systemMessage = null;
|
|
3191
|
-
_tools = [];
|
|
3192
|
-
adapter;
|
|
3193
|
-
serverManager = null;
|
|
3194
|
-
telemetry;
|
|
3195
|
-
modelProvider;
|
|
3196
|
-
modelName;
|
|
3197
|
-
// Observability support
|
|
3198
|
-
observabilityManager;
|
|
3199
|
-
callbacks = [];
|
|
3200
|
-
metadata = {};
|
|
3201
|
-
tags = [];
|
|
3202
|
-
// Remote agent support
|
|
3203
|
-
isRemote = false;
|
|
3204
|
-
remoteAgent = null;
|
|
3205
|
-
// Simplified mode support
|
|
3206
|
-
isSimplifiedMode = false;
|
|
3207
|
-
llmString;
|
|
3208
|
-
llmConfig;
|
|
3209
|
-
mcpServersConfig;
|
|
3210
|
-
clientOwnedByAgent = false;
|
|
3211
|
-
constructor(options) {
|
|
3212
|
-
if (options.agentId) {
|
|
3213
|
-
this.isRemote = true;
|
|
3214
|
-
this.remoteAgent = new RemoteAgent({
|
|
3215
|
-
agentId: options.agentId,
|
|
3216
|
-
apiKey: options.apiKey,
|
|
3217
|
-
baseUrl: options.baseUrl
|
|
3218
|
-
});
|
|
3219
|
-
this.maxSteps = options.maxSteps ?? 5;
|
|
3220
|
-
this.memoryEnabled = options.memoryEnabled ?? true;
|
|
3221
|
-
this.autoInitialize = options.autoInitialize ?? false;
|
|
3222
|
-
this.verbose = options.verbose ?? false;
|
|
3223
|
-
this.observe = options.observe ?? true;
|
|
3224
|
-
this.connectors = [];
|
|
3225
|
-
this.disallowedTools = [];
|
|
3226
|
-
this.additionalTools = [];
|
|
3227
|
-
this.useServerManager = false;
|
|
3228
|
-
this.adapter = new LangChainAdapter();
|
|
3229
|
-
this.telemetry = Telemetry.getInstance();
|
|
3230
|
-
this.modelProvider = "remote";
|
|
3231
|
-
this.modelName = "remote-agent";
|
|
3232
|
-
this.observabilityManager = new ObservabilityManager({
|
|
3233
|
-
customCallbacks: options.callbacks,
|
|
3234
|
-
agentId: options.agentId
|
|
3235
|
-
});
|
|
3236
|
-
this.callbacks = [];
|
|
3237
|
-
return;
|
|
3238
|
-
}
|
|
3239
|
-
if (!options.llm) {
|
|
3240
|
-
throw new Error(
|
|
3241
|
-
"llm is required for local execution. For remote execution, provide agentId instead."
|
|
3242
|
-
);
|
|
3243
|
-
}
|
|
3244
|
-
const isSimplifiedMode = typeof options.llm === "string";
|
|
3245
|
-
if (isSimplifiedMode) {
|
|
3246
|
-
this.isSimplifiedMode = true;
|
|
3247
|
-
this.llmString = options.llm;
|
|
3248
|
-
this.llmConfig = options.llmConfig;
|
|
3249
|
-
this.mcpServersConfig = options.mcpServers;
|
|
3250
|
-
if (!this.mcpServersConfig || Object.keys(this.mcpServersConfig).length === 0) {
|
|
3251
|
-
throw new Error(
|
|
3252
|
-
"Simplified mode requires 'mcpServers' configuration. Provide an object with server configurations, e.g., { filesystem: { command: 'npx', args: [...] } }"
|
|
3253
|
-
);
|
|
3254
|
-
}
|
|
3255
|
-
this.llm = void 0;
|
|
3256
|
-
this.client = void 0;
|
|
3257
|
-
this.clientOwnedByAgent = true;
|
|
3258
|
-
this.connectors = [];
|
|
3259
|
-
logger.info(
|
|
3260
|
-
`\u{1F3AF} Simplified mode enabled: LLM will be created from '${this.llmString}'`
|
|
3261
|
-
);
|
|
3262
|
-
} else {
|
|
3263
|
-
this.isSimplifiedMode = false;
|
|
3264
|
-
this.llm = options.llm;
|
|
3265
|
-
this.client = options.client;
|
|
3266
|
-
this.connectors = options.connectors ?? [];
|
|
3267
|
-
this.clientOwnedByAgent = false;
|
|
3268
|
-
if (!this.client && this.connectors.length === 0) {
|
|
3269
|
-
throw new Error(
|
|
3270
|
-
"Explicit mode requires either 'client' or at least one 'connector'. Alternatively, use simplified mode with 'llm' as a string and 'mcpServers' config."
|
|
3271
|
-
);
|
|
3272
|
-
}
|
|
3273
|
-
}
|
|
3274
|
-
this.maxSteps = options.maxSteps ?? 5;
|
|
3275
|
-
this.autoInitialize = options.autoInitialize ?? false;
|
|
3276
|
-
this.memoryEnabled = options.memoryEnabled ?? true;
|
|
3277
|
-
this.systemPrompt = options.systemPrompt ?? null;
|
|
3278
|
-
this.systemPromptTemplateOverride = options.systemPromptTemplate ?? null;
|
|
3279
|
-
this.additionalInstructions = options.additionalInstructions ?? null;
|
|
3280
|
-
this.disallowedTools = options.disallowedTools ?? [];
|
|
3281
|
-
this.additionalTools = options.additionalTools ?? [];
|
|
3282
|
-
this.toolsUsedNames = options.toolsUsedNames ?? [];
|
|
3283
|
-
this.useServerManager = options.useServerManager ?? false;
|
|
3284
|
-
this.verbose = options.verbose ?? false;
|
|
3285
|
-
this.observe = options.observe ?? true;
|
|
3286
|
-
if (!this.isSimplifiedMode) {
|
|
3287
|
-
if (this.useServerManager) {
|
|
3288
|
-
if (!this.client) {
|
|
3289
|
-
throw new Error(
|
|
3290
|
-
"'client' must be provided when 'useServerManager' is true."
|
|
3291
|
-
);
|
|
3292
|
-
}
|
|
3293
|
-
this.adapter = options.adapter ?? new LangChainAdapter(this.disallowedTools);
|
|
3294
|
-
this.serverManager = options.serverManagerFactory?.(this.client) ?? new ServerManager(this.client, this.adapter);
|
|
3295
|
-
} else {
|
|
3296
|
-
this.adapter = options.adapter ?? new LangChainAdapter(this.disallowedTools);
|
|
3297
|
-
}
|
|
3298
|
-
this.telemetry = Telemetry.getInstance();
|
|
3299
|
-
if (this.llm) {
|
|
3300
|
-
const [provider, name] = extractModelInfo(this.llm);
|
|
3301
|
-
this.modelProvider = provider;
|
|
3302
|
-
this.modelName = name;
|
|
3303
|
-
} else {
|
|
3304
|
-
this.modelProvider = "unknown";
|
|
3305
|
-
this.modelName = "unknown";
|
|
3306
|
-
}
|
|
3307
|
-
} else {
|
|
3308
|
-
this.adapter = options.adapter ?? new LangChainAdapter(this.disallowedTools);
|
|
3309
|
-
this.telemetry = Telemetry.getInstance();
|
|
3310
|
-
this.modelProvider = "unknown";
|
|
3311
|
-
this.modelName = "unknown";
|
|
3312
|
-
}
|
|
3313
|
-
this.observabilityManager = new ObservabilityManager({
|
|
3314
|
-
customCallbacks: options.callbacks,
|
|
3315
|
-
verbose: this.verbose,
|
|
3316
|
-
observe: this.observe,
|
|
3317
|
-
agentId: options.agentId,
|
|
3318
|
-
metadataProvider: /* @__PURE__ */ __name(() => this.getMetadata(), "metadataProvider"),
|
|
3319
|
-
tagsProvider: /* @__PURE__ */ __name(() => this.getTags(), "tagsProvider")
|
|
3320
|
-
});
|
|
3321
|
-
Object.defineProperty(this, "agentExecutor", {
|
|
3322
|
-
get: /* @__PURE__ */ __name(() => this._agentExecutor, "get"),
|
|
3323
|
-
configurable: true
|
|
3324
|
-
});
|
|
3325
|
-
Object.defineProperty(this, "tools", {
|
|
3326
|
-
get: /* @__PURE__ */ __name(() => this._tools, "get"),
|
|
3327
|
-
configurable: true
|
|
3328
|
-
});
|
|
3329
|
-
Object.defineProperty(this, "initialized", {
|
|
3330
|
-
get: /* @__PURE__ */ __name(() => this._initialized, "get"),
|
|
3331
|
-
configurable: true
|
|
3332
|
-
});
|
|
3333
|
-
}
|
|
3334
|
-
async initialize() {
|
|
3335
|
-
if (this.isRemote) {
|
|
3336
|
-
this._initialized = true;
|
|
3337
|
-
return;
|
|
3338
|
-
}
|
|
3339
|
-
logger.info("\u{1F680} Initializing MCP agent and connecting to services...");
|
|
3340
|
-
if (this.isSimplifiedMode) {
|
|
3341
|
-
logger.info(
|
|
3342
|
-
"\u{1F3AF} Simplified mode: Creating client and LLM from configuration..."
|
|
3343
|
-
);
|
|
3344
|
-
if (this.mcpServersConfig) {
|
|
3345
|
-
logger.info(
|
|
3346
|
-
`Creating MCPClient with ${Object.keys(this.mcpServersConfig).length} server(s)...`
|
|
3347
|
-
);
|
|
3348
|
-
this.client = new MCPClient({ mcpServers: this.mcpServersConfig });
|
|
3349
|
-
logger.info("\u2705 MCPClient created successfully");
|
|
3350
|
-
}
|
|
3351
|
-
if (this.llmString) {
|
|
3352
|
-
logger.info(`Creating LLM from string: ${this.llmString}...`);
|
|
3353
|
-
try {
|
|
3354
|
-
this.llm = await createLLMFromString(this.llmString, this.llmConfig);
|
|
3355
|
-
logger.info("\u2705 LLM created successfully");
|
|
3356
|
-
const [provider, name] = extractModelInfo(this.llm);
|
|
3357
|
-
this.modelProvider = provider;
|
|
3358
|
-
this.modelName = name;
|
|
3359
|
-
} catch (error) {
|
|
3360
|
-
throw new Error(
|
|
3361
|
-
`Failed to create LLM from string '${this.llmString}': ${error?.message || error}`
|
|
3362
|
-
);
|
|
3363
|
-
}
|
|
3364
|
-
}
|
|
3365
|
-
if (this.useServerManager) {
|
|
3366
|
-
if (!this.client) {
|
|
3367
|
-
throw new Error(
|
|
3368
|
-
"'client' must be available when 'useServerManager' is true."
|
|
3369
|
-
);
|
|
3370
|
-
}
|
|
3371
|
-
this.serverManager = new ServerManager(this.client, this.adapter);
|
|
3372
|
-
}
|
|
3373
|
-
}
|
|
3374
|
-
this.callbacks = await this.observabilityManager.getCallbacks();
|
|
3375
|
-
const handlerNames = await this.observabilityManager.getHandlerNames();
|
|
3376
|
-
if (handlerNames.length > 0) {
|
|
3377
|
-
logger.info(`\u{1F4CA} Observability enabled with: ${handlerNames.join(", ")}`);
|
|
3378
|
-
}
|
|
3379
|
-
if (this.useServerManager && this.serverManager) {
|
|
3380
|
-
await this.serverManager.initialize();
|
|
3381
|
-
const managementTools = this.serverManager.tools;
|
|
3382
|
-
this._tools = managementTools;
|
|
3383
|
-
this._tools.push(...this.additionalTools);
|
|
3384
|
-
logger.info(
|
|
3385
|
-
`\u{1F527} Server manager mode active with ${managementTools.length} management tools`
|
|
3386
|
-
);
|
|
3387
|
-
await this.createSystemMessageFromTools(this._tools);
|
|
3388
|
-
} else {
|
|
3389
|
-
if (this.client) {
|
|
3390
|
-
this.sessions = this.client.getAllActiveSessions();
|
|
3391
|
-
logger.info(
|
|
3392
|
-
`\u{1F50C} Found ${Object.keys(this.sessions).length} existing sessions`
|
|
3393
|
-
);
|
|
3394
|
-
const nonCodeModeSessions = Object.keys(this.sessions).filter(
|
|
3395
|
-
(name) => name !== "code_mode"
|
|
3396
|
-
);
|
|
3397
|
-
if (nonCodeModeSessions.length === 0) {
|
|
3398
|
-
logger.info("\u{1F504} No active sessions found, creating new ones...");
|
|
3399
|
-
this.sessions = await this.client.createAllSessions();
|
|
3400
|
-
logger.info(
|
|
3401
|
-
`\u2705 Created ${Object.keys(this.sessions).length} new sessions`
|
|
3402
|
-
);
|
|
3403
|
-
}
|
|
3404
|
-
if (this.client.codeMode) {
|
|
3405
|
-
const codeModeSession = this.sessions["code_mode"];
|
|
3406
|
-
if (codeModeSession) {
|
|
3407
|
-
this._tools = await this.adapter.createToolsFromConnectors([
|
|
3408
|
-
codeModeSession.connector
|
|
3409
|
-
]);
|
|
3410
|
-
logger.info(`\u{1F6E0}\uFE0F Created ${this._tools.length} code mode tools`);
|
|
3411
|
-
} else {
|
|
3412
|
-
throw new Error(
|
|
3413
|
-
"Code mode enabled but code_mode session not found"
|
|
3414
|
-
);
|
|
3415
|
-
}
|
|
3416
|
-
} else {
|
|
3417
|
-
const tools = await this.adapter.createToolsFromConnectors(
|
|
3418
|
-
Object.values(this.sessions).map((session) => session.connector)
|
|
3419
|
-
);
|
|
3420
|
-
const resources = await this.adapter.createResourcesFromConnectors(
|
|
3421
|
-
Object.values(this.sessions).map((session) => session.connector)
|
|
3422
|
-
);
|
|
3423
|
-
const prompts = await this.adapter.createPromptsFromConnectors(
|
|
3424
|
-
Object.values(this.sessions).map((session) => session.connector)
|
|
3425
|
-
);
|
|
3426
|
-
this._tools = [...tools, ...resources, ...prompts];
|
|
3427
|
-
logger.info(
|
|
3428
|
-
`\u{1F6E0}\uFE0F Created ${this._tools.length} LangChain items from client: ${tools.length} tools, ${resources.length} resources, ${prompts.length} prompts`
|
|
3429
|
-
);
|
|
3430
|
-
}
|
|
3431
|
-
this._tools.push(...this.additionalTools);
|
|
3432
|
-
} else {
|
|
3433
|
-
logger.info(
|
|
3434
|
-
`\u{1F517} Connecting to ${this.connectors.length} direct connectors...`
|
|
3435
|
-
);
|
|
3436
|
-
for (const connector of this.connectors) {
|
|
3437
|
-
if (!connector.isClientConnected) {
|
|
3438
|
-
await connector.connect();
|
|
3439
|
-
}
|
|
3440
|
-
}
|
|
3441
|
-
const tools = await this.adapter.createToolsFromConnectors(
|
|
3442
|
-
this.connectors
|
|
3443
|
-
);
|
|
3444
|
-
const resources = await this.adapter.createResourcesFromConnectors(
|
|
3445
|
-
this.connectors
|
|
3446
|
-
);
|
|
3447
|
-
const prompts = await this.adapter.createPromptsFromConnectors(
|
|
3448
|
-
this.connectors
|
|
3449
|
-
);
|
|
3450
|
-
this._tools = [...tools, ...resources, ...prompts];
|
|
3451
|
-
this._tools.push(...this.additionalTools);
|
|
3452
|
-
logger.info(
|
|
3453
|
-
`\u{1F6E0}\uFE0F Created ${this._tools.length} LangChain items from connectors: ${tools.length} tools, ${resources.length} resources, ${prompts.length} prompts`
|
|
3454
|
-
);
|
|
3455
|
-
}
|
|
3456
|
-
logger.info(`\u{1F9F0} Found ${this._tools.length} tools across all connectors`);
|
|
3457
|
-
await this.createSystemMessageFromTools(this._tools);
|
|
3458
|
-
}
|
|
3459
|
-
this._agentExecutor = this.createAgent();
|
|
3460
|
-
this._initialized = true;
|
|
3461
|
-
const mcpServerInfo = this.getMCPServerInfo();
|
|
3462
|
-
if (Object.keys(mcpServerInfo).length > 0) {
|
|
3463
|
-
this.setMetadata(mcpServerInfo);
|
|
3464
|
-
logger.debug(
|
|
3465
|
-
`MCP server info added to metadata: ${JSON.stringify(mcpServerInfo)}`
|
|
3466
|
-
);
|
|
3467
|
-
}
|
|
3468
|
-
logger.info("\u2728 Agent initialization complete");
|
|
3469
|
-
}
|
|
3470
|
-
async createSystemMessageFromTools(tools) {
|
|
3471
|
-
const systemPromptTemplate = this.systemPromptTemplateOverride ?? DEFAULT_SYSTEM_PROMPT_TEMPLATE;
|
|
3472
|
-
this.systemMessage = createSystemMessage(
|
|
3473
|
-
tools,
|
|
3474
|
-
systemPromptTemplate,
|
|
3475
|
-
SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE,
|
|
3476
|
-
this.useServerManager,
|
|
3477
|
-
this.disallowedTools,
|
|
3478
|
-
this.systemPrompt ?? void 0,
|
|
3479
|
-
this.additionalInstructions ?? void 0
|
|
3480
|
-
);
|
|
3481
|
-
if (this.memoryEnabled) {
|
|
3482
|
-
this.conversationHistory = [
|
|
3483
|
-
this.systemMessage,
|
|
3484
|
-
...this.conversationHistory.filter(
|
|
3485
|
-
(m) => !(m instanceof SystemMessage2)
|
|
3486
|
-
)
|
|
3487
|
-
];
|
|
3488
|
-
}
|
|
3489
|
-
}
|
|
3490
|
-
createAgent() {
|
|
3491
|
-
if (!this.llm) {
|
|
3492
|
-
throw new Error("LLM is required to create agent");
|
|
3493
|
-
}
|
|
3494
|
-
const systemContent = this.systemMessage?.content ?? "You are a helpful assistant.";
|
|
3495
|
-
const toolNames = this._tools.map((tool) => tool.name);
|
|
3496
|
-
logger.info(`\u{1F9E0} Agent ready with tools: ${toolNames.join(", ")}`);
|
|
3497
|
-
const middleware = [modelCallLimitMiddleware({ runLimit: this.maxSteps })];
|
|
3498
|
-
const agent = createAgent({
|
|
3499
|
-
model: this.llm,
|
|
3500
|
-
tools: this._tools,
|
|
3501
|
-
systemPrompt: systemContent,
|
|
3502
|
-
middleware
|
|
3503
|
-
});
|
|
3504
|
-
logger.debug(
|
|
3505
|
-
`Created agent with max_steps=${this.maxSteps} (via ModelCallLimitMiddleware) and ${this.callbacks.length} callbacks`
|
|
3506
|
-
);
|
|
3507
|
-
return agent;
|
|
3508
|
-
}
|
|
3509
|
-
getConversationHistory() {
|
|
3510
|
-
return [...this.conversationHistory];
|
|
3511
|
-
}
|
|
3512
|
-
clearConversationHistory() {
|
|
3513
|
-
this.conversationHistory = this.memoryEnabled && this.systemMessage ? [this.systemMessage] : [];
|
|
3514
|
-
}
|
|
3515
|
-
addToHistory(message) {
|
|
3516
|
-
if (this.memoryEnabled) this.conversationHistory.push(message);
|
|
3517
|
-
}
|
|
3518
|
-
getSystemMessage() {
|
|
3519
|
-
return this.systemMessage;
|
|
3520
|
-
}
|
|
3521
|
-
setSystemMessage(message) {
|
|
3522
|
-
this.systemMessage = new SystemMessage2(message);
|
|
3523
|
-
if (this.memoryEnabled) {
|
|
3524
|
-
this.conversationHistory = this.conversationHistory.filter(
|
|
3525
|
-
(m) => !(m instanceof SystemMessage2)
|
|
3526
|
-
);
|
|
3527
|
-
this.conversationHistory.unshift(this.systemMessage);
|
|
3528
|
-
}
|
|
3529
|
-
if (this._initialized && this._tools.length) {
|
|
3530
|
-
this._agentExecutor = this.createAgent();
|
|
3531
|
-
logger.debug("Agent recreated with new system message");
|
|
3532
|
-
}
|
|
3533
|
-
}
|
|
3534
|
-
setDisallowedTools(disallowedTools) {
|
|
3535
|
-
this.disallowedTools = disallowedTools;
|
|
3536
|
-
this.adapter = new LangChainAdapter(this.disallowedTools);
|
|
3537
|
-
if (this._initialized) {
|
|
3538
|
-
logger.debug(
|
|
3539
|
-
"Agent already initialized. Changes will take effect on next initialization."
|
|
3540
|
-
);
|
|
3541
|
-
}
|
|
3542
|
-
}
|
|
3543
|
-
getDisallowedTools() {
|
|
3544
|
-
return this.disallowedTools;
|
|
3545
|
-
}
|
|
3546
|
-
/**
|
|
3547
|
-
* Set metadata for observability traces
|
|
3548
|
-
* @param newMetadata - Key-value pairs to add to metadata. Keys should be strings, values should be serializable.
|
|
3549
|
-
*/
|
|
3550
|
-
setMetadata(newMetadata) {
|
|
3551
|
-
const sanitizedMetadata = this.sanitizeMetadata(newMetadata);
|
|
3552
|
-
this.metadata = { ...this.metadata, ...sanitizedMetadata };
|
|
3553
|
-
logger.debug(`Metadata set: ${JSON.stringify(this.metadata)}`);
|
|
3554
|
-
}
|
|
3555
|
-
/**
|
|
3556
|
-
* Get current metadata
|
|
3557
|
-
* @returns A copy of the current metadata object
|
|
3558
|
-
*/
|
|
3559
|
-
getMetadata() {
|
|
3560
|
-
return { ...this.metadata };
|
|
3561
|
-
}
|
|
3562
|
-
/**
|
|
3563
|
-
* Set tags for observability traces
|
|
3564
|
-
* @param newTags - Array of tag strings to add. Duplicates will be automatically removed.
|
|
3565
|
-
*/
|
|
3566
|
-
setTags(newTags) {
|
|
3567
|
-
const sanitizedTags = this.sanitizeTags(newTags);
|
|
3568
|
-
this.tags = [.../* @__PURE__ */ new Set([...this.tags, ...sanitizedTags])];
|
|
3569
|
-
logger.debug(`Tags set: ${JSON.stringify(this.tags)}`);
|
|
3570
|
-
}
|
|
3571
|
-
/**
|
|
3572
|
-
* Get current tags
|
|
3573
|
-
* @returns A copy of the current tags array
|
|
3574
|
-
*/
|
|
3575
|
-
getTags() {
|
|
3576
|
-
return [...this.tags];
|
|
3577
|
-
}
|
|
3578
|
-
/**
|
|
3579
|
-
* Sanitize metadata to ensure compatibility with observability platforms
|
|
3580
|
-
* @param metadata - Raw metadata object
|
|
3581
|
-
* @returns Sanitized metadata object
|
|
3582
|
-
*/
|
|
3583
|
-
sanitizeMetadata(metadata) {
|
|
3584
|
-
const sanitized = {};
|
|
3585
|
-
for (const [key, value] of Object.entries(metadata)) {
|
|
3586
|
-
if (typeof key !== "string" || key.length === 0) {
|
|
3587
|
-
logger.warn(`Invalid metadata key: ${key}. Skipping.`);
|
|
3588
|
-
continue;
|
|
3589
|
-
}
|
|
3590
|
-
const sanitizedKey = key.replace(/[^\w-]/g, "_");
|
|
3591
|
-
if (value === null || value === void 0) {
|
|
3592
|
-
sanitized[sanitizedKey] = value;
|
|
3593
|
-
} else if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
3594
|
-
sanitized[sanitizedKey] = value;
|
|
3595
|
-
} else if (Array.isArray(value)) {
|
|
3596
|
-
const sanitizedArray = value.filter(
|
|
3597
|
-
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean"
|
|
3598
|
-
);
|
|
3599
|
-
if (sanitizedArray.length > 0) {
|
|
3600
|
-
sanitized[sanitizedKey] = sanitizedArray;
|
|
3601
|
-
}
|
|
3602
|
-
} else if (typeof value === "object") {
|
|
3603
|
-
try {
|
|
3604
|
-
const serialized = JSON.stringify(value);
|
|
3605
|
-
if (serialized.length > 1e3) {
|
|
3606
|
-
logger.warn(
|
|
3607
|
-
`Metadata value for key '${sanitizedKey}' is too large. Truncating.`
|
|
3608
|
-
);
|
|
3609
|
-
sanitized[sanitizedKey] = `${serialized.substring(0, 1e3)}...`;
|
|
3610
|
-
} else {
|
|
3611
|
-
sanitized[sanitizedKey] = value;
|
|
3612
|
-
}
|
|
3613
|
-
} catch (error) {
|
|
3614
|
-
logger.warn(
|
|
3615
|
-
`Failed to serialize metadata value for key '${sanitizedKey}': ${error}. Skipping.`
|
|
3616
|
-
);
|
|
3617
|
-
}
|
|
3618
|
-
} else {
|
|
3619
|
-
logger.warn(
|
|
3620
|
-
`Unsupported metadata value type for key '${sanitizedKey}': ${typeof value}. Skipping.`
|
|
3621
|
-
);
|
|
3622
|
-
}
|
|
3623
|
-
}
|
|
3624
|
-
return sanitized;
|
|
3625
|
-
}
|
|
3626
|
-
/**
|
|
3627
|
-
* Sanitize tags to ensure compatibility with observability platforms
|
|
3628
|
-
* @param tags - Array of tag strings
|
|
3629
|
-
* @returns Array of sanitized tag strings
|
|
3630
|
-
*/
|
|
3631
|
-
sanitizeTags(tags) {
|
|
3632
|
-
return tags.filter((tag) => typeof tag === "string" && tag.length > 0).map((tag) => tag.replace(/[^\w:-]/g, "_")).filter((tag) => tag.length <= 50);
|
|
3633
|
-
}
|
|
3634
|
-
/**
|
|
3635
|
-
* Get MCP server information for observability metadata
|
|
3636
|
-
*/
|
|
3637
|
-
getMCPServerInfo() {
|
|
3638
|
-
const serverInfo = {};
|
|
3639
|
-
try {
|
|
3640
|
-
if (this.client) {
|
|
3641
|
-
const serverNames = this.client.getServerNames();
|
|
3642
|
-
serverInfo.mcp_servers_count = serverNames.length;
|
|
3643
|
-
serverInfo.mcp_server_names = serverNames;
|
|
3644
|
-
const serverConfigs = {};
|
|
3645
|
-
for (const serverName of serverNames) {
|
|
3646
|
-
try {
|
|
3647
|
-
const config = this.client.getServerConfig(serverName);
|
|
3648
|
-
if (config) {
|
|
3649
|
-
let serverType = "unknown";
|
|
3650
|
-
if (config.command) {
|
|
3651
|
-
serverType = "command";
|
|
3652
|
-
} else if (config.url) {
|
|
3653
|
-
serverType = "http";
|
|
3654
|
-
} else if (config.ws_url) {
|
|
3655
|
-
serverType = "websocket";
|
|
3656
|
-
}
|
|
3657
|
-
serverConfigs[serverName] = {
|
|
3658
|
-
type: serverType,
|
|
3659
|
-
// Include safe configuration details (avoid sensitive data)
|
|
3660
|
-
has_args: !!config.args,
|
|
3661
|
-
has_env: !!config.env,
|
|
3662
|
-
has_headers: !!config.headers,
|
|
3663
|
-
url: config.url || null,
|
|
3664
|
-
command: config.command || null
|
|
3665
|
-
};
|
|
3666
|
-
}
|
|
3667
|
-
} catch (error) {
|
|
3668
|
-
logger.warn(
|
|
3669
|
-
`Failed to get config for server '${serverName}': ${error}`
|
|
3670
|
-
);
|
|
3671
|
-
serverConfigs[serverName] = {
|
|
3672
|
-
type: "error",
|
|
3673
|
-
error: "config_unavailable"
|
|
3674
|
-
};
|
|
3675
|
-
}
|
|
3676
|
-
}
|
|
3677
|
-
serverInfo.mcp_server_configs = serverConfigs;
|
|
3678
|
-
} else if (this.connectors && this.connectors.length > 0) {
|
|
3679
|
-
serverInfo.mcp_servers_count = this.connectors.length;
|
|
3680
|
-
serverInfo.mcp_server_names = this.connectors.map(
|
|
3681
|
-
(c) => c.publicIdentifier
|
|
3682
|
-
);
|
|
3683
|
-
serverInfo.mcp_server_types = this.connectors.map(
|
|
3684
|
-
(c) => c.constructor.name
|
|
3685
|
-
);
|
|
3686
|
-
}
|
|
3687
|
-
} catch (error) {
|
|
3688
|
-
logger.warn(`Failed to collect MCP server info: ${error}`);
|
|
3689
|
-
serverInfo.error = "collection_failed";
|
|
3690
|
-
}
|
|
3691
|
-
return serverInfo;
|
|
3692
|
-
}
|
|
3693
|
-
_normalizeOutput(value) {
|
|
3694
|
-
try {
|
|
3695
|
-
if (typeof value === "string") {
|
|
3696
|
-
return value;
|
|
3697
|
-
}
|
|
3698
|
-
if (value && typeof value === "object" && "content" in value) {
|
|
3699
|
-
return this._normalizeOutput(value.content);
|
|
3700
|
-
}
|
|
3701
|
-
if (Array.isArray(value)) {
|
|
3702
|
-
const parts = [];
|
|
3703
|
-
for (const item of value) {
|
|
3704
|
-
if (typeof item === "object" && item !== null) {
|
|
3705
|
-
if ("text" in item && typeof item.text === "string") {
|
|
3706
|
-
parts.push(item.text);
|
|
3707
|
-
} else if ("content" in item) {
|
|
3708
|
-
parts.push(this._normalizeOutput(item.content));
|
|
3709
|
-
} else {
|
|
3710
|
-
parts.push(String(item));
|
|
3711
|
-
}
|
|
3712
|
-
} else {
|
|
3713
|
-
const partText = item && typeof item === "object" && "text" in item ? item.text : null;
|
|
3714
|
-
if (typeof partText === "string") {
|
|
3715
|
-
parts.push(partText);
|
|
3716
|
-
} else {
|
|
3717
|
-
const partContent = item && typeof item === "object" && "content" in item ? item.content : item;
|
|
3718
|
-
parts.push(this._normalizeOutput(partContent));
|
|
3719
|
-
}
|
|
3720
|
-
}
|
|
3721
|
-
}
|
|
3722
|
-
return parts.join("");
|
|
3723
|
-
}
|
|
3724
|
-
return String(value);
|
|
3725
|
-
} catch (error) {
|
|
3726
|
-
return String(value);
|
|
3727
|
-
}
|
|
3728
|
-
}
|
|
3729
|
-
/**
|
|
3730
|
-
* Check if a message is AI/assistant-like regardless of whether it's a class instance.
|
|
3731
|
-
* Handles version mismatches, serialization boundaries, and different message formats.
|
|
3732
|
-
*
|
|
3733
|
-
* This method solves the issue where messages from LangChain agents may be plain JavaScript
|
|
3734
|
-
* objects (e.g., `{ type: 'ai', content: '...' }`) instead of AIMessage instances due to
|
|
3735
|
-
* serialization/deserialization across module boundaries or version mismatches.
|
|
3736
|
-
*
|
|
3737
|
-
* @example
|
|
3738
|
-
* // Real AIMessage instance (standard case)
|
|
3739
|
-
* _isAIMessageLike(new AIMessage("hello")) // => true
|
|
3740
|
-
*
|
|
3741
|
-
* @example
|
|
3742
|
-
* // Plain object after serialization (fixes issue #446)
|
|
3743
|
-
* _isAIMessageLike({ type: "ai", content: "hello" }) // => true
|
|
3744
|
-
*
|
|
3745
|
-
* @example
|
|
3746
|
-
* // OpenAI-style format with role
|
|
3747
|
-
* _isAIMessageLike({ role: "assistant", content: "hello" }) // => true
|
|
3748
|
-
*
|
|
3749
|
-
* @example
|
|
3750
|
-
* // Object with getType() method
|
|
3751
|
-
* _isAIMessageLike({ getType: () => "ai", content: "hello" }) // => true
|
|
3752
|
-
*
|
|
3753
|
-
* @param message - The message object to check
|
|
3754
|
-
* @returns true if the message represents an AI/assistant message
|
|
3755
|
-
*/
|
|
3756
|
-
_isAIMessageLike(message) {
|
|
3757
|
-
if (message instanceof AIMessage) {
|
|
3758
|
-
return true;
|
|
3759
|
-
}
|
|
3760
|
-
if (typeof message !== "object" || message === null) {
|
|
3761
|
-
return false;
|
|
3762
|
-
}
|
|
3763
|
-
const msg = message;
|
|
3764
|
-
if (typeof msg.getType === "function") {
|
|
3765
|
-
try {
|
|
3766
|
-
const type = msg.getType();
|
|
3767
|
-
if (type === "ai" || type === "assistant") {
|
|
3768
|
-
return true;
|
|
3769
|
-
}
|
|
3770
|
-
} catch (error) {
|
|
3771
|
-
}
|
|
3772
|
-
}
|
|
3773
|
-
if (typeof msg._getType === "function") {
|
|
3774
|
-
try {
|
|
3775
|
-
const type = msg._getType();
|
|
3776
|
-
if (type === "ai" || type === "assistant") {
|
|
3777
|
-
return true;
|
|
3778
|
-
}
|
|
3779
|
-
} catch (error) {
|
|
3780
|
-
}
|
|
3781
|
-
}
|
|
3782
|
-
if ("type" in msg) {
|
|
3783
|
-
return msg.type === "ai" || msg.type === "assistant";
|
|
3784
|
-
}
|
|
3785
|
-
if ("role" in msg) {
|
|
3786
|
-
return msg.role === "ai" || msg.role === "assistant";
|
|
3787
|
-
}
|
|
3788
|
-
return false;
|
|
3789
|
-
}
|
|
3790
|
-
/**
|
|
3791
|
-
* Check if a message has tool calls, handling both class instances and plain objects.
|
|
3792
|
-
* Safely checks for tool_calls array presence.
|
|
3793
|
-
*
|
|
3794
|
-
* @example
|
|
3795
|
-
* // AIMessage with tool calls
|
|
3796
|
-
* const msg = new AIMessage({ content: "", tool_calls: [{ name: "add", args: {} }] });
|
|
3797
|
-
* _messageHasToolCalls(msg) // => true
|
|
3798
|
-
*
|
|
3799
|
-
* @example
|
|
3800
|
-
* // Plain object with tool calls
|
|
3801
|
-
* _messageHasToolCalls({ type: "ai", tool_calls: [{ name: "add" }] }) // => true
|
|
3802
|
-
*
|
|
3803
|
-
* @example
|
|
3804
|
-
* // Message without tool calls
|
|
3805
|
-
* _messageHasToolCalls({ type: "ai", content: "hello" }) // => false
|
|
3806
|
-
*
|
|
3807
|
-
* @param message - The message object to check
|
|
3808
|
-
* @returns true if the message has non-empty tool_calls array
|
|
3809
|
-
*/
|
|
3810
|
-
_messageHasToolCalls(message) {
|
|
3811
|
-
if (typeof message === "object" && message !== null && "tool_calls" in message && Array.isArray(message.tool_calls)) {
|
|
3812
|
-
return message.tool_calls.length > 0;
|
|
3813
|
-
}
|
|
3814
|
-
return false;
|
|
3815
|
-
}
|
|
3816
|
-
/**
|
|
3817
|
-
* Check if a message is a HumanMessage-like object.
|
|
3818
|
-
* Handles both class instances and plain objects from serialization.
|
|
3819
|
-
*
|
|
3820
|
-
* @example
|
|
3821
|
-
* _isHumanMessageLike(new HumanMessage("hello")) // => true
|
|
3822
|
-
* _isHumanMessageLike({ type: "human", content: "hello" }) // => true
|
|
3823
|
-
*
|
|
3824
|
-
* @param message - The message object to check
|
|
3825
|
-
* @returns true if the message represents a human message
|
|
3826
|
-
*/
|
|
3827
|
-
_isHumanMessageLike(message) {
|
|
3828
|
-
if (message instanceof HumanMessage) {
|
|
3829
|
-
return true;
|
|
3830
|
-
}
|
|
3831
|
-
if (typeof message !== "object" || message === null) {
|
|
3832
|
-
return false;
|
|
3833
|
-
}
|
|
3834
|
-
const msg = message;
|
|
3835
|
-
if (typeof msg.getType === "function") {
|
|
3836
|
-
try {
|
|
3837
|
-
const type = msg.getType();
|
|
3838
|
-
if (type === "human" || type === "user") {
|
|
3839
|
-
return true;
|
|
3840
|
-
}
|
|
3841
|
-
} catch (error) {
|
|
3842
|
-
}
|
|
3843
|
-
}
|
|
3844
|
-
if ("type" in msg && (msg.type === "human" || msg.type === "user")) {
|
|
3845
|
-
return true;
|
|
3846
|
-
}
|
|
3847
|
-
if ("role" in msg && (msg.role === "human" || msg.role === "user")) {
|
|
3848
|
-
return true;
|
|
3849
|
-
}
|
|
3850
|
-
return false;
|
|
3851
|
-
}
|
|
3852
|
-
/**
|
|
3853
|
-
* Check if a message is a ToolMessage-like object.
|
|
3854
|
-
* Handles both class instances and plain objects from serialization.
|
|
3855
|
-
*
|
|
3856
|
-
* @example
|
|
3857
|
-
* _isToolMessageLike(new ToolMessage({ content: "result", tool_call_id: "123" })) // => true
|
|
3858
|
-
* _isToolMessageLike({ type: "tool", content: "result" }) // => true
|
|
3859
|
-
*
|
|
3860
|
-
* @param message - The message object to check
|
|
3861
|
-
* @returns true if the message represents a tool message
|
|
3862
|
-
*/
|
|
3863
|
-
_isToolMessageLike(message) {
|
|
3864
|
-
if (message instanceof ToolMessage) {
|
|
3865
|
-
return true;
|
|
3866
|
-
}
|
|
3867
|
-
if (typeof message !== "object" || message === null) {
|
|
3868
|
-
return false;
|
|
3869
|
-
}
|
|
3870
|
-
const msg = message;
|
|
3871
|
-
if (typeof msg.getType === "function") {
|
|
3872
|
-
try {
|
|
3873
|
-
const type = msg.getType();
|
|
3874
|
-
if (type === "tool") {
|
|
3875
|
-
return true;
|
|
3876
|
-
}
|
|
3877
|
-
} catch (error) {
|
|
3878
|
-
}
|
|
3879
|
-
}
|
|
3880
|
-
if ("type" in msg && msg.type === "tool") {
|
|
3881
|
-
return true;
|
|
3882
|
-
}
|
|
3883
|
-
return false;
|
|
3884
|
-
}
|
|
3885
|
-
/**
|
|
3886
|
-
* Extract content from a message, handling both AIMessage instances and plain objects.
|
|
3887
|
-
*
|
|
3888
|
-
* @example
|
|
3889
|
-
* // From AIMessage instance
|
|
3890
|
-
* _getMessageContent(new AIMessage("hello")) // => "hello"
|
|
3891
|
-
*
|
|
3892
|
-
* @example
|
|
3893
|
-
* // From plain object
|
|
3894
|
-
* _getMessageContent({ type: "ai", content: "hello" }) // => "hello"
|
|
3895
|
-
*
|
|
3896
|
-
* @param message - The message object to extract content from
|
|
3897
|
-
* @returns The content of the message, or undefined if not present
|
|
3898
|
-
*/
|
|
3899
|
-
_getMessageContent(message) {
|
|
3900
|
-
if (message instanceof AIMessage) {
|
|
3901
|
-
return message.content;
|
|
3902
|
-
}
|
|
3903
|
-
if (message && typeof message === "object" && "content" in message) {
|
|
3904
|
-
return message.content;
|
|
3905
|
-
}
|
|
3906
|
-
return void 0;
|
|
3907
|
-
}
|
|
3908
|
-
async _consumeAndReturn(generator) {
|
|
3909
|
-
while (true) {
|
|
3910
|
-
const { done, value } = await generator.next();
|
|
3911
|
-
if (done) {
|
|
3912
|
-
return value;
|
|
3913
|
-
}
|
|
3914
|
-
}
|
|
3915
|
-
}
|
|
3916
|
-
async run(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema) {
|
|
3917
|
-
const {
|
|
3918
|
-
query,
|
|
3919
|
-
maxSteps: steps,
|
|
3920
|
-
manageConnector: manage,
|
|
3921
|
-
externalHistory: history,
|
|
3922
|
-
outputSchema: schema
|
|
3923
|
-
} = normalizeRunOptions(
|
|
3924
|
-
queryOrOptions,
|
|
3925
|
-
maxSteps,
|
|
3926
|
-
manageConnector,
|
|
3927
|
-
externalHistory,
|
|
3928
|
-
outputSchema
|
|
3929
|
-
);
|
|
3930
|
-
if (this.isRemote && this.remoteAgent) {
|
|
3931
|
-
return this.remoteAgent.run(query, steps, manage, history, schema);
|
|
3932
|
-
}
|
|
3933
|
-
const generator = this.stream(query, steps, manage, history, schema);
|
|
3934
|
-
return this._consumeAndReturn(generator);
|
|
3935
|
-
}
|
|
3936
|
-
async *stream(queryOrOptions, maxSteps, manageConnector = true, externalHistory, outputSchema) {
|
|
3937
|
-
const {
|
|
3938
|
-
query,
|
|
3939
|
-
maxSteps: steps,
|
|
3940
|
-
manageConnector: manage,
|
|
3941
|
-
externalHistory: history,
|
|
3942
|
-
outputSchema: schema
|
|
3943
|
-
} = normalizeRunOptions(
|
|
3944
|
-
queryOrOptions,
|
|
3945
|
-
maxSteps,
|
|
3946
|
-
manageConnector,
|
|
3947
|
-
externalHistory,
|
|
3948
|
-
outputSchema
|
|
3949
|
-
);
|
|
3950
|
-
if (this.isRemote && this.remoteAgent) {
|
|
3951
|
-
const result = await this.remoteAgent.run(
|
|
3952
|
-
query,
|
|
3953
|
-
steps,
|
|
3954
|
-
manage,
|
|
3955
|
-
history,
|
|
3956
|
-
schema
|
|
3957
|
-
);
|
|
3958
|
-
return result;
|
|
3959
|
-
}
|
|
3960
|
-
let initializedHere = false;
|
|
3961
|
-
const startTime = Date.now();
|
|
3962
|
-
let success = false;
|
|
3963
|
-
let finalOutput = null;
|
|
3964
|
-
let stepsTaken = 0;
|
|
3965
|
-
try {
|
|
3966
|
-
if (manage && !this._initialized) {
|
|
3967
|
-
await this.initialize();
|
|
3968
|
-
initializedHere = true;
|
|
3969
|
-
} else if (!this._initialized && this.autoInitialize) {
|
|
3970
|
-
await this.initialize();
|
|
3971
|
-
initializedHere = true;
|
|
3972
|
-
}
|
|
3973
|
-
if (!this._agentExecutor) {
|
|
3974
|
-
throw new Error("MCP agent failed to initialize");
|
|
3975
|
-
}
|
|
3976
|
-
if (this.useServerManager && this.serverManager) {
|
|
3977
|
-
const currentTools = this.serverManager.tools;
|
|
3978
|
-
const currentToolNames = new Set(currentTools.map((t) => t.name));
|
|
3979
|
-
const existingToolNames = new Set(this._tools.map((t) => t.name));
|
|
3980
|
-
if (currentToolNames.size !== existingToolNames.size || [...currentToolNames].some((n) => !existingToolNames.has(n))) {
|
|
3981
|
-
logger.info(
|
|
3982
|
-
`\u{1F504} Tools changed before execution, updating agent. New tools: ${[...currentToolNames].join(", ")}`
|
|
3983
|
-
);
|
|
3984
|
-
this._tools = currentTools;
|
|
3985
|
-
this._tools.push(...this.additionalTools);
|
|
3986
|
-
await this.createSystemMessageFromTools(this._tools);
|
|
3987
|
-
this._agentExecutor = this.createAgent();
|
|
3988
|
-
}
|
|
3989
|
-
}
|
|
3990
|
-
const historyToUse = history ?? this.conversationHistory;
|
|
3991
|
-
const langchainHistory = [];
|
|
3992
|
-
for (const msg of historyToUse) {
|
|
3993
|
-
if (this._isHumanMessageLike(msg) || this._isAIMessageLike(msg) || this._isToolMessageLike(msg)) {
|
|
3994
|
-
langchainHistory.push(msg);
|
|
3995
|
-
}
|
|
3996
|
-
}
|
|
3997
|
-
const displayQuery = query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, " ")}...` : query.replace(/\n/g, " ");
|
|
3998
|
-
logger.info(`\u{1F4AC} Received query: '${displayQuery}'`);
|
|
3999
|
-
logger.info("\u{1F3C1} Starting agent execution");
|
|
4000
|
-
const maxRestarts = 3;
|
|
4001
|
-
let restartCount = 0;
|
|
4002
|
-
const accumulatedMessages = [
|
|
4003
|
-
...langchainHistory,
|
|
4004
|
-
new HumanMessage(query)
|
|
4005
|
-
];
|
|
4006
|
-
while (restartCount <= maxRestarts) {
|
|
4007
|
-
const inputs = { messages: accumulatedMessages };
|
|
4008
|
-
let shouldRestart = false;
|
|
4009
|
-
const stream = await this._agentExecutor.stream(inputs, {
|
|
4010
|
-
streamMode: "updates",
|
|
4011
|
-
// Get updates as they happen
|
|
4012
|
-
callbacks: this.callbacks,
|
|
4013
|
-
metadata: this.getMetadata(),
|
|
4014
|
-
tags: this.getTags(),
|
|
4015
|
-
// Set trace name for LangChain/Langfuse
|
|
4016
|
-
runName: this.metadata.trace_name || "mcp-use-agent",
|
|
4017
|
-
// Set recursion limit to 3x maxSteps to account for model calls + tool executions
|
|
4018
|
-
recursionLimit: this.maxSteps * 3,
|
|
4019
|
-
// Pass sessionId for Langfuse if present in metadata
|
|
4020
|
-
...this.metadata.session_id && {
|
|
4021
|
-
sessionId: this.metadata.session_id
|
|
4022
|
-
}
|
|
4023
|
-
});
|
|
4024
|
-
for await (const chunk of stream) {
|
|
4025
|
-
for (const [nodeName, nodeOutput] of Object.entries(chunk)) {
|
|
4026
|
-
logger.debug(
|
|
4027
|
-
`\u{1F4E6} Node '${nodeName}' output: ${JSON.stringify(nodeOutput)}`
|
|
4028
|
-
);
|
|
4029
|
-
if (nodeOutput && typeof nodeOutput === "object" && "messages" in nodeOutput) {
|
|
4030
|
-
let messages = nodeOutput.messages;
|
|
4031
|
-
if (!Array.isArray(messages)) {
|
|
4032
|
-
messages = [messages];
|
|
4033
|
-
}
|
|
4034
|
-
for (const msg of messages) {
|
|
4035
|
-
if (!accumulatedMessages.includes(msg)) {
|
|
4036
|
-
accumulatedMessages.push(msg);
|
|
4037
|
-
}
|
|
4038
|
-
}
|
|
4039
|
-
for (const message of messages) {
|
|
4040
|
-
if ("tool_calls" in message && Array.isArray(message.tool_calls) && message.tool_calls.length > 0) {
|
|
4041
|
-
for (const toolCall of message.tool_calls) {
|
|
4042
|
-
const toolName = toolCall.name || "unknown";
|
|
4043
|
-
const toolInput = toolCall.args || {};
|
|
4044
|
-
this.toolsUsedNames.push(toolName);
|
|
4045
|
-
stepsTaken++;
|
|
4046
|
-
let toolInputStr = JSON.stringify(toolInput);
|
|
4047
|
-
if (toolInputStr.length > 100) {
|
|
4048
|
-
toolInputStr = `${toolInputStr.slice(0, 97)}...`;
|
|
4049
|
-
}
|
|
4050
|
-
logger.info(
|
|
4051
|
-
`\u{1F527} Tool call: ${toolName} with input: ${toolInputStr}`
|
|
4052
|
-
);
|
|
4053
|
-
yield {
|
|
4054
|
-
action: {
|
|
4055
|
-
tool: toolName,
|
|
4056
|
-
toolInput,
|
|
4057
|
-
log: `Calling tool ${toolName}`
|
|
4058
|
-
},
|
|
4059
|
-
observation: ""
|
|
4060
|
-
// Will be filled in by tool result
|
|
4061
|
-
};
|
|
4062
|
-
}
|
|
4063
|
-
}
|
|
4064
|
-
if (this._isToolMessageLike(message)) {
|
|
4065
|
-
const observation = message.content;
|
|
4066
|
-
let observationStr = String(observation);
|
|
4067
|
-
if (observationStr.length > 100) {
|
|
4068
|
-
observationStr = `${observationStr.slice(0, 97)}...`;
|
|
4069
|
-
}
|
|
4070
|
-
observationStr = observationStr.replace(/\n/g, " ");
|
|
4071
|
-
logger.info(`\u{1F4C4} Tool result: ${observationStr}`);
|
|
4072
|
-
if (this.useServerManager && this.serverManager) {
|
|
4073
|
-
const currentTools = this.serverManager.tools;
|
|
4074
|
-
const currentToolNames = new Set(
|
|
4075
|
-
currentTools.map((t) => t.name)
|
|
4076
|
-
);
|
|
4077
|
-
const existingToolNames = new Set(
|
|
4078
|
-
this._tools.map((t) => t.name)
|
|
4079
|
-
);
|
|
4080
|
-
if (currentToolNames.size !== existingToolNames.size || [...currentToolNames].some(
|
|
4081
|
-
(n) => !existingToolNames.has(n)
|
|
4082
|
-
)) {
|
|
4083
|
-
logger.info(
|
|
4084
|
-
`\u{1F504} Tools changed during execution. New tools: ${[...currentToolNames].join(", ")}`
|
|
4085
|
-
);
|
|
4086
|
-
this._tools = currentTools;
|
|
4087
|
-
this._tools.push(...this.additionalTools);
|
|
4088
|
-
await this.createSystemMessageFromTools(this._tools);
|
|
4089
|
-
this._agentExecutor = this.createAgent();
|
|
4090
|
-
shouldRestart = true;
|
|
4091
|
-
restartCount++;
|
|
4092
|
-
logger.info(
|
|
4093
|
-
`\u{1F503} Restarting execution with updated tools (restart ${restartCount}/${maxRestarts})`
|
|
4094
|
-
);
|
|
4095
|
-
break;
|
|
4096
|
-
}
|
|
4097
|
-
}
|
|
4098
|
-
}
|
|
4099
|
-
if (this._isAIMessageLike(message) && !this._messageHasToolCalls(message)) {
|
|
4100
|
-
finalOutput = this._normalizeOutput(
|
|
4101
|
-
this._getMessageContent(message)
|
|
4102
|
-
);
|
|
4103
|
-
logger.info("\u2705 Agent finished with output");
|
|
4104
|
-
}
|
|
4105
|
-
}
|
|
4106
|
-
if (shouldRestart) {
|
|
4107
|
-
break;
|
|
4108
|
-
}
|
|
4109
|
-
}
|
|
4110
|
-
}
|
|
4111
|
-
if (shouldRestart) {
|
|
4112
|
-
break;
|
|
4113
|
-
}
|
|
4114
|
-
}
|
|
4115
|
-
if (!shouldRestart) {
|
|
4116
|
-
break;
|
|
4117
|
-
}
|
|
4118
|
-
if (restartCount > maxRestarts) {
|
|
4119
|
-
logger.warn(
|
|
4120
|
-
`\u26A0\uFE0F Max restarts (${maxRestarts}) reached. Continuing with current tools.`
|
|
4121
|
-
);
|
|
4122
|
-
break;
|
|
4123
|
-
}
|
|
4124
|
-
}
|
|
4125
|
-
if (this.memoryEnabled) {
|
|
4126
|
-
const newMessages = accumulatedMessages.slice(langchainHistory.length);
|
|
4127
|
-
for (const msg of newMessages) {
|
|
4128
|
-
this.addToHistory(msg);
|
|
4129
|
-
}
|
|
4130
|
-
}
|
|
4131
|
-
if (schema && finalOutput) {
|
|
4132
|
-
try {
|
|
4133
|
-
logger.info("\u{1F527} Attempting structured output...");
|
|
4134
|
-
const structuredResult = await this._attemptStructuredOutput(
|
|
4135
|
-
finalOutput,
|
|
4136
|
-
this.llm,
|
|
4137
|
-
schema
|
|
4138
|
-
);
|
|
4139
|
-
if (this.memoryEnabled) {
|
|
4140
|
-
this.addToHistory(
|
|
4141
|
-
new AIMessage(
|
|
4142
|
-
`Structured result: ${JSON.stringify(structuredResult)}`
|
|
4143
|
-
)
|
|
4144
|
-
);
|
|
4145
|
-
}
|
|
4146
|
-
logger.info("\u2705 Structured output successful");
|
|
4147
|
-
success = true;
|
|
4148
|
-
return structuredResult;
|
|
4149
|
-
} catch (e) {
|
|
4150
|
-
logger.error(`\u274C Structured output failed: ${e}`);
|
|
4151
|
-
throw new Error(
|
|
4152
|
-
`Failed to generate structured output: ${e instanceof Error ? e.message : String(e)}`
|
|
4153
|
-
);
|
|
4154
|
-
}
|
|
4155
|
-
}
|
|
4156
|
-
logger.info(
|
|
4157
|
-
`\u{1F389} Agent execution complete in ${((Date.now() - startTime) / 1e3).toFixed(2)} seconds`
|
|
4158
|
-
);
|
|
4159
|
-
success = true;
|
|
4160
|
-
return finalOutput || "No output generated";
|
|
4161
|
-
} catch (e) {
|
|
4162
|
-
logger.error(`\u274C Error running query: ${e}`);
|
|
4163
|
-
if (initializedHere && manage) {
|
|
4164
|
-
logger.info("\u{1F9F9} Cleaning up resources after error");
|
|
4165
|
-
await this.close();
|
|
4166
|
-
}
|
|
4167
|
-
throw e;
|
|
4168
|
-
} finally {
|
|
4169
|
-
const executionTimeMs = Date.now() - startTime;
|
|
4170
|
-
let serverCount = 0;
|
|
4171
|
-
if (this.client) {
|
|
4172
|
-
serverCount = Object.keys(this.client.getAllActiveSessions()).length;
|
|
4173
|
-
} else if (this.connectors) {
|
|
4174
|
-
serverCount = this.connectors.length;
|
|
4175
|
-
}
|
|
4176
|
-
const conversationHistoryLength = this.memoryEnabled ? this.conversationHistory.length : 0;
|
|
4177
|
-
const toolsAvailable = this._tools || [];
|
|
4178
|
-
await this.telemetry.trackAgentExecution({
|
|
4179
|
-
executionMethod: "stream",
|
|
4180
|
-
query,
|
|
4181
|
-
success,
|
|
4182
|
-
modelProvider: this.modelProvider,
|
|
4183
|
-
modelName: this.modelName,
|
|
4184
|
-
serverCount,
|
|
4185
|
-
serverIdentifiers: this.connectors.map(
|
|
4186
|
-
(connector) => connector.publicIdentifier
|
|
4187
|
-
),
|
|
4188
|
-
totalToolsAvailable: toolsAvailable.length,
|
|
4189
|
-
toolsAvailableNames: toolsAvailable.map((t) => t.name),
|
|
4190
|
-
maxStepsConfigured: this.maxSteps,
|
|
4191
|
-
memoryEnabled: this.memoryEnabled,
|
|
4192
|
-
useServerManager: this.useServerManager,
|
|
4193
|
-
maxStepsUsed: steps ?? null,
|
|
4194
|
-
manageConnector: manage ?? true,
|
|
4195
|
-
externalHistoryUsed: history !== void 0,
|
|
4196
|
-
stepsTaken,
|
|
4197
|
-
toolsUsedCount: this.toolsUsedNames.length,
|
|
4198
|
-
toolsUsedNames: this.toolsUsedNames,
|
|
4199
|
-
response: finalOutput || "",
|
|
4200
|
-
executionTimeMs,
|
|
4201
|
-
errorType: success ? null : "execution_error",
|
|
4202
|
-
conversationHistoryLength
|
|
4203
|
-
});
|
|
4204
|
-
if (manage && !this.client && initializedHere) {
|
|
4205
|
-
logger.info("\u{1F9F9} Closing agent after stream completion");
|
|
4206
|
-
await this.close();
|
|
4207
|
-
}
|
|
4208
|
-
}
|
|
4209
|
-
}
|
|
4210
|
-
/**
|
|
4211
|
-
* Flush observability traces to the configured observability platform.
|
|
4212
|
-
* Important for serverless environments where traces need to be sent before function termination.
|
|
4213
|
-
*/
|
|
4214
|
-
async flush() {
|
|
4215
|
-
if (this.isRemote && this.remoteAgent) {
|
|
4216
|
-
return;
|
|
4217
|
-
}
|
|
4218
|
-
logger.debug("Flushing observability traces...");
|
|
4219
|
-
await this.observabilityManager.flush();
|
|
4220
|
-
}
|
|
4221
|
-
async close() {
|
|
4222
|
-
if (this.isRemote && this.remoteAgent) {
|
|
4223
|
-
await this.remoteAgent.close();
|
|
4224
|
-
return;
|
|
4225
|
-
}
|
|
4226
|
-
logger.info("\u{1F50C} Closing MCPAgent resources\u2026");
|
|
4227
|
-
await this.observabilityManager.shutdown();
|
|
4228
|
-
try {
|
|
4229
|
-
this._agentExecutor = null;
|
|
4230
|
-
this._tools = [];
|
|
4231
|
-
if (this.client) {
|
|
4232
|
-
if (this.clientOwnedByAgent) {
|
|
4233
|
-
logger.info(
|
|
4234
|
-
"\u{1F504} Closing internally-created client (simplified mode) and cleaning up resources"
|
|
4235
|
-
);
|
|
4236
|
-
await this.client.close();
|
|
4237
|
-
this.sessions = {};
|
|
4238
|
-
this.client = void 0;
|
|
4239
|
-
} else {
|
|
4240
|
-
logger.info("\u{1F504} Closing client and cleaning up resources");
|
|
4241
|
-
await this.client.close();
|
|
4242
|
-
this.sessions = {};
|
|
4243
|
-
}
|
|
4244
|
-
} else {
|
|
4245
|
-
for (const connector of this.connectors) {
|
|
4246
|
-
logger.info("\u{1F504} Disconnecting connector");
|
|
4247
|
-
await connector.disconnect();
|
|
4248
|
-
}
|
|
4249
|
-
}
|
|
4250
|
-
if (this.isSimplifiedMode && this.llm) {
|
|
4251
|
-
logger.debug("\u{1F504} Clearing LLM reference (simplified mode)");
|
|
4252
|
-
this.llm = void 0;
|
|
4253
|
-
}
|
|
4254
|
-
if ("connectorToolMap" in this.adapter) {
|
|
4255
|
-
this.adapter = new LangChainAdapter();
|
|
4256
|
-
}
|
|
4257
|
-
} finally {
|
|
4258
|
-
this._initialized = false;
|
|
4259
|
-
logger.info("\u{1F44B} Agent closed successfully");
|
|
4260
|
-
}
|
|
4261
|
-
}
|
|
4262
|
-
async *prettyStreamEvents(queryOrOptions, maxSteps, manageConnector = true, externalHistory, outputSchema) {
|
|
4263
|
-
const { prettyStreamEvents: prettyStream } = await import("./display-A5IEINAP.js");
|
|
4264
|
-
const finalResponse = "";
|
|
4265
|
-
for await (const _ of prettyStream(
|
|
4266
|
-
this.streamEvents(
|
|
4267
|
-
queryOrOptions,
|
|
4268
|
-
maxSteps,
|
|
4269
|
-
manageConnector,
|
|
4270
|
-
externalHistory,
|
|
4271
|
-
outputSchema
|
|
4272
|
-
)
|
|
4273
|
-
)) {
|
|
4274
|
-
yield;
|
|
4275
|
-
}
|
|
4276
|
-
return finalResponse;
|
|
4277
|
-
}
|
|
4278
|
-
async *streamEvents(queryOrOptions, maxSteps, manageConnector = true, externalHistory, outputSchema) {
|
|
4279
|
-
const normalized = normalizeRunOptions(
|
|
4280
|
-
queryOrOptions,
|
|
4281
|
-
maxSteps,
|
|
4282
|
-
manageConnector,
|
|
4283
|
-
externalHistory,
|
|
4284
|
-
outputSchema
|
|
4285
|
-
);
|
|
4286
|
-
let { query } = normalized;
|
|
4287
|
-
const {
|
|
4288
|
-
maxSteps: steps,
|
|
4289
|
-
manageConnector: manage,
|
|
4290
|
-
externalHistory: history,
|
|
4291
|
-
outputSchema: schema
|
|
4292
|
-
} = normalized;
|
|
4293
|
-
let initializedHere = false;
|
|
4294
|
-
const startTime = Date.now();
|
|
4295
|
-
let success = false;
|
|
4296
|
-
let eventCount = 0;
|
|
4297
|
-
let totalResponseLength = 0;
|
|
4298
|
-
let finalResponse = "";
|
|
4299
|
-
if (schema) {
|
|
4300
|
-
query = this._enhanceQueryWithSchema(query, schema);
|
|
4301
|
-
}
|
|
4302
|
-
try {
|
|
4303
|
-
if (manage && !this._initialized) {
|
|
4304
|
-
await this.initialize();
|
|
4305
|
-
initializedHere = true;
|
|
4306
|
-
} else if (!this._initialized && this.autoInitialize) {
|
|
4307
|
-
await this.initialize();
|
|
4308
|
-
initializedHere = true;
|
|
4309
|
-
}
|
|
4310
|
-
const agentExecutor = this._agentExecutor;
|
|
4311
|
-
if (!agentExecutor) {
|
|
4312
|
-
throw new Error("MCP agent failed to initialize");
|
|
4313
|
-
}
|
|
4314
|
-
this.maxSteps = steps ?? this.maxSteps;
|
|
4315
|
-
const display_query = typeof query === "string" && query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, " ")}...` : typeof query === "string" ? query.replace(/\n/g, " ") : String(query);
|
|
4316
|
-
logger.info(`\u{1F4AC} Received query for streamEvents: '${display_query}'`);
|
|
4317
|
-
if (this.memoryEnabled) {
|
|
4318
|
-
logger.info(`\u{1F504} Adding user message to history: ${display_query}`);
|
|
4319
|
-
this.addToHistory(new HumanMessage({ content: query }));
|
|
4320
|
-
}
|
|
4321
|
-
const historyToUse = history ?? this.conversationHistory;
|
|
4322
|
-
const langchainHistory = [];
|
|
4323
|
-
for (const msg of historyToUse) {
|
|
4324
|
-
if (this._isHumanMessageLike(msg) || this._isAIMessageLike(msg) || this._isToolMessageLike(msg)) {
|
|
4325
|
-
langchainHistory.push(msg);
|
|
4326
|
-
} else {
|
|
4327
|
-
logger.info(
|
|
4328
|
-
`\u26A0\uFE0F Skipped message of type: ${msg.constructor?.name || typeof msg}`
|
|
4329
|
-
);
|
|
4330
|
-
}
|
|
4331
|
-
}
|
|
4332
|
-
const inputs = [
|
|
4333
|
-
...langchainHistory,
|
|
4334
|
-
new HumanMessage(query)
|
|
4335
|
-
];
|
|
4336
|
-
logger.info("callbacks", this.callbacks);
|
|
4337
|
-
const eventStream = agentExecutor.streamEvents(
|
|
4338
|
-
{ messages: inputs },
|
|
4339
|
-
{
|
|
4340
|
-
streamMode: "messages",
|
|
4341
|
-
version: "v2",
|
|
4342
|
-
callbacks: this.callbacks,
|
|
4343
|
-
metadata: this.getMetadata(),
|
|
4344
|
-
tags: this.getTags(),
|
|
4345
|
-
// Set trace name for LangChain/Langfuse
|
|
4346
|
-
runName: this.metadata.trace_name || "mcp-use-agent",
|
|
4347
|
-
// Set recursion limit to 3x maxSteps to account for model calls + tool executions
|
|
4348
|
-
recursionLimit: this.maxSteps * 3,
|
|
4349
|
-
// Pass sessionId for Langfuse if present in metadata
|
|
4350
|
-
...this.metadata.session_id && {
|
|
4351
|
-
sessionId: this.metadata.session_id
|
|
4352
|
-
}
|
|
4353
|
-
}
|
|
4354
|
-
);
|
|
4355
|
-
for await (const event of eventStream) {
|
|
4356
|
-
eventCount++;
|
|
4357
|
-
if (!event || typeof event !== "object") {
|
|
4358
|
-
continue;
|
|
4359
|
-
}
|
|
4360
|
-
if (event.event === "on_chat_model_stream" && event.data?.chunk?.content) {
|
|
4361
|
-
totalResponseLength += event.data.chunk.content.length;
|
|
4362
|
-
}
|
|
4363
|
-
if (event.event === "on_chat_model_stream" && event.data?.chunk) {
|
|
4364
|
-
const chunk = event.data.chunk;
|
|
4365
|
-
if (chunk.content) {
|
|
4366
|
-
if (!finalResponse) {
|
|
4367
|
-
finalResponse = "";
|
|
4368
|
-
}
|
|
4369
|
-
const normalizedContent = this._normalizeOutput(chunk.content);
|
|
4370
|
-
finalResponse += normalizedContent;
|
|
4371
|
-
logger.debug(
|
|
4372
|
-
`\u{1F4DD} Accumulated response length: ${finalResponse.length}`
|
|
4373
|
-
);
|
|
4374
|
-
}
|
|
4375
|
-
}
|
|
4376
|
-
yield event;
|
|
4377
|
-
if (event.event === "on_chain_end" && event.data?.output && !finalResponse) {
|
|
4378
|
-
const output = event.data.output;
|
|
4379
|
-
if (Array.isArray(output) && output.length > 0 && output[0]?.text) {
|
|
4380
|
-
finalResponse = output[0].text;
|
|
4381
|
-
} else if (typeof output === "string") {
|
|
4382
|
-
finalResponse = output;
|
|
4383
|
-
} else if (output && typeof output === "object" && "output" in output) {
|
|
4384
|
-
finalResponse = output.output;
|
|
4385
|
-
}
|
|
4386
|
-
}
|
|
4387
|
-
}
|
|
4388
|
-
if (schema && finalResponse) {
|
|
4389
|
-
logger.info("\u{1F527} Attempting structured output conversion...");
|
|
4390
|
-
try {
|
|
4391
|
-
let conversionCompleted = false;
|
|
4392
|
-
let conversionResult = null;
|
|
4393
|
-
let conversionError = null;
|
|
4394
|
-
this._attemptStructuredOutput(finalResponse, this.llm, schema).then((result) => {
|
|
4395
|
-
conversionCompleted = true;
|
|
4396
|
-
conversionResult = result;
|
|
4397
|
-
return result;
|
|
4398
|
-
}).catch((error) => {
|
|
4399
|
-
conversionCompleted = true;
|
|
4400
|
-
conversionError = error;
|
|
4401
|
-
throw error;
|
|
4402
|
-
});
|
|
4403
|
-
let progressCount = 0;
|
|
4404
|
-
while (!conversionCompleted) {
|
|
4405
|
-
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
4406
|
-
if (!conversionCompleted) {
|
|
4407
|
-
progressCount++;
|
|
4408
|
-
yield {
|
|
4409
|
-
event: "on_structured_output_progress",
|
|
4410
|
-
data: {
|
|
4411
|
-
message: `Converting to structured output... (${progressCount * 2}s)`,
|
|
4412
|
-
elapsed: progressCount * 2
|
|
4413
|
-
}
|
|
4414
|
-
};
|
|
4415
|
-
}
|
|
4416
|
-
}
|
|
4417
|
-
if (conversionError) {
|
|
4418
|
-
throw conversionError;
|
|
4419
|
-
}
|
|
4420
|
-
if (conversionResult) {
|
|
4421
|
-
yield {
|
|
4422
|
-
event: "on_structured_output",
|
|
4423
|
-
data: { output: conversionResult }
|
|
4424
|
-
};
|
|
4425
|
-
if (this.memoryEnabled) {
|
|
4426
|
-
this.addToHistory(
|
|
4427
|
-
new AIMessage(
|
|
4428
|
-
`Structured result: ${JSON.stringify(conversionResult)}`
|
|
4429
|
-
)
|
|
4430
|
-
);
|
|
4431
|
-
}
|
|
4432
|
-
logger.info("\u2705 Structured output successful");
|
|
4433
|
-
}
|
|
4434
|
-
} catch (e) {
|
|
4435
|
-
logger.warn(`\u26A0\uFE0F Structured output failed: ${e}`);
|
|
4436
|
-
yield {
|
|
4437
|
-
event: "on_structured_output_error",
|
|
4438
|
-
data: { error: e instanceof Error ? e.message : String(e) }
|
|
4439
|
-
};
|
|
4440
|
-
}
|
|
4441
|
-
} else if (this.memoryEnabled && finalResponse) {
|
|
4442
|
-
this.addToHistory(new AIMessage(finalResponse));
|
|
4443
|
-
}
|
|
4444
|
-
console.log("\n\n");
|
|
4445
|
-
logger.info(`\u{1F389} StreamEvents complete - ${eventCount} events emitted`);
|
|
4446
|
-
success = true;
|
|
4447
|
-
} catch (e) {
|
|
4448
|
-
logger.error(`\u274C Error during streamEvents: ${e}`);
|
|
4449
|
-
if (initializedHere && manage) {
|
|
4450
|
-
logger.info(
|
|
4451
|
-
"\u{1F9F9} Cleaning up resources after initialization error in streamEvents"
|
|
4452
|
-
);
|
|
4453
|
-
await this.close();
|
|
4454
|
-
}
|
|
4455
|
-
throw e;
|
|
4456
|
-
} finally {
|
|
4457
|
-
const executionTimeMs = Date.now() - startTime;
|
|
4458
|
-
let serverCount = 0;
|
|
4459
|
-
if (this.client) {
|
|
4460
|
-
serverCount = Object.keys(this.client.getAllActiveSessions()).length;
|
|
4461
|
-
} else if (this.connectors) {
|
|
4462
|
-
serverCount = this.connectors.length;
|
|
4463
|
-
}
|
|
4464
|
-
const conversationHistoryLength = this.memoryEnabled ? this.conversationHistory.length : 0;
|
|
4465
|
-
await this.telemetry.trackAgentExecution({
|
|
4466
|
-
executionMethod: "streamEvents",
|
|
4467
|
-
query,
|
|
4468
|
-
success,
|
|
4469
|
-
modelProvider: this.modelProvider,
|
|
4470
|
-
modelName: this.modelName,
|
|
4471
|
-
serverCount,
|
|
4472
|
-
serverIdentifiers: this.connectors.map(
|
|
4473
|
-
(connector) => connector.publicIdentifier
|
|
4474
|
-
),
|
|
4475
|
-
totalToolsAvailable: this._tools.length,
|
|
4476
|
-
toolsAvailableNames: this._tools.map((t) => t.name),
|
|
4477
|
-
maxStepsConfigured: this.maxSteps,
|
|
4478
|
-
memoryEnabled: this.memoryEnabled,
|
|
4479
|
-
useServerManager: this.useServerManager,
|
|
4480
|
-
maxStepsUsed: steps ?? null,
|
|
4481
|
-
manageConnector: manage ?? true,
|
|
4482
|
-
externalHistoryUsed: history !== void 0,
|
|
4483
|
-
response: `[STREAMED RESPONSE - ${totalResponseLength} chars]`,
|
|
4484
|
-
executionTimeMs,
|
|
4485
|
-
errorType: success ? null : "streaming_error",
|
|
4486
|
-
conversationHistoryLength
|
|
4487
|
-
});
|
|
4488
|
-
if (manage && !this.client && initializedHere) {
|
|
4489
|
-
logger.info("\u{1F9F9} Closing agent after streamEvents completion");
|
|
4490
|
-
await this.close();
|
|
4491
|
-
}
|
|
4492
|
-
}
|
|
4493
|
-
}
|
|
4494
|
-
/**
|
|
4495
|
-
* Attempt to create structured output from raw result with validation and retry logic.
|
|
4496
|
-
*
|
|
4497
|
-
* @param rawResult - The raw text result from the agent
|
|
4498
|
-
* @param llm - LLM to use for structured output
|
|
4499
|
-
* @param outputSchema - The Zod schema to validate against
|
|
4500
|
-
*/
|
|
4501
|
-
async _attemptStructuredOutput(rawResult, llm, outputSchema) {
|
|
4502
|
-
logger.info(
|
|
4503
|
-
`\u{1F504} Attempting structured output with schema: ${JSON.stringify(outputSchema, null, 2)}`
|
|
4504
|
-
);
|
|
4505
|
-
logger.info(`\u{1F504} Raw result: ${JSON.stringify(rawResult, null, 2)}`);
|
|
4506
|
-
let structuredLlm = null;
|
|
4507
|
-
let schemaDescription = "";
|
|
4508
|
-
logger.debug(
|
|
4509
|
-
`\u{1F504} Structured output requested, schema: ${JSON.stringify(toJSONSchema2(outputSchema), null, 2)}`
|
|
4510
|
-
);
|
|
4511
|
-
if (llm && "withStructuredOutput" in llm && typeof llm.withStructuredOutput === "function") {
|
|
4512
|
-
structuredLlm = llm.withStructuredOutput(outputSchema);
|
|
4513
|
-
} else if (llm) {
|
|
4514
|
-
structuredLlm = llm;
|
|
4515
|
-
} else {
|
|
4516
|
-
throw new Error("LLM is required for structured output");
|
|
4517
|
-
}
|
|
4518
|
-
const jsonSchema = toJSONSchema2(outputSchema);
|
|
4519
|
-
const { $schema, additionalProperties, ...cleanSchema } = jsonSchema;
|
|
4520
|
-
schemaDescription = JSON.stringify(cleanSchema, null, 2);
|
|
4521
|
-
logger.info(`\u{1F504} Schema description: ${schemaDescription}`);
|
|
4522
|
-
let textContent = "";
|
|
4523
|
-
if (typeof rawResult === "string") {
|
|
4524
|
-
textContent = rawResult;
|
|
4525
|
-
} else if (rawResult && typeof rawResult === "object") {
|
|
4526
|
-
textContent = JSON.stringify(rawResult);
|
|
4527
|
-
}
|
|
4528
|
-
logger.info("rawResult", rawResult);
|
|
4529
|
-
if (!textContent) {
|
|
4530
|
-
textContent = JSON.stringify(rawResult);
|
|
4531
|
-
}
|
|
4532
|
-
const maxRetries = 3;
|
|
4533
|
-
let lastError = "";
|
|
4534
|
-
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
4535
|
-
logger.info(`\u{1F504} Structured output attempt ${attempt}/${maxRetries}`);
|
|
4536
|
-
let formatPrompt = `
|
|
4537
|
-
Please format the following information according to the EXACT schema specified below.
|
|
4538
|
-
You must use the exact field names and types as shown in the schema.
|
|
4539
|
-
|
|
4540
|
-
Required schema format:
|
|
4541
|
-
${schemaDescription}
|
|
4542
|
-
|
|
4543
|
-
Content to extract from:
|
|
4544
|
-
${textContent}
|
|
4545
|
-
|
|
4546
|
-
IMPORTANT:
|
|
4547
|
-
- Use ONLY the field names specified in the schema
|
|
4548
|
-
- Match the data types exactly (string, number, boolean, array, etc.)
|
|
4549
|
-
- Include ALL required fields
|
|
4550
|
-
- Return valid JSON that matches the schema structure exactly
|
|
4551
|
-
- For missing data: use null for nullable fields, omit optional fields entirely
|
|
4552
|
-
- Do NOT use empty strings ("") or zero (0) as placeholders for missing data
|
|
4553
|
-
`;
|
|
4554
|
-
if (attempt > 1) {
|
|
4555
|
-
formatPrompt += `
|
|
4556
|
-
|
|
4557
|
-
PREVIOUS ATTEMPT FAILED with error: ${lastError}
|
|
4558
|
-
Please fix the issues mentioned above and ensure the output matches the schema exactly.
|
|
4559
|
-
`;
|
|
4560
|
-
}
|
|
4561
|
-
try {
|
|
4562
|
-
logger.info(
|
|
4563
|
-
`\u{1F504} Structured output attempt ${attempt} - using streaming approach`
|
|
4564
|
-
);
|
|
4565
|
-
const contentPreview = textContent.length > 300 ? `${textContent.slice(0, 300)}...` : textContent;
|
|
4566
|
-
logger.info(
|
|
4567
|
-
`\u{1F504} Content being formatted (${textContent.length} chars): ${contentPreview}`
|
|
4568
|
-
);
|
|
4569
|
-
logger.info(
|
|
4570
|
-
`\u{1F504} Full format prompt (${formatPrompt.length} chars):
|
|
4571
|
-
${formatPrompt}`
|
|
4572
|
-
);
|
|
4573
|
-
const stream = await structuredLlm.stream(formatPrompt);
|
|
4574
|
-
let structuredResult = null;
|
|
4575
|
-
let chunkCount = 0;
|
|
4576
|
-
for await (const chunk of stream) {
|
|
4577
|
-
chunkCount++;
|
|
4578
|
-
logger.debug(
|
|
4579
|
-
`Chunk ${chunkCount}: ${JSON.stringify(chunk, null, 2)}`
|
|
4580
|
-
);
|
|
4581
|
-
if (typeof chunk === "string") {
|
|
4582
|
-
try {
|
|
4583
|
-
structuredResult = JSON.parse(chunk);
|
|
4584
|
-
} catch (e) {
|
|
4585
|
-
logger.warn(`\u{1F504} Failed to parse string chunk as JSON: ${chunk}`);
|
|
4586
|
-
}
|
|
4587
|
-
} else if (chunk && typeof chunk === "object") {
|
|
4588
|
-
structuredResult = chunk;
|
|
4589
|
-
} else {
|
|
4590
|
-
try {
|
|
4591
|
-
structuredResult = JSON.parse(String(chunk));
|
|
4592
|
-
} catch (e) {
|
|
4593
|
-
logger.warn(`\u{1F504} Failed to parse chunk as JSON: ${chunk}`);
|
|
4594
|
-
}
|
|
4595
|
-
}
|
|
4596
|
-
if (chunkCount % 10 === 0) {
|
|
4597
|
-
logger.debug(
|
|
4598
|
-
`\u{1F504} Structured output streaming: ${chunkCount} chunks`
|
|
4599
|
-
);
|
|
4600
|
-
}
|
|
4601
|
-
}
|
|
4602
|
-
logger.info(
|
|
4603
|
-
`\u{1F504} Structured result attempt ${attempt}: ${JSON.stringify(structuredResult, null, 2)}`
|
|
4604
|
-
);
|
|
4605
|
-
if (!structuredResult) {
|
|
4606
|
-
throw new Error("No structured result received from stream");
|
|
4607
|
-
}
|
|
4608
|
-
const validatedResult = this._validateStructuredResult(
|
|
4609
|
-
structuredResult,
|
|
4610
|
-
outputSchema
|
|
4611
|
-
);
|
|
4612
|
-
logger.info(`\u2705 Structured output successful on attempt ${attempt}`);
|
|
4613
|
-
return validatedResult;
|
|
4614
|
-
} catch (e) {
|
|
4615
|
-
lastError = e instanceof Error ? e.message : String(e);
|
|
4616
|
-
logger.warn(
|
|
4617
|
-
`\u26A0\uFE0F Structured output attempt ${attempt} failed: ${lastError}`
|
|
4618
|
-
);
|
|
4619
|
-
if (attempt === maxRetries) {
|
|
4620
|
-
logger.error(
|
|
4621
|
-
`\u274C All ${maxRetries} structured output attempts failed`
|
|
4622
|
-
);
|
|
4623
|
-
throw new Error(
|
|
4624
|
-
`Failed to generate valid structured output after ${maxRetries} attempts. Last error: ${lastError}`
|
|
4625
|
-
);
|
|
4626
|
-
}
|
|
4627
|
-
continue;
|
|
4628
|
-
}
|
|
4629
|
-
}
|
|
4630
|
-
throw new Error("Unexpected error in structured output generation");
|
|
4631
|
-
}
|
|
4632
|
-
/**
|
|
4633
|
-
* Validate the structured result against the schema with detailed error reporting
|
|
4634
|
-
*/
|
|
4635
|
-
_validateStructuredResult(structuredResult, outputSchema) {
|
|
4636
|
-
try {
|
|
4637
|
-
const validatedResult = outputSchema.parse(structuredResult);
|
|
4638
|
-
const schemaType = outputSchema;
|
|
4639
|
-
if (schemaType._def && schemaType._def.shape) {
|
|
4640
|
-
for (const [fieldName, fieldSchema] of Object.entries(
|
|
4641
|
-
schemaType._def.shape
|
|
4642
|
-
)) {
|
|
4643
|
-
const field = fieldSchema;
|
|
4644
|
-
const isOptional = field.isOptional?.() ?? field._def?.typeName === "ZodOptional";
|
|
4645
|
-
const isNullable = field.isNullable?.() ?? field._def?.typeName === "ZodNullable";
|
|
4646
|
-
if (!isOptional && !isNullable) {
|
|
4647
|
-
const value = validatedResult[fieldName];
|
|
4648
|
-
if (value === null || value === void 0 || typeof value === "string" && !value.trim() || Array.isArray(value) && value.length === 0) {
|
|
4649
|
-
throw new Error(
|
|
4650
|
-
`Required field '${fieldName}' is missing or empty`
|
|
4651
|
-
);
|
|
4652
|
-
}
|
|
4653
|
-
}
|
|
4654
|
-
}
|
|
4655
|
-
}
|
|
4656
|
-
return validatedResult;
|
|
4657
|
-
} catch (e) {
|
|
4658
|
-
logger.debug(`Validation details: ${e}`);
|
|
4659
|
-
throw e;
|
|
4660
|
-
}
|
|
4661
|
-
}
|
|
4662
|
-
/**
|
|
4663
|
-
* Enhance the query with schema information to make the agent aware of required fields.
|
|
4664
|
-
*/
|
|
4665
|
-
_enhanceQueryWithSchema(query, outputSchema) {
|
|
4666
|
-
try {
|
|
4667
|
-
const jsonSchema = toJSONSchema2(outputSchema);
|
|
4668
|
-
const { $schema, additionalProperties, ...cleanSchema } = jsonSchema;
|
|
4669
|
-
const schemaDescription = JSON.stringify(cleanSchema, null, 2);
|
|
4670
|
-
const enhancedQuery = `
|
|
4671
|
-
${query}
|
|
4672
|
-
|
|
4673
|
-
IMPORTANT: Your response must include sufficient information to populate the following structured output:
|
|
4674
|
-
|
|
4675
|
-
${schemaDescription}
|
|
4676
|
-
|
|
4677
|
-
Make sure you gather ALL the required information during your task execution.
|
|
4678
|
-
If any required information is missing, continue working to find it.
|
|
4679
|
-
`;
|
|
4680
|
-
return enhancedQuery;
|
|
4681
|
-
} catch (e) {
|
|
4682
|
-
logger.warn(`Could not extract schema details: ${e}`);
|
|
4683
|
-
return query;
|
|
4684
|
-
}
|
|
4685
|
-
}
|
|
4686
|
-
};
|
|
4687
|
-
|
|
4688
|
-
export {
|
|
4689
|
-
BaseAdapter,
|
|
4690
|
-
LangChainAdapter,
|
|
4691
|
-
BaseCodeExecutor,
|
|
4692
|
-
E2BCodeExecutor,
|
|
4693
|
-
isVMAvailable,
|
|
4694
|
-
VMCodeExecutor,
|
|
4695
|
-
StdioConnector,
|
|
4696
|
-
loadConfigFile,
|
|
4697
|
-
MCPClient,
|
|
4698
|
-
AcquireActiveMCPServerTool,
|
|
4699
|
-
AddMCPServerFromConfigTool,
|
|
4700
|
-
ConnectMCPServerTool,
|
|
4701
|
-
ListMCPServersTool,
|
|
4702
|
-
ReleaseMCPServerConnectionTool,
|
|
4703
|
-
ServerManager,
|
|
4704
|
-
ObservabilityManager,
|
|
4705
|
-
RemoteAgent,
|
|
4706
|
-
parseLLMString,
|
|
4707
|
-
createLLMFromString,
|
|
4708
|
-
isValidLLMString,
|
|
4709
|
-
getSupportedProviders,
|
|
4710
|
-
MCPAgent
|
|
4711
|
-
};
|