open-mcp-app 0.1.0 → 0.1.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.
- package/dist/core/index.d.ts +1171 -0
- package/dist/core/index.js +10910 -0
- package/dist/core/index.js.map +1 -0
- package/dist/react/index.d.ts +196 -0
- package/dist/react/index.js +10857 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +863 -0
- package/dist/server/index.js +15075 -0
- package/dist/server/index.js.map +1 -0
- package/dist/vite/index.d.ts +69 -0
- package/dist/vite/index.js +362 -0
- package/dist/vite/index.js.map +1 -0
- package/package.json +70 -16
- package/LICENSE +0 -22
- package/README.md +0 -36
- package/index.js +0 -363
|
@@ -0,0 +1,1171 @@
|
|
|
1
|
+
import { App } from '@modelcontextprotocol/ext-apps';
|
|
2
|
+
export { applyDocumentTheme, applyHostFonts, applyHostStyleVariables, getDocumentTheme } from '@modelcontextprotocol/ext-apps';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base Host Client Types
|
|
6
|
+
*
|
|
7
|
+
* These types define the spec-compliant interface for MCP Apps and ChatGPT Apps.
|
|
8
|
+
* Host-specific extensions are defined in their respective adapter modules.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Environment types for host detection.
|
|
12
|
+
*/
|
|
13
|
+
type Environment = "chatgpt" | "mcp-apps" | "standalone";
|
|
14
|
+
/**
|
|
15
|
+
* Display modes for UI resources.
|
|
16
|
+
* - "inline": Embedded within the conversation flow
|
|
17
|
+
* - "pip": Picture-in-picture floating panel
|
|
18
|
+
* - "fullscreen": Full-screen overlay
|
|
19
|
+
*/
|
|
20
|
+
type DisplayMode = "inline" | "pip" | "fullscreen";
|
|
21
|
+
/**
|
|
22
|
+
* Log severity levels matching MCP protocol LoggingLevel.
|
|
23
|
+
* These are displayed in the host's DevConsole with appropriate colors.
|
|
24
|
+
*/
|
|
25
|
+
type LogLevel = "debug" | "info" | "notice" | "warning" | "error";
|
|
26
|
+
/**
|
|
27
|
+
* Structured widget state format (ChatGPT-compatible).
|
|
28
|
+
* Allows separating model-visible content from private UI state.
|
|
29
|
+
*/
|
|
30
|
+
interface StructuredWidgetState {
|
|
31
|
+
/** Content visible to the AI model on follow-up turns */
|
|
32
|
+
modelContent?: string | Record<string, unknown> | null;
|
|
33
|
+
/** UI-only state, hidden from model */
|
|
34
|
+
privateContent?: Record<string, unknown> | null;
|
|
35
|
+
/** File IDs for images the model can see */
|
|
36
|
+
imageIds?: string[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Widget state can be structured (with modelContent/privateContent)
|
|
40
|
+
* or a simple key-value object.
|
|
41
|
+
*/
|
|
42
|
+
type WidgetState = StructuredWidgetState | Record<string, unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Tool call result structure.
|
|
45
|
+
*/
|
|
46
|
+
interface ToolResult<T = Record<string, unknown>> {
|
|
47
|
+
content?: Array<{
|
|
48
|
+
type: string;
|
|
49
|
+
text: string;
|
|
50
|
+
}>;
|
|
51
|
+
structuredContent?: T;
|
|
52
|
+
isError?: boolean;
|
|
53
|
+
source?: "agent" | "ui";
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Host context sent from MCP Apps host.
|
|
57
|
+
* Follows MCP Apps spec with extensions via [key: string]: unknown.
|
|
58
|
+
*/
|
|
59
|
+
interface HostContext {
|
|
60
|
+
theme?: "light" | "dark";
|
|
61
|
+
styles?: {
|
|
62
|
+
variables?: Record<string, string>;
|
|
63
|
+
};
|
|
64
|
+
displayMode?: DisplayMode;
|
|
65
|
+
availableDisplayModes?: DisplayMode[];
|
|
66
|
+
viewport?: {
|
|
67
|
+
width: number;
|
|
68
|
+
height: number;
|
|
69
|
+
};
|
|
70
|
+
platform?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Host application identifier per MCP Apps spec.
|
|
73
|
+
* Format: "<host>/<version>" (e.g. "creature/1.0.0", "chatgpt/2.0.0").
|
|
74
|
+
* Use this for spec-compliant host detection after ui/initialize.
|
|
75
|
+
*/
|
|
76
|
+
userAgent?: string;
|
|
77
|
+
/**
|
|
78
|
+
* Widget state restored from previous widget instance.
|
|
79
|
+
* Supported on both MCP Apps and ChatGPT.
|
|
80
|
+
*/
|
|
81
|
+
widgetState?: WidgetState;
|
|
82
|
+
/**
|
|
83
|
+
* Allow additional host-specific properties.
|
|
84
|
+
*/
|
|
85
|
+
[key: string]: unknown;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Configuration for creating a host client.
|
|
89
|
+
*/
|
|
90
|
+
interface HostClientConfig {
|
|
91
|
+
/** Name of the client (for protocol handshake) */
|
|
92
|
+
name: string;
|
|
93
|
+
/** Version of the client */
|
|
94
|
+
version: string;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* State managed by the host client.
|
|
98
|
+
*/
|
|
99
|
+
interface HostClientState {
|
|
100
|
+
/** Whether the host connection is ready */
|
|
101
|
+
isReady: boolean;
|
|
102
|
+
/** The detected environment */
|
|
103
|
+
environment: Environment;
|
|
104
|
+
/** Current widget state */
|
|
105
|
+
widgetState: WidgetState | null;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Base event handlers supported by all host clients.
|
|
109
|
+
* These are spec-compliant events that work across platforms.
|
|
110
|
+
*/
|
|
111
|
+
interface BaseHostClientEvents {
|
|
112
|
+
/** Called when tool input is received (before execution) */
|
|
113
|
+
"tool-input": (args: Record<string, unknown>) => void;
|
|
114
|
+
/** Called when tool result is received */
|
|
115
|
+
"tool-result": (result: ToolResult) => void;
|
|
116
|
+
/** Called when widget state changes (restored or updated) */
|
|
117
|
+
"widget-state-change": (widgetState: WidgetState | null) => void;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Extended event handlers for MCP Apps hosts.
|
|
121
|
+
* These are MCP-specific events not available on ChatGPT.
|
|
122
|
+
*/
|
|
123
|
+
interface McpAppsHostClientEvents extends BaseHostClientEvents {
|
|
124
|
+
/** Called when theme changes (MCP Apps only) */
|
|
125
|
+
"theme-change": (theme: "light" | "dark") => void;
|
|
126
|
+
/** Called when host requests teardown (MCP Apps only) */
|
|
127
|
+
teardown: () => Promise<void> | void;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Listener for state changes.
|
|
131
|
+
*/
|
|
132
|
+
type StateListener = (state: HostClientState, prevState: HostClientState) => void;
|
|
133
|
+
/**
|
|
134
|
+
* Base host client interface.
|
|
135
|
+
* Contains only spec-compliant methods that work across all platforms.
|
|
136
|
+
*/
|
|
137
|
+
interface BaseHostClient {
|
|
138
|
+
/** Get current state */
|
|
139
|
+
getState(): HostClientState;
|
|
140
|
+
/** Subscribe to state changes. Returns unsubscribe function. */
|
|
141
|
+
subscribe(listener: StateListener): () => void;
|
|
142
|
+
/** Call a tool on the MCP server */
|
|
143
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
144
|
+
/** Set widget state */
|
|
145
|
+
setWidgetState(state: WidgetState | null): void;
|
|
146
|
+
/**
|
|
147
|
+
* Request a display mode change from the host.
|
|
148
|
+
*
|
|
149
|
+
* The host may refuse or coerce the request (e.g., "pip" → "fullscreen" on mobile).
|
|
150
|
+
* Always check `availableDisplayModes` in host context before calling, and handle
|
|
151
|
+
* the returned mode differing from the requested mode.
|
|
152
|
+
*/
|
|
153
|
+
requestDisplayMode(params: {
|
|
154
|
+
mode: DisplayMode;
|
|
155
|
+
}): Promise<{
|
|
156
|
+
mode: DisplayMode;
|
|
157
|
+
}>;
|
|
158
|
+
/**
|
|
159
|
+
* Send a log message.
|
|
160
|
+
*
|
|
161
|
+
* On MCP Apps hosts, logs are sent via the MCP protocol's `notifications/message`
|
|
162
|
+
* and displayed in the host's unified log viewer alongside server logs.
|
|
163
|
+
* On ChatGPT, logs go to browser console only.
|
|
164
|
+
*/
|
|
165
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
166
|
+
/** Register an event handler. Returns unsubscribe function. */
|
|
167
|
+
on<K extends keyof BaseHostClientEvents>(event: K, handler: BaseHostClientEvents[K]): () => void;
|
|
168
|
+
/** Start listening for host messages */
|
|
169
|
+
connect(): void;
|
|
170
|
+
/** Stop listening for host messages */
|
|
171
|
+
disconnect(): void;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* WebSocket connection status.
|
|
175
|
+
*/
|
|
176
|
+
type WebSocketStatus = "disconnected" | "connecting" | "connected" | "error";
|
|
177
|
+
/**
|
|
178
|
+
* Configuration for creating a WebSocket client.
|
|
179
|
+
*/
|
|
180
|
+
interface WebSocketClientConfig<TReceive = unknown> {
|
|
181
|
+
/** Called when a message is received */
|
|
182
|
+
onMessage?: (message: TReceive) => void;
|
|
183
|
+
/** Called when connection status changes */
|
|
184
|
+
onStatusChange?: (status: WebSocketStatus, error?: string) => void;
|
|
185
|
+
/** Whether to auto-reconnect on disconnect (default: true) */
|
|
186
|
+
reconnect?: boolean;
|
|
187
|
+
/** Base interval for reconnection attempts in ms (default: 1000) */
|
|
188
|
+
reconnectInterval?: number;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* WebSocket client interface.
|
|
192
|
+
*/
|
|
193
|
+
interface WebSocketClient<TSend = unknown, TReceive = unknown> {
|
|
194
|
+
/** Current connection status */
|
|
195
|
+
readonly status: WebSocketStatus;
|
|
196
|
+
/** Error message if status is "error" */
|
|
197
|
+
readonly error: string | undefined;
|
|
198
|
+
/** Connect to the WebSocket server */
|
|
199
|
+
connect(): void;
|
|
200
|
+
/** Disconnect from the WebSocket server */
|
|
201
|
+
disconnect(): void;
|
|
202
|
+
/** Send a message to the server */
|
|
203
|
+
send(message: TSend): void;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Provider Adapter Types
|
|
208
|
+
*
|
|
209
|
+
* Defines the interfaces for host-specific adapters that extend
|
|
210
|
+
* the base host client with additional capabilities.
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* All events supported by the unified host client.
|
|
215
|
+
* Combines base events with MCP-specific events.
|
|
216
|
+
*/
|
|
217
|
+
interface UnifiedHostClientEvents extends BaseHostClientEvents, McpAppsHostClientEvents {
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Unified host client interface.
|
|
221
|
+
*
|
|
222
|
+
* This is the primary interface returned by `createHost()`.
|
|
223
|
+
* It combines the base interface with host-specific extensions,
|
|
224
|
+
* providing a consistent API across all platforms.
|
|
225
|
+
*/
|
|
226
|
+
interface UnifiedHostClient {
|
|
227
|
+
/** Get current state */
|
|
228
|
+
getState(): HostClientState;
|
|
229
|
+
/** Subscribe to state changes. Returns unsubscribe function. */
|
|
230
|
+
subscribe(listener: StateListener): () => void;
|
|
231
|
+
/** Call a tool on the MCP server */
|
|
232
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
233
|
+
/**
|
|
234
|
+
* Send a notification to the host.
|
|
235
|
+
*
|
|
236
|
+
* MCP Apps only - no-op on ChatGPT.
|
|
237
|
+
*/
|
|
238
|
+
sendNotification(method: string, params: unknown): void;
|
|
239
|
+
/** Set widget state */
|
|
240
|
+
setWidgetState(state: WidgetState | null): void;
|
|
241
|
+
/**
|
|
242
|
+
* Set the pip/widget title displayed in the host UI.
|
|
243
|
+
*
|
|
244
|
+
* Creature extension - no-op on ChatGPT and generic MCP Apps hosts.
|
|
245
|
+
*/
|
|
246
|
+
setTitle(title: string): void;
|
|
247
|
+
/**
|
|
248
|
+
* Request a display mode change from the host.
|
|
249
|
+
*/
|
|
250
|
+
requestDisplayMode(params: {
|
|
251
|
+
mode: DisplayMode;
|
|
252
|
+
}): Promise<{
|
|
253
|
+
mode: DisplayMode;
|
|
254
|
+
}>;
|
|
255
|
+
/**
|
|
256
|
+
* Send a log message.
|
|
257
|
+
*/
|
|
258
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
259
|
+
/** Register an event handler. Returns unsubscribe function. */
|
|
260
|
+
on<K extends keyof UnifiedHostClientEvents>(event: K, handler: UnifiedHostClientEvents[K]): () => void;
|
|
261
|
+
/** Start listening for host messages */
|
|
262
|
+
connect(): void;
|
|
263
|
+
/** Stop listening for host messages */
|
|
264
|
+
disconnect(): void;
|
|
265
|
+
/**
|
|
266
|
+
* The detected environment.
|
|
267
|
+
* Use this to conditionally enable host-specific features.
|
|
268
|
+
*/
|
|
269
|
+
readonly environment: Environment;
|
|
270
|
+
/**
|
|
271
|
+
* The adapter kind currently in use.
|
|
272
|
+
*/
|
|
273
|
+
readonly adapterKind: AdapterKind;
|
|
274
|
+
/**
|
|
275
|
+
* Whether this host is Creature (supports Creature-specific extensions).
|
|
276
|
+
* Checked via hostContext after connection.
|
|
277
|
+
*/
|
|
278
|
+
readonly isCreature: boolean;
|
|
279
|
+
/**
|
|
280
|
+
* Get the host context received from the host.
|
|
281
|
+
* Contains theme, styles, userAgent, and host-specific properties.
|
|
282
|
+
* Returns null before connection is established.
|
|
283
|
+
*/
|
|
284
|
+
getHostContext(): HostContext | null;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The kind of adapter in use.
|
|
288
|
+
*/
|
|
289
|
+
type AdapterKind = "mcp-apps" | "creature" | "chatgpt" | "standalone";
|
|
290
|
+
/**
|
|
291
|
+
* Base adapter interface.
|
|
292
|
+
* All adapters implement this interface.
|
|
293
|
+
*/
|
|
294
|
+
interface HostAdapter extends UnifiedHostClient {
|
|
295
|
+
/** The underlying base client */
|
|
296
|
+
readonly base: BaseHostClient;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Base class for subscribable host clients.
|
|
301
|
+
* Provides event emitter pattern for state changes and host events.
|
|
302
|
+
*/
|
|
303
|
+
declare abstract class Subscribable {
|
|
304
|
+
private stateListeners;
|
|
305
|
+
private eventHandlers;
|
|
306
|
+
subscribe(listener: StateListener): () => void;
|
|
307
|
+
on<K extends keyof BaseHostClientEvents>(event: K, handler: BaseHostClientEvents[K]): () => void;
|
|
308
|
+
protected notifyStateChange(state: HostClientState, prevState: HostClientState): void;
|
|
309
|
+
protected emit<K extends keyof BaseHostClientEvents>(event: K, ...args: Parameters<BaseHostClientEvents[K]>): void;
|
|
310
|
+
protected onSubscribe(): void;
|
|
311
|
+
protected onUnsubscribe(): void;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Extended subscribable for MCP Apps hosts that support additional events.
|
|
315
|
+
*/
|
|
316
|
+
declare abstract class McpAppsSubscribable extends Subscribable {
|
|
317
|
+
private mcpEventHandlers;
|
|
318
|
+
onMcpEvent<K extends "theme-change" | "teardown">(event: K, handler: K extends "theme-change" ? (theme: "light" | "dark") => void : () => Promise<void> | void): () => void;
|
|
319
|
+
protected emitMcpEvent<K extends "theme-change" | "teardown">(event: K, ...args: K extends "theme-change" ? ["light" | "dark"] : []): void | Promise<void>;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* MCP Apps base host client implementation.
|
|
324
|
+
*
|
|
325
|
+
* Wraps the official MCP Apps SDK `App` class to provide a spec-compliant interface.
|
|
326
|
+
* Handles the protocol handshake, tool calls, notifications, and automatic style/theme application.
|
|
327
|
+
*
|
|
328
|
+
* This is the base implementation - Creature-specific extensions are added via CreatureAdapter.
|
|
329
|
+
*/
|
|
330
|
+
declare class McpAppsBaseHostClient extends McpAppsSubscribable implements BaseHostClient {
|
|
331
|
+
private state;
|
|
332
|
+
protected config: HostClientConfig;
|
|
333
|
+
protected app: App | null;
|
|
334
|
+
private connected;
|
|
335
|
+
private hostContext;
|
|
336
|
+
constructor(config: HostClientConfig);
|
|
337
|
+
/**
|
|
338
|
+
* Get the current client state.
|
|
339
|
+
*/
|
|
340
|
+
getState(): HostClientState;
|
|
341
|
+
/**
|
|
342
|
+
* Get the host context received during initialization.
|
|
343
|
+
*/
|
|
344
|
+
getHostContext(): HostContext | null;
|
|
345
|
+
/**
|
|
346
|
+
* Connect to the MCP Apps host.
|
|
347
|
+
*
|
|
348
|
+
* Creates the App instance, registers notification handlers, and initiates
|
|
349
|
+
* the protocol handshake. The host will receive `ui/initialize` and respond
|
|
350
|
+
* with host context including theme, styles, and widgetState.
|
|
351
|
+
*/
|
|
352
|
+
connect(): void;
|
|
353
|
+
/**
|
|
354
|
+
* Disconnect from the host.
|
|
355
|
+
*
|
|
356
|
+
* Cleans up the App instance and resets state.
|
|
357
|
+
*/
|
|
358
|
+
disconnect(): void;
|
|
359
|
+
/**
|
|
360
|
+
* Call a tool on the MCP server via the host.
|
|
361
|
+
*
|
|
362
|
+
* Uses the SDK's callServerTool method which properly routes through
|
|
363
|
+
* the host to the MCP server.
|
|
364
|
+
*/
|
|
365
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
366
|
+
/**
|
|
367
|
+
* Send a notification to the host.
|
|
368
|
+
*
|
|
369
|
+
* Sends a notification via the ext-apps SDK transport.
|
|
370
|
+
*/
|
|
371
|
+
sendNotification(method: string, params: unknown): void;
|
|
372
|
+
/**
|
|
373
|
+
* Set widget state and notify the host.
|
|
374
|
+
*
|
|
375
|
+
* Widget state is synchronized with the host for persistence across
|
|
376
|
+
* sessions and visibility to the AI model.
|
|
377
|
+
*/
|
|
378
|
+
setWidgetState(state: WidgetState | null): void;
|
|
379
|
+
/**
|
|
380
|
+
* Request a display mode change from the host.
|
|
381
|
+
*/
|
|
382
|
+
requestDisplayMode(params: {
|
|
383
|
+
mode: DisplayMode;
|
|
384
|
+
}): Promise<{
|
|
385
|
+
mode: DisplayMode;
|
|
386
|
+
}>;
|
|
387
|
+
/**
|
|
388
|
+
* Send a log message to the host's DevConsole.
|
|
389
|
+
*
|
|
390
|
+
* Uses the MCP protocol's `notifications/message` notification to send logs
|
|
391
|
+
* to the host. Logs appear in the unified DevConsole alongside server logs,
|
|
392
|
+
* with appropriate color coding based on level.
|
|
393
|
+
*/
|
|
394
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
395
|
+
/**
|
|
396
|
+
* Get the underlying App instance for advanced use cases.
|
|
397
|
+
*/
|
|
398
|
+
getApp(): App | null;
|
|
399
|
+
/**
|
|
400
|
+
* Update internal state and notify listeners.
|
|
401
|
+
*/
|
|
402
|
+
protected setState(partial: Partial<HostClientState>): void;
|
|
403
|
+
/**
|
|
404
|
+
* Set up notification handlers on the App instance.
|
|
405
|
+
*
|
|
406
|
+
* Maps the official SDK's callback pattern to our event emitter pattern,
|
|
407
|
+
* allowing consumers to use `.on("tool-result", ...)` etc.
|
|
408
|
+
*/
|
|
409
|
+
private setupHandlers;
|
|
410
|
+
/**
|
|
411
|
+
* Initiate connection using PostMessageTransport.
|
|
412
|
+
*
|
|
413
|
+
* The SDK's App.connect() handles the protocol handshake correctly:
|
|
414
|
+
* the guest (App) sends `ui/initialize` to the host.
|
|
415
|
+
*/
|
|
416
|
+
private initiateConnection;
|
|
417
|
+
/**
|
|
418
|
+
* Apply theme, styles, and fonts from host context.
|
|
419
|
+
* Subclasses can override to add additional context handling.
|
|
420
|
+
*/
|
|
421
|
+
protected applyHostContext(context: {
|
|
422
|
+
theme?: unknown;
|
|
423
|
+
styles?: {
|
|
424
|
+
variables?: unknown;
|
|
425
|
+
css?: {
|
|
426
|
+
fonts?: string;
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
}): void;
|
|
430
|
+
/**
|
|
431
|
+
* Extract text content from SDK result content array.
|
|
432
|
+
* Filters to only include text items since our ToolResult type expects text.
|
|
433
|
+
*/
|
|
434
|
+
private extractTextContent;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* OpenAI bridge interface exposed by ChatGPT Apps SDK.
|
|
439
|
+
* Available on `window.openai` when running inside ChatGPT.
|
|
440
|
+
*/
|
|
441
|
+
interface OpenAIBridge {
|
|
442
|
+
toolOutput?: Record<string, unknown>;
|
|
443
|
+
widgetState?: WidgetState;
|
|
444
|
+
setWidgetState?: (state: WidgetState) => void;
|
|
445
|
+
callTool?: (name: string, args: Record<string, unknown>) => Promise<{
|
|
446
|
+
structuredContent?: Record<string, unknown>;
|
|
447
|
+
content?: Array<{
|
|
448
|
+
type: string;
|
|
449
|
+
text?: string;
|
|
450
|
+
}>;
|
|
451
|
+
}>;
|
|
452
|
+
requestDisplayMode?: (args: {
|
|
453
|
+
mode: string;
|
|
454
|
+
}) => Promise<{
|
|
455
|
+
mode: string;
|
|
456
|
+
}>;
|
|
457
|
+
}
|
|
458
|
+
declare global {
|
|
459
|
+
interface Window {
|
|
460
|
+
openai?: OpenAIBridge;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* ChatGPT Apps base host client implementation.
|
|
465
|
+
*
|
|
466
|
+
* Bridges the ChatGPT Apps SDK (`window.openai`) to provide a spec-compliant interface.
|
|
467
|
+
* Handles initial data processing, globals updates, and widget state synchronization.
|
|
468
|
+
*/
|
|
469
|
+
declare class ChatGptBaseHostClient extends Subscribable implements BaseHostClient {
|
|
470
|
+
private state;
|
|
471
|
+
protected config: HostClientConfig;
|
|
472
|
+
private connected;
|
|
473
|
+
private hasProcessedInitialData;
|
|
474
|
+
private globalsHandler;
|
|
475
|
+
private lastToolOutputJson;
|
|
476
|
+
private lastWidgetStateJson;
|
|
477
|
+
constructor(config: HostClientConfig);
|
|
478
|
+
/**
|
|
479
|
+
* Get the current client state.
|
|
480
|
+
*/
|
|
481
|
+
getState(): HostClientState;
|
|
482
|
+
/**
|
|
483
|
+
* Connect to the ChatGPT host.
|
|
484
|
+
*
|
|
485
|
+
* Processes initial data from `window.openai` and sets up a listener
|
|
486
|
+
* for subsequent `openai:set_globals` events.
|
|
487
|
+
*/
|
|
488
|
+
connect(): void;
|
|
489
|
+
/**
|
|
490
|
+
* Disconnect from the host.
|
|
491
|
+
*
|
|
492
|
+
* Removes the globals event listener and cleanup.
|
|
493
|
+
*/
|
|
494
|
+
disconnect(): void;
|
|
495
|
+
/**
|
|
496
|
+
* Call a tool on the MCP server via the ChatGPT bridge.
|
|
497
|
+
*/
|
|
498
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
499
|
+
/**
|
|
500
|
+
* Set widget state and sync with the ChatGPT host.
|
|
501
|
+
*/
|
|
502
|
+
setWidgetState(state: WidgetState | null): void;
|
|
503
|
+
/**
|
|
504
|
+
* Log a message to the console.
|
|
505
|
+
*
|
|
506
|
+
* ChatGPT doesn't have a DevConsole, so logs go to browser console only.
|
|
507
|
+
*/
|
|
508
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
509
|
+
/**
|
|
510
|
+
* Request a display mode change from the ChatGPT host.
|
|
511
|
+
*/
|
|
512
|
+
requestDisplayMode(params: {
|
|
513
|
+
mode: DisplayMode;
|
|
514
|
+
}): Promise<{
|
|
515
|
+
mode: DisplayMode;
|
|
516
|
+
}>;
|
|
517
|
+
/**
|
|
518
|
+
* Update internal state and notify listeners.
|
|
519
|
+
*/
|
|
520
|
+
protected setState(partial: Partial<HostClientState>): void;
|
|
521
|
+
/**
|
|
522
|
+
* Process initial data from `window.openai`.
|
|
523
|
+
*
|
|
524
|
+
* Called once on connect to handle any tool output or widget state
|
|
525
|
+
* that was set before the client connected.
|
|
526
|
+
*/
|
|
527
|
+
private processInitialData;
|
|
528
|
+
/**
|
|
529
|
+
* Set up listener for `openai:set_globals` events.
|
|
530
|
+
*
|
|
531
|
+
* ChatGPT dispatches this event when tool output or widget state
|
|
532
|
+
* changes after initial load. Uses JSON comparison to prevent
|
|
533
|
+
* infinite loops from unchanged data.
|
|
534
|
+
*/
|
|
535
|
+
private setupGlobalsListener;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Standalone base host client implementation.
|
|
540
|
+
*
|
|
541
|
+
* A minimal fallback for when running outside any host environment.
|
|
542
|
+
* Useful for development and testing scenarios.
|
|
543
|
+
*/
|
|
544
|
+
declare class StandaloneBaseHostClient extends Subscribable implements BaseHostClient {
|
|
545
|
+
private state;
|
|
546
|
+
protected config: HostClientConfig;
|
|
547
|
+
private connected;
|
|
548
|
+
constructor(config: HostClientConfig);
|
|
549
|
+
/**
|
|
550
|
+
* Get the current client state.
|
|
551
|
+
*/
|
|
552
|
+
getState(): HostClientState;
|
|
553
|
+
/**
|
|
554
|
+
* Connect in standalone mode.
|
|
555
|
+
*
|
|
556
|
+
* Simply marks the client as ready since there's no host to connect to.
|
|
557
|
+
*/
|
|
558
|
+
connect(): void;
|
|
559
|
+
/**
|
|
560
|
+
* Disconnect in standalone mode.
|
|
561
|
+
*/
|
|
562
|
+
disconnect(): void;
|
|
563
|
+
/**
|
|
564
|
+
* Call a tool - not available in standalone mode.
|
|
565
|
+
*/
|
|
566
|
+
callTool<T = Record<string, unknown>>(toolName: string, _args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
567
|
+
/**
|
|
568
|
+
* Set widget state locally (no host to sync with).
|
|
569
|
+
*/
|
|
570
|
+
setWidgetState(state: WidgetState | null): void;
|
|
571
|
+
/**
|
|
572
|
+
* Log a message to the console.
|
|
573
|
+
*/
|
|
574
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
575
|
+
/**
|
|
576
|
+
* Request a display mode - returns the requested mode since there's no host to negotiate with.
|
|
577
|
+
*/
|
|
578
|
+
requestDisplayMode(params: {
|
|
579
|
+
mode: DisplayMode;
|
|
580
|
+
}): Promise<{
|
|
581
|
+
mode: DisplayMode;
|
|
582
|
+
}>;
|
|
583
|
+
/**
|
|
584
|
+
* Update internal state and notify listeners.
|
|
585
|
+
*/
|
|
586
|
+
protected setState(partial: Partial<HostClientState>): void;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Host Identity Utilities
|
|
591
|
+
*
|
|
592
|
+
* Helpers for parsing and working with the `hostContext.userAgent` field.
|
|
593
|
+
* Per MCP Apps spec, userAgent identifies the host in format "<host>/<version>".
|
|
594
|
+
*/
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Parsed host identity from userAgent string.
|
|
598
|
+
*/
|
|
599
|
+
interface HostIdentity {
|
|
600
|
+
/** Host name (e.g. "creature", "chatgpt") */
|
|
601
|
+
host: string;
|
|
602
|
+
/** Host version if provided (e.g. "1.0.0") */
|
|
603
|
+
version?: string;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Parse a userAgent string into host and version components.
|
|
607
|
+
*
|
|
608
|
+
* Supports formats:
|
|
609
|
+
* - "<host>/<version>" (e.g. "creature/1.0.0")
|
|
610
|
+
* - "<host>" (e.g. "creature" - version will be undefined)
|
|
611
|
+
*
|
|
612
|
+
* @param userAgent - The userAgent string to parse
|
|
613
|
+
* @returns Parsed host identity
|
|
614
|
+
*/
|
|
615
|
+
declare function parseHostUserAgent(userAgent: string): HostIdentity;
|
|
616
|
+
/**
|
|
617
|
+
* Get host identity from a HostContext object.
|
|
618
|
+
*
|
|
619
|
+
* Convenience wrapper around parseHostUserAgent that handles null/undefined.
|
|
620
|
+
*
|
|
621
|
+
* @param context - The host context (may be null)
|
|
622
|
+
* @returns Parsed host identity, or empty object if userAgent not available
|
|
623
|
+
*/
|
|
624
|
+
declare function getHostIdentity(context: HostContext | null): Partial<HostIdentity>;
|
|
625
|
+
/**
|
|
626
|
+
* Check if the host context indicates a specific host.
|
|
627
|
+
*
|
|
628
|
+
* @param context - The host context to check
|
|
629
|
+
* @param hostName - The host name to match (case-insensitive)
|
|
630
|
+
* @returns true if the context indicates the specified host
|
|
631
|
+
*/
|
|
632
|
+
declare function isHost(context: HostContext | null, hostName: string): boolean;
|
|
633
|
+
/**
|
|
634
|
+
* Known host identifiers.
|
|
635
|
+
* Apps can use these constants for reliable host detection.
|
|
636
|
+
*/
|
|
637
|
+
declare const KNOWN_HOSTS: {
|
|
638
|
+
readonly CREATURE: "creature";
|
|
639
|
+
readonly CHATGPT: "chatgpt";
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* MCP Apps Adapter (Base)
|
|
644
|
+
*
|
|
645
|
+
* Base adapter for all MCP Apps hosts. This is the foundation that other
|
|
646
|
+
* host-specific adapters (like CreatureAdapter) extend.
|
|
647
|
+
*
|
|
648
|
+
* Works with any MCP Apps-compliant host out of the box.
|
|
649
|
+
*/
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* MCP Apps adapter implementation.
|
|
653
|
+
*
|
|
654
|
+
* This is the base adapter for all MCP Apps hosts. It provides the standard
|
|
655
|
+
* MCP Apps functionality that works across all compliant hosts.
|
|
656
|
+
*
|
|
657
|
+
* Host-specific adapters (like CreatureAdapter) should extend this class
|
|
658
|
+
* to add their own extensions while maintaining compatibility.
|
|
659
|
+
*/
|
|
660
|
+
declare class McpAppsAdapter implements HostAdapter {
|
|
661
|
+
readonly base: McpAppsBaseHostClient;
|
|
662
|
+
readonly adapterKind: AdapterKind;
|
|
663
|
+
constructor(config: HostClientConfig);
|
|
664
|
+
/**
|
|
665
|
+
* Factory method for creating the base client.
|
|
666
|
+
* Subclasses can override this to use a custom base client.
|
|
667
|
+
*/
|
|
668
|
+
protected createBaseClient(config: HostClientConfig): McpAppsBaseHostClient;
|
|
669
|
+
/**
|
|
670
|
+
* Create an MCP Apps adapter instance.
|
|
671
|
+
*/
|
|
672
|
+
static create(config: HostClientConfig): McpAppsAdapter;
|
|
673
|
+
/**
|
|
674
|
+
* Check if the current environment is MCP Apps (iframe with parent).
|
|
675
|
+
*/
|
|
676
|
+
static detect(): boolean;
|
|
677
|
+
get environment(): Environment;
|
|
678
|
+
/**
|
|
679
|
+
* Whether this is a Creature host.
|
|
680
|
+
* Base MCP Apps adapter returns false - CreatureAdapter overrides this.
|
|
681
|
+
*/
|
|
682
|
+
get isCreature(): boolean;
|
|
683
|
+
/**
|
|
684
|
+
* Get the host context received from the host.
|
|
685
|
+
* Useful for checking host-specific capabilities.
|
|
686
|
+
*/
|
|
687
|
+
getHostContext(): HostContext | null;
|
|
688
|
+
getState(): HostClientState;
|
|
689
|
+
subscribe(listener: StateListener): () => void;
|
|
690
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
691
|
+
sendNotification(method: string, params: unknown): void;
|
|
692
|
+
setWidgetState(state: WidgetState | null): void;
|
|
693
|
+
/**
|
|
694
|
+
* Set pip/widget title.
|
|
695
|
+
* Sends a notification - hosts that support it will update the title.
|
|
696
|
+
*/
|
|
697
|
+
setTitle(title: string): void;
|
|
698
|
+
requestDisplayMode(params: {
|
|
699
|
+
mode: DisplayMode;
|
|
700
|
+
}): Promise<{
|
|
701
|
+
mode: DisplayMode;
|
|
702
|
+
}>;
|
|
703
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
704
|
+
on<K extends keyof UnifiedHostClientEvents>(event: K, handler: UnifiedHostClientEvents[K]): () => void;
|
|
705
|
+
connect(): void;
|
|
706
|
+
disconnect(): void;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Upgrading MCP Apps Client
|
|
711
|
+
*
|
|
712
|
+
* A delegating wrapper for MCP Apps environments that determines the
|
|
713
|
+
* correct adapter type after connection based on hostContext.userAgent.
|
|
714
|
+
*
|
|
715
|
+
* This implements the spec-compliant host identification flow:
|
|
716
|
+
* 1. Start as a generic MCP Apps client
|
|
717
|
+
* 2. Connect and receive hostContext via ui/initialize
|
|
718
|
+
* 3. Check hostContext.userAgent to determine if it's Creature
|
|
719
|
+
* 4. Update adapterKind/isCreature accordingly
|
|
720
|
+
*/
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Extended MCP Apps base client that handles Creature-specific hostContext.
|
|
724
|
+
* Used internally by UpgradingMcpAppsClient.
|
|
725
|
+
*/
|
|
726
|
+
declare class CreatureCapableBaseHostClient extends McpAppsBaseHostClient {
|
|
727
|
+
private creatureStyles;
|
|
728
|
+
/**
|
|
729
|
+
* Get Creature-specific styles from host context.
|
|
730
|
+
*/
|
|
731
|
+
getCreatureStyles(): Record<string, string | undefined> | null;
|
|
732
|
+
/**
|
|
733
|
+
* Check if connected to a Creature host.
|
|
734
|
+
* Uses hostContext.userAgent (spec-compliant).
|
|
735
|
+
*/
|
|
736
|
+
isCreatureHost(): boolean;
|
|
737
|
+
/**
|
|
738
|
+
* Override to also apply Creature-specific styles.
|
|
739
|
+
*/
|
|
740
|
+
protected applyHostContext(context: {
|
|
741
|
+
theme?: unknown;
|
|
742
|
+
styles?: {
|
|
743
|
+
variables?: unknown;
|
|
744
|
+
css?: {
|
|
745
|
+
fonts?: string;
|
|
746
|
+
};
|
|
747
|
+
};
|
|
748
|
+
creatureStyles?: Record<string, string | undefined>;
|
|
749
|
+
}): void;
|
|
750
|
+
/**
|
|
751
|
+
* Apply Creature-specific CSS variables to the document root.
|
|
752
|
+
*/
|
|
753
|
+
private applyCreatureStyles;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Upgrading MCP Apps client that determines the correct adapter type
|
|
757
|
+
* after connection based on hostContext.userAgent.
|
|
758
|
+
*
|
|
759
|
+
* Before connection: adapterKind = "mcp-apps", isCreature = false
|
|
760
|
+
* After connection: adapterKind and isCreature reflect the actual host
|
|
761
|
+
*/
|
|
762
|
+
declare class UpgradingMcpAppsClient implements HostAdapter {
|
|
763
|
+
readonly base: CreatureCapableBaseHostClient;
|
|
764
|
+
constructor(config: HostClientConfig);
|
|
765
|
+
static create(config: HostClientConfig): UpgradingMcpAppsClient;
|
|
766
|
+
static detect(): boolean;
|
|
767
|
+
/**
|
|
768
|
+
* The adapter kind - determined after connection based on hostContext.userAgent.
|
|
769
|
+
* Returns "creature" if running in Creature, "mcp-apps" otherwise.
|
|
770
|
+
*/
|
|
771
|
+
get adapterKind(): AdapterKind;
|
|
772
|
+
/**
|
|
773
|
+
* Whether this host is Creature.
|
|
774
|
+
* Determined via hostContext.userAgent after connection.
|
|
775
|
+
*/
|
|
776
|
+
get isCreature(): boolean;
|
|
777
|
+
get environment(): Environment;
|
|
778
|
+
/**
|
|
779
|
+
* Get Creature-specific styles if available.
|
|
780
|
+
* Returns null when not running in Creature.
|
|
781
|
+
*/
|
|
782
|
+
getCreatureStyles(): Record<string, string | undefined> | null;
|
|
783
|
+
getHostContext(): HostContext | null;
|
|
784
|
+
getState(): HostClientState;
|
|
785
|
+
subscribe(listener: StateListener): () => void;
|
|
786
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
787
|
+
sendNotification(method: string, params: unknown): void;
|
|
788
|
+
setWidgetState(state: WidgetState | null): void;
|
|
789
|
+
/**
|
|
790
|
+
* Set pip/widget title.
|
|
791
|
+
* Sends a notification - hosts that support it will update the title.
|
|
792
|
+
*/
|
|
793
|
+
setTitle(title: string): void;
|
|
794
|
+
requestDisplayMode(params: {
|
|
795
|
+
mode: DisplayMode;
|
|
796
|
+
}): Promise<{
|
|
797
|
+
mode: DisplayMode;
|
|
798
|
+
}>;
|
|
799
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
800
|
+
on<K extends keyof UnifiedHostClientEvents>(event: K, handler: UnifiedHostClientEvents[K]): () => void;
|
|
801
|
+
connect(): void;
|
|
802
|
+
disconnect(): void;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* Creature Adapter
|
|
807
|
+
*
|
|
808
|
+
* Extends McpAppsAdapter with Creature-specific features:
|
|
809
|
+
* - isCreature detection via hostContext.userAgent (spec-compliant)
|
|
810
|
+
* - creatureStyles CSS variables from hostContext
|
|
811
|
+
* - Creature-specific token authentication (server-side)
|
|
812
|
+
* - Enhanced logging to DevConsole
|
|
813
|
+
*/
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* Extended MCP Apps base client that handles Creature-specific hostContext.
|
|
817
|
+
*/
|
|
818
|
+
declare class CreatureBaseHostClient extends McpAppsBaseHostClient {
|
|
819
|
+
private creatureStyles;
|
|
820
|
+
/**
|
|
821
|
+
* Get Creature-specific styles from host context.
|
|
822
|
+
*/
|
|
823
|
+
getCreatureStyles(): Record<string, string | undefined> | null;
|
|
824
|
+
/**
|
|
825
|
+
* Check if connected to a Creature host.
|
|
826
|
+
*
|
|
827
|
+
* Detection is done via hostContext.userAgent (spec-compliant approach).
|
|
828
|
+
*/
|
|
829
|
+
isCreatureHost(): boolean;
|
|
830
|
+
/**
|
|
831
|
+
* Override to also apply Creature-specific styles.
|
|
832
|
+
*/
|
|
833
|
+
protected applyHostContext(context: {
|
|
834
|
+
theme?: unknown;
|
|
835
|
+
styles?: {
|
|
836
|
+
variables?: unknown;
|
|
837
|
+
css?: {
|
|
838
|
+
fonts?: string;
|
|
839
|
+
};
|
|
840
|
+
};
|
|
841
|
+
creatureStyles?: Record<string, string | undefined>;
|
|
842
|
+
}): void;
|
|
843
|
+
/**
|
|
844
|
+
* Apply Creature-specific CSS variables to the document root.
|
|
845
|
+
*/
|
|
846
|
+
private applyCreatureStyles;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Creature adapter implementation.
|
|
850
|
+
*
|
|
851
|
+
* Extends McpAppsAdapter with Creature-specific functionality:
|
|
852
|
+
* - isCreature property that checks hostContext.userAgent
|
|
853
|
+
* - getCreatureStyles() for accessing Creature CSS variables
|
|
854
|
+
* - Full DevConsole logging
|
|
855
|
+
*
|
|
856
|
+
* This adapter works with generic MCP Apps hosts -
|
|
857
|
+
* Creature-specific features simply return null/false when not in Creature.
|
|
858
|
+
*/
|
|
859
|
+
declare class CreatureAdapter extends McpAppsAdapter {
|
|
860
|
+
readonly adapterKind: AdapterKind;
|
|
861
|
+
readonly base: CreatureBaseHostClient;
|
|
862
|
+
constructor(config: HostClientConfig);
|
|
863
|
+
/**
|
|
864
|
+
* Override to create the Creature-specific base client.
|
|
865
|
+
*/
|
|
866
|
+
protected createBaseClient(config: HostClientConfig): CreatureBaseHostClient;
|
|
867
|
+
/**
|
|
868
|
+
* Create a Creature adapter instance.
|
|
869
|
+
*/
|
|
870
|
+
static create(config: HostClientConfig): CreatureAdapter;
|
|
871
|
+
/**
|
|
872
|
+
* Whether this host is Creature.
|
|
873
|
+
* Checked via hostContext.userAgent after connection.
|
|
874
|
+
*/
|
|
875
|
+
get isCreature(): boolean;
|
|
876
|
+
/**
|
|
877
|
+
* Get Creature-specific styles if available.
|
|
878
|
+
* Returns null when not running in Creature.
|
|
879
|
+
*/
|
|
880
|
+
getCreatureStyles(): Record<string, string | undefined> | null;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* ChatGPT Adapter
|
|
885
|
+
*
|
|
886
|
+
* Wraps the ChatGPT base host client with the unified interface.
|
|
887
|
+
* Provides ChatGPT-specific features via the window.openai bridge.
|
|
888
|
+
*/
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* ChatGPT adapter implementation.
|
|
892
|
+
*
|
|
893
|
+
* Wraps ChatGptBaseHostClient and exposes the unified interface.
|
|
894
|
+
* MCP-specific features (like notifications and teardown) are no-ops.
|
|
895
|
+
*/
|
|
896
|
+
declare class ChatGptAdapter implements HostAdapter {
|
|
897
|
+
readonly base: ChatGptBaseHostClient;
|
|
898
|
+
readonly adapterKind: AdapterKind;
|
|
899
|
+
constructor(config: HostClientConfig);
|
|
900
|
+
/**
|
|
901
|
+
* Create a ChatGPT adapter instance.
|
|
902
|
+
*/
|
|
903
|
+
static create(config: HostClientConfig): ChatGptAdapter;
|
|
904
|
+
/**
|
|
905
|
+
* Check if the current environment is ChatGPT.
|
|
906
|
+
*/
|
|
907
|
+
static detect(): boolean;
|
|
908
|
+
get environment(): Environment;
|
|
909
|
+
get isCreature(): boolean;
|
|
910
|
+
/**
|
|
911
|
+
* Get host context - returns null for ChatGPT as it doesn't use MCP Apps protocol.
|
|
912
|
+
*/
|
|
913
|
+
getHostContext(): HostContext | null;
|
|
914
|
+
getState(): HostClientState;
|
|
915
|
+
subscribe(listener: StateListener): () => void;
|
|
916
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
917
|
+
/**
|
|
918
|
+
* Send notification - not supported on ChatGPT.
|
|
919
|
+
*/
|
|
920
|
+
sendNotification(_method: string, _params: unknown): void;
|
|
921
|
+
setWidgetState(state: WidgetState | null): void;
|
|
922
|
+
/**
|
|
923
|
+
* Set pip/widget title - not supported on ChatGPT.
|
|
924
|
+
*/
|
|
925
|
+
setTitle(_title: string): void;
|
|
926
|
+
requestDisplayMode(params: {
|
|
927
|
+
mode: DisplayMode;
|
|
928
|
+
}): Promise<{
|
|
929
|
+
mode: DisplayMode;
|
|
930
|
+
}>;
|
|
931
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
932
|
+
on<K extends keyof UnifiedHostClientEvents>(event: K, handler: UnifiedHostClientEvents[K]): () => void;
|
|
933
|
+
connect(): void;
|
|
934
|
+
disconnect(): void;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Standalone Adapter
|
|
939
|
+
*
|
|
940
|
+
* Wraps the Standalone base host client with the unified interface.
|
|
941
|
+
* Used when running outside any host environment (development/testing).
|
|
942
|
+
*/
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Standalone adapter implementation.
|
|
946
|
+
*
|
|
947
|
+
* A minimal fallback for development and testing scenarios.
|
|
948
|
+
* Host-specific features are no-ops or console-logged.
|
|
949
|
+
*/
|
|
950
|
+
declare class StandaloneAdapter implements HostAdapter {
|
|
951
|
+
readonly base: StandaloneBaseHostClient;
|
|
952
|
+
readonly adapterKind: AdapterKind;
|
|
953
|
+
constructor(config: HostClientConfig);
|
|
954
|
+
/**
|
|
955
|
+
* Create a Standalone adapter instance.
|
|
956
|
+
*/
|
|
957
|
+
static create(config: HostClientConfig): StandaloneAdapter;
|
|
958
|
+
/**
|
|
959
|
+
* Check if the current environment is standalone.
|
|
960
|
+
* Returns true when not in ChatGPT or MCP Apps.
|
|
961
|
+
*/
|
|
962
|
+
static detect(): boolean;
|
|
963
|
+
get environment(): Environment;
|
|
964
|
+
get isCreature(): boolean;
|
|
965
|
+
/**
|
|
966
|
+
* Get host context - returns null for standalone mode.
|
|
967
|
+
*/
|
|
968
|
+
getHostContext(): HostContext | null;
|
|
969
|
+
getState(): HostClientState;
|
|
970
|
+
subscribe(listener: StateListener): () => void;
|
|
971
|
+
callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
|
|
972
|
+
/**
|
|
973
|
+
* Send notification - logged in standalone mode.
|
|
974
|
+
*/
|
|
975
|
+
sendNotification(method: string, params: unknown): void;
|
|
976
|
+
setWidgetState(state: WidgetState | null): void;
|
|
977
|
+
/**
|
|
978
|
+
* Set pip/widget title - logged in standalone mode.
|
|
979
|
+
*/
|
|
980
|
+
setTitle(title: string): void;
|
|
981
|
+
requestDisplayMode(params: {
|
|
982
|
+
mode: DisplayMode;
|
|
983
|
+
}): Promise<{
|
|
984
|
+
mode: DisplayMode;
|
|
985
|
+
}>;
|
|
986
|
+
log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
|
|
987
|
+
on<K extends keyof UnifiedHostClientEvents>(event: K, handler: UnifiedHostClientEvents[K]): () => void;
|
|
988
|
+
connect(): void;
|
|
989
|
+
disconnect(): void;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Environment-Aware Default Styles
|
|
994
|
+
*
|
|
995
|
+
* This module provides default CSS variable values for MCP Apps and ChatGPT Apps.
|
|
996
|
+
* Import `initStyles` early in your app entry point to prevent flash of unstyled content.
|
|
997
|
+
*
|
|
998
|
+
* The MCP Apps spec defines CSS variable names (e.g., --color-background-primary).
|
|
999
|
+
* This module provides environment-specific VALUES for those variables as sensible
|
|
1000
|
+
* defaults until the host injects its own values.
|
|
1001
|
+
*
|
|
1002
|
+
* Flow:
|
|
1003
|
+
* 1. Detect environment (ChatGPT, MCP Apps, or Standalone)
|
|
1004
|
+
* 2. Detect theme (light or dark)
|
|
1005
|
+
* 3. Inject defaults immediately on document.documentElement
|
|
1006
|
+
* 4. When host connects, host variables override these defaults
|
|
1007
|
+
*
|
|
1008
|
+
* IMPORTANT: MCP Apps defaults match Creature's actual values exactly.
|
|
1009
|
+
* The observer only runs for ChatGPT (MCP Apps hosts handle theme changes themselves).
|
|
1010
|
+
*/
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* Theme mode for styling.
|
|
1014
|
+
*/
|
|
1015
|
+
type Theme = "light" | "dark";
|
|
1016
|
+
/**
|
|
1017
|
+
* Options for initializing styles.
|
|
1018
|
+
*/
|
|
1019
|
+
interface InitStylesOptions {
|
|
1020
|
+
/** Whether to set up a MutationObserver for dynamic theme changes. Default: true for ChatGPT only */
|
|
1021
|
+
observeThemeChanges?: boolean;
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* MCP Apps / Creature default values - DARK mode.
|
|
1025
|
+
* These values are extracted directly from Creature's globals.css.
|
|
1026
|
+
* They MUST match exactly what Creature sends via hostContext.styles.variables.
|
|
1027
|
+
*/
|
|
1028
|
+
declare const MCP_APPS_DARK_DEFAULTS: Record<string, string>;
|
|
1029
|
+
/**
|
|
1030
|
+
* MCP Apps / Creature default values - LIGHT mode.
|
|
1031
|
+
* These values are extracted directly from Creature's globals.css.
|
|
1032
|
+
*/
|
|
1033
|
+
declare const MCP_APPS_LIGHT_DEFAULTS: Record<string, string>;
|
|
1034
|
+
/**
|
|
1035
|
+
* Shared MCP Apps / Creature typography and layout tokens (same for light/dark).
|
|
1036
|
+
* These values are extracted directly from Creature's globals.css.
|
|
1037
|
+
*/
|
|
1038
|
+
declare const MCP_APPS_SHARED_DEFAULTS: Record<string, string>;
|
|
1039
|
+
/**
|
|
1040
|
+
* ChatGPT Apps default values - LIGHT mode.
|
|
1041
|
+
* Ported from @openai/apps-sdk-ui design tokens.
|
|
1042
|
+
*/
|
|
1043
|
+
declare const CHATGPT_LIGHT_DEFAULTS: Record<string, string>;
|
|
1044
|
+
/**
|
|
1045
|
+
* ChatGPT Apps default values - DARK mode.
|
|
1046
|
+
*/
|
|
1047
|
+
declare const CHATGPT_DARK_DEFAULTS: Record<string, string>;
|
|
1048
|
+
/**
|
|
1049
|
+
* Shared ChatGPT typography and layout tokens (same for light/dark).
|
|
1050
|
+
*/
|
|
1051
|
+
declare const CHATGPT_SHARED_DEFAULTS: Record<string, string>;
|
|
1052
|
+
/**
|
|
1053
|
+
* Get MCP Apps default styles for the specified theme.
|
|
1054
|
+
* Returns CSS variable names with their default values.
|
|
1055
|
+
* These values match exactly what Creature sends via hostContext.styles.variables.
|
|
1056
|
+
*
|
|
1057
|
+
* @param theme - The theme to get defaults for
|
|
1058
|
+
* @returns Record of CSS variable names to their values
|
|
1059
|
+
*/
|
|
1060
|
+
declare const getMcpAppDefaultStyles: ({ theme }: {
|
|
1061
|
+
theme: Theme;
|
|
1062
|
+
}) => Record<string, string>;
|
|
1063
|
+
/**
|
|
1064
|
+
* Get ChatGPT Apps default styles for the specified theme.
|
|
1065
|
+
* Returns CSS variable names with their values matching the @openai/apps-sdk-ui design tokens.
|
|
1066
|
+
*
|
|
1067
|
+
* @param theme - The theme to get defaults for
|
|
1068
|
+
* @returns Record of CSS variable names to their values
|
|
1069
|
+
*/
|
|
1070
|
+
declare const getChatGptDefaultStyles: ({ theme }: {
|
|
1071
|
+
theme: Theme;
|
|
1072
|
+
}) => Record<string, string>;
|
|
1073
|
+
/**
|
|
1074
|
+
* Detect the current theme from the document.
|
|
1075
|
+
* Checks data-theme attribute, dark class (Tailwind), and prefers-color-scheme.
|
|
1076
|
+
*
|
|
1077
|
+
* @returns The detected theme
|
|
1078
|
+
*/
|
|
1079
|
+
declare const detectTheme: () => Theme;
|
|
1080
|
+
/**
|
|
1081
|
+
* Apply CSS variables to the document root.
|
|
1082
|
+
*
|
|
1083
|
+
* @param styles - Record of CSS variable names to their values
|
|
1084
|
+
*/
|
|
1085
|
+
declare const applyStyles: ({ styles }: {
|
|
1086
|
+
styles: Record<string, string>;
|
|
1087
|
+
}) => void;
|
|
1088
|
+
/**
|
|
1089
|
+
* Initialize default styles based on detected environment and theme.
|
|
1090
|
+
* This should be called early in your app entry point to prevent FOUC.
|
|
1091
|
+
*
|
|
1092
|
+
* For MCP Apps (Creature): Applies defaults once. The host handles theme changes
|
|
1093
|
+
* and sends new styles via hostContext, which override these defaults.
|
|
1094
|
+
*
|
|
1095
|
+
* For ChatGPT: Applies defaults and sets up an observer to handle theme changes,
|
|
1096
|
+
* since ChatGPT may change theme before sending new hostContext.
|
|
1097
|
+
*
|
|
1098
|
+
* @param environment - The detected environment (from detectEnvironment)
|
|
1099
|
+
* @param options - Configuration options
|
|
1100
|
+
*/
|
|
1101
|
+
declare const initStyles: ({ environment, options, }: {
|
|
1102
|
+
environment: Environment;
|
|
1103
|
+
options?: InitStylesOptions;
|
|
1104
|
+
}) => void;
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* Create a WebSocket client with automatic reconnection.
|
|
1108
|
+
*
|
|
1109
|
+
* @param url - WebSocket server URL
|
|
1110
|
+
* @param config - Client configuration
|
|
1111
|
+
* @returns WebSocket client instance
|
|
1112
|
+
*/
|
|
1113
|
+
declare function createWebSocket<TSend = unknown, TReceive = unknown>(url: string, config?: WebSocketClientConfig<TReceive>): WebSocketClient<TSend, TReceive>;
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* Core Host Client Module
|
|
1117
|
+
*
|
|
1118
|
+
* Provides the unified host client factory and all related types.
|
|
1119
|
+
* This is the main entry point for client-side SDK usage.
|
|
1120
|
+
*/
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* Detect the current host environment.
|
|
1124
|
+
*
|
|
1125
|
+
* Used to auto-select the appropriate host client implementation:
|
|
1126
|
+
* - "chatgpt": Running inside ChatGPT's widget system
|
|
1127
|
+
* - "mcp-apps": Running inside an MCP Apps host (iframe with parent)
|
|
1128
|
+
* - "standalone": Running outside any host environment
|
|
1129
|
+
*
|
|
1130
|
+
* @returns The detected environment
|
|
1131
|
+
*/
|
|
1132
|
+
declare function detectEnvironment(): Environment;
|
|
1133
|
+
/**
|
|
1134
|
+
* Create a host client for the detected environment.
|
|
1135
|
+
*
|
|
1136
|
+
* Automatically selects the appropriate adapter based on runtime detection:
|
|
1137
|
+
* - ChatGPT: Uses ChatGptAdapter (detects window.openai)
|
|
1138
|
+
* - MCP Apps: Uses UpgradingMcpAppsClient (determines Creature vs generic after connection)
|
|
1139
|
+
* - Standalone: Uses StandaloneAdapter for development/testing
|
|
1140
|
+
*
|
|
1141
|
+
* For MCP Apps hosts, the returned client uses spec-compliant host identification
|
|
1142
|
+
* via hostContext.userAgent. The adapterKind and isCreature properties will
|
|
1143
|
+
* reflect the actual host after connection.
|
|
1144
|
+
*
|
|
1145
|
+
* @param config - Configuration for the host client
|
|
1146
|
+
* @returns The appropriate host client adapter for the environment
|
|
1147
|
+
*/
|
|
1148
|
+
declare function createHost(config: HostClientConfig): UnifiedHostClient;
|
|
1149
|
+
/**
|
|
1150
|
+
* Create a host client asynchronously, waiting for host identification.
|
|
1151
|
+
*
|
|
1152
|
+
* Unlike createHost(), this function waits for the connection to establish
|
|
1153
|
+
* and hostContext to be received before returning. This guarantees that
|
|
1154
|
+
* adapterKind and isCreature are accurate when the promise resolves.
|
|
1155
|
+
*
|
|
1156
|
+
* Use this when you need to know the exact host type before proceeding.
|
|
1157
|
+
*
|
|
1158
|
+
* @param config - Configuration for the host client
|
|
1159
|
+
* @returns Promise that resolves to the configured host client after connection
|
|
1160
|
+
*
|
|
1161
|
+
* @example
|
|
1162
|
+
* ```typescript
|
|
1163
|
+
* const host = await createHostAsync({ name: "my-app", version: "1.0.0" });
|
|
1164
|
+
* if (host.isCreature) {
|
|
1165
|
+
* // Creature-specific initialization
|
|
1166
|
+
* }
|
|
1167
|
+
* ```
|
|
1168
|
+
*/
|
|
1169
|
+
declare function createHostAsync(config: HostClientConfig): Promise<UnifiedHostClient>;
|
|
1170
|
+
|
|
1171
|
+
export { type AdapterKind, type BaseHostClient, type BaseHostClientEvents, CHATGPT_DARK_DEFAULTS, CHATGPT_LIGHT_DEFAULTS, CHATGPT_SHARED_DEFAULTS, ChatGptAdapter, ChatGptBaseHostClient, CreatureAdapter, type DisplayMode, type Environment, type HostAdapter, type HostClientConfig, type HostClientState, type HostContext, type HostIdentity, type InitStylesOptions, KNOWN_HOSTS, type LogLevel, MCP_APPS_DARK_DEFAULTS, MCP_APPS_LIGHT_DEFAULTS, MCP_APPS_SHARED_DEFAULTS, McpAppsAdapter, McpAppsBaseHostClient, type McpAppsHostClientEvents, StandaloneAdapter, StandaloneBaseHostClient, type StateListener, type StructuredWidgetState, type Theme, type ToolResult, type UnifiedHostClient, type UnifiedHostClientEvents, UpgradingMcpAppsClient, type WebSocketClient, type WebSocketClientConfig, type WebSocketStatus, type WidgetState, applyStyles, createHost, createHostAsync, createWebSocket, detectEnvironment, detectTheme, getChatGptDefaultStyles, getHostIdentity, getMcpAppDefaultStyles, initStyles, isHost, parseHostUserAgent };
|