@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,129 @@
1
+ import { JSONRPCMessage, MessageExtraInfo } from "@modelcontextprotocol/sdk/types.js";
2
+ import { Transport, TransportSendOptions } from "@modelcontextprotocol/sdk/shared/transport.js";
3
+ /**
4
+ * JSON-RPC transport using window.postMessage for iframe↔parent communication.
5
+ *
6
+ * This transport enables bidirectional communication between MCP Apps running in
7
+ * iframes and their host applications using the browser's postMessage API. It
8
+ * implements the MCP SDK's Transport interface.
9
+ *
10
+ * ## Security
11
+ *
12
+ * The `eventSource` parameter provides origin validation by filtering messages
13
+ * from specific sources. Guest UIs typically don't need to specify this (they only
14
+ * communicate with their parent), but hosts should validate the iframe source for
15
+ * security.
16
+ *
17
+ * ## Usage
18
+ *
19
+ * **Guest UI (default)**:
20
+ * ```typescript
21
+ * const transport = new PostMessageTransport(window.parent);
22
+ * await app.connect(transport);
23
+ * ```
24
+ *
25
+ * **Host (with source validation)**:
26
+ * ```typescript
27
+ * const iframe = document.getElementById('app-iframe') as HTMLIFrameElement;
28
+ * const transport = new PostMessageTransport(
29
+ * iframe.contentWindow!,
30
+ * iframe.contentWindow // Validate messages from this iframe only
31
+ * );
32
+ * await bridge.connect(transport);
33
+ * ```
34
+ *
35
+ * @see {@link app.App.connect} for Guest UI usage
36
+ * @see {@link app-bridge.AppBridge.connect} for Host usage
37
+ */
38
+ export declare class PostMessageTransport implements Transport {
39
+ private eventTarget;
40
+ private eventSource?;
41
+ private messageListener;
42
+ /**
43
+ * Create a new PostMessageTransport.
44
+ *
45
+ * @param eventTarget - Target window to send messages to (default: window.parent)
46
+ * @param eventSource - Optional source validation. If specified, only messages from
47
+ * this source will be accepted. Guest UIs typically don't need this (they only
48
+ * receive from parent), but hosts should validate the iframe source.
49
+ *
50
+ * @example Guest UI connecting to parent
51
+ * ```typescript
52
+ * const transport = new PostMessageTransport(window.parent);
53
+ * ```
54
+ *
55
+ * @example Host connecting to iframe with validation
56
+ * ```typescript
57
+ * const iframe = document.getElementById('app') as HTMLIFrameElement;
58
+ * const transport = new PostMessageTransport(
59
+ * iframe.contentWindow!,
60
+ * iframe.contentWindow // Only accept messages from this iframe
61
+ * );
62
+ * ```
63
+ */
64
+ constructor(eventTarget?: Window, eventSource?: MessageEventSource | undefined);
65
+ /**
66
+ * Begin listening for messages from the event source.
67
+ *
68
+ * Registers a message event listener on the window. Must be called before
69
+ * messages can be received.
70
+ */
71
+ start(): Promise<void>;
72
+ /**
73
+ * Send a JSON-RPC message to the target window.
74
+ *
75
+ * Messages are sent using postMessage with "*" origin, meaning they are visible
76
+ * to all frames. The receiver should validate the message source for security.
77
+ *
78
+ * @param message - JSON-RPC message to send
79
+ * @param options - Optional send options (currently unused)
80
+ */
81
+ send(message: JSONRPCMessage, options?: TransportSendOptions): Promise<void>;
82
+ /**
83
+ * Stop listening for messages and cleanup.
84
+ *
85
+ * Removes the message event listener and calls the {@link onclose} callback if set.
86
+ */
87
+ close(): Promise<void>;
88
+ /**
89
+ * Called when the transport is closed.
90
+ *
91
+ * Set this handler to be notified when {@link close} is called.
92
+ */
93
+ onclose?: () => void;
94
+ /**
95
+ * Called when a message parsing error occurs.
96
+ *
97
+ * This handler is invoked when a received message fails JSON-RPC schema
98
+ * validation. The error parameter contains details about the validation failure.
99
+ *
100
+ * @param error - Error describing the validation failure
101
+ */
102
+ onerror?: (error: Error) => void;
103
+ /**
104
+ * Called when a valid JSON-RPC message is received.
105
+ *
106
+ * This handler is invoked after message validation succeeds. The {@link start}
107
+ * method must be called before messages will be received.
108
+ *
109
+ * @param message - The validated JSON-RPC message
110
+ * @param extra - Optional metadata about the message (unused in this transport)
111
+ */
112
+ onmessage?: (message: JSONRPCMessage, extra?: MessageExtraInfo) => void;
113
+ /**
114
+ * Optional session identifier for this transport connection.
115
+ *
116
+ * Set by the MCP SDK to track the connection session. Not required for
117
+ * PostMessageTransport functionality.
118
+ */
119
+ sessionId?: string;
120
+ /**
121
+ * Callback to set the negotiated protocol version.
122
+ *
123
+ * The MCP SDK calls this during initialization to communicate the protocol
124
+ * version negotiated with the peer.
125
+ *
126
+ * @param version - The negotiated protocol version string
127
+ */
128
+ setProtocolVersion?: (version: string) => void;
129
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * React utilities for building MCP Apps.
3
+ *
4
+ * This module provides React hooks and utilities for easily building
5
+ * interactive MCP Apps using React. This is optional - the core SDK
6
+ * ({@link App}, {@link PostMessageTransport}) is framework-agnostic and can be
7
+ * used with any UI framework or vanilla JavaScript.
8
+ *
9
+ * ## Main Exports
10
+ *
11
+ * - {@link useApp} - React hook to create and connect an MCP App
12
+ * - {@link useAutoResize} - React hook for manual auto-resize control (rarely needed)
13
+ *
14
+ * @module @modelcontextprotocol/ext-apps/react
15
+ *
16
+ * @example Basic React App
17
+ * ```tsx
18
+ * import { useApp } from '@modelcontextprotocol/ext-apps/react';
19
+ *
20
+ * function MyApp() {
21
+ * const { app, isConnected, error } = useApp({
22
+ * appInfo: { name: "MyApp", version: "1.0.0" },
23
+ * capabilities: {}
24
+ * });
25
+ *
26
+ * if (error) return <div>Error: {error.message}</div>;
27
+ * if (!isConnected) return <div>Connecting...</div>;
28
+ *
29
+ * return <div>Connected!</div>;
30
+ * }
31
+ * ```
32
+ */
33
+ export * from "./useApp";
34
+ export * from "./useAutoResize";