@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.
- package/README.md +75 -0
- package/dist/src/app-bridge.d.ts +594 -0
- package/dist/src/app-bridge.js +29 -0
- package/dist/src/app-bridge.test.d.ts +1 -0
- package/dist/src/app.d.ts +620 -0
- package/dist/src/app.js +29 -0
- package/dist/src/message-transport.d.ts +129 -0
- package/dist/src/react/index.d.ts +34 -0
- package/dist/src/react/index.js +29 -0
- package/dist/src/react/useApp.d.ts +101 -0
- package/dist/src/react/useAutoResize.d.ts +51 -0
- package/dist/src/types.d.ts +838 -0
- package/package.json +83 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,620 @@
|
|
|
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";
|
|
4
|
+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
5
|
+
export { PostMessageTransport } from "./message-transport";
|
|
6
|
+
export * from "./types";
|
|
7
|
+
/**
|
|
8
|
+
* Metadata key for associating a resource URI with a tool call.
|
|
9
|
+
*
|
|
10
|
+
* MCP servers include this key in tool call result metadata to indicate which
|
|
11
|
+
* UI resource should be displayed for the tool. When hosts receive a tool result
|
|
12
|
+
* containing this metadata, they resolve and render the corresponding App.
|
|
13
|
+
*
|
|
14
|
+
* **Note**: This constant is provided for reference. MCP servers set this metadata
|
|
15
|
+
* in their tool handlers; App developers typically don't need to use it directly.
|
|
16
|
+
*
|
|
17
|
+
* @example How MCP servers use this key (server-side, not in Apps)
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // In an MCP server's tool handler:
|
|
20
|
+
* return {
|
|
21
|
+
* content: [{ type: "text", text: "Result" }],
|
|
22
|
+
* _meta: {
|
|
23
|
+
* [RESOURCE_URI_META_KEY]: "ui://weather/forecast"
|
|
24
|
+
* }
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example How hosts check for this metadata (host-side)
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const result = await mcpClient.callTool({ name: "weather", arguments: {} });
|
|
31
|
+
* const uiUri = result._meta?.[RESOURCE_URI_META_KEY];
|
|
32
|
+
* if (uiUri) {
|
|
33
|
+
* // Load and display the UI resource
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const RESOURCE_URI_META_KEY = "ui/resourceUri";
|
|
38
|
+
/**
|
|
39
|
+
* Options for configuring App behavior.
|
|
40
|
+
*
|
|
41
|
+
* Extends ProtocolOptions from the MCP SDK with App-specific configuration.
|
|
42
|
+
*
|
|
43
|
+
* @see ProtocolOptions from @modelcontextprotocol/sdk for inherited options
|
|
44
|
+
*/
|
|
45
|
+
type AppOptions = ProtocolOptions & {
|
|
46
|
+
/**
|
|
47
|
+
* Automatically report size changes to the host using ResizeObserver.
|
|
48
|
+
*
|
|
49
|
+
* When enabled, the App monitors `document.body` and `document.documentElement`
|
|
50
|
+
* for size changes and automatically sends `ui/notifications/size-changed`
|
|
51
|
+
* notifications to the host.
|
|
52
|
+
*
|
|
53
|
+
* @default true
|
|
54
|
+
*/
|
|
55
|
+
autoResize?: boolean;
|
|
56
|
+
};
|
|
57
|
+
type RequestHandlerExtra = Parameters<Parameters<App["setRequestHandler"]>[1]>[1];
|
|
58
|
+
/**
|
|
59
|
+
* Main class for MCP Apps to communicate with their host.
|
|
60
|
+
*
|
|
61
|
+
* The App class provides a framework-agnostic way to build interactive MCP Apps
|
|
62
|
+
* that run inside host applications. It extends the MCP SDK's Protocol class and
|
|
63
|
+
* handles the connection lifecycle, initialization handshake, and bidirectional
|
|
64
|
+
* communication with the host.
|
|
65
|
+
*
|
|
66
|
+
* ## Architecture
|
|
67
|
+
*
|
|
68
|
+
* Guest UIs (Apps) act as MCP clients connecting to the host via {@link PostMessageTransport}.
|
|
69
|
+
* The host proxies requests to the actual MCP server and forwards
|
|
70
|
+
* responses back to the App.
|
|
71
|
+
*
|
|
72
|
+
* ## Lifecycle
|
|
73
|
+
*
|
|
74
|
+
* 1. **Create**: Instantiate App with info and capabilities
|
|
75
|
+
* 2. **Connect**: Call `connect()` to establish transport and perform handshake
|
|
76
|
+
* 3. **Interactive**: Send requests, receive notifications, call tools
|
|
77
|
+
* 4. **Cleanup**: Host sends teardown request before unmounting
|
|
78
|
+
*
|
|
79
|
+
* ## Inherited Methods
|
|
80
|
+
*
|
|
81
|
+
* As a subclass of Protocol, App inherits key methods for handling communication:
|
|
82
|
+
* - `setRequestHandler()` - Register handlers for requests from host
|
|
83
|
+
* - `setNotificationHandler()` - Register handlers for notifications from host
|
|
84
|
+
*
|
|
85
|
+
* @see Protocol from @modelcontextprotocol/sdk for all inherited methods
|
|
86
|
+
*
|
|
87
|
+
* ## Notification Setters
|
|
88
|
+
*
|
|
89
|
+
* For common notifications, the App class provides convenient setter properties
|
|
90
|
+
* that simplify handler registration:
|
|
91
|
+
* - `ontoolinput` - Complete tool arguments from host
|
|
92
|
+
* - `ontoolinputpartial` - Streaming partial tool arguments
|
|
93
|
+
* - `ontoolresult` - Tool execution results
|
|
94
|
+
* - `onhostcontextchanged` - Host context changes (theme, viewport, etc.)
|
|
95
|
+
*
|
|
96
|
+
* These setters are convenience wrappers around `setNotificationHandler()`.
|
|
97
|
+
* Both patterns work; use whichever fits your coding style better.
|
|
98
|
+
*
|
|
99
|
+
* @example Basic usage with PostMessageTransport
|
|
100
|
+
* ```typescript
|
|
101
|
+
* import {
|
|
102
|
+
* App,
|
|
103
|
+
* PostMessageTransport,
|
|
104
|
+
* McpUiToolInputNotificationSchema
|
|
105
|
+
* } from '@modelcontextprotocol/ext-apps';
|
|
106
|
+
*
|
|
107
|
+
* const app = new App(
|
|
108
|
+
* { name: "WeatherApp", version: "1.0.0" },
|
|
109
|
+
* {} // capabilities
|
|
110
|
+
* );
|
|
111
|
+
*
|
|
112
|
+
* // Register notification handler using setter (simpler)
|
|
113
|
+
* app.ontoolinput = (params) => {
|
|
114
|
+
* console.log("Tool arguments:", params.arguments);
|
|
115
|
+
* };
|
|
116
|
+
*
|
|
117
|
+
* // OR using inherited setNotificationHandler (more explicit)
|
|
118
|
+
* app.setNotificationHandler(
|
|
119
|
+
* McpUiToolInputNotificationSchema,
|
|
120
|
+
* (notification) => {
|
|
121
|
+
* console.log("Tool arguments:", notification.params.arguments);
|
|
122
|
+
* }
|
|
123
|
+
* );
|
|
124
|
+
*
|
|
125
|
+
* await app.connect(new PostMessageTransport(window.parent));
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example Sending a message to the host's chat
|
|
129
|
+
* ```typescript
|
|
130
|
+
* await app.sendMessage({
|
|
131
|
+
* role: "user",
|
|
132
|
+
* content: [{ type: "text", text: "Weather updated!" }]
|
|
133
|
+
* });
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export declare class App extends Protocol<Request, Notification, Result> {
|
|
137
|
+
private _appInfo;
|
|
138
|
+
private _capabilities;
|
|
139
|
+
private options;
|
|
140
|
+
private _hostCapabilities?;
|
|
141
|
+
private _hostInfo?;
|
|
142
|
+
/**
|
|
143
|
+
* Create a new MCP App instance.
|
|
144
|
+
*
|
|
145
|
+
* @param _appInfo - App identification (name and version)
|
|
146
|
+
* @param _capabilities - Features and capabilities this app provides
|
|
147
|
+
* @param options - Configuration options including autoResize behavior
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const app = new App(
|
|
152
|
+
* { name: "MyApp", version: "1.0.0" },
|
|
153
|
+
* { tools: { listChanged: true } }, // capabilities
|
|
154
|
+
* { autoResize: true } // options
|
|
155
|
+
* );
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
constructor(_appInfo: Implementation, _capabilities?: McpUiAppCapabilities, options?: AppOptions);
|
|
159
|
+
/**
|
|
160
|
+
* Get the host's capabilities discovered during initialization.
|
|
161
|
+
*
|
|
162
|
+
* Returns the capabilities that the host advertised during the
|
|
163
|
+
* {@link connect} handshake. Returns `undefined` if called before
|
|
164
|
+
* connection is established.
|
|
165
|
+
*
|
|
166
|
+
* @returns Host capabilities, or `undefined` if not yet connected
|
|
167
|
+
*
|
|
168
|
+
* @example Check host capabilities after connection
|
|
169
|
+
* ```typescript
|
|
170
|
+
* await app.connect(transport);
|
|
171
|
+
* const caps = app.getHostCapabilities();
|
|
172
|
+
* if (caps === undefined) {
|
|
173
|
+
* console.error("Not connected");
|
|
174
|
+
* return;
|
|
175
|
+
* }
|
|
176
|
+
* if (caps.serverTools) {
|
|
177
|
+
* console.log("Host supports server tool calls");
|
|
178
|
+
* }
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @see {@link connect} for the initialization handshake
|
|
182
|
+
* @see {@link McpUiHostCapabilities} for the capabilities structure
|
|
183
|
+
*/
|
|
184
|
+
getHostCapabilities(): McpUiHostCapabilities | undefined;
|
|
185
|
+
/**
|
|
186
|
+
* Get the host's implementation info discovered during initialization.
|
|
187
|
+
*
|
|
188
|
+
* Returns the host's name and version as advertised during the
|
|
189
|
+
* {@link connect} handshake. Returns `undefined` if called before
|
|
190
|
+
* connection is established.
|
|
191
|
+
*
|
|
192
|
+
* @returns Host implementation info, or `undefined` if not yet connected
|
|
193
|
+
*
|
|
194
|
+
* @example Log host information after connection
|
|
195
|
+
* ```typescript
|
|
196
|
+
* await app.connect(transport);
|
|
197
|
+
* const host = app.getHostVersion();
|
|
198
|
+
* if (host === undefined) {
|
|
199
|
+
* console.error("Not connected");
|
|
200
|
+
* return;
|
|
201
|
+
* }
|
|
202
|
+
* console.log(`Connected to ${host.name} v${host.version}`);
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @see {@link connect} for the initialization handshake
|
|
206
|
+
*/
|
|
207
|
+
getHostVersion(): Implementation | undefined;
|
|
208
|
+
/**
|
|
209
|
+
* Convenience handler for receiving complete tool input from the host.
|
|
210
|
+
*
|
|
211
|
+
* Set this property to register a handler that will be called when the host
|
|
212
|
+
* sends a tool's complete arguments. This is sent after a tool call begins
|
|
213
|
+
* and before the tool result is available.
|
|
214
|
+
*
|
|
215
|
+
* This setter is a convenience wrapper around `setNotificationHandler()` that
|
|
216
|
+
* automatically handles the notification schema and extracts the params for you.
|
|
217
|
+
*
|
|
218
|
+
* Register handlers before calling {@link connect} to avoid missing notifications.
|
|
219
|
+
*
|
|
220
|
+
* @param callback - Function called with the tool input params
|
|
221
|
+
*
|
|
222
|
+
* @example Using the setter (simpler)
|
|
223
|
+
* ```typescript
|
|
224
|
+
* // Register before connecting to ensure no notifications are missed
|
|
225
|
+
* app.ontoolinput = (params) => {
|
|
226
|
+
* console.log("Tool:", params.arguments);
|
|
227
|
+
* // Update your UI with the tool arguments
|
|
228
|
+
* };
|
|
229
|
+
* await app.connect(transport);
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @example Using setNotificationHandler (more explicit)
|
|
233
|
+
* ```typescript
|
|
234
|
+
* app.setNotificationHandler(
|
|
235
|
+
* McpUiToolInputNotificationSchema,
|
|
236
|
+
* (notification) => {
|
|
237
|
+
* console.log("Tool:", notification.params.arguments);
|
|
238
|
+
* }
|
|
239
|
+
* );
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @see {@link setNotificationHandler} for the underlying method
|
|
243
|
+
* @see {@link McpUiToolInputNotification} for the notification structure
|
|
244
|
+
*/
|
|
245
|
+
set ontoolinput(callback: (params: McpUiToolInputNotification["params"]) => void);
|
|
246
|
+
/**
|
|
247
|
+
* Convenience handler for receiving streaming partial tool input from the host.
|
|
248
|
+
*
|
|
249
|
+
* Set this property to register a handler that will be called as the host
|
|
250
|
+
* streams partial tool arguments during tool call initialization. This enables
|
|
251
|
+
* progressive rendering of tool arguments before they're complete.
|
|
252
|
+
*
|
|
253
|
+
* This setter is a convenience wrapper around `setNotificationHandler()` that
|
|
254
|
+
* automatically handles the notification schema and extracts the params for you.
|
|
255
|
+
*
|
|
256
|
+
* Register handlers before calling {@link connect} to avoid missing notifications.
|
|
257
|
+
*
|
|
258
|
+
* @param callback - Function called with each partial tool input update
|
|
259
|
+
*
|
|
260
|
+
* @example Progressive rendering of tool arguments
|
|
261
|
+
* ```typescript
|
|
262
|
+
* app.ontoolinputpartial = (params) => {
|
|
263
|
+
* console.log("Partial args:", params.arguments);
|
|
264
|
+
* // Update your UI progressively as arguments stream in
|
|
265
|
+
* };
|
|
266
|
+
* ```
|
|
267
|
+
*
|
|
268
|
+
* @see {@link setNotificationHandler} for the underlying method
|
|
269
|
+
* @see {@link McpUiToolInputPartialNotification} for the notification structure
|
|
270
|
+
* @see {@link ontoolinput} for the complete tool input handler
|
|
271
|
+
*/
|
|
272
|
+
set ontoolinputpartial(callback: (params: McpUiToolInputPartialNotification["params"]) => void);
|
|
273
|
+
/**
|
|
274
|
+
* Convenience handler for receiving tool execution results from the host.
|
|
275
|
+
*
|
|
276
|
+
* Set this property to register a handler that will be called when the host
|
|
277
|
+
* sends the result of a tool execution. This is sent after the tool completes
|
|
278
|
+
* on the MCP server, allowing your app to display the results or update its state.
|
|
279
|
+
*
|
|
280
|
+
* This setter is a convenience wrapper around `setNotificationHandler()` that
|
|
281
|
+
* automatically handles the notification schema and extracts the params for you.
|
|
282
|
+
*
|
|
283
|
+
* Register handlers before calling {@link connect} to avoid missing notifications.
|
|
284
|
+
*
|
|
285
|
+
* @param callback - Function called with the tool result
|
|
286
|
+
*
|
|
287
|
+
* @example Display tool execution results
|
|
288
|
+
* ```typescript
|
|
289
|
+
* app.ontoolresult = (params) => {
|
|
290
|
+
* if (params.content) {
|
|
291
|
+
* console.log("Tool output:", params.content);
|
|
292
|
+
* }
|
|
293
|
+
* if (params.isError) {
|
|
294
|
+
* console.error("Tool execution failed");
|
|
295
|
+
* }
|
|
296
|
+
* };
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* @see {@link setNotificationHandler} for the underlying method
|
|
300
|
+
* @see {@link McpUiToolResultNotification} for the notification structure
|
|
301
|
+
* @see {@link ontoolinput} for the initial tool input handler
|
|
302
|
+
*/
|
|
303
|
+
set ontoolresult(callback: (params: McpUiToolResultNotification["params"]) => void);
|
|
304
|
+
/**
|
|
305
|
+
* Convenience handler for host context changes (theme, viewport, locale, etc.).
|
|
306
|
+
*
|
|
307
|
+
* Set this property to register a handler that will be called when the host's
|
|
308
|
+
* context changes, such as theme switching (light/dark), viewport size changes,
|
|
309
|
+
* locale changes, or other environmental updates. Apps should respond by
|
|
310
|
+
* updating their UI accordingly.
|
|
311
|
+
*
|
|
312
|
+
* This setter is a convenience wrapper around `setNotificationHandler()` that
|
|
313
|
+
* automatically handles the notification schema and extracts the params for you.
|
|
314
|
+
*
|
|
315
|
+
* Register handlers before calling {@link connect} to avoid missing notifications.
|
|
316
|
+
*
|
|
317
|
+
* @param callback - Function called with the updated host context
|
|
318
|
+
*
|
|
319
|
+
* @example Respond to theme changes
|
|
320
|
+
* ```typescript
|
|
321
|
+
* app.onhostcontextchanged = (params) => {
|
|
322
|
+
* if (params.theme === "dark") {
|
|
323
|
+
* document.body.classList.add("dark-theme");
|
|
324
|
+
* } else {
|
|
325
|
+
* document.body.classList.remove("dark-theme");
|
|
326
|
+
* }
|
|
327
|
+
* };
|
|
328
|
+
* ```
|
|
329
|
+
*
|
|
330
|
+
* @see {@link setNotificationHandler} for the underlying method
|
|
331
|
+
* @see {@link McpUiHostContextChangedNotification} for the notification structure
|
|
332
|
+
* @see {@link McpUiHostContext} for the full context structure
|
|
333
|
+
*/
|
|
334
|
+
set onhostcontextchanged(callback: (params: McpUiHostContextChangedNotification["params"]) => void);
|
|
335
|
+
/**
|
|
336
|
+
* Convenience handler for tool call requests from the host.
|
|
337
|
+
*
|
|
338
|
+
* Set this property to register a handler that will be called when the host
|
|
339
|
+
* requests this app to execute a tool. This enables apps to provide their own
|
|
340
|
+
* tools that can be called by the host or LLM.
|
|
341
|
+
*
|
|
342
|
+
* The app must declare tool capabilities in the constructor to use this handler.
|
|
343
|
+
*
|
|
344
|
+
* This setter is a convenience wrapper around `setRequestHandler()` that
|
|
345
|
+
* automatically handles the request schema and extracts the params for you.
|
|
346
|
+
*
|
|
347
|
+
* Register handlers before calling {@link connect} to avoid missing requests.
|
|
348
|
+
*
|
|
349
|
+
* @param callback - Async function that executes the tool and returns the result.
|
|
350
|
+
* The callback will only be invoked if the app declared tool capabilities
|
|
351
|
+
* in the constructor.
|
|
352
|
+
*
|
|
353
|
+
* @example Handle tool calls from the host
|
|
354
|
+
* ```typescript
|
|
355
|
+
* app.oncalltool = async (params, extra) => {
|
|
356
|
+
* if (params.name === "greet") {
|
|
357
|
+
* const name = params.arguments?.name ?? "World";
|
|
358
|
+
* return { content: [{ type: "text", text: `Hello, ${name}!` }] };
|
|
359
|
+
* }
|
|
360
|
+
* throw new Error(`Unknown tool: ${params.name}`);
|
|
361
|
+
* };
|
|
362
|
+
* ```
|
|
363
|
+
*
|
|
364
|
+
* @see {@link setRequestHandler} for the underlying method
|
|
365
|
+
*/
|
|
366
|
+
set oncalltool(callback: (params: CallToolRequest["params"], extra: RequestHandlerExtra) => Promise<CallToolResult>);
|
|
367
|
+
/**
|
|
368
|
+
* Convenience handler for listing available tools.
|
|
369
|
+
*
|
|
370
|
+
* Set this property to register a handler that will be called when the host
|
|
371
|
+
* requests a list of tools this app provides. This enables dynamic tool
|
|
372
|
+
* discovery by the host or LLM.
|
|
373
|
+
*
|
|
374
|
+
* The app must declare tool capabilities in the constructor to use this handler.
|
|
375
|
+
*
|
|
376
|
+
* This setter is a convenience wrapper around `setRequestHandler()` that
|
|
377
|
+
* automatically handles the request schema and extracts the params for you.
|
|
378
|
+
*
|
|
379
|
+
* Register handlers before calling {@link connect} to avoid missing requests.
|
|
380
|
+
*
|
|
381
|
+
* @param callback - Async function that returns the list of available tools.
|
|
382
|
+
* The callback will only be invoked if the app declared tool capabilities
|
|
383
|
+
* in the constructor.
|
|
384
|
+
*
|
|
385
|
+
* @example Return available tools
|
|
386
|
+
* ```typescript
|
|
387
|
+
* app.onlisttools = async (params, extra) => {
|
|
388
|
+
* return {
|
|
389
|
+
* tools: ["calculate", "convert", "format"]
|
|
390
|
+
* };
|
|
391
|
+
* };
|
|
392
|
+
* ```
|
|
393
|
+
*
|
|
394
|
+
* @see {@link setRequestHandler} for the underlying method
|
|
395
|
+
* @see {@link oncalltool} for handling tool execution
|
|
396
|
+
*/
|
|
397
|
+
set onlisttools(callback: (params: ListToolsRequest["params"], extra: RequestHandlerExtra) => Promise<{
|
|
398
|
+
tools: string[];
|
|
399
|
+
}>);
|
|
400
|
+
/**
|
|
401
|
+
* Verify that the host supports the capability required for the given request method.
|
|
402
|
+
* @internal
|
|
403
|
+
*/
|
|
404
|
+
assertCapabilityForMethod(method: Request["method"]): void;
|
|
405
|
+
/**
|
|
406
|
+
* Verify that the app declared the capability required for the given request method.
|
|
407
|
+
* @internal
|
|
408
|
+
*/
|
|
409
|
+
assertRequestHandlerCapability(method: Request["method"]): void;
|
|
410
|
+
/**
|
|
411
|
+
* Verify that the app supports the capability required for the given notification method.
|
|
412
|
+
* @internal
|
|
413
|
+
*/
|
|
414
|
+
assertNotificationCapability(method: Notification["method"]): void;
|
|
415
|
+
/**
|
|
416
|
+
* Verify that task creation is supported for the given request method.
|
|
417
|
+
* @internal
|
|
418
|
+
*/
|
|
419
|
+
protected assertTaskCapability(_method: string): void;
|
|
420
|
+
/**
|
|
421
|
+
* Verify that task handler is supported for the given method.
|
|
422
|
+
* @internal
|
|
423
|
+
*/
|
|
424
|
+
protected assertTaskHandlerCapability(_method: string): void;
|
|
425
|
+
/**
|
|
426
|
+
* Call a tool on the originating MCP server (proxied through the host).
|
|
427
|
+
*
|
|
428
|
+
* Apps can call tools to fetch fresh data or trigger server-side actions.
|
|
429
|
+
* The host proxies the request to the actual MCP server and returns the result.
|
|
430
|
+
*
|
|
431
|
+
* @param params - Tool name and arguments
|
|
432
|
+
* @param options - Request options (timeout, etc.)
|
|
433
|
+
* @returns Tool execution result
|
|
434
|
+
*
|
|
435
|
+
* @throws {Error} If the tool does not exist on the server
|
|
436
|
+
* @throws {Error} If the request times out or the connection is lost
|
|
437
|
+
* @throws {Error} If the host rejects the request
|
|
438
|
+
*
|
|
439
|
+
* Note: Tool-level execution errors are returned in the result with `isError: true`
|
|
440
|
+
* rather than throwing exceptions. Always check `result.isError` to distinguish
|
|
441
|
+
* between transport failures (thrown) and tool execution failures (returned).
|
|
442
|
+
*
|
|
443
|
+
* @example Fetch updated weather data
|
|
444
|
+
* ```typescript
|
|
445
|
+
* try {
|
|
446
|
+
* const result = await app.callServerTool({
|
|
447
|
+
* name: "get_weather",
|
|
448
|
+
* arguments: { location: "Tokyo" }
|
|
449
|
+
* });
|
|
450
|
+
* if (result.isError) {
|
|
451
|
+
* console.error("Tool returned error:", result.content);
|
|
452
|
+
* } else {
|
|
453
|
+
* console.log(result.content);
|
|
454
|
+
* }
|
|
455
|
+
* } catch (error) {
|
|
456
|
+
* console.error("Tool call failed:", error);
|
|
457
|
+
* }
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
460
|
+
callServerTool(params: CallToolRequest["params"], options?: RequestOptions): Promise<CallToolResult>;
|
|
461
|
+
/**
|
|
462
|
+
* Send a message to the host's chat interface.
|
|
463
|
+
*
|
|
464
|
+
* Enables the app to add messages to the conversation thread. Useful for
|
|
465
|
+
* user-initiated messages or app-to-conversation communication.
|
|
466
|
+
*
|
|
467
|
+
* @param params - Message role and content
|
|
468
|
+
* @param options - Request options (timeout, etc.)
|
|
469
|
+
* @returns Result indicating success or error (no message content returned)
|
|
470
|
+
*
|
|
471
|
+
* @throws {Error} If the host rejects the message
|
|
472
|
+
*
|
|
473
|
+
* @example Send a text message from user interaction
|
|
474
|
+
* ```typescript
|
|
475
|
+
* try {
|
|
476
|
+
* await app.sendMessage({
|
|
477
|
+
* role: "user",
|
|
478
|
+
* content: [{ type: "text", text: "Show me details for item #42" }]
|
|
479
|
+
* });
|
|
480
|
+
* } catch (error) {
|
|
481
|
+
* console.error("Failed to send message:", error);
|
|
482
|
+
* // Handle error appropriately for your app
|
|
483
|
+
* }
|
|
484
|
+
* ```
|
|
485
|
+
*
|
|
486
|
+
* @see {@link McpUiMessageRequest} for request structure
|
|
487
|
+
*/
|
|
488
|
+
sendMessage(params: McpUiMessageRequest["params"], options?: RequestOptions): Promise<import("./types").McpUiMessageResult>;
|
|
489
|
+
/**
|
|
490
|
+
* Send log messages to the host for debugging and telemetry.
|
|
491
|
+
*
|
|
492
|
+
* Logs are not added to the conversation but may be recorded by the host
|
|
493
|
+
* for debugging purposes.
|
|
494
|
+
*
|
|
495
|
+
* @param params - Log level and message
|
|
496
|
+
*
|
|
497
|
+
* @example Log app state for debugging
|
|
498
|
+
* ```typescript
|
|
499
|
+
* app.sendLog({
|
|
500
|
+
* level: "info",
|
|
501
|
+
* data: "Weather data refreshed",
|
|
502
|
+
* logger: "WeatherApp"
|
|
503
|
+
* });
|
|
504
|
+
* ```
|
|
505
|
+
*
|
|
506
|
+
* @returns Promise that resolves when the log notification is sent
|
|
507
|
+
*/
|
|
508
|
+
sendLog(params: LoggingMessageNotification["params"]): Promise<void>;
|
|
509
|
+
/**
|
|
510
|
+
* Request the host to open an external URL in the default browser.
|
|
511
|
+
*
|
|
512
|
+
* The host may deny this request based on user preferences or security policy.
|
|
513
|
+
* Apps should handle rejection gracefully.
|
|
514
|
+
*
|
|
515
|
+
* @param params - URL to open
|
|
516
|
+
* @param options - Request options (timeout, etc.)
|
|
517
|
+
* @returns Result indicating success or error
|
|
518
|
+
*
|
|
519
|
+
* @throws {Error} If the host denies the request (e.g., blocked domain, user cancelled)
|
|
520
|
+
* @throws {Error} If the request times out or the connection is lost
|
|
521
|
+
*
|
|
522
|
+
* @example Open documentation link
|
|
523
|
+
* ```typescript
|
|
524
|
+
* try {
|
|
525
|
+
* await app.sendOpenLink({ url: "https://docs.example.com" });
|
|
526
|
+
* } catch (error) {
|
|
527
|
+
* console.error("Failed to open link:", error);
|
|
528
|
+
* // Optionally show fallback: display URL for manual copy
|
|
529
|
+
* }
|
|
530
|
+
* ```
|
|
531
|
+
*
|
|
532
|
+
* @see {@link McpUiOpenLinkRequest} for request structure
|
|
533
|
+
*/
|
|
534
|
+
sendOpenLink(params: McpUiOpenLinkRequest["params"], options?: RequestOptions): Promise<import("./types").McpUiOpenLinkResult>;
|
|
535
|
+
/**
|
|
536
|
+
* Notify the host of UI size changes.
|
|
537
|
+
*
|
|
538
|
+
* Apps can manually report size changes to help the host adjust the container.
|
|
539
|
+
* If `autoResize` is enabled (default), this is called automatically.
|
|
540
|
+
*
|
|
541
|
+
* @param params - New width and height in pixels
|
|
542
|
+
*
|
|
543
|
+
* @example Manually notify host of size change
|
|
544
|
+
* ```typescript
|
|
545
|
+
* app.sendSizeChanged({
|
|
546
|
+
* width: 400,
|
|
547
|
+
* height: 600
|
|
548
|
+
* });
|
|
549
|
+
* ```
|
|
550
|
+
*
|
|
551
|
+
* @returns Promise that resolves when the notification is sent
|
|
552
|
+
*
|
|
553
|
+
* @see {@link McpUiSizeChangedNotification} for notification structure
|
|
554
|
+
*/
|
|
555
|
+
sendSizeChanged(params: McpUiSizeChangedNotification["params"]): Promise<void>;
|
|
556
|
+
/**
|
|
557
|
+
* Set up automatic size change notifications using ResizeObserver.
|
|
558
|
+
*
|
|
559
|
+
* Observes both `document.documentElement` and `document.body` for size changes
|
|
560
|
+
* and automatically sends `ui/notifications/size-changed` notifications to the host.
|
|
561
|
+
* The notifications are debounced using requestAnimationFrame to avoid duplicates.
|
|
562
|
+
*
|
|
563
|
+
* Note: This method is automatically called by `connect()` if the `autoResize`
|
|
564
|
+
* option is true (default). You typically don't need to call this manually unless
|
|
565
|
+
* you disabled autoResize and want to enable it later.
|
|
566
|
+
*
|
|
567
|
+
* @returns Cleanup function to disconnect the observer
|
|
568
|
+
*
|
|
569
|
+
* @example Manual setup for custom scenarios
|
|
570
|
+
* ```typescript
|
|
571
|
+
* const app = new App(appInfo, capabilities, { autoResize: false });
|
|
572
|
+
* await app.connect(transport);
|
|
573
|
+
*
|
|
574
|
+
* // Later, enable auto-resize manually
|
|
575
|
+
* const cleanup = app.setupSizeChangedNotifications();
|
|
576
|
+
*
|
|
577
|
+
* // Clean up when done
|
|
578
|
+
* cleanup();
|
|
579
|
+
* ```
|
|
580
|
+
*/
|
|
581
|
+
setupSizeChangedNotifications(): () => void;
|
|
582
|
+
/**
|
|
583
|
+
* Establish connection with the host and perform initialization handshake.
|
|
584
|
+
*
|
|
585
|
+
* This method performs the following steps:
|
|
586
|
+
* 1. Connects the transport layer
|
|
587
|
+
* 2. Sends `ui/initialize` request with app info and capabilities
|
|
588
|
+
* 3. Receives host capabilities and context in response
|
|
589
|
+
* 4. Sends `ui/notifications/initialized` notification
|
|
590
|
+
* 5. Sets up auto-resize using {@link setupSizeChangedNotifications} if enabled (default)
|
|
591
|
+
*
|
|
592
|
+
* If initialization fails, the connection is automatically closed and an error
|
|
593
|
+
* is thrown.
|
|
594
|
+
*
|
|
595
|
+
* @param transport - Transport layer (typically PostMessageTransport)
|
|
596
|
+
* @param options - Request options for the initialize request
|
|
597
|
+
*
|
|
598
|
+
* @throws {Error} If initialization fails or connection is lost
|
|
599
|
+
*
|
|
600
|
+
* @example Connect with PostMessageTransport
|
|
601
|
+
* ```typescript
|
|
602
|
+
* const app = new App(
|
|
603
|
+
* { name: "MyApp", version: "1.0.0" },
|
|
604
|
+
* {}
|
|
605
|
+
* );
|
|
606
|
+
*
|
|
607
|
+
* try {
|
|
608
|
+
* await app.connect(new PostMessageTransport(window.parent));
|
|
609
|
+
* console.log("Connected successfully!");
|
|
610
|
+
* } catch (error) {
|
|
611
|
+
* console.error("Failed to connect:", error);
|
|
612
|
+
* }
|
|
613
|
+
* ```
|
|
614
|
+
*
|
|
615
|
+
* @see {@link McpUiInitializeRequest} for the initialization request structure
|
|
616
|
+
* @see {@link McpUiInitializedNotification} for the initialized notification
|
|
617
|
+
* @see {@link PostMessageTransport} for the typical transport implementation
|
|
618
|
+
*/
|
|
619
|
+
connect(transport: Transport, options?: RequestOptions): Promise<void>;
|
|
620
|
+
}
|