@mcp-use/client 2.0.0-beta.13 → 2.0.0-beta.14

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.
@@ -1534,7 +1534,7 @@ var HttpConnector = class extends BaseConnector {
1534
1534
  };
1535
1535
 
1536
1536
  // src/utils/version.ts
1537
- var VERSION = "2.0.0-beta.13";
1537
+ var VERSION = "2.0.0-beta.14";
1538
1538
  function getPackageVersion() {
1539
1539
  return VERSION;
1540
1540
  }
@@ -2068,7 +2068,7 @@ var OAuthSessionStore = class _OAuthSessionStore {
2068
2068
  */
2069
2069
  async storeAuthorizationState(authorizationUrl, opts = {}) {
2070
2070
  const state = globalThis.crypto.randomUUID();
2071
- const stateKey = `${this.storageKeyPrefix}:state_${state}`;
2071
+ const stateKey = `${this.storageKeyPrefix}_${this.serverUrlHash}_state_${state}`;
2072
2072
  const stateData = {
2073
2073
  serverUrlHash: this.serverUrlHash,
2074
2074
  expiry: Date.now() + 1e3 * 60 * 10,
@@ -2140,6 +2140,7 @@ var BrowserOAuthClientProvider = class {
2140
2140
  connectionUrl;
2141
2141
  proxyOAuthRequests;
2142
2142
  lastAttemptedAuthUrl = null;
2143
+ authorizationPending = false;
2143
2144
  onPopupWindow;
2144
2145
  constructor(serverUrl, options = {}) {
2145
2146
  if (options.staticClientInfo?.client_secret) {
@@ -2190,6 +2191,12 @@ var BrowserOAuthClientProvider = class {
2190
2191
  getKey(keySuffix) {
2191
2192
  return this.session.getKey(keySuffix);
2192
2193
  }
2194
+ get hasPendingFlow() {
2195
+ return this.authorizationPending;
2196
+ }
2197
+ markFlowComplete() {
2198
+ this.authorizationPending = false;
2199
+ }
2193
2200
  /**
2194
2201
  * Re-anchor an SDK-derived OAuth discovery URL from the MCP connection
2195
2202
  * (proxy) origin onto the actual MCP server.
@@ -2370,6 +2377,7 @@ var BrowserOAuthClientProvider = class {
2370
2377
  }
2371
2378
  saveTokens(tokens, ctx) {
2372
2379
  this.lastAttemptedAuthUrl = null;
2380
+ this.authorizationPending = false;
2373
2381
  return this.session.saveTokens(tokens, ctx);
2374
2382
  }
2375
2383
  async clientInformation(ctx) {
@@ -2450,6 +2458,7 @@ var BrowserOAuthClientProvider = class {
2450
2458
  }
2451
2459
  );
2452
2460
  this.lastAttemptedAuthUrl = prepared;
2461
+ this.authorizationPending = true;
2453
2462
  return prepared;
2454
2463
  }
2455
2464
  /**
@@ -2506,15 +2515,13 @@ var BrowserOAuthClientProvider = class {
2506
2515
  }
2507
2516
  clearStorage() {
2508
2517
  this.lastAttemptedAuthUrl = null;
2518
+ this.authorizationPending = false;
2509
2519
  const prefixPattern = `${this.storageKeyPrefix}_${this.serverUrlHash}_`;
2510
- const statePattern = `${this.storageKeyPrefix}:state_`;
2511
2520
  const keysToRemove = [];
2512
2521
  let count = 0;
2513
2522
  for (const key of this.storage.keys()) {
2514
2523
  if (key.startsWith(prefixPattern)) {
2515
2524
  keysToRemove.push(key);
2516
- } else if (key.startsWith(statePattern)) {
2517
- keysToRemove.push(key);
2518
2525
  }
2519
2526
  }
2520
2527
  const uniqueKeysToRemove = [...new Set(keysToRemove)];
@@ -2550,16 +2557,13 @@ var MCPAgentExecutionEvent = class extends BaseTelemetryEvent {
2550
2557
  return {
2551
2558
  // Core execution info
2552
2559
  execution_method: this.data.executionMethod,
2553
- query: this.data.query,
2554
2560
  query_length: this.data.query.length,
2555
2561
  success: this.data.success,
2556
2562
  // Agent configuration
2557
2563
  model_provider: this.data.modelProvider,
2558
2564
  model_name: this.data.modelName,
2559
2565
  server_count: this.data.serverCount,
2560
- server_identifiers: this.data.serverIdentifiers,
2561
2566
  total_tools_available: this.data.totalToolsAvailable,
2562
- tools_available_names: this.data.toolsAvailableNames,
2563
2567
  max_steps_configured: this.data.maxStepsConfigured,
2564
2568
  memory_enabled: this.data.memoryEnabled,
2565
2569
  use_server_manager: this.data.useServerManager,
@@ -2570,8 +2574,6 @@ var MCPAgentExecutionEvent = class extends BaseTelemetryEvent {
2570
2574
  // Execution results (always include, even if null)
2571
2575
  steps_taken: this.data.stepsTaken ?? null,
2572
2576
  tools_used_count: this.data.toolsUsedCount ?? null,
2573
- tools_used_names: this.data.toolsUsedNames ?? null,
2574
- response: this.data.response ?? null,
2575
2577
  response_length: this.data.response ? this.data.response.length : null,
2576
2578
  execution_time_ms: this.data.executionTimeMs ?? null,
2577
2579
  error_type: this.data.errorType ?? null,
@@ -2667,21 +2669,72 @@ async function telFetch(url, init) {
2667
2669
  }
2668
2670
  var POSTHOG_HOST = "https://eu.i.posthog.com";
2669
2671
  var POSTHOG_API_KEY = "phc_lyTtbYwvkdSbrcMQNPiKiiRWrrM1seyKIMjycSvItEI";
2670
- function capturePostHog(params) {
2671
- const host = params.host ?? POSTHOG_HOST;
2672
- const apiKey = params.apiKey ?? POSTHOG_API_KEY;
2673
- return telFetch(`${host}/i/v0/e/`, {
2674
- method: "POST",
2675
- headers: { "Content-Type": "application/json" },
2676
- keepalive: true,
2677
- body: JSON.stringify({
2672
+ var CONTENT_PROPERTY = /(^|_)(arguments?|args|body|command|headers?|location|message|query|response|secret|subject|token|uri|url|user_agent)(_|$)/i;
2673
+ var IDENTIFYING_PROPERTY = /(^|_)(server_identifiers?|server_names?|servers|tool_names?|tools_(available|used)_names)(_|$)/i;
2674
+ var AGGREGATE_PROPERTY = /(_count|_length|_duration(?:_ms)?|_time_ms|(^|_)num_[a-z0-9_]+)$/i;
2675
+ function normalizePropertyKey(key) {
2676
+ return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^a-z0-9_$]+/gi, "_").toLowerCase();
2677
+ }
2678
+ function sanitizeValue(value, seen) {
2679
+ if (Array.isArray(value)) {
2680
+ if (seen.has(value)) {
2681
+ throw new TypeError("Cyclic telemetry properties are not supported");
2682
+ }
2683
+ seen.add(value);
2684
+ const sanitized = value.map((item) => sanitizeValue(item, seen));
2685
+ seen.delete(value);
2686
+ return sanitized;
2687
+ }
2688
+ if (value !== null && typeof value === "object" && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
2689
+ if (seen.has(value)) {
2690
+ throw new TypeError("Cyclic telemetry properties are not supported");
2691
+ }
2692
+ seen.add(value);
2693
+ const sanitized = sanitizeProperties(
2694
+ value,
2695
+ seen
2696
+ );
2697
+ seen.delete(value);
2698
+ return sanitized;
2699
+ }
2700
+ return value;
2701
+ }
2702
+ function sanitizeProperties(properties, seen = /* @__PURE__ */ new WeakSet()) {
2703
+ const sanitized = {};
2704
+ for (const [key, value] of Object.entries(properties)) {
2705
+ const normalizedKey = normalizePropertyKey(key);
2706
+ if (AGGREGATE_PROPERTY.test(normalizedKey)) {
2707
+ if (value === null || typeof value === "number") {
2708
+ sanitized[key] = value;
2709
+ }
2710
+ continue;
2711
+ }
2712
+ if (IDENTIFYING_PROPERTY.test(normalizedKey) || CONTENT_PROPERTY.test(normalizedKey)) {
2713
+ continue;
2714
+ }
2715
+ sanitized[key] = sanitizeValue(value, seen);
2716
+ }
2717
+ return sanitized;
2718
+ }
2719
+ async function capturePostHog(params) {
2720
+ try {
2721
+ const host = params.host ?? POSTHOG_HOST;
2722
+ const apiKey = params.apiKey ?? POSTHOG_API_KEY;
2723
+ const body = JSON.stringify({
2678
2724
  api_key: apiKey,
2679
2725
  event: params.event,
2680
2726
  distinct_id: params.distinctId,
2681
- properties: params.properties,
2727
+ properties: sanitizeProperties(params.properties),
2682
2728
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
2683
- })
2684
- });
2729
+ });
2730
+ await telFetch(`${host}/i/v0/e/`, {
2731
+ method: "POST",
2732
+ headers: { "Content-Type": "application/json" },
2733
+ keepalive: true,
2734
+ body
2735
+ });
2736
+ } catch {
2737
+ }
2685
2738
  }
2686
2739
 
2687
2740
  // src/telemetry/telemetry.ts
@@ -3013,11 +3066,12 @@ async function completeOAuthFlow(provider, serverUrl, options = {}) {
3013
3066
  throw new Error(`Unexpected OAuth auth() result: ${result}`);
3014
3067
  }
3015
3068
  }
3016
- if (typeof flowProvider.getAuthorizationCode === "function") {
3017
- const code = await flowProvider.getAuthorizationCode();
3069
+ if (typeof flowProvider.getAuthorizationResponse === "function" || typeof flowProvider.getAuthorizationCode === "function") {
3070
+ const response = typeof flowProvider.getAuthorizationResponse === "function" ? await flowProvider.getAuthorizationResponse() : { code: await flowProvider.getAuthorizationCode() };
3018
3071
  await auth(provider, {
3019
3072
  serverUrl,
3020
- authorizationCode: code,
3073
+ authorizationCode: response.code,
3074
+ ...response.iss !== void 0 ? { iss: response.iss } : {},
3021
3075
  fetchFn
3022
3076
  });
3023
3077
  return;
@@ -3031,6 +3085,8 @@ async function waitForBrowserAuthComplete(provider, timeoutMs) {
3031
3085
  );
3032
3086
  }
3033
3087
  if (provider.useRedirectFlow) {
3088
+ await new Promise(() => {
3089
+ });
3034
3090
  return;
3035
3091
  }
3036
3092
  const tokensKey = provider.getKey?.("tokens");
@@ -3047,25 +3103,29 @@ async function waitForBrowserAuthComplete(provider, timeoutMs) {
3047
3103
  } catch {
3048
3104
  }
3049
3105
  }
3050
- const result = await runAuthPopup({
3051
- popup: null,
3052
- state,
3053
- tokensKey,
3054
- timeoutMs
3055
- });
3056
- switch (result.kind) {
3057
- case "success":
3058
- return;
3059
- case "cancelled":
3060
- throw new Error("OAuth authentication was cancelled.");
3061
- case "timeout":
3062
- throw new Error(
3063
- `OAuth callback not received within ${timeoutMs}ms. Ensure /oauth/callback calls onMcpAuthorization().`
3064
- );
3065
- case "error":
3066
- throw new Error(result.error);
3067
- default:
3068
- throw new Error("Unexpected OAuth popup result");
3106
+ try {
3107
+ const result = await runAuthPopup({
3108
+ popup: null,
3109
+ state,
3110
+ tokensKey,
3111
+ timeoutMs
3112
+ });
3113
+ switch (result.kind) {
3114
+ case "success":
3115
+ return;
3116
+ case "cancelled":
3117
+ throw new Error("OAuth authentication was cancelled.");
3118
+ case "timeout":
3119
+ throw new Error(
3120
+ `OAuth callback not received within ${timeoutMs}ms. Ensure /oauth/callback calls onMcpAuthorization().`
3121
+ );
3122
+ case "error":
3123
+ throw new Error(result.error);
3124
+ default:
3125
+ throw new Error("Unexpected OAuth popup result");
3126
+ }
3127
+ } finally {
3128
+ provider.markFlowComplete?.();
3069
3129
  }
3070
3130
  }
3071
3131
 
@@ -4544,6 +4604,16 @@ function useMcpOperations(params) {
4544
4604
  };
4545
4605
  }
4546
4606
 
4607
+ // src/react/token-expiry.ts
4608
+ function getOAuthTokenExpiry(tokens) {
4609
+ try {
4610
+ const payload = JSON.parse(atob(tokens.access_token?.split(".")[1] ?? ""));
4611
+ if (typeof payload.exp === "number") return payload.exp * 1e3;
4612
+ } catch {
4613
+ }
4614
+ return typeof tokens.expires_in === "number" ? Date.now() + tokens.expires_in * 1e3 : void 0;
4615
+ }
4616
+
4547
4617
  // src/react/useMcp.ts
4548
4618
  var DEFAULT_RECONNECT_DELAY = 3e3;
4549
4619
  var DEFAULT_RETRY_DELAY = 5e3;
@@ -5257,7 +5327,7 @@ function useMcp(options) {
5257
5327
  return "failed";
5258
5328
  }
5259
5329
  if (tokens?.access_token) {
5260
- const expiresAt = tokens.expires_in ? Date.now() + tokens.expires_in * 1e3 : void 0;
5330
+ const expiresAt = getOAuthTokenExpiry(tokens);
5261
5331
  let tokenEndpoint = null;
5262
5332
  let resource = null;
5263
5333
  let clientCreds = null;
@@ -5367,8 +5437,10 @@ function useMcp(options) {
5367
5437
  fetchFn: authProviderRef.current.getProxyFetch?.()
5368
5438
  });
5369
5439
  if (authResult === "REDIRECT") {
5370
- const authCode = await authProviderRef.current.getAuthorizationCode?.();
5371
- if (!authCode) {
5440
+ const flowProvider = authProviderRef.current;
5441
+ const authResponse = await flowProvider.getAuthorizationResponse?.();
5442
+ const authCode = authResponse?.code ?? await flowProvider.getAuthorizationCode?.();
5443
+ if (typeof authCode !== "string") {
5372
5444
  throw new Error(
5373
5445
  "Authorization code not captured by headless provider"
5374
5446
  );
@@ -5376,6 +5448,7 @@ function useMcp(options) {
5376
5448
  await auth2(authProviderRef.current, {
5377
5449
  serverUrl: url,
5378
5450
  authorizationCode: authCode,
5451
+ ...authResponse?.iss !== void 0 ? { iss: authResponse.iss } : {},
5379
5452
  fetchFn: authProviderRef.current.getProxyFetch?.()
5380
5453
  });
5381
5454
  }
@@ -5970,20 +6043,14 @@ function renderResult(title, message, error, returnUrl) {
5970
6043
  }
5971
6044
  document.body.appendChild(container);
5972
6045
  }
5973
- function findStoredState(state) {
5974
- const defaultKey = `mcp:auth:state_${state}`;
5975
- let key = localStorage.getItem(defaultKey) ? defaultKey : null;
5976
- if (!key) {
5977
- const suffix = `:state_${state}`;
5978
- for (let index = 0; index < localStorage.length; index++) {
5979
- const candidate = localStorage.key(index);
5980
- if (candidate?.endsWith(suffix)) {
5981
- key = candidate;
5982
- break;
5983
- }
5984
- }
5985
- }
5986
- const serialized = key ? localStorage.getItem(key) : null;
6046
+ async function findStoredState(state) {
6047
+ const store = new LocalStorageKVStore();
6048
+ const legacySuffix = `:state_${state}`;
6049
+ const scopedSuffix = `_state_${state}`;
6050
+ const key = (await store.keys()).find(
6051
+ (candidate) => candidate.endsWith(legacySuffix) || candidate.endsWith(scopedSuffix)
6052
+ );
6053
+ const serialized = key ? await store.get(key) : null;
5987
6054
  if (!key || !serialized) {
5988
6055
  throw new Error(`Invalid or expired OAuth state "${state}".`);
5989
6056
  }
@@ -5991,10 +6058,10 @@ function findStoredState(state) {
5991
6058
  try {
5992
6059
  value = JSON.parse(serialized);
5993
6060
  } catch {
5994
- localStorage.removeItem(key);
6061
+ await store.remove(key);
5995
6062
  throw new Error("Failed to parse stored OAuth state.");
5996
6063
  }
5997
- return { key, value };
6064
+ return { key, value, store };
5998
6065
  }
5999
6066
  function redirectWithError(returnUrl, message) {
6000
6067
  const url = new URL(returnUrl);
@@ -6053,17 +6120,19 @@ async function completeAuthorization() {
6053
6120
  const callbackParams = new URLSearchParams(window.location.search);
6054
6121
  const state = callbackParams.get("state");
6055
6122
  let stateKey = null;
6123
+ let stateStore = null;
6056
6124
  let storedState = null;
6057
6125
  let provider = null;
6058
6126
  try {
6059
6127
  if (!state) {
6060
6128
  throw new Error("OAuth callback is missing the state parameter.");
6061
6129
  }
6062
- const stored = findStoredState(state);
6130
+ const stored = await findStoredState(state);
6063
6131
  stateKey = stored.key;
6132
+ stateStore = stored.store;
6064
6133
  storedState = stored.value;
6065
6134
  if (!storedState.expiry || storedState.expiry < Date.now()) {
6066
- localStorage.removeItem(stateKey);
6135
+ await stateStore.remove(stateKey);
6067
6136
  throw new Error(
6068
6137
  "OAuth state has expired. Please start authentication again."
6069
6138
  );
@@ -6078,7 +6147,7 @@ async function completeAuthorization() {
6078
6147
  fetch: provider.getProxyFetch()
6079
6148
  });
6080
6149
  await transport.finishAuth(callbackParams);
6081
- localStorage.removeItem(stateKey);
6150
+ await stateStore.remove(stateKey);
6082
6151
  signalResult(true, void 0, storedState, {
6083
6152
  state,
6084
6153
  serverUrlHash: storedState.serverUrlHash
@@ -6086,8 +6155,12 @@ async function completeAuthorization() {
6086
6155
  } catch (error) {
6087
6156
  const message = error instanceof Error ? error.message : String(error);
6088
6157
  console.error("[mcp-callback] OAuth callback failed:", error);
6089
- if (stateKey) localStorage.removeItem(stateKey);
6090
- if (provider) localStorage.removeItem(provider.getKey("last_auth_url"));
6158
+ if (stateKey && stateStore) await stateStore.remove(stateKey);
6159
+ if (provider) {
6160
+ await (stateStore ?? new LocalStorageKVStore()).remove(
6161
+ provider.getKey("last_auth_url")
6162
+ );
6163
+ }
6091
6164
  signalResult(false, message, storedState, {
6092
6165
  state,
6093
6166
  serverUrlHash: storedState?.serverUrlHash
@@ -6963,7 +7036,13 @@ var LocalStorageProvider = class {
6963
7036
  );
6964
7037
  const serialized = JSON.stringify(sanitized);
6965
7038
  if (serialized !== stored) {
6966
- localStorage.setItem(this.storageKey, serialized);
7039
+ try {
7040
+ localStorage.setItem(this.storageKey, serialized);
7041
+ } catch {
7042
+ console.error(
7043
+ "[LocalStorageProvider] Failed to persist sanitized servers."
7044
+ );
7045
+ }
6967
7046
  }
6968
7047
  return sanitized;
6969
7048
  } catch {
@@ -7158,8 +7237,20 @@ function findOpeningConstructEnd(html, lowercasePrefix) {
7158
7237
  if (start === -1) return void 0;
7159
7238
  const boundary = lowercaseHtml[start + lowercasePrefix.length];
7160
7239
  if (boundary === ">" || boundary === " " || boundary === " " || boundary === "\n" || boundary === "\r" || boundary === "\f") {
7161
- const end = lowercaseHtml.indexOf(">", start + lowercasePrefix.length);
7162
- return end === -1 ? void 0 : end + 1;
7240
+ let quote;
7241
+ for (let index = start + lowercasePrefix.length; index < html.length; index++) {
7242
+ const character = html[index];
7243
+ if (quote) {
7244
+ if (character === quote) quote = void 0;
7245
+ continue;
7246
+ }
7247
+ if (character === '"' || character === "'") {
7248
+ quote = character;
7249
+ continue;
7250
+ }
7251
+ if (character === ">") return index + 1;
7252
+ }
7253
+ return void 0;
7163
7254
  }
7164
7255
  searchFrom = start + lowercasePrefix.length;
7165
7256
  }
@@ -7582,6 +7673,63 @@ var VIEW_DIMENSIONS = {
7582
7673
  FULLSCREEN_HEADER_HEIGHT: 50
7583
7674
  };
7584
7675
 
7676
+ // src/react/view/view-host-policy.ts
7677
+ function buildDefaultHostCapabilities({
7678
+ hasConnection,
7679
+ hasMessageHandler,
7680
+ hasModelContextHandler,
7681
+ hasLogHandler,
7682
+ messageCapabilities,
7683
+ modelContextCapabilities
7684
+ }) {
7685
+ return {
7686
+ openLinks: {},
7687
+ ...hasConnection ? {
7688
+ serverTools: {},
7689
+ serverResources: {}
7690
+ } : {},
7691
+ ...hasLogHandler ? { logging: {} } : {},
7692
+ ...hasModelContextHandler ? { updateModelContext: modelContextCapabilities ?? { text: {} } } : {},
7693
+ ...hasMessageHandler ? { message: messageCapabilities ?? { text: {} } } : {}
7694
+ };
7695
+ }
7696
+ function isToolVisibleToModel(tool) {
7697
+ if (!tool._meta || typeof tool._meta !== "object") return true;
7698
+ const ui = tool._meta.ui;
7699
+ if (!ui || typeof ui !== "object") return true;
7700
+ const visibility = ui.visibility;
7701
+ return !Array.isArray(visibility) || visibility.some((value) => value === "model");
7702
+ }
7703
+ async function dispatchUiMessage(handler, content) {
7704
+ if (!handler) {
7705
+ throw new Error("This host surface does not support ui/message");
7706
+ }
7707
+ if (content.length === 0) {
7708
+ throw new Error("ui/message requires at least one content block");
7709
+ }
7710
+ await handler(content);
7711
+ }
7712
+ function resolveRequestedDisplayMode({
7713
+ requested,
7714
+ current,
7715
+ hostAvailable,
7716
+ appAvailable
7717
+ }) {
7718
+ const hostModes = hostAvailable ?? ["inline"];
7719
+ const appModes = appAvailable ?? ["inline"];
7720
+ return hostModes.includes(requested) && appModes.includes(requested) ? requested : current;
7721
+ }
7722
+ function assertAppCanCallTool(tools, name) {
7723
+ const tool = tools?.find((candidate) => candidate.name === name);
7724
+ if (!tool) {
7725
+ throw new Error(`Tool "${name}" is not available to this app`);
7726
+ }
7727
+ const visibility = tool._meta?.ui?.visibility;
7728
+ if (visibility && !visibility.includes("app")) {
7729
+ throw new Error(`Tool "${name}" is not available to this app`);
7730
+ }
7731
+ }
7732
+
7585
7733
  // src/react/view/view-detection.ts
7586
7734
  function getViewResourceUri(toolMeta) {
7587
7735
  const uri = toolMeta?.ui;
@@ -7601,15 +7749,6 @@ function isViewResource(mimeType) {
7601
7749
  var DEFAULT_HOST_INFO = { name: "mcp-use-client", version: "2.0.0" };
7602
7750
  var DEFAULT_TOOL_CALL_TIMEOUT = 6e5;
7603
7751
  var SANDBOX_PROXY_READY = "ui/notifications/sandbox-proxy-ready";
7604
- var DEFAULT_HOST_CAPABILITIES = {
7605
- openLinks: {},
7606
- serverTools: {},
7607
- serverResources: {},
7608
- logging: {},
7609
- updateModelContext: { text: {} },
7610
- // ponytail: always advertised; bridge.onmessage no-ops when onMessage unset
7611
- message: { text: {} }
7612
- };
7613
7752
  function CloseIcon() {
7614
7753
  return /* @__PURE__ */ React2.createElement(
7615
7754
  "svg",
@@ -7673,6 +7812,8 @@ function ViewRendererBase({
7673
7812
  hostInfo = DEFAULT_HOST_INFO,
7674
7813
  hostContext,
7675
7814
  hostCapabilities,
7815
+ messageCapabilities,
7816
+ modelContextCapabilities,
7676
7817
  cspMode = "widget-declared",
7677
7818
  displayMode: displayModeProp,
7678
7819
  onDisplayModeChange,
@@ -7716,6 +7857,28 @@ function ViewRendererBase({
7716
7857
  );
7717
7858
  const [internalDisplayMode, setInternalDisplayMode] = useState4("inline");
7718
7859
  const displayMode = displayModeProp ?? internalDisplayMode;
7860
+ const effectiveHostCapabilities = useMemo3(
7861
+ () => ({
7862
+ ...buildDefaultHostCapabilities({
7863
+ hasConnection: source.kind === "live",
7864
+ hasMessageHandler: onMessage !== void 0,
7865
+ hasModelContextHandler: onModelContextUpdate !== void 0,
7866
+ hasLogHandler: onLog !== void 0,
7867
+ messageCapabilities,
7868
+ modelContextCapabilities
7869
+ }),
7870
+ ...hostCapabilities
7871
+ }),
7872
+ [
7873
+ hostCapabilities,
7874
+ messageCapabilities,
7875
+ modelContextCapabilities,
7876
+ onLog,
7877
+ onMessage,
7878
+ onModelContextUpdate,
7879
+ source.kind
7880
+ ]
7881
+ );
7719
7882
  const effectiveHostContext = useMemo3(() => {
7720
7883
  if (!hostContext) return hostContext;
7721
7884
  if (hostContext.displayMode === displayMode) return hostContext;
@@ -7941,7 +8104,7 @@ function ViewRendererBase({
7941
8104
  await readyPromise;
7942
8105
  if (disposed) return;
7943
8106
  const capabilities = {
7944
- ...hostCapabilities ?? DEFAULT_HOST_CAPABILITIES,
8107
+ ...effectiveHostCapabilities,
7945
8108
  sandbox: {
7946
8109
  csp: cspMode === "permissive" ? void 0 : resolved.csp,
7947
8110
  permissions: resolved.permissions
@@ -7950,75 +8113,92 @@ function ViewRendererBase({
7950
8113
  bridge = new AppBridge(null, hostInfo, capabilities, {
7951
8114
  hostContext: hostContextRef.current
7952
8115
  });
7953
- bridge.onmessage = async ({
7954
- content
7955
- }) => {
7956
- if (content.length > 0 && onMessageRef.current) {
7957
- onMessageRef.current(content);
7958
- }
7959
- return {};
7960
- };
8116
+ if (capabilities.message) {
8117
+ bridge.onmessage = async ({
8118
+ content
8119
+ }) => {
8120
+ await dispatchUiMessage(onMessageRef.current, content);
8121
+ return {};
8122
+ };
8123
+ }
7961
8124
  bridge.onopenlink = async ({ url }) => {
7962
8125
  if (url) window.open(url, "_blank", "noopener,noreferrer");
7963
8126
  return {};
7964
8127
  };
7965
- bridge.oncalltool = (async ({
7966
- name,
7967
- arguments: args
7968
- }) => {
7969
- const conn = connectionRef.current;
7970
- if (!conn) throw new Error("Server connection not available");
7971
- try {
7972
- return await conn.callTool(name, args || {}, {
7973
- timeout: toolCallTimeout,
7974
- resetTimeoutOnProgress: true
7975
- });
7976
- } catch (error) {
7977
- bridge?.sendToolCancelled({
7978
- reason: error instanceof Error ? error.message : String(error)
7979
- });
7980
- throw error;
7981
- }
7982
- });
7983
- bridge.onreadresource = (async ({
7984
- uri
7985
- }) => {
7986
- const conn = connectionRef.current;
7987
- if (!conn) throw new Error("Server connection not available");
7988
- return await conn.readResource(uri);
7989
- });
7990
- bridge.onlistresources = (async () => {
7991
- const conn = connectionRef.current;
7992
- if (!conn) throw new Error("Server connection not available");
7993
- return { resources: [...conn.resources ?? []] };
7994
- });
8128
+ if (capabilities.serverTools) {
8129
+ bridge.oncalltool = (async ({
8130
+ name,
8131
+ arguments: args
8132
+ }) => {
8133
+ const conn = connectionRef.current;
8134
+ if (!conn) throw new Error("Server connection not available");
8135
+ assertAppCanCallTool(conn.tools, name);
8136
+ try {
8137
+ return await conn.callTool(name, args || {}, {
8138
+ timeout: toolCallTimeout,
8139
+ resetTimeoutOnProgress: true
8140
+ });
8141
+ } catch (error) {
8142
+ bridge?.sendToolCancelled({
8143
+ reason: error instanceof Error ? error.message : String(error)
8144
+ });
8145
+ throw error;
8146
+ }
8147
+ });
8148
+ }
8149
+ if (capabilities.serverResources) {
8150
+ bridge.onreadresource = (async ({
8151
+ uri
8152
+ }) => {
8153
+ const conn = connectionRef.current;
8154
+ if (!conn) throw new Error("Server connection not available");
8155
+ return await conn.readResource(uri);
8156
+ });
8157
+ bridge.onlistresources = (async () => {
8158
+ const conn = connectionRef.current;
8159
+ if (!conn) throw new Error("Server connection not available");
8160
+ return { resources: [...conn.resources ?? []] };
8161
+ });
8162
+ }
7995
8163
  bridge.onrequestdisplaymode = async ({
7996
8164
  mode
7997
8165
  }) => {
7998
8166
  const requested = mode ?? "inline";
7999
- const available = hostContextRef.current?.availableDisplayModes ?? [
8000
- "inline",
8001
- "pip",
8002
- "fullscreen"
8003
- ];
8004
- const effective = available.includes(requested) ? requested : displayModeRef.current;
8167
+ const effective = resolveRequestedDisplayMode({
8168
+ requested,
8169
+ current: displayModeRef.current,
8170
+ hostAvailable: hostContextRef.current?.availableDisplayModes,
8171
+ appAvailable: bridge?.getAppCapabilities()?.availableDisplayModes
8172
+ });
8005
8173
  await handleDisplayModeChangeRef.current(effective);
8006
8174
  return { mode: effective };
8007
8175
  };
8008
- bridge.onupdatemodelcontext = async ({
8009
- content,
8010
- structuredContent
8011
- }) => {
8012
- onModelContextUpdateRef.current?.({ content, structuredContent });
8013
- return {};
8014
- };
8015
- bridge.onloggingmessage = async ({
8016
- level,
8017
- data
8018
- }) => {
8019
- onLogRef.current?.({ level, data });
8020
- return {};
8021
- };
8176
+ if (capabilities.updateModelContext) {
8177
+ bridge.onupdatemodelcontext = async ({
8178
+ content,
8179
+ structuredContent
8180
+ }) => {
8181
+ if (!onModelContextUpdateRef.current) {
8182
+ throw new Error(
8183
+ "This host surface does not support model context updates"
8184
+ );
8185
+ }
8186
+ await onModelContextUpdateRef.current({
8187
+ content,
8188
+ structuredContent
8189
+ });
8190
+ return {};
8191
+ };
8192
+ }
8193
+ if (capabilities.logging) {
8194
+ bridge.onloggingmessage = async ({
8195
+ level,
8196
+ data
8197
+ }) => {
8198
+ onLogRef.current?.({ level, data });
8199
+ return {};
8200
+ };
8201
+ }
8022
8202
  bridge.onsizechange = async ({
8023
8203
  height
8024
8204
  }) => {
@@ -8104,7 +8284,7 @@ function ViewRendererBase({
8104
8284
  resolved,
8105
8285
  activeSandboxUrl,
8106
8286
  hostInfo,
8107
- hostCapabilities,
8287
+ effectiveHostCapabilities,
8108
8288
  cspMode,
8109
8289
  viewId,
8110
8290
  wrapTransport,
@@ -8270,6 +8450,11 @@ function viewRendererAreEqual(prev, next) {
8270
8450
  if (prev.customProps !== next.customProps) return false;
8271
8451
  if (prev.hostContext !== next.hostContext) return false;
8272
8452
  if (prev.hostCapabilities !== next.hostCapabilities) return false;
8453
+ if (prev.messageCapabilities !== next.messageCapabilities) return false;
8454
+ if (prev.modelContextCapabilities !== next.modelContextCapabilities)
8455
+ return false;
8456
+ if (prev.onMessage !== next.onMessage) return false;
8457
+ if (prev.onModelContextUpdate !== next.onModelContextUpdate) return false;
8273
8458
  if (prev.cspMode !== next.cspMode) return false;
8274
8459
  if (prev.mockOpenAiFileApis !== next.mockOpenAiFileApis) return false;
8275
8460
  if (prev.onInlineHeightChange !== next.onInlineHeightChange) return false;
@@ -8294,6 +8479,7 @@ export {
8294
8479
  getAllRpcLogs,
8295
8480
  getRpcLogs,
8296
8481
  getViewResourceUri,
8482
+ isToolVisibleToModel,
8297
8483
  isViewResource,
8298
8484
  isViewTool,
8299
8485
  onMcpAuthorization,