@ai-sdk/mcp 0.0.17 → 0.0.19

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
@@ -37,6 +37,7 @@ import {
37
37
  } from "@ai-sdk/provider-utils";
38
38
 
39
39
  // src/tool/json-rpc-message.ts
40
+ import { parseJSON } from "@ai-sdk/provider-utils";
40
41
  import { z as z2 } from "zod/v4";
41
42
 
42
43
  // src/tool/types.ts
@@ -256,6 +257,9 @@ var JSONRPCMessageSchema = z2.union([
256
257
  JSONRPCResponseSchema,
257
258
  JSONRPCErrorSchema
258
259
  ]);
260
+ async function parseJSONRPCMessage(text) {
261
+ return JSONRPCMessageSchema.parse(await parseJSON({ text }));
262
+ }
259
263
 
260
264
  // src/version.ts
261
265
  var VERSION = typeof __PACKAGE_VERSION__ !== "undefined" ? __PACKAGE_VERSION__ : "0.0.0-test";
@@ -433,6 +437,7 @@ function checkResourceAllowed({
433
437
  }
434
438
 
435
439
  // src/tool/oauth.ts
440
+ import { parseJSON as parseJSON2 } from "@ai-sdk/provider-utils";
436
441
  var UnauthorizedError = class extends Error {
437
442
  constructor(message = "Unauthorized") {
438
443
  super(message);
@@ -711,7 +716,9 @@ async function parseErrorResponse(input) {
711
716
  const statusCode = input instanceof Response ? input.status : void 0;
712
717
  const body = input instanceof Response ? await input.text() : input;
713
718
  try {
714
- const result = OAuthErrorResponseSchema.parse(JSON.parse(body));
719
+ const result = OAuthErrorResponseSchema.parse(
720
+ await parseJSON2({ text: body })
721
+ );
715
722
  const { error, error_description, error_uri } = result;
716
723
  const errorClass = OAUTH_ERRORS[error] || ServerError;
717
724
  return new errorClass({
@@ -1079,7 +1086,7 @@ var SseMCPTransport = class {
1079
1086
  const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream());
1080
1087
  const reader = stream.getReader();
1081
1088
  const processEvents = async () => {
1082
- var _a4, _b4, _c2;
1089
+ var _a4, _b4, _c2, _d2, _e2;
1083
1090
  try {
1084
1091
  while (true) {
1085
1092
  const { done, value } = await reader.read();
@@ -1094,26 +1101,32 @@ var SseMCPTransport = class {
1094
1101
  }
1095
1102
  const { event, data } = value;
1096
1103
  if (event === "endpoint") {
1097
- this.endpoint = new URL(data, this.url);
1098
- if (this.endpoint.origin !== this.url.origin) {
1104
+ if (this.endpoint) {
1105
+ continue;
1106
+ }
1107
+ const endpoint = new URL(data, this.url);
1108
+ if (endpoint.origin !== this.url.origin) {
1109
+ this.connected = false;
1110
+ this.endpoint = void 0;
1111
+ (_a4 = this.sseConnection) == null ? void 0 : _a4.close();
1112
+ (_b4 = this.abortController) == null ? void 0 : _b4.abort();
1099
1113
  throw new MCPClientError({
1100
- message: `MCP SSE Transport Error: Endpoint origin does not match connection origin: ${this.endpoint.origin}`
1114
+ message: `MCP SSE Transport Error: Endpoint origin does not match connection origin: ${endpoint.origin}`
1101
1115
  });
1102
1116
  }
1117
+ this.endpoint = endpoint;
1103
1118
  this.connected = true;
1104
1119
  resolve();
1105
1120
  } else if (event === "message") {
1106
1121
  try {
1107
- const message = JSONRPCMessageSchema.parse(
1108
- JSON.parse(data)
1109
- );
1110
- (_a4 = this.onmessage) == null ? void 0 : _a4.call(this, message);
1122
+ const message = await parseJSONRPCMessage(data);
1123
+ (_c2 = this.onmessage) == null ? void 0 : _c2.call(this, message);
1111
1124
  } catch (error) {
1112
1125
  const e = new MCPClientError({
1113
1126
  message: "MCP SSE Transport Error: Failed to parse message",
1114
1127
  cause: error
1115
1128
  });
1116
- (_b4 = this.onerror) == null ? void 0 : _b4.call(this, e);
1129
+ (_d2 = this.onerror) == null ? void 0 : _d2.call(this, e);
1117
1130
  }
1118
1131
  }
1119
1132
  }
@@ -1121,7 +1134,7 @@ var SseMCPTransport = class {
1121
1134
  if (error instanceof Error && error.name === "AbortError") {
1122
1135
  return;
1123
1136
  }
1124
- (_c2 = this.onerror) == null ? void 0 : _c2.call(this, error);
1137
+ (_e2 = this.onerror) == null ? void 0 : _e2.call(this, error);
1125
1138
  reject(error);
1126
1139
  }
1127
1140
  };
@@ -1143,6 +1156,7 @@ var SseMCPTransport = class {
1143
1156
  async close() {
1144
1157
  var _a3, _b3, _c;
1145
1158
  this.connected = false;
1159
+ this.endpoint = void 0;
1146
1160
  (_a3 = this.sseConnection) == null ? void 0 : _a3.close();
1147
1161
  (_b3 = this.abortController) == null ? void 0 : _b3.abort();
1148
1162
  (_c = this.onclose) == null ? void 0 : _c.call(this);
@@ -1352,7 +1366,7 @@ var HttpMCPTransport = class {
1352
1366
  const { event, data } = value;
1353
1367
  if (event === "message") {
1354
1368
  try {
1355
- const msg = JSONRPCMessageSchema.parse(JSON.parse(data));
1369
+ const msg = await parseJSONRPCMessage(data);
1356
1370
  (_a4 = this.onmessage) == null ? void 0 : _a4.call(this, msg);
1357
1371
  } catch (error2) {
1358
1372
  const e = new MCPClientError({
@@ -1477,7 +1491,7 @@ var HttpMCPTransport = class {
1477
1491
  }
1478
1492
  if (event === "message") {
1479
1493
  try {
1480
- const msg = JSONRPCMessageSchema.parse(JSON.parse(data));
1494
+ const msg = await parseJSONRPCMessage(data);
1481
1495
  (_a4 = this.onmessage) == null ? void 0 : _a4.call(this, msg);
1482
1496
  } catch (error) {
1483
1497
  const e = new MCPClientError({
@@ -1830,7 +1844,7 @@ var DefaultMCPClient = class {
1830
1844
  try {
1831
1845
  const listToolsResult = await this.listTools();
1832
1846
  for (const { name: name3, description, inputSchema } of listToolsResult.tools) {
1833
- if (schemas !== "automatic" && !(name3 in schemas)) {
1847
+ if (schemas !== "automatic" && !Object.prototype.hasOwnProperty.call(schemas, name3)) {
1834
1848
  continue;
1835
1849
  }
1836
1850
  const self = this;