@ai-sdk/mcp 1.0.59 → 1.0.60

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
@@ -1601,7 +1601,7 @@ var HttpMCPTransport = class {
1601
1601
  });
1602
1602
  }
1603
1603
  this.abortController = new AbortController();
1604
- void this.openInboundSse();
1604
+ this.startInboundSse();
1605
1605
  }
1606
1606
  async close() {
1607
1607
  var _a3, _b3, _c;
@@ -1657,7 +1657,7 @@ var HttpMCPTransport = class {
1657
1657
  }
1658
1658
  if (response.status === 202) {
1659
1659
  if (!this.inboundSseConnection) {
1660
- void this.openInboundSse();
1660
+ this.startInboundSse();
1661
1661
  }
1662
1662
  return;
1663
1663
  }
@@ -1728,7 +1728,13 @@ var HttpMCPTransport = class {
1728
1728
  (_c2 = this.onerror) == null ? void 0 : _c2.call(this, error2);
1729
1729
  }
1730
1730
  };
1731
- processEvents();
1731
+ void processEvents().catch((error2) => {
1732
+ var _a4;
1733
+ if (error2 instanceof Error && error2.name === "AbortError") {
1734
+ return;
1735
+ }
1736
+ (_a4 = this.onerror) == null ? void 0 : _a4.call(this, error2);
1737
+ });
1732
1738
  return;
1733
1739
  }
1734
1740
  const error = new MCPClientError({
@@ -1770,12 +1776,21 @@ var HttpMCPTransport = class {
1770
1776
  }
1771
1777
  const delay = this.getNextReconnectionDelay(this.inboundReconnectAttempts);
1772
1778
  this.inboundReconnectAttempts += 1;
1773
- setTimeout(async () => {
1779
+ setTimeout(() => {
1774
1780
  var _a4;
1775
1781
  if ((_a4 = this.abortController) == null ? void 0 : _a4.signal.aborted) return;
1776
- await this.openInboundSse(false, this.lastInboundEventId);
1782
+ this.startInboundSse(false, this.lastInboundEventId);
1777
1783
  }, delay);
1778
1784
  }
1785
+ startInboundSse(triedAuth = false, resumeToken) {
1786
+ void this.openInboundSse(triedAuth, resumeToken).catch((error) => {
1787
+ var _a3;
1788
+ if (error instanceof Error && error.name === "AbortError") {
1789
+ return;
1790
+ }
1791
+ (_a3 = this.onerror) == null ? void 0 : _a3.call(this, error);
1792
+ });
1793
+ }
1779
1794
  // Open optional inbound SSE stream; best-effort and resumable
1780
1795
  async openInboundSse(triedAuth = false, resumeToken) {
1781
1796
  var _a3, _b3, _c, _d, _e, _f;
@@ -1859,10 +1874,27 @@ var HttpMCPTransport = class {
1859
1874
  }
1860
1875
  };
1861
1876
  this.inboundSseConnection = {
1862
- close: () => reader.cancel()
1877
+ close: () => {
1878
+ void reader.cancel().catch((error) => {
1879
+ var _a4;
1880
+ if (error instanceof Error && error.name === "AbortError") {
1881
+ return;
1882
+ }
1883
+ (_a4 = this.onerror) == null ? void 0 : _a4.call(this, error);
1884
+ });
1885
+ }
1863
1886
  };
1864
1887
  this.inboundReconnectAttempts = 0;
1865
- processEvents();
1888
+ void processEvents().catch((error) => {
1889
+ var _a4, _b4;
1890
+ if (error instanceof Error && error.name === "AbortError") {
1891
+ return;
1892
+ }
1893
+ (_a4 = this.onerror) == null ? void 0 : _a4.call(this, error);
1894
+ if (!((_b4 = this.abortController) == null ? void 0 : _b4.signal.aborted)) {
1895
+ this.scheduleInboundSseReconnection();
1896
+ }
1897
+ });
1866
1898
  } catch (error) {
1867
1899
  if (error instanceof Error && error.name === "AbortError") {
1868
1900
  return;
@@ -2135,35 +2167,49 @@ var DefaultMCPClient = class {
2135
2167
  jsonrpc: "2.0",
2136
2168
  id: messageId
2137
2169
  };
2170
+ const rejectWithAbortError = () => {
2171
+ reject(
2172
+ new MCPClientError({
2173
+ message: "Request was aborted",
2174
+ cause: signal == null ? void 0 : signal.reason
2175
+ })
2176
+ );
2177
+ };
2138
2178
  const cleanup = () => {
2139
2179
  this.responseHandlers.delete(messageId);
2180
+ signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
2181
+ };
2182
+ const rejectAndCleanup = (error) => {
2183
+ cleanup();
2184
+ reject(error);
2185
+ };
2186
+ const onAbort = () => {
2187
+ cleanup();
2188
+ rejectWithAbortError();
2140
2189
  };
2141
2190
  this.responseHandlers.set(messageId, (response) => {
2142
2191
  if (signal == null ? void 0 : signal.aborted) {
2143
- return reject(
2144
- new MCPClientError({
2145
- message: "Request was aborted",
2146
- cause: signal.reason
2147
- })
2148
- );
2192
+ cleanup();
2193
+ return rejectWithAbortError();
2149
2194
  }
2150
2195
  if (response instanceof Error) {
2151
- return reject(response);
2196
+ return rejectAndCleanup(response);
2152
2197
  }
2153
2198
  try {
2154
2199
  const result = resultSchema.parse(response.result);
2200
+ cleanup();
2155
2201
  resolve(result);
2156
2202
  } catch (error) {
2157
2203
  const parseError = new MCPClientError({
2158
2204
  message: "Failed to parse server response",
2159
2205
  cause: error
2160
2206
  });
2161
- reject(parseError);
2207
+ rejectAndCleanup(parseError);
2162
2208
  }
2163
2209
  });
2210
+ signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
2164
2211
  this.transport.send(jsonrpcRequest).catch((error) => {
2165
- cleanup();
2166
- reject(error);
2212
+ rejectAndCleanup(error);
2167
2213
  });
2168
2214
  });
2169
2215
  }