@ai-sdk/mcp 1.0.55 → 1.0.57

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @ai-sdk/mcp
2
2
 
3
+ ## 1.0.57
4
+
5
+ ### Patch Changes
6
+
7
+ - ea1e95b: feat(mcp): add maxRetries option for failed mcp tool calls
8
+ - Updated dependencies [ea1e95b]
9
+ - @ai-sdk/provider-utils@4.0.35
10
+
11
+ ## 1.0.56
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [fa850e6]
16
+ - @ai-sdk/provider@3.0.13
17
+ - @ai-sdk/provider-utils@4.0.34
18
+
3
19
  ## 1.0.55
4
20
 
5
21
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -534,6 +534,15 @@ interface MCPClientConfig {
534
534
  transport: MCPTransportConfig | MCPTransport;
535
535
  /** Optional callback for uncaught errors */
536
536
  onUncaughtError?: (error: unknown) => void;
537
+ /**
538
+ * Maximum number of retries for transient MCP tool call failures.
539
+ *
540
+ * Set to 0 to disable retries. Retries only apply to tools/call requests.
541
+ * JSON-RPC application errors, such as invalid params, are not retried.
542
+ *
543
+ * @default 0
544
+ */
545
+ maxRetries?: number;
537
546
  /** Optional client name, defaults to 'ai-sdk-mcp-client' */
538
547
  clientName?: string;
539
548
  /**
package/dist/index.d.ts CHANGED
@@ -534,6 +534,15 @@ interface MCPClientConfig {
534
534
  transport: MCPTransportConfig | MCPTransport;
535
535
  /** Optional callback for uncaught errors */
536
536
  onUncaughtError?: (error: unknown) => void;
537
+ /**
538
+ * Maximum number of retries for transient MCP tool call failures.
539
+ *
540
+ * Set to 0 to disable retries. Retries only apply to tools/call requests.
541
+ * JSON-RPC application errors, such as invalid params, are not retried.
542
+ *
543
+ * @default 0
544
+ */
545
+ maxRetries?: number;
537
546
  /** Optional client name, defaults to 'ai-sdk-mcp-client' */
538
547
  clientName?: string;
539
548
  /**
package/dist/index.js CHANGED
@@ -1914,6 +1914,55 @@ function isCustomMcpTransport(transport) {
1914
1914
 
1915
1915
  // src/tool/mcp-client.ts
1916
1916
  var CLIENT_VERSION = "1.0.0";
1917
+ var DEFAULT_MAX_TOOL_CALL_RETRIES = 0;
1918
+ var DEFAULT_RETRY_ERROR_CODES = [
1919
+ "ConnectionRefused",
1920
+ "ConnectionClosed",
1921
+ "FailedToOpenSocket",
1922
+ "ECONNRESET",
1923
+ "ECONNREFUSED",
1924
+ "ETIMEDOUT",
1925
+ "EPIPE"
1926
+ ];
1927
+ function getErrorStatusCode(error) {
1928
+ if (error != null && typeof error === "object" && "statusCode" in error && typeof error.statusCode === "number") {
1929
+ return error.statusCode;
1930
+ }
1931
+ return void 0;
1932
+ }
1933
+ function getStringErrorCode(error) {
1934
+ if (error != null && typeof error === "object" && "code" in error && typeof error.code === "string") {
1935
+ return error.code;
1936
+ }
1937
+ return void 0;
1938
+ }
1939
+ function isRetryableMCPToolCallError(error) {
1940
+ const statusCode = getErrorStatusCode(error);
1941
+ if (statusCode != null) {
1942
+ return statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500;
1943
+ }
1944
+ if (MCPClientError.isInstance(error) && error.code != null) {
1945
+ return false;
1946
+ }
1947
+ const errorCode = getStringErrorCode(error);
1948
+ return errorCode != null && DEFAULT_RETRY_ERROR_CODES.includes(errorCode);
1949
+ }
1950
+ function prepareMaxRetries(maxRetries) {
1951
+ if (maxRetries == null) {
1952
+ return DEFAULT_MAX_TOOL_CALL_RETRIES;
1953
+ }
1954
+ if (!Number.isInteger(maxRetries)) {
1955
+ throw new MCPClientError({
1956
+ message: "maxRetries must be an integer"
1957
+ });
1958
+ }
1959
+ if (maxRetries < 0) {
1960
+ throw new MCPClientError({
1961
+ message: "maxRetries must be >= 0"
1962
+ });
1963
+ }
1964
+ return maxRetries;
1965
+ }
1917
1966
  function mcpToModelOutput({
1918
1967
  output
1919
1968
  }) {
@@ -1950,6 +1999,7 @@ var DefaultMCPClient = class {
1950
1999
  clientName = name3 != null ? name3 : "ai-sdk-mcp-client",
1951
2000
  version = CLIENT_VERSION,
1952
2001
  onUncaughtError,
2002
+ maxRetries,
1953
2003
  capabilities
1954
2004
  }) {
1955
2005
  this.requestMessageId = 0;
@@ -1958,6 +2008,7 @@ var DefaultMCPClient = class {
1958
2008
  this._serverInfo = { name: "", version: "" };
1959
2009
  this.isClosed = true;
1960
2010
  this.onUncaughtError = onUncaughtError;
2011
+ this.maxRetries = prepareMaxRetries(maxRetries);
1961
2012
  this.clientCapabilities = capabilities != null ? capabilities : {};
1962
2013
  if (isCustomMcpTransport(transportConfig)) {
1963
2014
  this.transport = transportConfig;
@@ -2146,18 +2197,41 @@ var DefaultMCPClient = class {
2146
2197
  options
2147
2198
  });
2148
2199
  }
2200
+ async callToolWithRetry({
2201
+ options,
2202
+ execute
2203
+ }) {
2204
+ if (this.maxRetries === 0) {
2205
+ return execute();
2206
+ }
2207
+ return (0, import_provider_utils5.retryWithExponentialBackoff)({
2208
+ maxRetries: this.maxRetries,
2209
+ abortSignal: options == null ? void 0 : options.abortSignal,
2210
+ shouldRetry: isRetryableMCPToolCallError,
2211
+ createRetryError: ({ message, errors }) => new MCPClientError({
2212
+ message,
2213
+ cause: errors[errors.length - 1]
2214
+ })
2215
+ })(execute);
2216
+ }
2149
2217
  async callTool({
2150
2218
  name: name3,
2151
2219
  args,
2152
2220
  options
2153
2221
  }) {
2154
2222
  try {
2155
- return this.request({
2156
- request: { method: "tools/call", params: { name: name3, arguments: args } },
2157
- resultSchema: CallToolResultSchema,
2158
- options: {
2159
- signal: options == null ? void 0 : options.abortSignal
2160
- }
2223
+ return this.callToolWithRetry({
2224
+ options,
2225
+ execute: () => this.request({
2226
+ request: {
2227
+ method: "tools/call",
2228
+ params: { name: name3, arguments: args }
2229
+ },
2230
+ resultSchema: CallToolResultSchema,
2231
+ options: {
2232
+ signal: options == null ? void 0 : options.abortSignal
2233
+ }
2234
+ })
2161
2235
  });
2162
2236
  } catch (error) {
2163
2237
  throw error;