@modelcontextprotocol/ext-apps 0.4.1 → 0.4.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.
@@ -2,14 +2,14 @@ import { Implementation } from "@modelcontextprotocol/sdk/types.js";
2
2
  import { App, McpUiAppCapabilities } from "../app";
3
3
  export * from "../app";
4
4
  /**
5
- * Options for configuring the {@link useApp} hook.
5
+ * Options for configuring the {@link useApp `useApp`} hook.
6
6
  *
7
- * Note: This interface does NOT expose {@link App} options like `autoResize`.
7
+ * Note: This interface does NOT expose {@link App `App`} options like `autoResize`.
8
8
  * The hook creates the `App` with default options (`autoResize: true`). If you
9
9
  * need custom `App` options, create the `App` manually instead of using this hook.
10
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
11
+ * @see {@link useApp `useApp`} for the hook that uses these options
12
+ * @see {@link useAutoResize `useAutoResize`} for manual auto-resize control with custom `App` options
13
13
  */
14
14
  export interface UseAppOptions {
15
15
  /** App identification (name and version) */
@@ -19,34 +19,33 @@ export interface UseAppOptions {
19
19
  */
20
20
  capabilities: McpUiAppCapabilities;
21
21
  /**
22
- * Called after {@link App} is created but before connection.
22
+ * Called after {@link App `App`} is created but before connection.
23
23
  *
24
24
  * Use this to register request/notification handlers that need to be in place
25
25
  * before the initialization handshake completes.
26
26
  *
27
27
  * @param app - The newly created `App` instance
28
28
  *
29
- * @example Register a notification handler
30
- * ```typescript
31
- * import { McpUiToolInputNotificationSchema } from '@modelcontextprotocol/ext-apps/react';
32
- *
33
- * onAppCreated: (app) => {
34
- * app.setNotificationHandler(
35
- * McpUiToolInputNotificationSchema,
36
- * (notification) => {
37
- * console.log("Tool input:", notification.params.arguments);
38
- * }
39
- * );
40
- * }
29
+ * @example Register an event handler
30
+ * ```tsx source="./useApp.examples.tsx#useApp_registerHandler"
31
+ * useApp({
32
+ * appInfo: { name: "MyApp", version: "1.0.0" },
33
+ * capabilities: {},
34
+ * onAppCreated: (app) => {
35
+ * app.ontoolresult = (result) => {
36
+ * console.log("Tool result:", result);
37
+ * };
38
+ * },
39
+ * });
41
40
  * ```
42
41
  */
43
42
  onAppCreated?: (app: App) => void;
44
43
  }
45
44
  /**
46
- * State returned by the {@link useApp} hook.
45
+ * State returned by the {@link useApp `useApp`} hook.
47
46
  */
48
47
  export interface AppState {
49
- /** The connected {@link App} instance, null during initialization */
48
+ /** The connected {@link App `App`} instance, null during initialization */
50
49
  app: App | null;
51
50
  /** Whether initialization completed successfully */
52
51
  isConnected: boolean;
@@ -56,8 +55,8 @@ export interface AppState {
56
55
  /**
57
56
  * React hook to create and connect an MCP App.
58
57
  *
59
- * This hook manages {@link App} creation and connection. It automatically
60
- * creates a {@link PostMessageTransport} to window.parent and handles
58
+ * This hook manages {@link App `App`} creation and connection. It automatically
59
+ * creates a {@link PostMessageTransport `PostMessageTransport`} to window.parent and handles
61
60
  * initialization.
62
61
  *
63
62
  * This hook is part of the optional React integration. The core SDK (`App`,
@@ -68,39 +67,49 @@ export interface AppState {
68
67
  * to avoid reconnection loops. Options are only used during the initial mount.
69
68
  * Furthermore, the `App` instance is NOT closed on unmount. This avoids cleanup
70
69
  * issues during React Strict Mode's double-mount cycle. If you need to
71
- * explicitly close the `App`, call {@link App.close} manually.
70
+ * explicitly close the `App`, call {@link App.close `App.close`} manually.
72
71
  *
73
72
  * @param options - Configuration for the app
74
73
  * @returns Current connection state and app instance. If connection fails during
75
74
  * initialization, the `error` field will contain the error (typically connection
76
75
  * timeouts, initialization handshake failures, or transport errors).
77
76
  *
78
- * @example Basic usage
79
- * ```typescript
80
- * import { useApp, McpUiToolInputNotificationSchema } from '@modelcontextprotocol/ext-apps/react';
81
- *
77
+ * @example Basic usage of useApp hook with common event handlers
78
+ * ```tsx source="./useApp.examples.tsx#useApp_basicUsage"
82
79
  * function MyApp() {
80
+ * const [hostContext, setHostContext] = useState<
81
+ * McpUiHostContext | undefined
82
+ * >(undefined);
83
+ *
83
84
  * const { app, isConnected, error } = useApp({
84
85
  * appInfo: { name: "MyApp", version: "1.0.0" },
85
86
  * capabilities: {},
86
87
  * onAppCreated: (app) => {
87
- * // Register handlers before connection
88
- * app.setNotificationHandler(
89
- * McpUiToolInputNotificationSchema,
90
- * (notification) => {
91
- * console.log("Tool input:", notification.params.arguments);
92
- * }
93
- * );
88
+ * app.ontoolinput = (input) => {
89
+ * console.log("Tool input:", input);
90
+ * };
91
+ * app.ontoolresult = (result) => {
92
+ * console.log("Tool result:", result);
93
+ * };
94
+ * app.ontoolcancelled = (params) => {
95
+ * console.log("Tool cancelled:", params.reason);
96
+ * };
97
+ * app.onerror = (error) => {
98
+ * console.log("Error:", error);
99
+ * };
100
+ * app.onhostcontextchanged = (params) => {
101
+ * setHostContext((prev) => ({ ...prev, ...params }));
102
+ * };
94
103
  * },
95
104
  * });
96
105
  *
97
106
  * if (error) return <div>Error: {error.message}</div>;
98
107
  * if (!isConnected) return <div>Connecting...</div>;
99
- * return <div>Connected!</div>;
108
+ * return <div>Theme: {hostContext?.theme}</div>;
100
109
  * }
101
110
  * ```
102
111
  *
103
- * @see {@link App.connect} for the underlying connection method
104
- * @see {@link useAutoResize} for manual auto-resize control when using custom App options
112
+ * @see {@link App.connect `App.connect`} for the underlying connection method
113
+ * @see {@link useAutoResize `useAutoResize`} for manual auto-resize control when using custom App options
105
114
  */
106
115
  export declare function useApp({ appInfo, capabilities, onAppCreated, }: UseAppOptions): AppState;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type-checked examples for the useApp hook.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
@@ -8,18 +8,18 @@ import { App } from "../app";
8
8
  *
9
9
  * The hook automatically cleans up the `ResizeObserver` when the component unmounts.
10
10
  *
11
- * **Note**: This hook is rarely needed since the {@link useApp} hook automatically enables
11
+ * **Note**: This hook is rarely needed since the {@link useApp `useApp`} hook automatically enables
12
12
  * auto-resize by default. This hook is provided for advanced cases where you
13
- * create the {@link App} manually with `autoResize: false` and want to add auto-resize
13
+ * create the {@link App `App`} manually with `autoResize: false` and want to add auto-resize
14
14
  * behavior later.
15
15
  *
16
- * @param app - The connected {@link App} instance, or null during initialization
16
+ * @param app - The connected {@link App `App`} instance, or null during initialization
17
17
  * @param elementRef - Currently unused. The hook always observes `document.body`
18
18
  * and `document.documentElement` regardless of this value. Passing a ref will
19
19
  * cause unnecessary effect re-runs; omit this parameter.
20
20
  *
21
21
  * @example Manual App creation with custom auto-resize control
22
- * ```tsx
22
+ * ```tsx source="./useAutoResize.examples.tsx#useAutoResize_manualApp"
23
23
  * function MyComponent() {
24
24
  * // For custom App options, create App manually instead of using useApp
25
25
  * const [app, setApp] = useState<App | null>(null);
@@ -29,11 +29,12 @@ import { App } from "../app";
29
29
  * const myApp = new App(
30
30
  * { name: "MyApp", version: "1.0.0" },
31
31
  * {}, // capabilities
32
- * { autoResize: false } // Disable default auto-resize
32
+ * { autoResize: false }, // Disable default auto-resize
33
33
  * );
34
34
  *
35
35
  * const transport = new PostMessageTransport(window.parent, window.parent);
36
- * myApp.connect(transport)
36
+ * myApp
37
+ * .connect(transport)
37
38
  * .then(() => setApp(myApp))
38
39
  * .catch((err) => setError(err));
39
40
  * }, []);
@@ -46,8 +47,8 @@ import { App } from "../app";
46
47
  * }
47
48
  * ```
48
49
  *
49
- * @see {@link App.setupSizeChangedNotifications} for the underlying implementation
50
- * @see {@link useApp} which enables auto-resize by default
51
- * @see {@link App} constructor for configuring `autoResize` option
50
+ * @see {@link App.setupSizeChangedNotifications `App.setupSizeChangedNotifications`} for the underlying implementation
51
+ * @see {@link useApp `useApp`} which enables auto-resize by default
52
+ * @see {@link App `App`} constructor for configuring `autoResize` option
52
53
  */
53
54
  export declare function useAutoResize(app: App | null, elementRef?: RefObject<HTMLElement | null>): void;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type-checked examples for the useAutoResize hook.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
@@ -12,37 +12,33 @@ import { McpUiTheme } from "../types";
12
12
  * @returns The current theme ("light" or "dark")
13
13
  *
14
14
  * @example Conditionally render based on theme
15
- * ```tsx
16
- * import { useDocumentTheme } from '@modelcontextprotocol/ext-apps/react';
17
- *
15
+ * ```tsx source="./useDocumentTheme.examples.tsx#useDocumentTheme_conditionalRender"
18
16
  * function MyApp() {
19
17
  * const theme = useDocumentTheme();
20
18
  *
21
- * return (
22
- * <div>
23
- * {theme === 'dark' ? <DarkIcon /> : <LightIcon />}
24
- * </div>
25
- * );
19
+ * return <div>{theme === "dark" ? <DarkIcon /> : <LightIcon />}</div>;
26
20
  * }
27
21
  * ```
28
22
  *
29
23
  * @example Use with theme-aware styling
30
- * ```tsx
24
+ * ```tsx source="./useDocumentTheme.examples.tsx#useDocumentTheme_themedButton"
31
25
  * function ThemedButton() {
32
26
  * const theme = useDocumentTheme();
33
27
  *
34
28
  * return (
35
- * <button style={{
36
- * background: theme === 'dark' ? '#333' : '#fff',
37
- * color: theme === 'dark' ? '#fff' : '#333',
38
- * }}>
29
+ * <button
30
+ * style={{
31
+ * background: theme === "dark" ? "#333" : "#fff",
32
+ * color: theme === "dark" ? "#fff" : "#333",
33
+ * }}
34
+ * >
39
35
  * Click me
40
36
  * </button>
41
37
  * );
42
38
  * }
43
39
  * ```
44
40
  *
45
- * @see {@link getDocumentTheme} for the underlying function
46
- * @see {@link applyDocumentTheme} to set the theme
41
+ * @see {@link getDocumentTheme `getDocumentTheme`} for the underlying function
42
+ * @see {@link applyDocumentTheme `applyDocumentTheme`} to set the theme
47
43
  */
48
44
  export declare function useDocumentTheme(): McpUiTheme;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type-checked examples for the useDocumentTheme hook.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
@@ -14,16 +14,14 @@ import { McpUiHostContext } from "../types";
14
14
  * this hook ensures they work correctly by setting the `color-scheme` property
15
15
  * based on the host's theme preference.
16
16
  *
17
- * @param app - The connected {@link App} instance, or null during initialization
17
+ * @param app - The connected {@link App `App`} instance, or null during initialization
18
18
  * @param initialContext - Initial host context from the connection (optional).
19
19
  * If provided, styles and theme will be applied immediately on mount.
20
20
  *
21
21
  * @example
22
- * ```tsx
23
- * import { useApp, useHostStyleVariables } from '@modelcontextprotocol/ext-apps/react';
24
- *
22
+ * ```tsx source="./useHostStyles.examples.tsx#useHostStyleVariables_basicUsage"
25
23
  * function MyApp() {
26
- * const { app, isConnected } = useApp({
24
+ * const { app } = useApp({
27
25
  * appInfo: { name: "MyApp", version: "1.0.0" },
28
26
  * capabilities: {},
29
27
  * });
@@ -32,17 +30,17 @@ import { McpUiHostContext } from "../types";
32
30
  * useHostStyleVariables(app, app?.getHostContext());
33
31
  *
34
32
  * return (
35
- * <div style={{ background: 'var(--color-background-primary)' }}>
33
+ * <div style={{ background: "var(--color-background-primary)" }}>
36
34
  * Hello!
37
35
  * </div>
38
36
  * );
39
37
  * }
40
38
  * ```
41
39
  *
42
- * @see {@link applyHostStyleVariables} for the underlying styles function
43
- * @see {@link applyDocumentTheme} for the underlying theme function
44
- * @see {@link useHostFonts} for applying host fonts
45
- * @see {@link McpUiStyles} for available CSS variables
40
+ * @see {@link applyHostStyleVariables `applyHostStyleVariables`} for the underlying styles function
41
+ * @see {@link applyDocumentTheme `applyDocumentTheme`} for the underlying theme function
42
+ * @see {@link useHostFonts `useHostFonts`} for applying host fonts
43
+ * @see {@link McpUiStyles `McpUiStyles`} for available CSS variables
46
44
  */
47
45
  export declare function useHostStyleVariables(app: App | null, initialContext?: McpUiHostContext | null): void;
48
46
  /**
@@ -57,66 +55,59 @@ export declare function useHostStyleVariables(app: App | null, initialContext?:
57
55
  * The hook also applies fonts from the initial host context when
58
56
  * the app first connects.
59
57
  *
60
- * @param app - The connected {@link App} instance, or null during initialization
58
+ * @param app - The connected {@link App `App`} instance, or null during initialization
61
59
  * @param initialContext - Initial host context from the connection (optional).
62
60
  * If provided, fonts will be applied immediately on mount.
63
61
  *
64
62
  * @example Basic usage with useApp
65
- * ```tsx
66
- * import { useApp } from '@modelcontextprotocol/ext-apps/react';
67
- * import { useHostFonts } from '@modelcontextprotocol/ext-apps/react';
68
- *
63
+ * ```tsx source="./useHostStyles.examples.tsx#useHostFonts_basicUsage"
69
64
  * function MyApp() {
70
- * const { app, isConnected } = useApp({
65
+ * const { app } = useApp({
71
66
  * appInfo: { name: "MyApp", version: "1.0.0" },
72
67
  * capabilities: {},
73
68
  * });
74
69
  *
75
- * // Automatically apply host fonts
76
- * useHostFonts(app);
70
+ * // Apply host fonts - pass initial context to apply fonts from connect() immediately
71
+ * useHostFonts(app, app?.getHostContext());
77
72
  *
78
- * return (
79
- * <div style={{ fontFamily: 'var(--font-sans)' }}>
80
- * Hello!
81
- * </div>
82
- * );
73
+ * return <div style={{ fontFamily: "var(--font-sans)" }}>Hello!</div>;
83
74
  * }
84
75
  * ```
85
76
  *
86
- * @example With initial context
87
- * ```tsx
88
- * const [hostContext, setHostContext] = useState<McpUiHostContext | null>(null);
89
- *
90
- * // ... get initial context from app.connect() result
91
- *
92
- * useHostFonts(app, hostContext);
93
- * ```
94
- *
95
- * @see {@link applyHostFonts} for the underlying fonts function
96
- * @see {@link useHostStyleVariables} for applying style variables and theme
77
+ * @see {@link applyHostFonts `applyHostFonts`} for the underlying fonts function
78
+ * @see {@link useHostStyleVariables `useHostStyleVariables`} for applying style variables and theme
97
79
  */
98
80
  export declare function useHostFonts(app: App | null, initialContext?: McpUiHostContext | null): void;
99
81
  /**
100
82
  * Applies all host styling (CSS variables, theme, and fonts) to match the host application.
101
83
  *
102
- * This is a convenience hook that combines {@link useHostStyleVariables} and
103
- * {@link useHostFonts}. Use the individual hooks if you need more control.
84
+ * This is a convenience hook that combines {@link useHostStyleVariables `useHostStyleVariables`} and
85
+ * {@link useHostFonts `useHostFonts`}. Use the individual hooks if you need more control.
104
86
  *
105
- * @param app - The connected {@link App} instance, or null during initialization
87
+ * @param app - The connected {@link App `App`} instance, or null during initialization
106
88
  * @param initialContext - Initial host context from the connection (optional).
107
89
  * Pass `app?.getHostContext()` to apply styles immediately on mount.
108
90
  *
109
91
  * @example
110
- * ```tsx
92
+ * ```tsx source="./useHostStyles.examples.tsx#useHostStyles_basicUsage"
111
93
  * function MyApp() {
112
- * const { app } = useApp({ appInfo, capabilities: {} });
94
+ * const { app } = useApp({
95
+ * appInfo: { name: "MyApp", version: "1.0.0" },
96
+ * capabilities: {},
97
+ * });
98
+ *
99
+ * // Apply all host styles - pass initial context to apply styles from connect() immediately
113
100
  * useHostStyles(app, app?.getHostContext());
114
101
  *
115
- * return <div style={{ background: 'var(--color-background-primary)' }}>...</div>;
102
+ * return (
103
+ * <div style={{ background: "var(--color-background-primary)" }}>
104
+ * Hello!
105
+ * </div>
106
+ * );
116
107
  * }
117
108
  * ```
118
109
  *
119
- * @see {@link useHostStyleVariables} for style variables and theme only
120
- * @see {@link useHostFonts} for fonts only
110
+ * @see {@link useHostStyleVariables `useHostStyleVariables`} for style variables and theme only
111
+ * @see {@link useHostFonts `useHostFonts`} for fonts only
121
112
  */
122
113
  export declare function useHostStyles(app: App | null, initialContext?: McpUiHostContext | null): void;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type-checked examples for the useHostStyles hooks.
3
+ *
4
+ * @module
5
+ */
6
+ export {};