@ai-sdk/mcp 1.0.59 → 1.0.61

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,22 @@
1
1
  # @ai-sdk/mcp
2
2
 
3
+ ## 1.0.61
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [e1af05f]
8
+ - @ai-sdk/provider@3.0.14
9
+ - @ai-sdk/provider-utils@4.0.38
10
+
11
+ ## 1.0.60
12
+
13
+ ### Patch Changes
14
+
15
+ - 937d731: Reject in-flight MCP requests when their abort signal fires and remove the pending response handler.
16
+ - 2b6c67f: Prevent streamable HTTP MCP background SSE disconnects from surfacing as unhandled promise rejections.
17
+ - Updated dependencies [d559de9]
18
+ - @ai-sdk/provider-utils@4.0.37
19
+
3
20
  ## 1.0.59
4
21
 
5
22
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1627,7 +1627,7 @@ var HttpMCPTransport = class {
1627
1627
  });
1628
1628
  }
1629
1629
  this.abortController = new AbortController();
1630
- void this.openInboundSse();
1630
+ this.startInboundSse();
1631
1631
  }
1632
1632
  async close() {
1633
1633
  var _a3, _b3, _c;
@@ -1683,7 +1683,7 @@ var HttpMCPTransport = class {
1683
1683
  }
1684
1684
  if (response.status === 202) {
1685
1685
  if (!this.inboundSseConnection) {
1686
- void this.openInboundSse();
1686
+ this.startInboundSse();
1687
1687
  }
1688
1688
  return;
1689
1689
  }
@@ -1754,7 +1754,13 @@ var HttpMCPTransport = class {
1754
1754
  (_c2 = this.onerror) == null ? void 0 : _c2.call(this, error2);
1755
1755
  }
1756
1756
  };
1757
- processEvents();
1757
+ void processEvents().catch((error2) => {
1758
+ var _a4;
1759
+ if (error2 instanceof Error && error2.name === "AbortError") {
1760
+ return;
1761
+ }
1762
+ (_a4 = this.onerror) == null ? void 0 : _a4.call(this, error2);
1763
+ });
1758
1764
  return;
1759
1765
  }
1760
1766
  const error = new MCPClientError({
@@ -1796,12 +1802,21 @@ var HttpMCPTransport = class {
1796
1802
  }
1797
1803
  const delay = this.getNextReconnectionDelay(this.inboundReconnectAttempts);
1798
1804
  this.inboundReconnectAttempts += 1;
1799
- setTimeout(async () => {
1805
+ setTimeout(() => {
1800
1806
  var _a4;
1801
1807
  if ((_a4 = this.abortController) == null ? void 0 : _a4.signal.aborted) return;
1802
- await this.openInboundSse(false, this.lastInboundEventId);
1808
+ this.startInboundSse(false, this.lastInboundEventId);
1803
1809
  }, delay);
1804
1810
  }
1811
+ startInboundSse(triedAuth = false, resumeToken) {
1812
+ void this.openInboundSse(triedAuth, resumeToken).catch((error) => {
1813
+ var _a3;
1814
+ if (error instanceof Error && error.name === "AbortError") {
1815
+ return;
1816
+ }
1817
+ (_a3 = this.onerror) == null ? void 0 : _a3.call(this, error);
1818
+ });
1819
+ }
1805
1820
  // Open optional inbound SSE stream; best-effort and resumable
1806
1821
  async openInboundSse(triedAuth = false, resumeToken) {
1807
1822
  var _a3, _b3, _c, _d, _e, _f;
@@ -1885,10 +1900,27 @@ var HttpMCPTransport = class {
1885
1900
  }
1886
1901
  };
1887
1902
  this.inboundSseConnection = {
1888
- close: () => reader.cancel()
1903
+ close: () => {
1904
+ void reader.cancel().catch((error) => {
1905
+ var _a4;
1906
+ if (error instanceof Error && error.name === "AbortError") {
1907
+ return;
1908
+ }
1909
+ (_a4 = this.onerror) == null ? void 0 : _a4.call(this, error);
1910
+ });
1911
+ }
1889
1912
  };
1890
1913
  this.inboundReconnectAttempts = 0;
1891
- processEvents();
1914
+ void processEvents().catch((error) => {
1915
+ var _a4, _b4;
1916
+ if (error instanceof Error && error.name === "AbortError") {
1917
+ return;
1918
+ }
1919
+ (_a4 = this.onerror) == null ? void 0 : _a4.call(this, error);
1920
+ if (!((_b4 = this.abortController) == null ? void 0 : _b4.signal.aborted)) {
1921
+ this.scheduleInboundSseReconnection();
1922
+ }
1923
+ });
1892
1924
  } catch (error) {
1893
1925
  if (error instanceof Error && error.name === "AbortError") {
1894
1926
  return;
@@ -2161,35 +2193,49 @@ var DefaultMCPClient = class {
2161
2193
  jsonrpc: "2.0",
2162
2194
  id: messageId
2163
2195
  };
2196
+ const rejectWithAbortError = () => {
2197
+ reject(
2198
+ new MCPClientError({
2199
+ message: "Request was aborted",
2200
+ cause: signal == null ? void 0 : signal.reason
2201
+ })
2202
+ );
2203
+ };
2164
2204
  const cleanup = () => {
2165
2205
  this.responseHandlers.delete(messageId);
2206
+ signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
2207
+ };
2208
+ const rejectAndCleanup = (error) => {
2209
+ cleanup();
2210
+ reject(error);
2211
+ };
2212
+ const onAbort = () => {
2213
+ cleanup();
2214
+ rejectWithAbortError();
2166
2215
  };
2167
2216
  this.responseHandlers.set(messageId, (response) => {
2168
2217
  if (signal == null ? void 0 : signal.aborted) {
2169
- return reject(
2170
- new MCPClientError({
2171
- message: "Request was aborted",
2172
- cause: signal.reason
2173
- })
2174
- );
2218
+ cleanup();
2219
+ return rejectWithAbortError();
2175
2220
  }
2176
2221
  if (response instanceof Error) {
2177
- return reject(response);
2222
+ return rejectAndCleanup(response);
2178
2223
  }
2179
2224
  try {
2180
2225
  const result = resultSchema.parse(response.result);
2226
+ cleanup();
2181
2227
  resolve(result);
2182
2228
  } catch (error) {
2183
2229
  const parseError = new MCPClientError({
2184
2230
  message: "Failed to parse server response",
2185
2231
  cause: error
2186
2232
  });
2187
- reject(parseError);
2233
+ rejectAndCleanup(parseError);
2188
2234
  }
2189
2235
  });
2236
+ signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
2190
2237
  this.transport.send(jsonrpcRequest).catch((error) => {
2191
- cleanup();
2192
- reject(error);
2238
+ rejectAndCleanup(error);
2193
2239
  });
2194
2240
  });
2195
2241
  }