@mastra/mcp 1.12.1 → 1.13.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
@@ -1,6 +1,7 @@
1
1
  import { AsyncLocalStorage } from 'async_hooks';
2
2
  import { createRequire } from 'module';
3
3
  import { MastraBase } from '@mastra/core/base';
4
+ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
4
5
  import { createTool, isValidationError } from '@mastra/core/tools';
5
6
  import { Client } from '@modelcontextprotocol/sdk/client/index.js';
6
7
  import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
@@ -10,7 +11,6 @@ import { DEFAULT_REQUEST_TIMEOUT_MSEC } from '@modelcontextprotocol/sdk/shared/p
10
11
  import { LoggingMessageNotificationSchema, ListRootsRequestSchema, ListResourcesResultSchema, ReadResourceResultSchema, EmptyResultSchema, ListResourceTemplatesResultSchema, ListPromptsResultSchema, GetPromptResultSchema, PromptListChangedNotificationSchema, ResourceUpdatedNotificationSchema, ResourceListChangedNotificationSchema, ElicitRequestSchema, ProgressNotificationSchema, ListToolsRequestSchema, CallToolRequestSchema, SetLevelRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, McpError, ErrorCode, ListResourceTemplatesRequestSchema, SubscribeRequestSchema, UnsubscribeRequestSchema, ListPromptsRequestSchema, PromptSchema, GetPromptRequestSchema, CallToolResultSchema, JSONRPCMessageSchema } from '@modelcontextprotocol/sdk/types.js';
11
12
  import { asyncExitHook, gracefulExit } from 'exit-hook';
12
13
  import { createHash, randomUUID } from 'crypto';
13
- import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
14
14
  import equal from 'fast-deep-equal';
15
15
  import { MCPServerBase } from '@mastra/core/mcp';
16
16
  import { isStandardSchemaWithJSON, standardSchemaToJSONSchema } from '@mastra/core/schema';
@@ -424,6 +424,14 @@ var DATADOG_TRACER_TEST_SYMBOL = /* @__PURE__ */ Symbol.for("mastra.mcp.dd-trace
424
424
  function shouldDetachPersistentTransportRequest(init) {
425
425
  return (init?.method ?? "GET").toUpperCase() === "GET";
426
426
  }
427
+ function extractToolErrorText(content) {
428
+ const fallback = "MCP tool execution failed";
429
+ if (!Array.isArray(content)) return fallback;
430
+ const text = content.filter((part) => {
431
+ return !!part && typeof part === "object" && part.type === "text";
432
+ }).map((part) => part.text).join("\n").trim();
433
+ return text || fallback;
434
+ }
427
435
  function getDatadogScope() {
428
436
  const testTracer = globalThis[DATADOG_TRACER_TEST_SYMBOL];
429
437
  const tracer = testTracer ?? loadDatadogTracer();
@@ -505,6 +513,7 @@ var InternalMastraMCPClient = class extends MastraBase {
505
513
  serverInstructions;
506
514
  _roots;
507
515
  requireToolApproval;
516
+ onToolError;
508
517
  /** Provides access to resource operations (list, read, subscribe, etc.) */
509
518
  resources;
510
519
  /** Provides access to prompt operations (list, get, notifications) */
@@ -531,6 +540,7 @@ var InternalMastraMCPClient = class extends MastraBase {
531
540
  this.serverConfig = server;
532
541
  this.enableProgressTracking = !!server.enableProgressTracking;
533
542
  this.requireToolApproval = server.requireToolApproval;
543
+ this.onToolError = server.onToolError ?? "throw";
534
544
  this._roots = server.roots ?? [];
535
545
  const hasRoots = this._roots.length > 0 || !!capabilities.roots;
536
546
  const clientCapabilities = {
@@ -1051,6 +1061,17 @@ var InternalMastraMCPClient = class extends MastraBase {
1051
1061
  signal: context?.abortSignal
1052
1062
  }
1053
1063
  );
1064
+ if (res.isError && this.onToolError === "throw") {
1065
+ const errorText = extractToolErrorText(res.content);
1066
+ this.log("debug", `Tool reported an error: ${tool.name}`, { error: errorText });
1067
+ throw new MastraError({
1068
+ id: "MCP_CLIENT_TOOL_EXECUTION_FAILED",
1069
+ domain: ErrorDomain.MCP,
1070
+ category: ErrorCategory.THIRD_PARTY,
1071
+ text: errorText,
1072
+ details: { toolName: tool.name, serverName: this.name }
1073
+ });
1074
+ }
1054
1075
  this.log("debug", `Tool executed successfully: ${tool.name}`);
1055
1076
  if (res.structuredContent !== void 0) {
1056
1077
  return res.structuredContent;