@mastra/mcp 1.8.1 → 1.9.0-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
@@ -418,6 +418,7 @@ function isReconnectableMCPError(error) {
418
418
 
419
419
  // src/client/client.ts
420
420
  var DEFAULT_SERVER_CONNECT_TIMEOUT_MSEC = 3e3;
421
+ var DEFAULT_INSTRUCTIONS_MAX_LENGTH = 512;
421
422
  var require2 = createRequire(import.meta.url);
422
423
  var SSE_FALLBACK_STATUS_CODES = [400, 404, 405];
423
424
  var DATADOG_TRACER_TEST_SYMBOL = /* @__PURE__ */ Symbol.for("mastra.mcp.dd-trace-test-tracer");
@@ -500,6 +501,7 @@ var InternalMastraMCPClient = class extends MastraBase {
500
501
  exitHookUnsubscribe;
501
502
  sigTermHandler;
502
503
  sigHupHandler;
504
+ serverInstructions;
503
505
  _roots;
504
506
  requireToolApproval;
505
507
  /** Provides access to resource operations (list, read, subscribe, etc.) */
@@ -746,11 +748,19 @@ var InternalMastraMCPClient = class extends MastraBase {
746
748
  } else {
747
749
  throw new Error("Server configuration must include either a command or a url.");
748
750
  }
751
+ this.refreshServerInstructions();
749
752
  resolve(true);
750
753
  const originalOnClose = this.client.onclose;
751
754
  this.client.onclose = () => {
752
755
  this.log("debug", `MCP server connection closed`);
756
+ const staleTransport = this.transport;
757
+ this.transport = void 0;
753
758
  this.isConnected = null;
759
+ this.serverInstructions = void 0;
760
+ if (staleTransport) {
761
+ void staleTransport.close().catch(() => {
762
+ });
763
+ }
754
764
  if (typeof originalOnClose === "function") {
755
765
  originalOnClose();
756
766
  }
@@ -808,6 +818,18 @@ var InternalMastraMCPClient = class extends MastraBase {
808
818
  }
809
819
  return null;
810
820
  }
821
+ get instructions() {
822
+ return this.serverInstructions;
823
+ }
824
+ get forwardInstructions() {
825
+ return this.serverConfig.forwardInstructions ?? false;
826
+ }
827
+ get instructionsMaxLength() {
828
+ return this.serverConfig.instructionsMaxLength ?? DEFAULT_INSTRUCTIONS_MAX_LENGTH;
829
+ }
830
+ refreshServerInstructions() {
831
+ this.serverInstructions = this.client.getInstructions();
832
+ }
811
833
  async disconnect() {
812
834
  if (!this.transport) {
813
835
  this.log("debug", "Disconnect called but no transport was connected.");
@@ -825,6 +847,7 @@ var InternalMastraMCPClient = class extends MastraBase {
825
847
  } finally {
826
848
  this.transport = void 0;
827
849
  this.isConnected = null;
850
+ this.serverInstructions = void 0;
828
851
  if (this.exitHookUnsubscribe) {
829
852
  this.exitHookUnsubscribe();
830
853
  this.exitHookUnsubscribe = void 0;
@@ -863,6 +886,7 @@ var InternalMastraMCPClient = class extends MastraBase {
863
886
  }
864
887
  this.transport = void 0;
865
888
  this.isConnected = null;
889
+ this.serverInstructions = void 0;
866
890
  await this.connect();
867
891
  this.log("debug", "Successfully reconnected to MCP server");
868
892
  }
@@ -1001,7 +1025,10 @@ var InternalMastraMCPClient = class extends MastraBase {
1001
1025
  requireApproval,
1002
1026
  mcpMetadata: {
1003
1027
  serverName: this.name,
1004
- serverVersion: this.client.getServerVersion()?.version
1028
+ serverVersion: this.client.getServerVersion()?.version,
1029
+ serverInstructions: this.serverInstructions,
1030
+ forwardInstructions: this.forwardInstructions,
1031
+ instructionsMaxLength: this.instructionsMaxLength
1005
1032
  },
1006
1033
  execute: async (input, context) => {
1007
1034
  const operationContext = context?.requestContext ?? null;
@@ -1865,6 +1892,19 @@ To fix this you have three different options:
1865
1892
  await this.getConnectedClientForServer(serverName);
1866
1893
  }
1867
1894
  }
1895
+ /**
1896
+ * Returns instructions advertised by connected MCP servers during initialize.
1897
+ *
1898
+ * Servers that have not connected yet, or did not advertise instructions,
1899
+ * return `undefined`.
1900
+ */
1901
+ getServerInstructions() {
1902
+ const instructions = {};
1903
+ for (const serverName of Object.keys(this.serverConfigs)) {
1904
+ instructions[serverName] = this.mcpClientsById.get(serverName)?.instructions;
1905
+ }
1906
+ return instructions;
1907
+ }
1868
1908
  /**
1869
1909
  * Retrieves all tools from all configured servers with namespaced names.
1870
1910
  *
@@ -2310,7 +2350,7 @@ function createSimpleTokenProvider(accessToken, options) {
2310
2350
  });
2311
2351
  }
2312
2352
 
2313
- // ../../node_modules/.pnpm/hono@4.12.18/node_modules/hono/dist/utils/stream.js
2353
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/hono/4.12.18/a4592d669eb2c33e4f24995614864047f2c1a155accc9d64dd2de0f4e8d608a9/node_modules/hono/dist/utils/stream.js
2314
2354
  var StreamingApi = class {
2315
2355
  writer;
2316
2356
  encoder;
@@ -2387,7 +2427,7 @@ var StreamingApi = class {
2387
2427
  }
2388
2428
  };
2389
2429
 
2390
- // ../../node_modules/.pnpm/hono@4.12.18/node_modules/hono/dist/helper/streaming/utils.js
2430
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/hono/4.12.18/a4592d669eb2c33e4f24995614864047f2c1a155accc9d64dd2de0f4e8d608a9/node_modules/hono/dist/helper/streaming/utils.js
2391
2431
  var isOldBunVersion = () => {
2392
2432
  const version = typeof Bun !== "undefined" ? Bun.version : void 0;
2393
2433
  if (version === void 0) {
@@ -2398,7 +2438,7 @@ var isOldBunVersion = () => {
2398
2438
  return result;
2399
2439
  };
2400
2440
 
2401
- // ../../node_modules/.pnpm/hono@4.12.18/node_modules/hono/dist/utils/html.js
2441
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/hono/4.12.18/a4592d669eb2c33e4f24995614864047f2c1a155accc9d64dd2de0f4e8d608a9/node_modules/hono/dist/utils/html.js
2402
2442
  var HtmlEscapedCallbackPhase = {
2403
2443
  Stringify: 1};
2404
2444
  var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
@@ -2429,7 +2469,7 @@ var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) =>
2429
2469
  }
2430
2470
  };
2431
2471
 
2432
- // ../../node_modules/.pnpm/hono@4.12.18/node_modules/hono/dist/helper/streaming/sse.js
2472
+ // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/hono/4.12.18/a4592d669eb2c33e4f24995614864047f2c1a155accc9d64dd2de0f4e8d608a9/node_modules/hono/dist/helper/streaming/sse.js
2433
2473
  var SSEStreamingApi = class extends StreamingApi {
2434
2474
  constructor(writable, readable) {
2435
2475
  super(writable, readable);