@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/dist/index.mjs CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  asSchema,
4
4
  dynamicTool,
5
5
  jsonSchema,
6
+ retryWithExponentialBackoff,
6
7
  safeParseJSON,
7
8
  safeValidateTypes,
8
9
  tool
@@ -1888,6 +1889,55 @@ function isCustomMcpTransport(transport) {
1888
1889
 
1889
1890
  // src/tool/mcp-client.ts
1890
1891
  var CLIENT_VERSION = "1.0.0";
1892
+ var DEFAULT_MAX_TOOL_CALL_RETRIES = 0;
1893
+ var DEFAULT_RETRY_ERROR_CODES = [
1894
+ "ConnectionRefused",
1895
+ "ConnectionClosed",
1896
+ "FailedToOpenSocket",
1897
+ "ECONNRESET",
1898
+ "ECONNREFUSED",
1899
+ "ETIMEDOUT",
1900
+ "EPIPE"
1901
+ ];
1902
+ function getErrorStatusCode(error) {
1903
+ if (error != null && typeof error === "object" && "statusCode" in error && typeof error.statusCode === "number") {
1904
+ return error.statusCode;
1905
+ }
1906
+ return void 0;
1907
+ }
1908
+ function getStringErrorCode(error) {
1909
+ if (error != null && typeof error === "object" && "code" in error && typeof error.code === "string") {
1910
+ return error.code;
1911
+ }
1912
+ return void 0;
1913
+ }
1914
+ function isRetryableMCPToolCallError(error) {
1915
+ const statusCode = getErrorStatusCode(error);
1916
+ if (statusCode != null) {
1917
+ return statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500;
1918
+ }
1919
+ if (MCPClientError.isInstance(error) && error.code != null) {
1920
+ return false;
1921
+ }
1922
+ const errorCode = getStringErrorCode(error);
1923
+ return errorCode != null && DEFAULT_RETRY_ERROR_CODES.includes(errorCode);
1924
+ }
1925
+ function prepareMaxRetries(maxRetries) {
1926
+ if (maxRetries == null) {
1927
+ return DEFAULT_MAX_TOOL_CALL_RETRIES;
1928
+ }
1929
+ if (!Number.isInteger(maxRetries)) {
1930
+ throw new MCPClientError({
1931
+ message: "maxRetries must be an integer"
1932
+ });
1933
+ }
1934
+ if (maxRetries < 0) {
1935
+ throw new MCPClientError({
1936
+ message: "maxRetries must be >= 0"
1937
+ });
1938
+ }
1939
+ return maxRetries;
1940
+ }
1891
1941
  function mcpToModelOutput({
1892
1942
  output
1893
1943
  }) {
@@ -1924,6 +1974,7 @@ var DefaultMCPClient = class {
1924
1974
  clientName = name3 != null ? name3 : "ai-sdk-mcp-client",
1925
1975
  version = CLIENT_VERSION,
1926
1976
  onUncaughtError,
1977
+ maxRetries,
1927
1978
  capabilities
1928
1979
  }) {
1929
1980
  this.requestMessageId = 0;
@@ -1932,6 +1983,7 @@ var DefaultMCPClient = class {
1932
1983
  this._serverInfo = { name: "", version: "" };
1933
1984
  this.isClosed = true;
1934
1985
  this.onUncaughtError = onUncaughtError;
1986
+ this.maxRetries = prepareMaxRetries(maxRetries);
1935
1987
  this.clientCapabilities = capabilities != null ? capabilities : {};
1936
1988
  if (isCustomMcpTransport(transportConfig)) {
1937
1989
  this.transport = transportConfig;
@@ -2120,18 +2172,41 @@ var DefaultMCPClient = class {
2120
2172
  options
2121
2173
  });
2122
2174
  }
2175
+ async callToolWithRetry({
2176
+ options,
2177
+ execute
2178
+ }) {
2179
+ if (this.maxRetries === 0) {
2180
+ return execute();
2181
+ }
2182
+ return retryWithExponentialBackoff({
2183
+ maxRetries: this.maxRetries,
2184
+ abortSignal: options == null ? void 0 : options.abortSignal,
2185
+ shouldRetry: isRetryableMCPToolCallError,
2186
+ createRetryError: ({ message, errors }) => new MCPClientError({
2187
+ message,
2188
+ cause: errors[errors.length - 1]
2189
+ })
2190
+ })(execute);
2191
+ }
2123
2192
  async callTool({
2124
2193
  name: name3,
2125
2194
  args,
2126
2195
  options
2127
2196
  }) {
2128
2197
  try {
2129
- return this.request({
2130
- request: { method: "tools/call", params: { name: name3, arguments: args } },
2131
- resultSchema: CallToolResultSchema,
2132
- options: {
2133
- signal: options == null ? void 0 : options.abortSignal
2134
- }
2198
+ return this.callToolWithRetry({
2199
+ options,
2200
+ execute: () => this.request({
2201
+ request: {
2202
+ method: "tools/call",
2203
+ params: { name: name3, arguments: args }
2204
+ },
2205
+ resultSchema: CallToolResultSchema,
2206
+ options: {
2207
+ signal: options == null ? void 0 : options.abortSignal
2208
+ }
2209
+ })
2135
2210
  });
2136
2211
  } catch (error) {
2137
2212
  throw error;