@mcp-use/client 2.4.3 → 2.4.4-canary.1

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.
@@ -456,6 +456,7 @@ import {
456
456
  discoverOAuthProtectedResourceMetadata,
457
457
  SdkError,
458
458
  SdkHttpError,
459
+ selectResourceURL,
459
460
  StreamableHTTPClientTransport,
460
461
  UnauthorizedError as UnauthorizedError2
461
462
  } from "@modelcontextprotocol/client";
@@ -632,6 +633,89 @@ function trackConnectorTelemetry(data) {
632
633
  void tracker?.(data);
633
634
  }
634
635
 
636
+ // src/transport/mcp-param-headers.ts
637
+ var MCP_PARAM_HEADER_PREFIX = "Mcp-Param-";
638
+ var X_MCP_HEADER_KEY = "x-mcp-header";
639
+ var RFC9110_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
640
+ var PERMITTED_TYPES = /* @__PURE__ */ new Set(["string", "integer", "boolean", "number"]);
641
+ var BASE64_SENTINEL_PREFIX = "=?base64?";
642
+ var BASE64_SENTINEL_SUFFIX = "?=";
643
+ function sdkMirrorsMcpParamHeaders() {
644
+ const g = globalThis;
645
+ return g.window === void 0 || g.document === void 0;
646
+ }
647
+ function collectDeclarations(schema, path, seen, out) {
648
+ if (schema === null || typeof schema !== "object") return;
649
+ const node = schema;
650
+ const headerName = node[X_MCP_HEADER_KEY];
651
+ if (path.length > 0 && typeof headerName === "string" && RFC9110_TOKEN.test(headerName) && typeof node.type === "string" && PERMITTED_TYPES.has(node.type) && !seen.has(headerName.toLowerCase())) {
652
+ seen.add(headerName.toLowerCase());
653
+ out.push({ path, headerName });
654
+ }
655
+ const properties = node.properties;
656
+ if (properties !== null && typeof properties === "object") {
657
+ for (const [key, child] of Object.entries(properties)) {
658
+ collectDeclarations(child, [...path, key], seen, out);
659
+ }
660
+ }
661
+ }
662
+ function valueAtPath(root, path) {
663
+ let node = root;
664
+ for (const key of path) {
665
+ if (node === null || typeof node !== "object") return void 0;
666
+ node = node[key];
667
+ }
668
+ return node;
669
+ }
670
+ function primitiveToString(value) {
671
+ if (typeof value === "string") return value;
672
+ if (typeof value === "boolean") return value ? "true" : "false";
673
+ if (typeof value === "number") {
674
+ if (!Number.isFinite(value)) return void 0;
675
+ if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
676
+ return void 0;
677
+ }
678
+ return String(value);
679
+ }
680
+ return void 0;
681
+ }
682
+ function needsBase64(value) {
683
+ if (value.length === 0) return true;
684
+ if (value.startsWith(BASE64_SENTINEL_PREFIX) && value.endsWith(BASE64_SENTINEL_SUFFIX)) {
685
+ return true;
686
+ }
687
+ if (value !== value.trim()) return true;
688
+ for (let i = 0; i < value.length; i++) {
689
+ const code = value.charCodeAt(i);
690
+ if (code === 9 || code >= 32 && code <= 126) continue;
691
+ return true;
692
+ }
693
+ return false;
694
+ }
695
+ function utf8ToBase64(value) {
696
+ let binary = "";
697
+ for (const byte of new TextEncoder().encode(value)) {
698
+ binary += String.fromCharCode(byte);
699
+ }
700
+ return btoa(binary);
701
+ }
702
+ function encodeValue(value) {
703
+ return needsBase64(value) ? `${BASE64_SENTINEL_PREFIX}${utf8ToBase64(value)}${BASE64_SENTINEL_SUFFIX}` : value;
704
+ }
705
+ function buildMcpParamHeaders(inputSchema, args) {
706
+ const declarations = [];
707
+ collectDeclarations(inputSchema, [], /* @__PURE__ */ new Set(), declarations);
708
+ const headers = {};
709
+ for (const { path, headerName } of declarations) {
710
+ const raw = valueAtPath(args, path);
711
+ if (raw === void 0 || raw === null) continue;
712
+ const value = primitiveToString(raw);
713
+ if (value === void 0) continue;
714
+ headers[`${MCP_PARAM_HEADER_PREFIX}${headerName}`] = encodeValue(value);
715
+ }
716
+ return headers;
717
+ }
718
+
635
719
  // src/transport/base.ts
636
720
  var passthroughResultSchema = {
637
721
  "~standard": {
@@ -1092,7 +1176,7 @@ var BaseConnector = class {
1092
1176
  if (!this.client) {
1093
1177
  throw new Error("MCP client is not connected");
1094
1178
  }
1095
- const enhancedOptions = options ? { ...options } : void 0;
1179
+ let enhancedOptions = options ? { ...options } : void 0;
1096
1180
  if (enhancedOptions?.resetTimeoutOnProgress && !enhancedOptions.onprogress) {
1097
1181
  enhancedOptions.onprogress = () => {
1098
1182
  };
@@ -1100,6 +1184,16 @@ var BaseConnector = class {
1100
1184
  `[BaseConnector] Added onprogress callback for tool '${name}' to enable progressToken`
1101
1185
  );
1102
1186
  }
1187
+ if (this.protocolEra === "modern" && !sdkMirrorsMcpParamHeaders()) {
1188
+ const tool = this.toolsCache?.find((t) => t.name === name);
1189
+ const paramHeaders = tool ? buildMcpParamHeaders(tool.inputSchema, args) : {};
1190
+ if (Object.keys(paramHeaders).length > 0) {
1191
+ enhancedOptions = {
1192
+ ...enhancedOptions,
1193
+ headers: { ...enhancedOptions?.headers, ...paramHeaders }
1194
+ };
1195
+ }
1196
+ }
1103
1197
  logger.debug(`Calling tool '${name}' with args`, args);
1104
1198
  const res = await this.executeRequest(
1105
1199
  () => this.client.callTool({ name, arguments: args }, enhancedOptions)
@@ -1533,19 +1627,20 @@ var HttpConnector = class extends BaseConnector {
1533
1627
  await this.completeInteractiveAuthorization();
1534
1628
  }
1535
1629
  async discoverAuthorization() {
1536
- if (!this.detectMixedAuth || !this.oauthProvider || this.hadAccessTokenAtConnect) {
1630
+ const provider = this.oauthProvider;
1631
+ if (!this.detectMixedAuth || !provider || this.hadAccessTokenAtConnect) {
1537
1632
  return this.authorizationCache;
1538
1633
  }
1539
1634
  if (this.authorizationDiscovery) return this.authorizationDiscovery;
1540
- this.authorizationDiscovery = this.discoverMixedAuthorization().then(
1541
- (authorization) => {
1542
- if (!authorization) this.authorizationDiscovery = null;
1543
- return authorization;
1544
- }
1545
- );
1635
+ this.authorizationDiscovery = this.discoverMixedAuthorization(
1636
+ provider
1637
+ ).then((authorization) => {
1638
+ if (!authorization) this.authorizationDiscovery = null;
1639
+ return authorization;
1640
+ });
1546
1641
  return this.authorizationDiscovery;
1547
1642
  }
1548
- async discoverMixedAuthorization() {
1643
+ async discoverMixedAuthorization(provider) {
1549
1644
  const controller = new AbortController();
1550
1645
  let timeout;
1551
1646
  const discoveryTimeout = new Promise((_, reject) => {
@@ -1567,6 +1662,7 @@ var HttpConnector = class extends BaseConnector {
1567
1662
  ),
1568
1663
  discoveryTimeout
1569
1664
  ]);
1665
+ await selectResourceURL(this.baseUrl, provider, metadata);
1570
1666
  this.authorizationCache = {
1571
1667
  mode: "mixed",
1572
1668
  authenticated: false,
@@ -1883,7 +1979,7 @@ var HttpConnector = class extends BaseConnector {
1883
1979
  };
1884
1980
 
1885
1981
  // src/utils/version.ts
1886
- var VERSION = "2.4.3";
1982
+ var VERSION = "2.4.4-canary.1";
1887
1983
  function getPackageVersion() {
1888
1984
  return VERSION;
1889
1985
  }
@@ -2232,7 +2328,11 @@ var OAuthSessionStore = class _OAuthSessionStore {
2232
2328
  if (legacyData) {
2233
2329
  try {
2234
2330
  const legacyValue = JSON.parse(legacyData);
2235
- if (!legacyValue.issuer || legacyValue.issuer === ctx.issuer) {
2331
+ if (typeof legacyValue.issuer !== "string" || !legacyValue.issuer.trim()) {
2332
+ await this.store.remove(legacyKey);
2333
+ return void 0;
2334
+ }
2335
+ if (legacyValue.issuer === ctx.issuer) {
2236
2336
  const migratedValue = {
2237
2337
  ...legacyValue,
2238
2338
  issuer: ctx.issuer
@@ -2250,7 +2350,12 @@ var OAuthSessionStore = class _OAuthSessionStore {
2250
2350
  }
2251
2351
  if (!data) return void 0;
2252
2352
  try {
2253
- return { key, value: JSON.parse(data) };
2353
+ const value = JSON.parse(data);
2354
+ if (typeof value?.issuer !== "string" || !value.issuer.trim() || ctx && value.issuer !== ctx.issuer) {
2355
+ await this.store.remove(key);
2356
+ return void 0;
2357
+ }
2358
+ return { key, value };
2254
2359
  } catch (e) {
2255
2360
  console.warn(
2256
2361
  `[${this.storageKeyPrefix}] Failed to parse ${kind.replace("_", " ")}:`,