@modelcontextprotocol/ext-apps 0.0.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.
@@ -0,0 +1,101 @@
1
+ import { Implementation } from "@modelcontextprotocol/sdk/types.js";
2
+ import { App, McpUiAppCapabilities } from "../app";
3
+ export * from "../app";
4
+ /**
5
+ * Options for configuring the useApp hook.
6
+ *
7
+ * Note: This interface does NOT expose App options like `autoResize`.
8
+ * The hook creates the App with default options (autoResize: true). If you need
9
+ * custom App options, create the App manually instead of using this hook.
10
+ *
11
+ * @see {@link useApp} for the hook that uses these options
12
+ * @see {@link useAutoResize} for manual auto-resize control with custom App options
13
+ */
14
+ export interface UseAppOptions {
15
+ /** App identification (name and version) */
16
+ appInfo: Implementation;
17
+ /** Features and capabilities this app provides */
18
+ capabilities: McpUiAppCapabilities;
19
+ /**
20
+ * Called after App is created but before connection.
21
+ *
22
+ * Use this to register request/notification handlers that need to be in place
23
+ * before the initialization handshake completes.
24
+ *
25
+ * @param app - The newly created App instance
26
+ *
27
+ * @example Register a notification handler
28
+ * ```typescript
29
+ * import { McpUiToolInputNotificationSchema } from '@modelcontextprotocol/ext-apps/react';
30
+ *
31
+ * onAppCreated: (app) => {
32
+ * app.setNotificationHandler(
33
+ * McpUiToolInputNotificationSchema,
34
+ * (notification) => {
35
+ * console.log("Tool input:", notification.params.arguments);
36
+ * }
37
+ * );
38
+ * }
39
+ * ```
40
+ */
41
+ onAppCreated?: (app: App) => void;
42
+ }
43
+ /**
44
+ * State returned by the useApp hook.
45
+ */
46
+ export interface AppState {
47
+ /** The connected App instance, null during initialization */
48
+ app: App | null;
49
+ /** Whether initialization completed successfully */
50
+ isConnected: boolean;
51
+ /** Connection error if initialization failed, null otherwise */
52
+ error: Error | null;
53
+ }
54
+ /**
55
+ * React hook to create and connect an MCP App.
56
+ *
57
+ * This hook manages the complete lifecycle of an {@link App}: creation, connection,
58
+ * and cleanup. It automatically creates a {@link PostMessageTransport} to window.parent
59
+ * and handles initialization.
60
+ *
61
+ * **Important**: The hook intentionally does NOT re-run when options change
62
+ * to avoid reconnection loops. Options are only used during the initial mount.
63
+ *
64
+ * **Note**: This is part of the optional React integration. The core SDK
65
+ * (App, PostMessageTransport) is framework-agnostic and can be
66
+ * used with any UI framework or vanilla JavaScript.
67
+ *
68
+ * @param options - Configuration for the app
69
+ * @returns Current connection state and app instance. If connection fails during
70
+ * initialization, the `error` field will contain the error (typically connection
71
+ * timeouts, initialization handshake failures, or transport errors).
72
+ *
73
+ * @example Basic usage
74
+ * ```typescript
75
+ * import { useApp, McpUiToolInputNotificationSchema } from '@modelcontextprotocol/ext-apps/react';
76
+ *
77
+ * function MyApp() {
78
+ * const { app, isConnected, error } = useApp({
79
+ * appInfo: { name: "MyApp", version: "1.0.0" },
80
+ * capabilities: {},
81
+ * onAppCreated: (app) => {
82
+ * // Register handlers before connection
83
+ * app.setNotificationHandler(
84
+ * McpUiToolInputNotificationSchema,
85
+ * (notification) => {
86
+ * console.log("Tool input:", notification.params.arguments);
87
+ * }
88
+ * );
89
+ * },
90
+ * });
91
+ *
92
+ * if (error) return <div>Error: {error.message}</div>;
93
+ * if (!isConnected) return <div>Connecting...</div>;
94
+ * return <div>Connected!</div>;
95
+ * }
96
+ * ```
97
+ *
98
+ * @see {@link App.connect} for the underlying connection method
99
+ * @see {@link useAutoResize} for manual auto-resize control when using custom App options
100
+ */
101
+ export declare function useApp({ appInfo, capabilities, onAppCreated, }: UseAppOptions): AppState;
@@ -0,0 +1,51 @@
1
+ import { RefObject } from "react";
2
+ import { App } from "../app";
3
+ /**
4
+ * React hook that automatically reports UI size changes to the host.
5
+ *
6
+ * Uses ResizeObserver to watch `document.body` and `document.documentElement` for
7
+ * size changes and sends `ui/notifications/size-changed` notifications.
8
+ *
9
+ * **Note**: This hook is rarely needed since the {@link useApp} hook automatically enables
10
+ * auto-resize by default. This hook is provided for advanced cases where you
11
+ * create the {@link App} manually with `autoResize: false` and want to add auto-resize
12
+ * behavior later.
13
+ *
14
+ * @param app - The connected App instance, or null during initialization
15
+ * @param elementRef - Currently unused. The hook always observes `document.body`
16
+ * and `document.documentElement` regardless of this value. Passing a ref will
17
+ * cause unnecessary effect re-runs; omit this parameter.
18
+ *
19
+ * @example Manual App creation with custom auto-resize control
20
+ * ```tsx
21
+ * function MyComponent() {
22
+ * // For custom App options, create App manually instead of using useApp
23
+ * const [app, setApp] = useState<App | null>(null);
24
+ * const [error, setError] = useState<Error | null>(null);
25
+ *
26
+ * useEffect(() => {
27
+ * const myApp = new App(
28
+ * { name: "MyApp", version: "1.0.0" },
29
+ * {}, // capabilities
30
+ * { autoResize: false } // Disable default auto-resize
31
+ * );
32
+ *
33
+ * const transport = new PostMessageTransport(window.parent);
34
+ * myApp.connect(transport)
35
+ * .then(() => setApp(myApp))
36
+ * .catch((err) => setError(err));
37
+ * }, []);
38
+ *
39
+ * // Add manual auto-resize control
40
+ * useAutoResize(app);
41
+ *
42
+ * if (error) return <div>Connection failed: {error.message}</div>;
43
+ * return <div>My content</div>;
44
+ * }
45
+ * ```
46
+ *
47
+ * @see {@link App.setupSizeChangedNotifications} for the underlying implementation
48
+ * @see {@link useApp} which enables auto-resize by default
49
+ * @see {@link App} constructor for configuring `autoResize` option
50
+ */
51
+ export declare function useAutoResize(app: App | null, elementRef?: RefObject<HTMLElement | null>): void;