@mcpjam/widget-react 0.1.1 → 0.1.2

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
@@ -897,6 +897,19 @@ interface MCPAppsRendererProps {
897
897
  * collapse the inline view).
898
898
  */
899
899
  onRequestTeardown?: (toolCallId: string, displayWidgetId?: string) => void;
900
+ /**
901
+ * Ask the host to confirm a widget-initiated `ui/download-file` before the
902
+ * download runs (SEP-1865: host SHOULD confirm). Resolve `true` to proceed,
903
+ * `false` to deny (the widget then receives a JSON-RPC "Download denied by
904
+ * user" error). When omitted, downloads proceed unprompted — so embedders of
905
+ * the published package keep the prior behavior.
906
+ */
907
+ onConfirmDownload?: (info: {
908
+ kind: "resource" | "resource_link";
909
+ filename?: string;
910
+ mimeType?: string;
911
+ uri?: string;
912
+ }) => Promise<boolean>;
900
913
  /** Whether the server is offline (for using cached content) */
901
914
  isOffline?: boolean;
902
915
  /** URL to cached widget HTML for offline rendering */
@@ -960,7 +973,7 @@ interface MCPAppsRendererProps {
960
973
  persistentSurfaceInitialToolCallId?: string;
961
974
  }
962
975
  declare function MCPAppsRenderer(props: MCPAppsRendererProps): react_jsx_runtime.JSX.Element;
963
- declare function MCPAppsRendererSurface({ chatSessionId, serverId, serverName, toolCallId, toolName, toolState, toolInput, toolOutput, toolResponseMetadata, toolErrorText, resourceUri, toolMetadata, toolsMetadata, onSendFollowUp, onCallTool, onAppToolInvocationChange, onWidgetStateChange, recordMode, onRecorderStep, onRecorderReady, onReplayControllerReady, pipWidgetId, fullscreenWidgetId, onRequestPip, onExitPip, displayMode: displayModeProp, onDisplayModeChange, onRequestFullscreen, onExitFullscreen, onModelContextUpdate, onAppSupportedDisplayModesChange, onRequestTeardown, isOffline, cachedWidgetHtmlUrl, liveFetchPreferred, widgetCsp: initialWidgetCsp, widgetPermissions: initialWidgetPermissions, widgetPermissive: initialWidgetPermissive, prefersBorder: initialPrefersBorder, injectedOpenAiCompat: initialInjectedOpenAiCompat, injectedOpenAiCompatCapabilities: initialInjectedOpenAiCompatCapabilities, initialWidgetState, minimalMode, persistentSurfaceId, persistentSurfaceInitialToolCallId, }: MCPAppsRendererProps): react_jsx_runtime.JSX.Element | null;
976
+ declare function MCPAppsRendererSurface({ chatSessionId, serverId, serverName, toolCallId, toolName, toolState, toolInput, toolOutput, toolResponseMetadata, toolErrorText, resourceUri, toolMetadata, toolsMetadata, onSendFollowUp, onCallTool, onAppToolInvocationChange, onWidgetStateChange, recordMode, onRecorderStep, onRecorderReady, onReplayControllerReady, pipWidgetId, fullscreenWidgetId, onRequestPip, onExitPip, displayMode: displayModeProp, onDisplayModeChange, onRequestFullscreen, onExitFullscreen, onModelContextUpdate, onAppSupportedDisplayModesChange, onRequestTeardown, onConfirmDownload, isOffline, cachedWidgetHtmlUrl, liveFetchPreferred, widgetCsp: initialWidgetCsp, widgetPermissions: initialWidgetPermissions, widgetPermissive: initialWidgetPermissive, prefersBorder: initialPrefersBorder, injectedOpenAiCompat: initialInjectedOpenAiCompat, injectedOpenAiCompatCapabilities: initialInjectedOpenAiCompatCapabilities, initialWidgetState, minimalMode, persistentSurfaceId, persistentSurfaceInitialToolCallId, }: MCPAppsRendererProps): react_jsx_runtime.JSX.Element | null;
964
977
 
965
978
  declare function WidgetSurfaceHostProvider({ children, }: {
966
979
  children: ReactNode;
package/dist/index.js CHANGED
@@ -1534,6 +1534,14 @@ function toSafeOpenInAppUrl(value) {
1534
1534
  return null;
1535
1535
  }
1536
1536
  }
1537
+ function toSafeDownloadLinkUrl(uri) {
1538
+ try {
1539
+ const parsed = new URL(uri, window.location.href);
1540
+ return SAFE_OPEN_IN_APP_PROTOCOLS.has(parsed.protocol) ? parsed.href : null;
1541
+ } catch {
1542
+ return null;
1543
+ }
1544
+ }
1537
1545
  function readExplicitBaseHref(html) {
1538
1546
  if (!html) return null;
1539
1547
  if (typeof DOMParser !== "undefined") {
@@ -1805,6 +1813,7 @@ function MCPAppsRendererSurface({
1805
1813
  onModelContextUpdate,
1806
1814
  onAppSupportedDisplayModesChange,
1807
1815
  onRequestTeardown,
1816
+ onConfirmDownload,
1808
1817
  isOffline,
1809
1818
  cachedWidgetHtmlUrl,
1810
1819
  liveFetchPreferred,
@@ -2177,6 +2186,7 @@ function MCPAppsRendererSurface({
2177
2186
  const onRequestPipRef = useRef(onRequestPip);
2178
2187
  const onExitPipRef = useRef(onExitPip);
2179
2188
  const onRequestTeardownRef = useRef(onRequestTeardown);
2189
+ const onConfirmDownloadRef = useRef(onConfirmDownload);
2180
2190
  const setDisplayModeRef = useRef(setDisplayMode);
2181
2191
  const isPlaygroundActiveRef = useRef(isPlaygroundActive);
2182
2192
  const playgroundDeviceTypeRef = useRef(playgroundDeviceType);
@@ -3063,6 +3073,7 @@ function MCPAppsRendererSurface({
3063
3073
  onModelContextUpdateRef.current = onModelContextUpdate;
3064
3074
  onAppSupportedDisplayModesChangeRef.current = onAppSupportedDisplayModesChange;
3065
3075
  onRequestTeardownRef.current = onRequestTeardown;
3076
+ onConfirmDownloadRef.current = onConfirmDownload;
3066
3077
  }, [
3067
3078
  onSendFollowUp,
3068
3079
  onCallTool,
@@ -3081,7 +3092,8 @@ function MCPAppsRendererSurface({
3081
3092
  toolsMetadata,
3082
3093
  onModelContextUpdate,
3083
3094
  onAppSupportedDisplayModesChange,
3084
- onRequestTeardown
3095
+ onRequestTeardown,
3096
+ onConfirmDownload
3085
3097
  ]);
3086
3098
  const registerBridgeHandlers = useCallback(
3087
3099
  (bridge) => {
@@ -3253,6 +3265,38 @@ function MCPAppsRendererSurface({
3253
3265
  });
3254
3266
  },
3255
3267
  onDownloadFile: async ({ contents }) => {
3268
+ const confirmDownload = onConfirmDownloadRef.current;
3269
+ if (confirmDownload) {
3270
+ for (const item of contents) {
3271
+ let info;
3272
+ if (item.type === "resource" && item.resource) {
3273
+ const res = item.resource;
3274
+ info = {
3275
+ kind: "resource",
3276
+ filename: res.uri.split("/").pop() || void 0,
3277
+ mimeType: res.mimeType,
3278
+ uri: res.uri
3279
+ };
3280
+ } else if (item.type === "resource_link") {
3281
+ const link = item;
3282
+ if (toSafeDownloadLinkUrl(link.uri) === null) {
3283
+ return { isError: true };
3284
+ }
3285
+ info = {
3286
+ kind: "resource_link",
3287
+ filename: link.uri.split("/").pop() || void 0,
3288
+ mimeType: link.mimeType,
3289
+ uri: link.uri
3290
+ };
3291
+ } else {
3292
+ continue;
3293
+ }
3294
+ const approved = await confirmDownload(info);
3295
+ if (!approved) {
3296
+ return { isError: true };
3297
+ }
3298
+ }
3299
+ }
3256
3300
  try {
3257
3301
  for (const item of contents) {
3258
3302
  if (item.type === "resource" && item.resource) {
@@ -3283,13 +3327,13 @@ function MCPAppsRendererSurface({
3283
3327
  }
3284
3328
  } else if (item.type === "resource_link") {
3285
3329
  const link = item;
3286
- const parsed = new URL(link.uri, window.location.href);
3287
- if (!["http:", "https:"].includes(parsed.protocol)) {
3330
+ const safeHref = toSafeDownloadLinkUrl(link.uri);
3331
+ if (safeHref === null) {
3288
3332
  throw new Error(
3289
- `Unsupported download URI protocol: ${parsed.protocol}`
3333
+ `Unsupported download URI: ${link.uri}`
3290
3334
  );
3291
3335
  }
3292
- window.open(parsed.href, "_blank", "noopener,noreferrer");
3336
+ window.open(safeHref, "_blank", "noopener,noreferrer");
3293
3337
  }
3294
3338
  }
3295
3339
  return {};