@dexto/core 1.6.26 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/dist/agent/DextoAgent.cjs +75 -90
  2. package/dist/agent/DextoAgent.d.ts +7 -7
  3. package/dist/agent/DextoAgent.d.ts.map +1 -1
  4. package/dist/agent/DextoAgent.js +76 -91
  5. package/dist/agent/error-codes.cjs +1 -0
  6. package/dist/agent/error-codes.d.ts +1 -0
  7. package/dist/agent/error-codes.d.ts.map +1 -1
  8. package/dist/agent/error-codes.js +1 -0
  9. package/dist/agent/errors.cjs +13 -0
  10. package/dist/agent/errors.d.ts +6 -0
  11. package/dist/agent/errors.d.ts.map +1 -1
  12. package/dist/agent/errors.js +13 -0
  13. package/dist/agent/index.d.ts +1 -0
  14. package/dist/agent/index.d.ts.map +1 -1
  15. package/dist/agent/schemas.cjs +5 -5
  16. package/dist/agent/schemas.d.ts +69 -980
  17. package/dist/agent/schemas.d.ts.map +1 -1
  18. package/dist/agent/schemas.js +5 -5
  19. package/dist/agent/types.d.ts +11 -0
  20. package/dist/agent/types.d.ts.map +1 -1
  21. package/dist/approval/factory.cjs +1 -0
  22. package/dist/approval/factory.d.ts.map +1 -1
  23. package/dist/approval/factory.js +1 -0
  24. package/dist/approval/manager.cjs +19 -6
  25. package/dist/approval/manager.d.ts +6 -0
  26. package/dist/approval/manager.d.ts.map +1 -1
  27. package/dist/approval/manager.js +19 -6
  28. package/dist/approval/schemas.cjs +16 -6
  29. package/dist/approval/schemas.d.ts +373 -1044
  30. package/dist/approval/schemas.d.ts.map +1 -1
  31. package/dist/approval/schemas.js +16 -6
  32. package/dist/approval/session-approval-store.cjs +1 -1
  33. package/dist/approval/session-approval-store.d.ts +12 -30
  34. package/dist/approval/session-approval-store.d.ts.map +1 -1
  35. package/dist/approval/session-approval-store.js +1 -1
  36. package/dist/events/index.cjs +210 -75
  37. package/dist/events/index.d.ts +44 -181
  38. package/dist/events/index.d.ts.map +1 -1
  39. package/dist/events/index.js +206 -74
  40. package/dist/hooks/manager.cjs +5 -2
  41. package/dist/hooks/manager.d.ts +2 -0
  42. package/dist/hooks/manager.d.ts.map +1 -1
  43. package/dist/hooks/manager.js +5 -2
  44. package/dist/hooks/types.d.ts +3 -0
  45. package/dist/hooks/types.d.ts.map +1 -1
  46. package/dist/index.browser.d.ts +1 -0
  47. package/dist/index.browser.d.ts.map +1 -1
  48. package/dist/index.cjs +3 -1
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +1 -0
  52. package/dist/llm/executor/turn-executor.cjs +8 -4
  53. package/dist/llm/executor/turn-executor.d.ts +3 -1
  54. package/dist/llm/executor/turn-executor.d.ts.map +1 -1
  55. package/dist/llm/executor/turn-executor.js +8 -4
  56. package/dist/llm/providers/local/schemas.d.ts +102 -262
  57. package/dist/llm/providers/local/schemas.d.ts.map +1 -1
  58. package/dist/llm/registry/index.d.ts +5 -3
  59. package/dist/llm/registry/index.d.ts.map +1 -1
  60. package/dist/llm/resolver.cjs +0 -1
  61. package/dist/llm/resolver.d.ts.map +1 -1
  62. package/dist/llm/resolver.js +0 -1
  63. package/dist/llm/schemas.d.ts +95 -199
  64. package/dist/llm/schemas.d.ts.map +1 -1
  65. package/dist/llm/services/vercel.cjs +29 -6
  66. package/dist/llm/services/vercel.d.ts +3 -0
  67. package/dist/llm/services/vercel.d.ts.map +1 -1
  68. package/dist/llm/services/vercel.js +27 -5
  69. package/dist/logger/default-logger-factory.d.ts +28 -256
  70. package/dist/logger/default-logger-factory.d.ts.map +1 -1
  71. package/dist/logger/v2/schemas.d.ts +18 -120
  72. package/dist/logger/v2/schemas.d.ts.map +1 -1
  73. package/dist/mcp/manager.cjs +7 -2
  74. package/dist/mcp/manager.d.ts +3 -1
  75. package/dist/mcp/manager.d.ts.map +1 -1
  76. package/dist/mcp/manager.js +7 -2
  77. package/dist/mcp/mcp-client.cjs +71 -62
  78. package/dist/mcp/mcp-client.d.ts +3 -2
  79. package/dist/mcp/mcp-client.d.ts.map +1 -1
  80. package/dist/mcp/mcp-client.js +71 -62
  81. package/dist/mcp/schemas.cjs +4 -4
  82. package/dist/mcp/schemas.d.ts +77 -270
  83. package/dist/mcp/schemas.d.ts.map +1 -1
  84. package/dist/mcp/schemas.js +4 -4
  85. package/dist/memory/schemas.d.ts +37 -117
  86. package/dist/memory/schemas.d.ts.map +1 -1
  87. package/dist/prompts/schemas.d.ts +46 -190
  88. package/dist/prompts/schemas.d.ts.map +1 -1
  89. package/dist/resources/handlers/filesystem-handler.cjs +22 -3
  90. package/dist/resources/handlers/filesystem-handler.d.ts.map +1 -1
  91. package/dist/resources/handlers/filesystem-handler.js +22 -3
  92. package/dist/resources/schemas.d.ts +14 -68
  93. package/dist/resources/schemas.d.ts.map +1 -1
  94. package/dist/runtime/host-runtime.cjs +163 -0
  95. package/dist/runtime/host-runtime.d.ts +19 -0
  96. package/dist/runtime/host-runtime.d.ts.map +1 -0
  97. package/dist/runtime/host-runtime.js +133 -0
  98. package/dist/runtime/index.cjs +42 -0
  99. package/dist/runtime/index.d.ts +2 -0
  100. package/dist/runtime/index.d.ts.map +1 -0
  101. package/dist/runtime/index.js +21 -0
  102. package/dist/runtime/run-context.cjs +53 -0
  103. package/dist/runtime/run-context.d.ts +13 -0
  104. package/dist/runtime/run-context.d.ts.map +1 -0
  105. package/dist/runtime/run-context.js +34 -0
  106. package/dist/session/chat-session.cjs +30 -32
  107. package/dist/session/chat-session.d.ts +5 -15
  108. package/dist/session/chat-session.d.ts.map +1 -1
  109. package/dist/session/chat-session.js +31 -33
  110. package/dist/session/error-codes.cjs +1 -0
  111. package/dist/session/error-codes.d.ts +2 -1
  112. package/dist/session/error-codes.d.ts.map +1 -1
  113. package/dist/session/error-codes.js +1 -0
  114. package/dist/session/errors.cjs +13 -0
  115. package/dist/session/errors.d.ts +6 -0
  116. package/dist/session/errors.d.ts.map +1 -1
  117. package/dist/session/errors.js +13 -0
  118. package/dist/session/schemas.d.ts +1 -7
  119. package/dist/session/schemas.d.ts.map +1 -1
  120. package/dist/systemPrompt/schemas.cjs +2 -10
  121. package/dist/systemPrompt/schemas.d.ts +53 -343
  122. package/dist/systemPrompt/schemas.d.ts.map +1 -1
  123. package/dist/systemPrompt/schemas.js +2 -10
  124. package/dist/telemetry/decorators.cjs +75 -57
  125. package/dist/telemetry/decorators.d.ts +2 -0
  126. package/dist/telemetry/decorators.d.ts.map +1 -1
  127. package/dist/telemetry/decorators.js +75 -57
  128. package/dist/telemetry/schemas.cjs +1 -1
  129. package/dist/telemetry/schemas.d.ts +9 -44
  130. package/dist/telemetry/schemas.d.ts.map +1 -1
  131. package/dist/telemetry/schemas.js +1 -1
  132. package/dist/telemetry/utils.cjs +9 -6
  133. package/dist/telemetry/utils.d.ts +3 -0
  134. package/dist/telemetry/utils.d.ts.map +1 -1
  135. package/dist/telemetry/utils.js +9 -6
  136. package/dist/tools/schemas.cjs +1 -1
  137. package/dist/tools/schemas.d.ts +19 -62
  138. package/dist/tools/schemas.d.ts.map +1 -1
  139. package/dist/tools/schemas.js +1 -1
  140. package/dist/tools/session-tool-preferences-store.d.ts +3 -9
  141. package/dist/tools/session-tool-preferences-store.d.ts.map +1 -1
  142. package/dist/tools/tool-manager.cjs +92 -36
  143. package/dist/tools/tool-manager.d.ts +12 -3
  144. package/dist/tools/tool-manager.d.ts.map +1 -1
  145. package/dist/tools/tool-manager.js +92 -36
  146. package/dist/tools/types.d.ts +7 -1
  147. package/dist/tools/types.d.ts.map +1 -1
  148. package/dist/utils/result.cjs +11 -8
  149. package/dist/utils/result.d.ts +6 -6
  150. package/dist/utils/result.d.ts.map +1 -1
  151. package/dist/utils/result.js +12 -9
  152. package/dist/utils/schema.cjs +6 -2
  153. package/dist/utils/schema.d.ts +2 -7
  154. package/dist/utils/schema.d.ts.map +1 -1
  155. package/dist/utils/schema.js +6 -2
  156. package/dist/utils/zod-schema-converter.cjs +66 -66
  157. package/dist/utils/zod-schema-converter.d.ts +3 -2
  158. package/dist/utils/zod-schema-converter.d.ts.map +1 -1
  159. package/dist/utils/zod-schema-converter.js +66 -66
  160. package/package.json +4 -5
@@ -22,6 +22,7 @@ __export(mcp_client_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(mcp_client_exports);
24
24
  var import_client = require("@modelcontextprotocol/sdk/client/index.js");
25
+ var import_async_hooks = require("async_hooks");
25
26
  var import_stdio = require("@modelcontextprotocol/sdk/client/stdio.js");
26
27
  var import_sse = require("@modelcontextprotocol/sdk/client/sse.js");
27
28
  var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
@@ -49,6 +50,7 @@ function buildClientCapabilities() {
49
50
  };
50
51
  }
51
52
  class DextoMcpClient extends import_events.EventEmitter {
53
+ toolInvocationContext = new import_async_hooks.AsyncLocalStorage();
52
54
  client = null;
53
55
  transport = null;
54
56
  isConnected = false;
@@ -322,71 +324,75 @@ class DextoMcpClient extends import_events.EventEmitter {
322
324
  * @param args Tool arguments
323
325
  * @returns Result of the tool execution
324
326
  */
325
- async callTool(name, args) {
327
+ async callTool(name, args, invocation) {
326
328
  this.ensureConnected();
327
- const shouldTrace = (0, import_utils.hasActiveTelemetry)();
328
- const tracer = shouldTrace ? import_api.trace.getTracer("dexto") : null;
329
- const span = tracer?.startSpan(`mcp.tool.${name}`, {
330
- kind: import_api.SpanKind.CLIENT
331
- });
332
- try {
333
- if (span) {
334
- const ctx = import_api.trace.setSpan(import_api.context.active(), span);
335
- (0, import_utils.addBaggageAttributesToSpan)(span, ctx, this.logger);
336
- span.setAttribute("tool.name", name);
337
- span.setAttribute("tool.server", this.serverAlias || "unknown");
338
- span.setAttribute("tool.timeout", this.timeout);
339
- span.setAttribute("tool.arguments", (0, import_safe_stringify.safeStringify)(args, 4096));
340
- }
341
- this.logger.debug(`Calling tool '${name}' with args: ${JSON.stringify(args, null, 2)}`);
342
- let toolArgs = args;
343
- if (typeof args === "string") {
344
- try {
345
- toolArgs = JSON.parse(args);
346
- } catch {
347
- toolArgs = { input: args };
329
+ return await this.toolInvocationContext.run(invocation, async () => {
330
+ const shouldTrace = (0, import_utils.hasActiveTelemetry)();
331
+ const tracer = shouldTrace ? import_api.trace.getTracer("dexto") : null;
332
+ const span = tracer?.startSpan(`mcp.tool.${name}`, {
333
+ kind: import_api.SpanKind.CLIENT
334
+ });
335
+ try {
336
+ if (span) {
337
+ const ctx = import_api.trace.setSpan(import_api.context.active(), span);
338
+ (0, import_utils.addBaggageAttributesToSpan)(span, ctx, this.logger);
339
+ span.setAttribute("tool.name", name);
340
+ span.setAttribute("tool.server", this.serverAlias || "unknown");
341
+ span.setAttribute("tool.timeout", this.timeout);
342
+ span.setAttribute("tool.arguments", (0, import_safe_stringify.safeStringify)(args, 4096));
348
343
  }
349
- }
350
- this.logger.debug(`Using timeout: ${this.timeout}`);
351
- const result = await this.client.callTool(
352
- { name, arguments: toolArgs },
353
- void 0,
354
- // resultSchema (optional)
355
- { timeout: this.timeout }
356
- // Use server-specific timeout, default 1 minute
357
- );
358
- const logResult = JSON.stringify(
359
- result,
360
- (key, value) => {
361
- if (key === "data" && typeof value === "string" && value.length > 100) {
362
- return `[Base64 data: ${value.length} chars]`;
344
+ this.logger.debug(
345
+ `Calling tool '${name}' with args: ${JSON.stringify(args, null, 2)}`
346
+ );
347
+ let toolArgs = args;
348
+ if (typeof args === "string") {
349
+ try {
350
+ toolArgs = JSON.parse(args);
351
+ } catch {
352
+ toolArgs = { input: args };
363
353
  }
364
- return value;
365
- },
366
- 2
367
- );
368
- this.logger.debug(`Tool '${name}' result: ${logResult}`);
369
- if (span) {
370
- span.setAttribute("tool.result", (0, import_safe_stringify.safeStringify)(result, 4096));
371
- span.setStatus({ code: import_api.SpanStatusCode.OK });
372
- }
373
- if (result === null || result === void 0) {
374
- return "Tool executed successfully with no result data.";
375
- }
376
- return result;
377
- } catch (error) {
378
- this.logger.error(`Tool call '${name}' failed: ${JSON.stringify(error, null, 2)}`);
379
- if (span) {
380
- span.recordException(error);
381
- span.setStatus({
382
- code: import_api.SpanStatusCode.ERROR,
383
- message: error instanceof Error ? error.message : String(error)
384
- });
354
+ }
355
+ this.logger.debug(`Using timeout: ${this.timeout}`);
356
+ const result = await this.client.callTool(
357
+ { name, arguments: toolArgs },
358
+ void 0,
359
+ // resultSchema (optional)
360
+ { timeout: this.timeout }
361
+ // Use server-specific timeout, default 1 minute
362
+ );
363
+ const logResult = JSON.stringify(
364
+ result,
365
+ (key, value) => {
366
+ if (key === "data" && typeof value === "string" && value.length > 100) {
367
+ return `[Base64 data: ${value.length} chars]`;
368
+ }
369
+ return value;
370
+ },
371
+ 2
372
+ );
373
+ this.logger.debug(`Tool '${name}' result: ${logResult}`);
374
+ if (span) {
375
+ span.setAttribute("tool.result", (0, import_safe_stringify.safeStringify)(result, 4096));
376
+ span.setStatus({ code: import_api.SpanStatusCode.OK });
377
+ }
378
+ if (result === null || result === void 0) {
379
+ return "Tool executed successfully with no result data.";
380
+ }
381
+ return result;
382
+ } catch (error) {
383
+ this.logger.error(`Tool call '${name}' failed: ${JSON.stringify(error, null, 2)}`);
384
+ if (span) {
385
+ span.recordException(error);
386
+ span.setStatus({
387
+ code: import_api.SpanStatusCode.ERROR,
388
+ message: error instanceof Error ? error.message : String(error)
389
+ });
390
+ }
391
+ return `Error executing tool '${name}': ${error instanceof Error ? error.message : String(error)}`;
392
+ } finally {
393
+ span?.end();
385
394
  }
386
- return `Error executing tool '${name}': ${error instanceof Error ? error.message : String(error)}`;
387
- } finally {
388
- span?.end();
389
- }
395
+ });
390
396
  }
391
397
  /**
392
398
  * Get the list of tools provided by this client
@@ -657,10 +663,13 @@ class DextoMcpClient extends import_events.EventEmitter {
657
663
  );
658
664
  return { action: "decline" };
659
665
  }
666
+ const invocation = this.toolInvocationContext.getStore();
660
667
  const response = await this.approvalManager.requestElicitation({
661
668
  schema: params.requestedSchema,
662
669
  prompt: params.message,
663
- serverName: this.serverAlias || "unknown"
670
+ serverName: this.serverAlias || "unknown",
671
+ ...invocation?.sessionId !== void 0 ? { sessionId: invocation.sessionId } : {},
672
+ ...invocation?.runContext?.hostRuntime !== void 0 ? { hostRuntime: invocation.runContext.hostRuntime } : {}
664
673
  });
665
674
  if (response.status === import_types2.ApprovalStatus.APPROVED && response.data) {
666
675
  const formData = response.data && typeof response.data === "object" && "formData" in response.data ? response.data.formData : {};
@@ -3,13 +3,14 @@ import { EventEmitter } from 'events';
3
3
  import type { Logger } from '../logger/v2/types.js';
4
4
  import type { ApprovalManager } from '../approval/manager.js';
5
5
  import type { ValidatedMcpServerConfig } from './schemas.js';
6
- import type { ToolSet } from '../tools/types.js';
6
+ import type { ToolExecutionContextBase, ToolSet } from '../tools/types.js';
7
7
  import type { McpClient, MCPResourceSummary, McpAuthProviderFactory } from './types.js';
8
8
  import type { GetPromptResult, ReadResourceResult, Prompt } from '@modelcontextprotocol/sdk/types.js';
9
9
  /**
10
10
  * Wrapper on top of Client class provided in model context protocol SDK, to add additional metadata about the server
11
11
  */
12
12
  export declare class DextoMcpClient extends EventEmitter implements McpClient {
13
+ private readonly toolInvocationContext;
13
14
  private client;
14
15
  private transport;
15
16
  private isConnected;
@@ -52,7 +53,7 @@ export declare class DextoMcpClient extends EventEmitter implements McpClient {
52
53
  * @param args Tool arguments
53
54
  * @returns Result of the tool execution
54
55
  */
55
- callTool(name: string, args: any): Promise<any>;
56
+ callTool(name: string, args: any, invocation?: Pick<ToolExecutionContextBase, 'sessionId' | 'runContext'>): Promise<any>;
56
57
  /**
57
58
  * Get the list of tools provided by this client
58
59
  * @returns Array of available tools
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-client.d.ts","sourceRoot":"","sources":["../../src/mcp/mcp-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAKnE,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEpD,OAA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ol/sdk/client/index.js";
3
+ import { AsyncLocalStorage } from "async_hooks";
3
4
  import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
4
5
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
5
6
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
@@ -31,6 +32,7 @@ function buildClientCapabilities() {
31
32
  };
32
33
  }
33
34
  class DextoMcpClient extends EventEmitter {
35
+ toolInvocationContext = new AsyncLocalStorage();
34
36
  client = null;
35
37
  transport = null;
36
38
  isConnected = false;
@@ -304,71 +306,75 @@ class DextoMcpClient extends EventEmitter {
304
306
  * @param args Tool arguments
305
307
  * @returns Result of the tool execution
306
308
  */
307
- async callTool(name, args) {
309
+ async callTool(name, args, invocation) {
308
310
  this.ensureConnected();
309
- const shouldTrace = hasActiveTelemetry();
310
- const tracer = shouldTrace ? trace.getTracer("dexto") : null;
311
- const span = tracer?.startSpan(`mcp.tool.${name}`, {
312
- kind: SpanKind.CLIENT
313
- });
314
- try {
315
- if (span) {
316
- const ctx = trace.setSpan(context.active(), span);
317
- addBaggageAttributesToSpan(span, ctx, this.logger);
318
- span.setAttribute("tool.name", name);
319
- span.setAttribute("tool.server", this.serverAlias || "unknown");
320
- span.setAttribute("tool.timeout", this.timeout);
321
- span.setAttribute("tool.arguments", safeStringify(args, 4096));
322
- }
323
- this.logger.debug(`Calling tool '${name}' with args: ${JSON.stringify(args, null, 2)}`);
324
- let toolArgs = args;
325
- if (typeof args === "string") {
326
- try {
327
- toolArgs = JSON.parse(args);
328
- } catch {
329
- toolArgs = { input: args };
311
+ return await this.toolInvocationContext.run(invocation, async () => {
312
+ const shouldTrace = hasActiveTelemetry();
313
+ const tracer = shouldTrace ? trace.getTracer("dexto") : null;
314
+ const span = tracer?.startSpan(`mcp.tool.${name}`, {
315
+ kind: SpanKind.CLIENT
316
+ });
317
+ try {
318
+ if (span) {
319
+ const ctx = trace.setSpan(context.active(), span);
320
+ addBaggageAttributesToSpan(span, ctx, this.logger);
321
+ span.setAttribute("tool.name", name);
322
+ span.setAttribute("tool.server", this.serverAlias || "unknown");
323
+ span.setAttribute("tool.timeout", this.timeout);
324
+ span.setAttribute("tool.arguments", safeStringify(args, 4096));
330
325
  }
331
- }
332
- this.logger.debug(`Using timeout: ${this.timeout}`);
333
- const result = await this.client.callTool(
334
- { name, arguments: toolArgs },
335
- void 0,
336
- // resultSchema (optional)
337
- { timeout: this.timeout }
338
- // Use server-specific timeout, default 1 minute
339
- );
340
- const logResult = JSON.stringify(
341
- result,
342
- (key, value) => {
343
- if (key === "data" && typeof value === "string" && value.length > 100) {
344
- return `[Base64 data: ${value.length} chars]`;
326
+ this.logger.debug(
327
+ `Calling tool '${name}' with args: ${JSON.stringify(args, null, 2)}`
328
+ );
329
+ let toolArgs = args;
330
+ if (typeof args === "string") {
331
+ try {
332
+ toolArgs = JSON.parse(args);
333
+ } catch {
334
+ toolArgs = { input: args };
345
335
  }
346
- return value;
347
- },
348
- 2
349
- );
350
- this.logger.debug(`Tool '${name}' result: ${logResult}`);
351
- if (span) {
352
- span.setAttribute("tool.result", safeStringify(result, 4096));
353
- span.setStatus({ code: SpanStatusCode.OK });
354
- }
355
- if (result === null || result === void 0) {
356
- return "Tool executed successfully with no result data.";
357
- }
358
- return result;
359
- } catch (error) {
360
- this.logger.error(`Tool call '${name}' failed: ${JSON.stringify(error, null, 2)}`);
361
- if (span) {
362
- span.recordException(error);
363
- span.setStatus({
364
- code: SpanStatusCode.ERROR,
365
- message: error instanceof Error ? error.message : String(error)
366
- });
336
+ }
337
+ this.logger.debug(`Using timeout: ${this.timeout}`);
338
+ const result = await this.client.callTool(
339
+ { name, arguments: toolArgs },
340
+ void 0,
341
+ // resultSchema (optional)
342
+ { timeout: this.timeout }
343
+ // Use server-specific timeout, default 1 minute
344
+ );
345
+ const logResult = JSON.stringify(
346
+ result,
347
+ (key, value) => {
348
+ if (key === "data" && typeof value === "string" && value.length > 100) {
349
+ return `[Base64 data: ${value.length} chars]`;
350
+ }
351
+ return value;
352
+ },
353
+ 2
354
+ );
355
+ this.logger.debug(`Tool '${name}' result: ${logResult}`);
356
+ if (span) {
357
+ span.setAttribute("tool.result", safeStringify(result, 4096));
358
+ span.setStatus({ code: SpanStatusCode.OK });
359
+ }
360
+ if (result === null || result === void 0) {
361
+ return "Tool executed successfully with no result data.";
362
+ }
363
+ return result;
364
+ } catch (error) {
365
+ this.logger.error(`Tool call '${name}' failed: ${JSON.stringify(error, null, 2)}`);
366
+ if (span) {
367
+ span.recordException(error);
368
+ span.setStatus({
369
+ code: SpanStatusCode.ERROR,
370
+ message: error instanceof Error ? error.message : String(error)
371
+ });
372
+ }
373
+ return `Error executing tool '${name}': ${error instanceof Error ? error.message : String(error)}`;
374
+ } finally {
375
+ span?.end();
367
376
  }
368
- return `Error executing tool '${name}': ${error instanceof Error ? error.message : String(error)}`;
369
- } finally {
370
- span?.end();
371
- }
377
+ });
372
378
  }
373
379
  /**
374
380
  * Get the list of tools provided by this client
@@ -639,10 +645,13 @@ class DextoMcpClient extends EventEmitter {
639
645
  );
640
646
  return { action: "decline" };
641
647
  }
648
+ const invocation = this.toolInvocationContext.getStore();
642
649
  const response = await this.approvalManager.requestElicitation({
643
650
  schema: params.requestedSchema,
644
651
  prompt: params.message,
645
- serverName: this.serverAlias || "unknown"
652
+ serverName: this.serverAlias || "unknown",
653
+ ...invocation?.sessionId !== void 0 ? { sessionId: invocation.sessionId } : {},
654
+ ...invocation?.runContext?.hostRuntime !== void 0 ? { hostRuntime: invocation.runContext.hostRuntime } : {}
646
655
  });
647
656
  if (response.status === ApprovalStatus.APPROVED && response.data) {
648
657
  const formData = response.data && typeof response.data === "object" && "formData" in response.data ? response.data.formData : {};
@@ -60,7 +60,7 @@ const StdioServerConfigSchema = import_zod.z.object({
60
60
  }
61
61
  }),
62
62
  args: import_zod.z.array((0, import_result.EnvExpandedString)()).default([]).describe("Array of arguments for the command (e.g., ['script.js'])"),
63
- env: import_zod.z.record((0, import_result.EnvExpandedString)()).default({}).describe("Optional environment variables for the server process"),
63
+ env: import_zod.z.record(import_zod.z.string(), (0, import_result.EnvExpandedString)()).default({}).describe("Optional environment variables for the server process"),
64
64
  timeout: import_zod.z.coerce.number().int().positive().default(3e4),
65
65
  connectionMode: import_zod.z.enum(MCP_CONNECTION_MODES).default(DEFAULT_MCP_CONNECTION_MODE)
66
66
  }).strict();
@@ -68,7 +68,7 @@ const SseServerConfigSchema = import_zod.z.object({
68
68
  type: import_zod.z.literal("sse"),
69
69
  enabled: import_zod.z.boolean().default(true).describe("Whether this server is enabled (disabled servers are not connected)"),
70
70
  url: (0, import_result.RequiredEnvURL)(process.env).describe("URL for the SSE server endpoint"),
71
- headers: import_zod.z.record((0, import_result.EnvExpandedString)()).default({}),
71
+ headers: import_zod.z.record(import_zod.z.string(), (0, import_result.EnvExpandedString)()).default({}),
72
72
  timeout: import_zod.z.coerce.number().int().positive().default(3e4),
73
73
  connectionMode: import_zod.z.enum(MCP_CONNECTION_MODES).default(DEFAULT_MCP_CONNECTION_MODE)
74
74
  }).strict();
@@ -76,7 +76,7 @@ const HttpServerConfigSchema = import_zod.z.object({
76
76
  type: import_zod.z.literal("http"),
77
77
  enabled: import_zod.z.boolean().default(true).describe("Whether this server is enabled (disabled servers are not connected)"),
78
78
  url: (0, import_result.RequiredEnvURL)(process.env).describe("URL for the HTTP server"),
79
- headers: import_zod.z.record((0, import_result.EnvExpandedString)()).default({}),
79
+ headers: import_zod.z.record(import_zod.z.string(), (0, import_result.EnvExpandedString)()).default({}),
80
80
  timeout: import_zod.z.coerce.number().int().positive().default(3e4),
81
81
  connectionMode: import_zod.z.enum(MCP_CONNECTION_MODES).default(DEFAULT_MCP_CONNECTION_MODE)
82
82
  }).strict();
@@ -86,7 +86,7 @@ const McpServerConfigSchema = import_zod.z.discriminatedUnion("type", [
86
86
  HttpServerConfigSchema
87
87
  ]).superRefine((_data, _ctx) => {
88
88
  }).brand();
89
- const ServersConfigSchema = import_zod.z.record(McpServerConfigSchema).describe("A dictionary of server configurations, keyed by server name").brand();
89
+ const ServersConfigSchema = import_zod.z.record(import_zod.z.string(), McpServerConfigSchema).describe("A dictionary of server configurations, keyed by server name").brand();
90
90
  // Annotate the CommonJS export names for ESM import in node:
91
91
  0 && (module.exports = {
92
92
  DEFAULT_MCP_CONNECTION_MODE,