@ai-sdk/mcp 1.0.56 → 1.0.58

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
@@ -1,50 +1,3 @@
1
- // src/tool/mcp-client.ts
2
- import {
3
- asSchema,
4
- dynamicTool,
5
- jsonSchema,
6
- safeParseJSON,
7
- safeValidateTypes,
8
- tool
9
- } from "@ai-sdk/provider-utils";
10
-
11
- // src/error/mcp-client-error.ts
12
- import { AISDKError } from "@ai-sdk/provider";
13
- var name = "AI_MCPClientError";
14
- var marker = `vercel.ai.error.${name}`;
15
- var symbol = Symbol.for(marker);
16
- var _a, _b;
17
- var MCPClientError = class extends (_b = AISDKError, _a = symbol, _b) {
18
- constructor({
19
- name: name3 = "MCPClientError",
20
- message,
21
- cause,
22
- data,
23
- code,
24
- statusCode,
25
- url,
26
- responseBody
27
- }) {
28
- super({ name: name3, message, cause });
29
- this[_a] = true;
30
- this.data = data;
31
- this.code = code;
32
- this.statusCode = statusCode;
33
- this.url = url;
34
- this.responseBody = responseBody;
35
- }
36
- static isInstance(error) {
37
- return AISDKError.hasMarker(error, marker);
38
- }
39
- };
40
-
41
- // src/tool/mcp-sse-transport.ts
42
- import {
43
- EventSourceParserStream,
44
- withUserAgentSuffix,
45
- getRuntimeEnvironmentUserAgent
46
- } from "@ai-sdk/provider-utils";
47
-
48
1
  // src/tool/json-rpc-message.ts
49
2
  import { parseJSON } from "@ai-sdk/provider-utils";
50
3
  import { z as z2 } from "zod/v4";
@@ -329,10 +282,61 @@ var JSONRPCMessageSchema = z2.union([
329
282
  JSONRPCResponseSchema,
330
283
  JSONRPCErrorSchema
331
284
  ]);
285
+ function validateJSONRPCMessage(message) {
286
+ return JSONRPCMessageSchema.parse(message);
287
+ }
332
288
  async function parseJSONRPCMessage(text) {
333
- return JSONRPCMessageSchema.parse(await parseJSON({ text }));
289
+ return validateJSONRPCMessage(await parseJSON({ text }));
334
290
  }
335
291
 
292
+ // src/tool/mcp-client.ts
293
+ import {
294
+ asSchema,
295
+ dynamicTool,
296
+ jsonSchema,
297
+ retryWithExponentialBackoff,
298
+ safeParseJSON,
299
+ safeValidateTypes,
300
+ tool
301
+ } from "@ai-sdk/provider-utils";
302
+
303
+ // src/error/mcp-client-error.ts
304
+ import { AISDKError } from "@ai-sdk/provider";
305
+ var name = "AI_MCPClientError";
306
+ var marker = `vercel.ai.error.${name}`;
307
+ var symbol = Symbol.for(marker);
308
+ var _a, _b;
309
+ var MCPClientError = class extends (_b = AISDKError, _a = symbol, _b) {
310
+ constructor({
311
+ name: name3 = "MCPClientError",
312
+ message,
313
+ cause,
314
+ data,
315
+ code,
316
+ statusCode,
317
+ url,
318
+ responseBody
319
+ }) {
320
+ super({ name: name3, message, cause });
321
+ this[_a] = true;
322
+ this.data = data;
323
+ this.code = code;
324
+ this.statusCode = statusCode;
325
+ this.url = url;
326
+ this.responseBody = responseBody;
327
+ }
328
+ static isInstance(error) {
329
+ return AISDKError.hasMarker(error, marker);
330
+ }
331
+ };
332
+
333
+ // src/tool/mcp-sse-transport.ts
334
+ import {
335
+ EventSourceParserStream,
336
+ withUserAgentSuffix,
337
+ getRuntimeEnvironmentUserAgent
338
+ } from "@ai-sdk/provider-utils";
339
+
336
340
  // src/version.ts
337
341
  var VERSION = typeof __PACKAGE_VERSION__ !== "undefined" ? __PACKAGE_VERSION__ : "0.0.0-test";
338
342
 
@@ -1679,8 +1683,10 @@ var HttpMCPTransport = class {
1679
1683
  const contentType = response.headers.get("content-type") || "";
1680
1684
  if (contentType.includes("application/json")) {
1681
1685
  const data = await response.json();
1682
- const messages = Array.isArray(data) ? data.map((m) => JSONRPCMessageSchema.parse(m)) : [JSONRPCMessageSchema.parse(data)];
1683
- for (const m of messages) (_d = this.onmessage) == null ? void 0 : _d.call(this, m);
1686
+ const messages = Array.isArray(data) ? data.map((message2) => validateJSONRPCMessage(message2)) : [validateJSONRPCMessage(data)];
1687
+ for (const jsonRpcMessage of messages) {
1688
+ (_d = this.onmessage) == null ? void 0 : _d.call(this, jsonRpcMessage);
1689
+ }
1684
1690
  return;
1685
1691
  }
1686
1692
  if (contentType.includes("text/event-stream")) {
@@ -1888,6 +1894,55 @@ function isCustomMcpTransport(transport) {
1888
1894
 
1889
1895
  // src/tool/mcp-client.ts
1890
1896
  var CLIENT_VERSION = "1.0.0";
1897
+ var DEFAULT_MAX_TOOL_CALL_RETRIES = 0;
1898
+ var DEFAULT_RETRY_ERROR_CODES = [
1899
+ "ConnectionRefused",
1900
+ "ConnectionClosed",
1901
+ "FailedToOpenSocket",
1902
+ "ECONNRESET",
1903
+ "ECONNREFUSED",
1904
+ "ETIMEDOUT",
1905
+ "EPIPE"
1906
+ ];
1907
+ function getErrorStatusCode(error) {
1908
+ if (error != null && typeof error === "object" && "statusCode" in error && typeof error.statusCode === "number") {
1909
+ return error.statusCode;
1910
+ }
1911
+ return void 0;
1912
+ }
1913
+ function getStringErrorCode(error) {
1914
+ if (error != null && typeof error === "object" && "code" in error && typeof error.code === "string") {
1915
+ return error.code;
1916
+ }
1917
+ return void 0;
1918
+ }
1919
+ function isRetryableMCPToolCallError(error) {
1920
+ const statusCode = getErrorStatusCode(error);
1921
+ if (statusCode != null) {
1922
+ return statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500;
1923
+ }
1924
+ if (MCPClientError.isInstance(error) && error.code != null) {
1925
+ return false;
1926
+ }
1927
+ const errorCode = getStringErrorCode(error);
1928
+ return errorCode != null && DEFAULT_RETRY_ERROR_CODES.includes(errorCode);
1929
+ }
1930
+ function prepareMaxRetries(maxRetries) {
1931
+ if (maxRetries == null) {
1932
+ return DEFAULT_MAX_TOOL_CALL_RETRIES;
1933
+ }
1934
+ if (!Number.isInteger(maxRetries)) {
1935
+ throw new MCPClientError({
1936
+ message: "maxRetries must be an integer"
1937
+ });
1938
+ }
1939
+ if (maxRetries < 0) {
1940
+ throw new MCPClientError({
1941
+ message: "maxRetries must be >= 0"
1942
+ });
1943
+ }
1944
+ return maxRetries;
1945
+ }
1891
1946
  function mcpToModelOutput({
1892
1947
  output
1893
1948
  }) {
@@ -1924,6 +1979,7 @@ var DefaultMCPClient = class {
1924
1979
  clientName = name3 != null ? name3 : "ai-sdk-mcp-client",
1925
1980
  version = CLIENT_VERSION,
1926
1981
  onUncaughtError,
1982
+ maxRetries,
1927
1983
  capabilities
1928
1984
  }) {
1929
1985
  this.requestMessageId = 0;
@@ -1932,6 +1988,7 @@ var DefaultMCPClient = class {
1932
1988
  this._serverInfo = { name: "", version: "" };
1933
1989
  this.isClosed = true;
1934
1990
  this.onUncaughtError = onUncaughtError;
1991
+ this.maxRetries = prepareMaxRetries(maxRetries);
1935
1992
  this.clientCapabilities = capabilities != null ? capabilities : {};
1936
1993
  if (isCustomMcpTransport(transportConfig)) {
1937
1994
  this.transport = transportConfig;
@@ -2120,18 +2177,41 @@ var DefaultMCPClient = class {
2120
2177
  options
2121
2178
  });
2122
2179
  }
2180
+ async callToolWithRetry({
2181
+ options,
2182
+ execute
2183
+ }) {
2184
+ if (this.maxRetries === 0) {
2185
+ return execute();
2186
+ }
2187
+ return retryWithExponentialBackoff({
2188
+ maxRetries: this.maxRetries,
2189
+ abortSignal: options == null ? void 0 : options.abortSignal,
2190
+ shouldRetry: isRetryableMCPToolCallError,
2191
+ createRetryError: ({ message, errors }) => new MCPClientError({
2192
+ message,
2193
+ cause: errors[errors.length - 1]
2194
+ })
2195
+ })(execute);
2196
+ }
2123
2197
  async callTool({
2124
2198
  name: name3,
2125
2199
  args,
2126
2200
  options
2127
2201
  }) {
2128
2202
  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
- }
2203
+ return this.callToolWithRetry({
2204
+ options,
2205
+ execute: () => this.request({
2206
+ request: {
2207
+ method: "tools/call",
2208
+ params: { name: name3, arguments: args }
2209
+ },
2210
+ resultSchema: CallToolResultSchema,
2211
+ options: {
2212
+ signal: options == null ? void 0 : options.abortSignal
2213
+ }
2214
+ })
2135
2215
  });
2136
2216
  } catch (error) {
2137
2217
  throw error;
@@ -2489,6 +2569,7 @@ export {
2489
2569
  UnauthorizedError,
2490
2570
  auth,
2491
2571
  createMCPClient,
2492
- createMCPClient as experimental_createMCPClient
2572
+ createMCPClient as experimental_createMCPClient,
2573
+ validateJSONRPCMessage
2493
2574
  };
2494
2575
  //# sourceMappingURL=index.mjs.map