@mastra/mcp 1.6.0 → 1.6.1-alpha.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.
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AsyncLocalStorage } from 'async_hooks';
2
+ import { createRequire } from 'module';
2
3
  import { MastraBase } from '@mastra/core/base';
3
4
  import { createTool, isValidationError } from '@mastra/core/tools';
4
5
  import { Client } from '@modelcontextprotocol/sdk/client/index.js';
@@ -415,7 +416,57 @@ function isReconnectableMCPError(error) {
415
416
 
416
417
  // src/client/client.ts
417
418
  var DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC = 3e3;
419
+ var require2 = createRequire(import.meta.url);
418
420
  var SSE_FALLBACK_STATUS_CODES = [400, 404, 405];
421
+ var DATADOG_TRACER_TEST_SYMBOL = /* @__PURE__ */ Symbol.for("mastra.mcp.dd-trace-test-tracer");
422
+ function shouldDetachPersistentTransportRequest(init) {
423
+ return (init?.method ?? "GET").toUpperCase() === "GET";
424
+ }
425
+ function getDatadogScope() {
426
+ const testTracer = globalThis[DATADOG_TRACER_TEST_SYMBOL];
427
+ const tracer = testTracer ?? loadDatadogTracer();
428
+ if (typeof tracer?.scope === "function") {
429
+ return tracer.scope();
430
+ }
431
+ if (typeof tracer?.default?.scope === "function") {
432
+ return tracer.default.scope();
433
+ }
434
+ return null;
435
+ }
436
+ function loadDatadogTracer() {
437
+ if (!isDatadogTracerLikelyLoaded()) {
438
+ return null;
439
+ }
440
+ try {
441
+ return require2("dd-trace");
442
+ } catch {
443
+ return null;
444
+ }
445
+ }
446
+ function isDatadogTracerLikelyLoaded() {
447
+ if (globalThis[DATADOG_TRACER_TEST_SYMBOL]) {
448
+ return true;
449
+ }
450
+ if (process.execArgv.some((arg) => arg.includes("dd-trace"))) {
451
+ return true;
452
+ }
453
+ if (process.env.NODE_OPTIONS?.includes("dd-trace")) {
454
+ return true;
455
+ }
456
+ try {
457
+ const resolvedPath = require2.resolve("dd-trace");
458
+ return Boolean(require2.cache[resolvedPath]);
459
+ } catch {
460
+ return false;
461
+ }
462
+ }
463
+ function runOutsideDatadogTraceScope(callback) {
464
+ const scope = getDatadogScope();
465
+ if (!scope) {
466
+ return callback();
467
+ }
468
+ return scope.activate(null, callback);
469
+ }
419
470
  function convertLogLevelToLoggerMethod(level) {
420
471
  switch (level) {
421
472
  case "debug":
@@ -610,7 +661,11 @@ var InternalMastraMCPClient = class extends MastraBase {
610
661
  }
611
662
  async connectHttp(url) {
612
663
  const { requestInit, eventSourceInit, authProvider, connectTimeout, fetch: userFetch } = this.serverConfig;
613
- const fetch2 = userFetch ? (url2, init) => userFetch(url2, init, this.operationContextStore.getStore() ?? null) : void 0;
664
+ const fetch2 = (requestUrl, init) => {
665
+ const requestContext = this.operationContextStore.getStore() ?? null;
666
+ const executeFetch = () => userFetch ? userFetch(requestUrl, init, requestContext) : globalThis.fetch(requestUrl, init);
667
+ return shouldDetachPersistentTransportRequest(init) ? runOutsideDatadogTraceScope(executeFetch) : executeFetch();
668
+ };
614
669
  this.log("debug", `Attempting to connect to URL: ${url}`);
615
670
  let shouldTrySSE = url.pathname.endsWith(`/sse`);
616
671
  if (!shouldTrySSE) {
@@ -639,7 +694,7 @@ var InternalMastraMCPClient = class extends MastraBase {
639
694
  if (shouldTrySSE) {
640
695
  this.log("debug", "Falling back to deprecated HTTP+SSE transport...");
641
696
  try {
642
- const sseEventSourceInit = fetch2 ? { ...eventSourceInit, fetch: fetch2 } : eventSourceInit;
697
+ const sseEventSourceInit = { ...eventSourceInit, fetch: fetch2 };
643
698
  const sseTransport = new SSEClientTransport(url, {
644
699
  requestInit,
645
700
  eventSourceInit: sseEventSourceInit,