@mcpjam/widget-react 0.2.2 → 0.3.0

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.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as react from 'react';
2
2
  import { ComponentType, ReactNode, ContextType } from 'react';
3
3
  import * as _mcpjam_sdk_widget_runtime from '@mcpjam/sdk/widget-runtime';
4
- import { ToolResultPolicy, PrepareSandboxViewRequest, PreparedSandboxView, ReleaseSandboxViewRequest, BrowserStoragePolicy, AppToolRequestContext, AppToolInvocationUpdate } from '@mcpjam/sdk/widget-runtime';
4
+ import { ToolResultPolicy, PrepareSandboxViewRequest, PreparedSandboxView, ReleaseSandboxViewRequest, BrowserStoragePolicy, DisplayBehavior, AppToolRequestContext, AppToolInvocationUpdate } from '@mcpjam/sdk/widget-runtime';
5
5
  export { getToolVisibility, isVisibleToAppOnly, isVisibleToModelOnly } from '@mcpjam/sdk/widget-runtime';
6
6
  import { McpUiResourceCsp, McpUiResourcePermissions, McpUiHostCapabilities, McpUiStyles, AppBridge, McpUiHostContext } from '@modelcontextprotocol/ext-apps/app-bridge';
7
7
  import { MCPPrompt, MCPResourceTemplate, SandboxCspPolicy, SandboxPermissionsPolicy } from '@mcpjam/sdk/browser';
@@ -177,14 +177,16 @@ interface WidgetSurfaceInfo {
177
177
  */
178
178
  fixedDisplayMode?: DisplayMode;
179
179
  /**
180
- * Apply OpenAI's display-mode rules (ChatGPT and Codex with the Display
181
- * modes extension on): omitted resource modes mean every host mode, a
182
- * preference picks the starting mode without narrowing, a resource that
183
- * declares both inline and fullscreen starts fullscreen, and a fixed mode
184
- * the initialized App says it cannot show refuses the App. Absent: modes are
185
- * negotiated as before.
180
+ * How the client picks display modes while its Display modes extension is
181
+ * on. `hints` names the resource metadata the hints come from. `start:
182
+ * "hint-first"` is OpenAI's rules: omitted resource modes mean every host
183
+ * mode, a preference picks the starting mode without narrowing, a resource
184
+ * that declares both inline and fullscreen starts fullscreen, and a fixed
185
+ * mode the initialized App says it cannot show refuses the App.
186
+ * `"inline-then-move"` is Claude's: the App starts inline and moves once to
187
+ * the side panel. Absent: modes are negotiated as before.
186
188
  */
187
- openAIDisplayModes?: boolean;
189
+ displayBehavior?: DisplayBehavior;
188
190
  }
189
191
  interface WidgetMount {
190
192
  index: number;
@@ -573,6 +575,10 @@ interface FetchWidgetContentRequest {
573
575
  interface FetchWidgetContentResponse {
574
576
  sourceReceipt?: string;
575
577
  resourceDisplayHints?: _mcpjam_sdk_widget_runtime.ResourceDisplayHints;
578
+ /** `_meta.ui` display hints, read under `displayBehavior.hints: "ui"`. */
579
+ uiDisplayHints?: _mcpjam_sdk_widget_runtime.ResourceDisplayHints;
580
+ /** A `_meta.ui.preferredDisplayMode` that isn't a mode, as JSON; ignored. */
581
+ ignoredPreferredDisplayMode?: string;
576
582
  html: string;
577
583
  csp?: McpUiResourceCsp;
578
584
  permissions?: McpUiResourcePermissions;
@@ -1503,6 +1509,12 @@ interface WidgetFullscreenPlacement {
1503
1509
  * opposed to the host ending it (another tab chosen, the panel hidden).
1504
1510
  */
1505
1511
  onExit?: () => void;
1512
+ /**
1513
+ * Bring the panel holding the placed App back into view (open it, or widen
1514
+ * it from a sliver): the conversation's card for an App that is only full
1515
+ * screen calls it.
1516
+ */
1517
+ reveal?: () => void;
1506
1518
  }
1507
1519
  declare const WidgetFullscreenPlacementContext: react.Context<WidgetFullscreenPlacement | null>;
1508
1520
 
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createContext, forwardRef, useRef, useState, useMemo, useImperativeHandle, useCallback, useEffect, useContext, useLayoutEffect, Fragment as Fragment$1 } from 'react';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
  import { isUIResource } from '@mcp-ui/client';
4
- import { buildOuterAllowAttribute, buildOuterSandboxAttribute, stableStringifyJson, resolveToolUiResourceUri, callViewWebmcpTool, applyToolResultPolicy, openAIDisplayModes, negotiateResourceDisplayModes, registerHostBridgeHandlers, pickOpenAIDisplayMode, createHostAppBridge, LoggingTransport, extractMethod, appToolSourceFor, discoverViewAppTools, listViewWebmcpTools } from '@mcpjam/sdk/widget-runtime';
4
+ import { buildOuterAllowAttribute, buildOuterSandboxAttribute, stableStringifyJson, resolveToolUiResourceUri, callViewWebmcpTool, applyToolResultPolicy, openAIDisplayModes, inlineThenMoveModes, negotiateResourceDisplayModes, registerHostBridgeHandlers, pickOpenAIDisplayMode, createHostAppBridge, LoggingTransport, extractMethod, shouldMoveToPanel, inlineThenMoveStart, appToolSourceFor, discoverViewAppTools, listViewWebmcpTools } from '@mcpjam/sdk/widget-runtime';
5
5
  export { getToolVisibility, isVisibleToAppOnly, isVisibleToModelOnly } from '@mcpjam/sdk/widget-runtime';
6
6
  import { useStore, create } from 'zustand';
7
7
  import { useShallow } from 'zustand/react/shallow';
@@ -1267,8 +1267,9 @@ var SandboxedIframe = forwardRef(function SandboxedIframe2({
1267
1267
  );
1268
1268
  const sandboxProxyUrl = preparedView.required ? preparedView.result?.proxyUrl : legacyProxyUrl;
1269
1269
  const sandboxProxyOrigin = useMemo(() => {
1270
+ if (!sandboxProxyUrl) return null;
1270
1271
  try {
1271
- return new URL(sandboxProxyUrl ?? "about:blank").origin;
1272
+ return new URL(sandboxProxyUrl).origin;
1272
1273
  } catch {
1273
1274
  return "*";
1274
1275
  }
@@ -1277,7 +1278,7 @@ var SandboxedIframe = forwardRef(function SandboxedIframe2({
1277
1278
  ref,
1278
1279
  () => ({
1279
1280
  postMessage: (data) => {
1280
- if (sandboxProxyUrl) outerRef.current?.contentWindow?.postMessage(data, sandboxProxyOrigin);
1281
+ if (sandboxProxyOrigin) outerRef.current?.contentWindow?.postMessage(data, sandboxProxyOrigin);
1281
1282
  },
1282
1283
  getIframeElement: () => outerRef.current
1283
1284
  }),
@@ -1382,7 +1383,7 @@ var SandboxedIframe = forwardRef(function SandboxedIframe2({
1382
1383
  if (!proxyReady) lastResourceReadyKeyRef.current = null;
1383
1384
  }, [proxyReady]);
1384
1385
  useEffect(() => {
1385
- if (!proxyReady || !html) return;
1386
+ if (!proxyReady || !html || !sandboxProxyOrigin) return;
1386
1387
  const resourceTargetKey = `${sandboxProxyOrigin}\0${resourceReadyKey}`;
1387
1388
  if (lastResourceReadyKeyRef.current === resourceTargetKey) return;
1388
1389
  lastResourceReadyKeyRef.current = resourceTargetKey;
@@ -1426,7 +1427,7 @@ var SandboxedIframe = forwardRef(function SandboxedIframe2({
1426
1427
  });
1427
1428
  }, [proxyReady, html, resourceReadyKey, sandboxProxyOrigin]);
1428
1429
  useEffect(() => {
1429
- if (!proxyReady || !colorScheme) return;
1430
+ if (!proxyReady || !colorScheme || !sandboxProxyOrigin) return;
1430
1431
  outerRef.current?.contentWindow?.postMessage(
1431
1432
  {
1432
1433
  jsonrpc: "2.0",
@@ -1686,6 +1687,51 @@ function useSandboxProxyGeneration({
1686
1687
  return { generation, onReady };
1687
1688
  }
1688
1689
  var WidgetFullscreenPlacementContext = createContext(null);
1690
+ var SETTLED_TOOL_STATES = /* @__PURE__ */ new Set([
1691
+ "output-available",
1692
+ "output-error",
1693
+ "output-denied"
1694
+ ]);
1695
+ function isErrorResult(output) {
1696
+ return typeof output === "object" && output !== null && "isError" in output && output.isError === true;
1697
+ }
1698
+ function useInlineThenMove(args) {
1699
+ const latest = useRef(args);
1700
+ useLayoutEffect(() => {
1701
+ latest.current = args;
1702
+ });
1703
+ const on = args.behavior?.start === "inline-then-move";
1704
+ const attempt = on && args.ready && SETTLED_TOOL_STATES.has(args.toolState ?? "");
1705
+ const tried = useRef(false);
1706
+ useEffect(() => {
1707
+ if (!attempt || tried.current) return;
1708
+ tried.current = true;
1709
+ const now = latest.current;
1710
+ const toolResultOk = now.toolState === "output-available" && !isErrorResult(now.toolOutput);
1711
+ if (!now.fullscreen && !now.preferInline() && shouldMoveToPanel({
1712
+ modes: now.modes,
1713
+ preferred: now.preferred,
1714
+ toolResultOk,
1715
+ panelAvailable: now.panelAvailable
1716
+ }))
1717
+ now.moveToPanel();
1718
+ }, [attempt]);
1719
+ const startMode = useCallback(
1720
+ (negotiated) => {
1721
+ const now = latest.current;
1722
+ if (now.behavior?.start !== "inline-then-move") return void 0;
1723
+ const start = inlineThenMoveStart(negotiated);
1724
+ return start === "fullscreen" && !now.panelAvailable ? void 0 : start;
1725
+ },
1726
+ []
1727
+ );
1728
+ const showPanel = useCallback(() => latest.current.revealPanel?.(), []);
1729
+ return {
1730
+ startMode,
1731
+ panelCard: on && args.placed && !args.modes.includes("inline"),
1732
+ showPanel
1733
+ };
1734
+ }
1689
1735
  function McpAppsModal({
1690
1736
  open,
1691
1737
  onOpenChange,
@@ -2811,7 +2857,10 @@ function MCPAppsRendererSurface({
2811
2857
  const fixedDisplayMode = host.surface.fixedDisplayMode;
2812
2858
  const fixedDisplayModeRef = useRef(fixedDisplayMode);
2813
2859
  fixedDisplayModeRef.current = fixedDisplayMode;
2814
- const openAIRules = host.surface.openAIDisplayModes === true;
2860
+ const displayBehavior = host.surface.displayBehavior;
2861
+ const displayBehaviorRef = useRef(displayBehavior);
2862
+ displayBehaviorRef.current = displayBehavior;
2863
+ const openAIRules = displayBehavior?.start === "hint-first";
2815
2864
  const openAIRulesRef = useRef(openAIRules);
2816
2865
  openAIRulesRef.current = openAIRules;
2817
2866
  const displayModesFor = useCallback(
@@ -2819,6 +2868,8 @@ function MCPAppsRendererSurface({
2819
2868
  const fixed = fixedDisplayModeRef.current;
2820
2869
  if (openAIRulesRef.current)
2821
2870
  return openAIDisplayModes(hostModes, hints, appModes, fixed);
2871
+ if (displayBehaviorRef.current?.start === "inline-then-move" && !fixed)
2872
+ return inlineThenMoveModes(hostModes, appModes);
2822
2873
  return fixed && hostModes.includes(fixed) ? [fixed] : negotiateResourceDisplayModes([...hostModes], hints, appModes);
2823
2874
  },
2824
2875
  []
@@ -2958,7 +3009,8 @@ function MCPAppsRendererSurface({
2958
3009
  });
2959
3010
  const lastForcedDisplayModeRef = useRef(null);
2960
3011
  const fullscreenPlacement = useContext(WidgetFullscreenPlacementContext);
2961
- const placedFullscreen = !hostManagedPresentation && effectiveDisplayMode === "fullscreen" && !fullscreenDismissal.collapsed && !(isPlaygroundActive && (playgroundDeviceType === "mobile" || playgroundDeviceType === "tablet")) && !!fullscreenPlacement?.rect;
3012
+ const panelAvailable = !hostManagedPresentation && !(isPlaygroundActive && (playgroundDeviceType === "mobile" || playgroundDeviceType === "tablet")) && !!fullscreenPlacement;
3013
+ const placedFullscreen = panelAvailable && effectiveDisplayMode === "fullscreen" && !fullscreenDismissal.collapsed && !!fullscreenPlacement?.rect;
2962
3014
  const fullscreenPlacementRef = useRef(fullscreenPlacement);
2963
3015
  fullscreenPlacementRef.current = fullscreenPlacement;
2964
3016
  const placedExitRef = useRef(() => {
@@ -2991,6 +3043,20 @@ function MCPAppsRendererSurface({
2991
3043
  const [reinitCount, setReinitCount] = useState(0);
2992
3044
  const [loadError, setLoadError] = useState(null);
2993
3045
  useInitialRenderOutcome(isReady, loadError !== null, onInitialRenderOutcome);
3046
+ const inlineThenMove = useInlineThenMove({
3047
+ behavior: displayBehavior,
3048
+ ready: isReady,
3049
+ toolState,
3050
+ toolOutput,
3051
+ modes: effectiveAvailableDisplayModes,
3052
+ preferred: resourceDisplayHints?.preferredDisplayMode,
3053
+ panelAvailable,
3054
+ fullscreen: effectiveDisplayMode === "fullscreen",
3055
+ placed: placedFullscreen,
3056
+ preferInline: () => userPreferInlineRef.current,
3057
+ moveToPanel: () => setDisplayModeRef.current("fullscreen"),
3058
+ revealPanel: fullscreenPlacement?.reveal
3059
+ });
2994
3060
  const [firstCspBlock, setFirstCspBlock] = useState(null);
2995
3061
  const [widgetHtml, setWidgetHtml] = useState(null);
2996
3062
  const [openInAppUrlOverride, setOpenInAppUrlOverride] = useState(null);
@@ -3255,7 +3321,8 @@ function MCPAppsRendererSurface({
3255
3321
  mimeTypeWarning: warning,
3256
3322
  mimeTypeValid: valid,
3257
3323
  prefersBorder: prefersBorder2,
3258
- resourceDisplayHints: resourceHints,
3324
+ resourceDisplayHints: openAIHints,
3325
+ uiDisplayHints,
3259
3326
  declaredDomain: serverDeclaredDomain,
3260
3327
  viewOriginLabel: serverViewOriginLabel,
3261
3328
  sourceReceipt: serverSourceReceipt,
@@ -3304,6 +3371,7 @@ function MCPAppsRendererSurface({
3304
3371
  setBridgeTransportReady(false);
3305
3372
  hasRenderedLiveRef.current = true;
3306
3373
  liveRenderIdentityRef.current = liveRenderIdentityKey;
3374
+ const resourceHints = displayBehaviorRef.current?.hints === "ui" ? uiDisplayHints : openAIHints;
3307
3375
  const allowed = displayModesFor(
3308
3376
  hostAvailableDisplayModesRef.current,
3309
3377
  resourceHints
@@ -3313,7 +3381,7 @@ function MCPAppsRendererSurface({
3313
3381
  return false;
3314
3382
  }
3315
3383
  setResourceDisplayHints(resourceHints);
3316
- const initialMode = openAIRulesRef.current ? pickOpenAIDisplayMode(allowed, resourceHints) : resourceHints?.preferredDisplayMode;
3384
+ const initialMode = openAIRulesRef.current ? pickOpenAIDisplayMode(allowed, resourceHints) : displayBehaviorRef.current?.start === "inline-then-move" ? void 0 : resourceHints?.preferredDisplayMode;
3317
3385
  if (initialMode && allowed.includes(initialMode) && !(openAIRulesRef.current && initialMode === effectiveDisplayModeRef.current)) {
3318
3386
  setDisplayMode(initialMode);
3319
3387
  }
@@ -4106,11 +4174,11 @@ function MCPAppsRendererSurface({
4106
4174
  void b.close();
4107
4175
  return;
4108
4176
  }
4109
- if (openAIRulesRef.current && !negotiated.includes(effectiveDisplayModeRef.current)) {
4110
- const next = pickOpenAIDisplayMode(
4177
+ if (!negotiated.includes(effectiveDisplayModeRef.current)) {
4178
+ const next = openAIRulesRef.current ? pickOpenAIDisplayMode(
4111
4179
  negotiated,
4112
4180
  resourceDisplayHintsRef.current
4113
- );
4181
+ ) : inlineThenMove.startMode(negotiated);
4114
4182
  if (next) setDisplayModeRef.current(next);
4115
4183
  }
4116
4184
  if (wasReady) {
@@ -4996,7 +5064,15 @@ function MCPAppsRendererSurface({
4996
5064
  }
4997
5065
  );
4998
5066
  return /* @__PURE__ */ jsxs(Fragment, { children: [
4999
- fullscreenDismissal.collapsed && /* @__PURE__ */ jsx("button", { className: "rounded-md border border-border px-3 py-2 text-sm text-foreground", disabled: fullscreenDismissal.pending, onClick: fullscreenDismissal.open, children: fullscreenDismissal.pending ? "App is waiting for input" : "Open App" }),
5067
+ (fullscreenDismissal.collapsed || inlineThenMove.panelCard) && /* @__PURE__ */ jsx(
5068
+ "button",
5069
+ {
5070
+ className: "rounded-md border border-border px-3 py-2 text-sm text-foreground",
5071
+ disabled: fullscreenDismissal.pending,
5072
+ onClick: fullscreenDismissal.collapsed ? fullscreenDismissal.open : inlineThenMove.showPanel,
5073
+ children: fullscreenDismissal.pending ? "App is waiting for input" : "Open App"
5074
+ }
5075
+ ),
5000
5076
  /* @__PURE__ */ jsxs(
5001
5077
  "div",
5002
5078
  {