@modelcontextprotocol/ext-apps 0.0.7 → 0.2.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/src/app.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { type RequestOptions, Protocol, ProtocolOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
2
- import { CallToolRequest, CallToolResult, Implementation, ListToolsRequest, LoggingMessageNotification, Notification, Request, Result } from "@modelcontextprotocol/sdk/types.js";
3
- import { McpUiAppCapabilities, McpUiHostCapabilities, McpUiHostContextChangedNotification, McpUiMessageRequest, McpUiOpenLinkRequest, McpUiSizeChangedNotification, McpUiToolInputNotification, McpUiToolInputPartialNotification, McpUiToolResultNotification } from "./types";
2
+ import { CallToolRequest, CallToolResult, Implementation, ListToolsRequest, LoggingMessageNotification } from "@modelcontextprotocol/sdk/types.js";
3
+ import { AppNotification, AppRequest, AppResult } from "./types";
4
+ import { McpUiAppCapabilities, McpUiHostCapabilities, McpUiHostContext, McpUiHostContextChangedNotification, McpUiMessageRequest, McpUiOpenLinkRequest, McpUiResourceTeardownRequest, McpUiResourceTeardownResult, McpUiSizeChangedNotification, McpUiToolCancelledNotification, McpUiToolInputNotification, McpUiToolInputPartialNotification, McpUiToolResultNotification, McpUiRequestDisplayModeRequest } from "./types";
4
5
  import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
5
6
  export { PostMessageTransport } from "./message-transport";
6
7
  export * from "./types";
8
+ export { applyHostStyleVariables, getDocumentTheme, applyDocumentTheme, } from "./styles";
7
9
  /**
8
10
  * Metadata key for associating a resource URI with a tool call.
9
11
  *
@@ -137,12 +139,13 @@ type RequestHandlerExtra = Parameters<Parameters<App["setRequestHandler"]>[1]>[1
137
139
  * });
138
140
  * ```
139
141
  */
140
- export declare class App extends Protocol<Request, Notification, Result> {
142
+ export declare class App extends Protocol<AppRequest, AppNotification, AppResult> {
141
143
  private _appInfo;
142
144
  private _capabilities;
143
145
  private options;
144
146
  private _hostCapabilities?;
145
147
  private _hostInfo?;
148
+ private _hostContext?;
146
149
  /**
147
150
  * Create a new MCP App instance.
148
151
  *
@@ -209,6 +212,39 @@ export declare class App extends Protocol<Request, Notification, Result> {
209
212
  * @see {@link connect} for the initialization handshake
210
213
  */
211
214
  getHostVersion(): Implementation | undefined;
215
+ /**
216
+ * Get the host context discovered during initialization.
217
+ *
218
+ * Returns the host context that was provided in the initialization response,
219
+ * including tool info, theme, viewport, locale, and other environment details.
220
+ * This context is automatically updated when the host sends
221
+ * `ui/notifications/host-context-changed` notifications.
222
+ *
223
+ * Returns `undefined` if called before connection is established.
224
+ *
225
+ * @returns Host context, or `undefined` if not yet connected
226
+ *
227
+ * @example Access host context after connection
228
+ * ```typescript
229
+ * await app.connect(transport);
230
+ * const context = app.getHostContext();
231
+ * if (context === undefined) {
232
+ * console.error("Not connected");
233
+ * return;
234
+ * }
235
+ * if (context.theme === "dark") {
236
+ * document.body.classList.add("dark-theme");
237
+ * }
238
+ * if (context.toolInfo) {
239
+ * console.log("Tool:", context.toolInfo.tool.name);
240
+ * }
241
+ * ```
242
+ *
243
+ * @see {@link connect} for the initialization handshake
244
+ * @see {@link onhostcontextchanged} for context change notifications
245
+ * @see {@link McpUiHostContext} for the context structure
246
+ */
247
+ getHostContext(): McpUiHostContext | undefined;
212
248
  /**
213
249
  * Convenience handler for receiving complete tool input from the host.
214
250
  *
@@ -305,6 +341,34 @@ export declare class App extends Protocol<Request, Notification, Result> {
305
341
  * @see {@link ontoolinput} for the initial tool input handler
306
342
  */
307
343
  set ontoolresult(callback: (params: McpUiToolResultNotification["params"]) => void);
344
+ /**
345
+ * Convenience handler for receiving tool cancellation notifications from the host.
346
+ *
347
+ * Set this property to register a handler that will be called when the host
348
+ * notifies that tool execution was cancelled. This can occur for various reasons
349
+ * including user action, sampling error, classifier intervention, or other
350
+ * interruptions. Apps should update their state and display appropriate feedback.
351
+ *
352
+ * This setter is a convenience wrapper around `setNotificationHandler()` that
353
+ * automatically handles the notification schema and extracts the params for you.
354
+ *
355
+ * Register handlers before calling {@link connect} to avoid missing notifications.
356
+ *
357
+ * @param callback - Function called when tool execution is cancelled
358
+ *
359
+ * @example Handle tool cancellation
360
+ * ```typescript
361
+ * app.ontoolcancelled = (params) => {
362
+ * console.log("Tool cancelled:", params.reason);
363
+ * showCancelledMessage(params.reason ?? "Operation was cancelled");
364
+ * };
365
+ * ```
366
+ *
367
+ * @see {@link setNotificationHandler} for the underlying method
368
+ * @see {@link McpUiToolCancelledNotification} for the notification structure
369
+ * @see {@link ontoolresult} for successful tool completion
370
+ */
371
+ set ontoolcancelled(callback: (params: McpUiToolCancelledNotification["params"]) => void);
308
372
  /**
309
373
  * Convenience handler for host context changes (theme, viewport, locale, etc.).
310
374
  *
@@ -336,6 +400,37 @@ export declare class App extends Protocol<Request, Notification, Result> {
336
400
  * @see {@link McpUiHostContext} for the full context structure
337
401
  */
338
402
  set onhostcontextchanged(callback: (params: McpUiHostContextChangedNotification["params"]) => void);
403
+ /**
404
+ * Convenience handler for graceful shutdown requests from the host.
405
+ *
406
+ * Set this property to register a handler that will be called when the host
407
+ * requests the app to prepare for teardown. This allows the app to perform
408
+ * cleanup operations (save state, close connections, etc.) before being unmounted.
409
+ *
410
+ * The handler can be sync or async. The host will wait for the returned promise
411
+ * to resolve before proceeding with teardown.
412
+ *
413
+ * This setter is a convenience wrapper around `setRequestHandler()` that
414
+ * automatically handles the request schema.
415
+ *
416
+ * Register handlers before calling {@link connect} to avoid missing requests.
417
+ *
418
+ * @param callback - Function called when teardown is requested.
419
+ * Can return void or a Promise that resolves when cleanup is complete.
420
+ *
421
+ * @example Perform cleanup before teardown
422
+ * ```typescript
423
+ * app.onteardown = async () => {
424
+ * await saveState();
425
+ * closeConnections();
426
+ * console.log("App ready for teardown");
427
+ * };
428
+ * ```
429
+ *
430
+ * @see {@link setRequestHandler} for the underlying method
431
+ * @see {@link McpUiResourceTeardownRequest} for the request structure
432
+ */
433
+ set onteardown(callback: (params: McpUiResourceTeardownRequest["params"], extra: RequestHandlerExtra) => McpUiResourceTeardownResult | Promise<McpUiResourceTeardownResult>);
339
434
  /**
340
435
  * Convenience handler for tool call requests from the host.
341
436
  *
@@ -405,17 +500,17 @@ export declare class App extends Protocol<Request, Notification, Result> {
405
500
  * Verify that the host supports the capability required for the given request method.
406
501
  * @internal
407
502
  */
408
- assertCapabilityForMethod(method: Request["method"]): void;
503
+ assertCapabilityForMethod(method: AppRequest["method"]): void;
409
504
  /**
410
505
  * Verify that the app declared the capability required for the given request method.
411
506
  * @internal
412
507
  */
413
- assertRequestHandlerCapability(method: Request["method"]): void;
508
+ assertRequestHandlerCapability(method: AppRequest["method"]): void;
414
509
  /**
415
510
  * Verify that the app supports the capability required for the given notification method.
416
511
  * @internal
417
512
  */
418
- assertNotificationCapability(method: Notification["method"]): void;
513
+ assertNotificationCapability(method: AppNotification["method"]): void;
419
514
  /**
420
515
  * Verify that task creation is supported for the given request method.
421
516
  * @internal
@@ -489,7 +584,10 @@ export declare class App extends Protocol<Request, Notification, Result> {
489
584
  *
490
585
  * @see {@link McpUiMessageRequest} for request structure
491
586
  */
492
- sendMessage(params: McpUiMessageRequest["params"], options?: RequestOptions): Promise<import("./types").McpUiMessageResult>;
587
+ sendMessage(params: McpUiMessageRequest["params"], options?: RequestOptions): Promise<{
588
+ [x: string]: unknown;
589
+ isError?: boolean | undefined;
590
+ }>;
493
591
  /**
494
592
  * Send log messages to the host for debugging and telemetry.
495
593
  *
@@ -526,7 +624,7 @@ export declare class App extends Protocol<Request, Notification, Result> {
526
624
  * @example Open documentation link
527
625
  * ```typescript
528
626
  * try {
529
- * await app.sendOpenLink({ url: "https://docs.example.com" });
627
+ * await app.openLink({ url: "https://docs.example.com" });
530
628
  * } catch (error) {
531
629
  * console.error("Failed to open link:", error);
532
630
  * // Optionally show fallback: display URL for manual copy
@@ -535,7 +633,40 @@ export declare class App extends Protocol<Request, Notification, Result> {
535
633
  *
536
634
  * @see {@link McpUiOpenLinkRequest} for request structure
537
635
  */
538
- sendOpenLink(params: McpUiOpenLinkRequest["params"], options?: RequestOptions): Promise<import("./types").McpUiOpenLinkResult>;
636
+ openLink(params: McpUiOpenLinkRequest["params"], options?: RequestOptions): Promise<{
637
+ [x: string]: unknown;
638
+ isError?: boolean | undefined;
639
+ }>;
640
+ /** @deprecated Use {@link openLink} instead */
641
+ sendOpenLink: App["openLink"];
642
+ /**
643
+ * Request a change to the display mode.
644
+ *
645
+ * Requests the host to change the UI container to the specified display mode
646
+ * (e.g., "inline", "fullscreen", "pip"). The host will respond with the actual
647
+ * display mode that was set, which may differ from the requested mode if
648
+ * the requested mode is not available (check `availableDisplayModes` in host context).
649
+ *
650
+ * @param params - The display mode being requested
651
+ * @param options - Request options (timeout, etc.)
652
+ * @returns Result containing the actual display mode that was set
653
+ *
654
+ * @example Request fullscreen mode
655
+ * ```typescript
656
+ * const context = app.getHostContext();
657
+ * if (context?.availableDisplayModes?.includes("fullscreen")) {
658
+ * const result = await app.requestDisplayMode({ mode: "fullscreen" });
659
+ * console.log("Display mode set to:", result.mode);
660
+ * }
661
+ * ```
662
+ *
663
+ * @see {@link McpUiRequestDisplayModeRequest} for request structure
664
+ * @see {@link McpUiHostContext} for checking availableDisplayModes
665
+ */
666
+ requestDisplayMode(params: McpUiRequestDisplayModeRequest["params"], options?: RequestOptions): Promise<{
667
+ [x: string]: unknown;
668
+ mode: "inline" | "fullscreen" | "pip";
669
+ }>;
539
670
  /**
540
671
  * Notify the host of UI size changes.
541
672
  *
@@ -620,5 +751,5 @@ export declare class App extends Protocol<Request, Notification, Result> {
620
751
  * @see {@link McpUiInitializedNotification} for the initialized notification
621
752
  * @see {@link PostMessageTransport} for the typical transport implementation
622
753
  */
623
- connect(transport: Transport, options?: RequestOptions): Promise<void>;
754
+ connect(transport?: Transport, options?: RequestOptions): Promise<void>;
624
755
  }