@creature-ai/sdk 0.1.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,215 @@
1
+ import { g as StateListener, f as HostClientEvents, e as HostClientState, H as HostClient, d as HostClientConfig, T as ToolResult, W as WidgetState, L as LogLevel, E as Environment, a as AppSessionState, b as AppSessionListener } from '../types-DcoqlK46.js';
2
+ export { A as AppSessionOptions, c as HostContext, S as StructuredWidgetState } from '../types-DcoqlK46.js';
3
+ export { applyDocumentTheme, applyHostFonts, applyHostStyleVariables, getDocumentTheme } from '@modelcontextprotocol/ext-apps';
4
+
5
+ declare abstract class Subscribable {
6
+ private stateListeners;
7
+ private eventHandlers;
8
+ subscribe(listener: StateListener): () => void;
9
+ on<K extends keyof HostClientEvents>(event: K, handler: HostClientEvents[K]): () => void;
10
+ protected notifyStateChange(state: HostClientState, prevState: HostClientState): void;
11
+ protected emit<K extends keyof HostClientEvents>(event: K, ...args: Parameters<HostClientEvents[K]>): void;
12
+ protected onSubscribe(): void;
13
+ protected onUnsubscribe(): void;
14
+ }
15
+
16
+ /**
17
+ * MCP Apps host client implementation.
18
+ *
19
+ * Wraps the official MCP Apps SDK `App` class to provide a consistent interface
20
+ * with the ChatGPT host client. Handles the protocol handshake, tool calls,
21
+ * notifications, and automatic style/theme application.
22
+ */
23
+ declare class McpHostClient extends Subscribable implements HostClient {
24
+ private state;
25
+ private config;
26
+ private app;
27
+ private connected;
28
+ constructor(config: HostClientConfig);
29
+ getState(): HostClientState;
30
+ private setState;
31
+ /**
32
+ * Connect to the MCP Apps host.
33
+ *
34
+ * Creates the App instance, registers notification handlers, and initiates
35
+ * the protocol handshake. The host will receive `ui/initialize` and respond
36
+ * with host context including theme, styles, and widgetState.
37
+ */
38
+ connect(): void;
39
+ /**
40
+ * Set up notification handlers on the App instance.
41
+ *
42
+ * Maps the official SDK's callback pattern to our event emitter pattern,
43
+ * allowing consumers to use `.on("tool-result", ...)` etc.
44
+ */
45
+ private setupHandlers;
46
+ /**
47
+ * Initiate connection using PostMessageTransport.
48
+ *
49
+ * The SDK's App.connect() handles the protocol handshake correctly:
50
+ * the guest (App) sends `ui/initialize` to the host.
51
+ */
52
+ private initiateConnection;
53
+ /**
54
+ * Disconnect from the host.
55
+ *
56
+ * Cleans up the App instance and resets state.
57
+ */
58
+ disconnect(): void;
59
+ /**
60
+ * Call a tool on the MCP server via the host.
61
+ *
62
+ * Uses the SDK's callServerTool method which properly routes through
63
+ * the host to the MCP server.
64
+ *
65
+ * @param toolName - Name of the tool to call
66
+ * @param args - Arguments to pass to the tool
67
+ * @returns Tool result with content and structuredContent
68
+ */
69
+ callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
70
+ /**
71
+ * Send a notification to the host.
72
+ *
73
+ * Sends a notification via the ext-apps SDK transport. Can be used for both
74
+ * spec-compliant and custom (Creature-specific) notifications.
75
+ *
76
+ * @param method - Notification method name
77
+ * @param params - Notification parameters
78
+ */
79
+ sendNotification(method: string, params: unknown): void;
80
+ /**
81
+ * Set widget state and notify the host.
82
+ *
83
+ * Widget state is synchronized with the host for persistence across
84
+ * sessions and visibility to the AI model.
85
+ *
86
+ * Note: This is a Creature-specific extension, not part of the MCP Apps spec.
87
+ * The host should handle `ui/notifications/widget-state-changed` notifications.
88
+ *
89
+ * @param state - New widget state (or null to clear)
90
+ */
91
+ setWidgetState(state: WidgetState | null): void;
92
+ /**
93
+ * Send a log message to the host's DevConsole.
94
+ *
95
+ * Uses the MCP protocol's `notifications/message` notification to send logs
96
+ * to the host. Logs appear in the unified DevConsole alongside server logs,
97
+ * with appropriate color coding based on level.
98
+ *
99
+ * The logger name is automatically set to the app name from config.
100
+ *
101
+ * @param level - Log severity level (debug, info, notice, warning, error)
102
+ * @param message - Log message
103
+ * @param data - Optional structured data to include with the log
104
+ */
105
+ log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
106
+ }
107
+
108
+ interface OpenAIBridge {
109
+ toolOutput?: Record<string, unknown>;
110
+ widgetState?: WidgetState;
111
+ setWidgetState?: (state: WidgetState) => void;
112
+ callTool?: (name: string, args: Record<string, unknown>) => Promise<{
113
+ structuredContent?: Record<string, unknown>;
114
+ content?: Array<{
115
+ type: string;
116
+ text?: string;
117
+ }>;
118
+ }>;
119
+ }
120
+ declare global {
121
+ interface Window {
122
+ openai?: OpenAIBridge;
123
+ }
124
+ }
125
+ declare class ChatGPTHostClient extends Subscribable implements HostClient {
126
+ private state;
127
+ private config;
128
+ private connected;
129
+ private hasProcessedInitialData;
130
+ private globalsHandler;
131
+ constructor(config: HostClientConfig);
132
+ getState(): HostClientState;
133
+ private setState;
134
+ connect(): void;
135
+ disconnect(): void;
136
+ private processInitialData;
137
+ private setupGlobalsListener;
138
+ callTool<T = Record<string, unknown>>(toolName: string, args: Record<string, unknown>): Promise<ToolResult<T>>;
139
+ sendNotification(_method: string, _params: unknown): void;
140
+ setWidgetState(state: WidgetState | null): void;
141
+ /**
142
+ * Log a message to the console.
143
+ *
144
+ * ChatGPT doesn't have a DevConsole, so logs go to browser console only.
145
+ * This provides API parity with McpHostClient.
146
+ *
147
+ * @param level - Log severity level
148
+ * @param message - Log message
149
+ * @param data - Optional structured data
150
+ */
151
+ log(level: LogLevel, message: string, data?: Record<string, unknown>): void;
152
+ }
153
+
154
+ /**
155
+ * Detect the current host environment.
156
+ *
157
+ * Used to auto-select the appropriate host client implementation:
158
+ * - "chatgpt": Running inside ChatGPT's widget system
159
+ * - "mcp-apps": Running inside an MCP Apps host (iframe with parent)
160
+ * - "standalone": Running outside any host environment
161
+ *
162
+ * @returns The detected environment
163
+ */
164
+ declare function detectEnvironment(): Environment;
165
+
166
+ type PartialState<TState extends AppSessionState> = {
167
+ [K in keyof TState]?: Partial<TState[K]> | TState[K];
168
+ };
169
+ declare class AppSession<TInternal extends Record<string, unknown> = Record<string, unknown>, TBackend extends Record<string, unknown> = Record<string, unknown>, TUi extends WidgetState | null = WidgetState | null> {
170
+ readonly id: string;
171
+ private _state;
172
+ private listeners;
173
+ private hostClient;
174
+ private hostUnsubscribe;
175
+ constructor(initialState?: Partial<AppSessionState<TInternal, TBackend, TUi>>, options?: {
176
+ id?: string;
177
+ });
178
+ private generateId;
179
+ get state(): AppSessionState<TInternal, TBackend, TUi>;
180
+ get internal(): TInternal;
181
+ get backend(): TBackend;
182
+ get ui(): TUi;
183
+ subscribe(listener: AppSessionListener<AppSessionState<TInternal, TBackend, TUi>>): () => void;
184
+ private notify;
185
+ setState(partial: PartialState<AppSessionState<TInternal, TBackend, TUi>>): void;
186
+ setInternal(internal: TInternal | Partial<TInternal>): void;
187
+ setBackend(backend: TBackend | Partial<TBackend>): void;
188
+ setUi(ui: TUi): void;
189
+ updateUi(partial: TUi extends null ? never : Partial<NonNullable<TUi>>): void;
190
+ bindHost(host: HostClient): () => void;
191
+ unbindHost(): void;
192
+ injectAppSessionId<T extends Record<string, unknown>>(data: T): T & {
193
+ appSessionId: string;
194
+ };
195
+ }
196
+
197
+ type ChannelStatus = "disconnected" | "connecting" | "connected" | "error";
198
+ interface ChannelClientConfig<TReceive = unknown> {
199
+ onMessage?: (message: TReceive) => void;
200
+ onStatusChange?: (status: ChannelStatus, error?: string) => void;
201
+ reconnect?: boolean;
202
+ reconnectInterval?: number;
203
+ }
204
+ interface ChannelClient<TSend = unknown, TReceive = unknown> {
205
+ readonly status: ChannelStatus;
206
+ readonly error: string | undefined;
207
+ connect(): void;
208
+ disconnect(): void;
209
+ send(message: TSend): void;
210
+ }
211
+ declare function createChannel<TSend = unknown, TReceive = unknown>(url: string, config?: ChannelClientConfig<TReceive>): ChannelClient<TSend, TReceive>;
212
+
213
+ declare function createHost(config: HostClientConfig): HostClient;
214
+
215
+ export { AppSession, AppSessionListener, AppSessionState, type ChannelClient, type ChannelClientConfig, type ChannelStatus, ChatGPTHostClient, Environment, HostClient, HostClientConfig, HostClientEvents, HostClientState, LogLevel, McpHostClient, StateListener, ToolResult, WidgetState, createChannel, createHost, detectEnvironment };